From b4adcc4dee0b295cf7f79791763d45f37e7a9693 Mon Sep 17 00:00:00 2001 From: Vlad Utkin Date: Thu, 13 May 2021 16:03:07 +0300 Subject: [PATCH] Refactor code --- .vscode/extensions.json | 14 +-- src/bmp180.h | 23 +++-- src/bno055.h | 97 ++++++++++-------- src/constants.h | 8 ++ src/gps.h | 6 +- src/lora.h | 82 ++++++++------- src/main.cpp | 62 +++++++----- src/sdcard.h | 218 +++++++++++++++++++++++++--------------- src/sht31.h | 32 +++--- src/wifi_setup.h | 48 +++++---- 10 files changed, 347 insertions(+), 243 deletions(-) create mode 100644 src/constants.h diff --git a/.vscode/extensions.json b/.vscode/extensions.json index e80666b..0f0d740 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,7 +1,7 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/src/bmp180.h b/src/bmp180.h index f2597e6..389f216 100644 --- a/src/bmp180.h +++ b/src/bmp180.h @@ -2,15 +2,18 @@ Adafruit_BMP085 bmp180; -void setup_180() { - if (!bmp180.begin()) { - Serial.println("Could not find a valid BMP085 sensor, check wiring!"); - } +void setup_180() +{ + if (!bmp180.begin()) + { + Serial.println("Could not find a valid BMP085 sensor, check wiring!"); + } } -void loop_180(JsonObject &root) { - JsonObject bmp388 = root.createNestedObject("bmp388"); - /* +void loop_180(JsonObject &root) +{ + JsonObject bmp388 = root.createNestedObject("bmp388"); + /* Serial.print("Temperature = "); Serial.print(bmp180.readTemperature()); Serial.println(" *C"); @@ -39,7 +42,7 @@ void loop_180(JsonObject &root) { Serial.println(); */ - bmp388["temperature"] = bmp180.readTemperature(); - bmp388["pressure"] = bmp180.readPressure(); - bmp388["altitude"] = bmp180.readAltitude(); + bmp388["temperature"] = bmp180.readTemperature(); + bmp388["pressure"] = bmp180.readPressure(); + bmp388["altitude"] = bmp180.readAltitude(); } \ No newline at end of file diff --git a/src/bno055.h b/src/bno055.h index 654b39b..d1a102d 100644 --- a/src/bno055.h +++ b/src/bno055.h @@ -8,22 +8,32 @@ void displaySensorDetails(void) sensor_t sensor; bno.getSensor(&sensor); Serial.println("------------------------------------"); - Serial.print ("Sensor: "); Serial.println(sensor.name); - Serial.print ("Driver Ver: "); Serial.println(sensor.version); - Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); - Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" xxx"); - Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" xxx"); - Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" xxx"); + Serial.print("Sensor: "); + Serial.println(sensor.name); + Serial.print("Driver Ver: "); + Serial.println(sensor.version); + Serial.print("Unique ID: "); + Serial.println(sensor.sensor_id); + Serial.print("Max Value: "); + Serial.print(sensor.max_value); + Serial.println(" xxx"); + Serial.print("Min Value: "); + Serial.print(sensor.min_value); + Serial.println(" xxx"); + Serial.print("Resolution: "); + Serial.print(sensor.resolution); + Serial.println(" xxx"); Serial.println("------------------------------------"); Serial.println(""); } void setup_bno(void) { - Serial.println("Orientation Sensor Test"); Serial.println(""); + Serial.println("Orientation Sensor Test"); + Serial.println(""); /* Initialise the sensor */ - if(!bno.begin()) + if (!bno.begin()) { /* There was a problem detecting the BNO055 ... check your connections */ Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!"); @@ -32,84 +42,91 @@ void setup_bno(void) /* Use external crystal for better accuracy */ bno.setExtCrystalUse(true); - + /* Display some basic information on this sensor */ displaySensorDetails(); } -void printEvent(sensors_event_t* event, JsonObject obj) { - double x = -1000000, y = -1000000 , z = -1000000; //dumb values, easy to spot problem - if (event->type == SENSOR_TYPE_ACCELEROMETER) { - #ifdef debug +void printEvent(sensors_event_t *event, JsonObject obj) +{ + double x = -1000000, y = -1000000, z = -1000000; //dumb values, easy to spot problem + if (event->type == SENSOR_TYPE_ACCELEROMETER) + { +#ifdef debug Serial.print("Accl:"); - #endif +#endif x = event->acceleration.x; y = event->acceleration.y; z = event->acceleration.z; } - else if (event->type == SENSOR_TYPE_ORIENTATION) { - #ifdef debug + else if (event->type == SENSOR_TYPE_ORIENTATION) + { +#ifdef debug Serial.print("Orient:"); - #endif +#endif x = event->orientation.x; y = event->orientation.y; z = event->orientation.z; } - else if (event->type == SENSOR_TYPE_MAGNETIC_FIELD) { - #ifdef debug + else if (event->type == SENSOR_TYPE_MAGNETIC_FIELD) + { +#ifdef debug Serial.print("Mag:"); - #endif +#endif x = event->magnetic.x; y = event->magnetic.y; z = event->magnetic.z; } - else if (event->type == SENSOR_TYPE_GYROSCOPE) { - #ifdef debug + else if (event->type == SENSOR_TYPE_GYROSCOPE) + { +#ifdef debug Serial.print("Gyro:"); - #endif +#endif x = event->gyro.x; y = event->gyro.y; z = event->gyro.z; } - else if (event->type == SENSOR_TYPE_ROTATION_VECTOR) { - #ifdef debug + else if (event->type == SENSOR_TYPE_ROTATION_VECTOR) + { +#ifdef debug Serial.print("Rot:"); - #endif +#endif x = event->gyro.x; y = event->gyro.y; z = event->gyro.z; } - else if (event->type == SENSOR_TYPE_LINEAR_ACCELERATION) { - #ifdef debug + else if (event->type == SENSOR_TYPE_LINEAR_ACCELERATION) + { +#ifdef debug Serial.print("Linear:"); - #endif +#endif x = event->acceleration.x; y = event->acceleration.y; z = event->acceleration.z; } - else { - #ifdef debug + else + { +#ifdef debug Serial.print("Unk:"); - #endif +#endif } obj["x"] = x; obj["y"] = y; obj["z"] = z; - #ifdef debug +#ifdef debug Serial.print("\tx= "); Serial.print(x); Serial.print(" |\ty= "); Serial.print(y); Serial.print(" |\tz= "); Serial.println(z); - #endif +#endif } - void loop_bno(JsonObject &root) { JsonObject bno055 = root.createNestedObject("bno055"); - sensors_event_t orientationData , angVelocityData , linearAccelData, magnetometerData, accelerometerData, gravityData; + sensors_event_t orientationData, angVelocityData, linearAccelData, magnetometerData, accelerometerData, gravityData; bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER); bno.getEvent(&angVelocityData, Adafruit_BNO055::VECTOR_GYROSCOPE); bno.getEvent(&linearAccelData, Adafruit_BNO055::VECTOR_LINEARACCEL); @@ -130,11 +147,11 @@ void loop_bno(JsonObject &root) printEvent(&gravityData, gravity_data); int8_t boardTemp = bno.getTemp(); - #ifdef bebug +#ifdef bebug Serial.println(); Serial.print(F("temperature: ")); Serial.println(boardTemp); - #endif +#endif bno055["temperature"] = boardTemp; uint8_t system, gyro, accel, mag = 0; @@ -144,7 +161,7 @@ void loop_bno(JsonObject &root) calibration["gyro"] = gyro; calibration["accel"] = accel; calibration["mag"] = mag; - #ifdef debug +#ifdef debug Serial.println(); Serial.print("Calibration: Sys="); Serial.print(system); @@ -156,5 +173,5 @@ void loop_bno(JsonObject &root) Serial.println(mag); Serial.println("--"); - #endif +#endif } \ No newline at end of file diff --git a/src/constants.h b/src/constants.h new file mode 100644 index 0000000..f83848b --- /dev/null +++ b/src/constants.h @@ -0,0 +1,8 @@ +#define SEALEVELPRESSURE_HPA (1013.25) +#define NETWORK_NAME "Rocket" +#define NEWORK_PASSWORD "" +#define UDP_ADDRESS "192.168.1.255" +#define ALTITUDE_THRESHOLD 50.0 +#define PRESSURE_THRESHOLD 3.0 +#define START_ACCELERATION_THRESHOLD 5.0 +#define TIME_TO_DEPLOY 8 * 1000 diff --git a/src/gps.h b/src/gps.h index d6e60b5..82cec94 100644 --- a/src/gps.h +++ b/src/gps.h @@ -7,8 +7,7 @@ #endif - -static NMEAGPS gps; +static NMEAGPS gps; gps_fix fix; void setup_gps() @@ -18,7 +17,8 @@ void setup_gps() void loop_gps(JsonObject &root) { - if (gps.available( gpsPort)) { + if (gps.available(gpsPort)) + { fix = gps.read(); } JsonObject gps = root.createNestedObject("gps"); diff --git a/src/lora.h b/src/lora.h index b100f5a..553c123 100644 --- a/src/lora.h +++ b/src/lora.h @@ -1,36 +1,37 @@ #include #include -#define NSS 5 //select on LoRa device -#define SCK 18 //SCK on SPI3 -#define MISO 19 //MISO on SPI3 -#define MOSI 23 //MOSI on SPI3 +#define NSS 5 //select on LoRa device +#define SCK 18 //SCK on SPI3 +#define MISO 19 //MISO on SPI3 +#define MOSI 23 //MOSI on SPI3 -#define NRESET 35 //reset on LoRa device -#define DIO0 33 //DIO0 on LoRa device, used for RX and TX done -#define DIO1 32 //DIO1 on LoRa device, normally not used so set to -1 -#define DIO2 -1 //DIO2 on LoRa device, normally not used so set to -1 +#define NRESET 35 //reset on LoRa device +#define DIO0 33 //DIO0 on LoRa device, used for RX and TX done +#define DIO1 32 //DIO1 on LoRa device, normally not used so set to -1 +#define DIO2 -1 //DIO2 on LoRa device, normally not used so set to -1 -#define LORA_DEVICE DEVICE_SX1278 //this is the device we are using +#define LORA_DEVICE DEVICE_SX1278 //this is the device we are using //******* Setup LoRa Test Parameters Here ! *************** //LoRa Modem Parameters -const uint32_t Frequency = 429700000; //frequency of transmissions -const uint32_t Offset = 0; //offset frequency for calibration purposes +const uint32_t Frequency = 429700000; //frequency of transmissions +const uint32_t Offset = 0; //offset frequency for calibration purposes -const uint8_t Bandwidth = LORA_BW_062; //LoRa bandwidth -const uint8_t SpreadingFactor = LORA_SF12; //LoRa spreading factor -const uint8_t CodeRate = LORA_CR_4_8; //LoRa coding rate -const uint8_t Optimisation = LDRO_AUTO; //low data rate optimisation setting +const uint8_t Bandwidth = LORA_BW_062; //LoRa bandwidth +const uint8_t SpreadingFactor = LORA_SF12; //LoRa spreading factor +const uint8_t CodeRate = LORA_CR_4_8; //LoRa coding rate +const uint8_t Optimisation = LDRO_AUTO; //low data rate optimisation setting -const int8_t TXpower = 20; //LoRa transmit power in dBm +const int8_t TXpower = 20; //LoRa transmit power in dBm SX127XLT LT; -void setup_lora() { - SPI.begin(SCK, MISO, MOSI, NSS); -if (LT.begin(NSS, NRESET, DIO0, DIO1, DIO2, LORA_DEVICE)) +void setup_lora() +{ + SPI.begin(SCK, MISO, MOSI, NSS); + if (LT.begin(NSS, NRESET, DIO0, DIO1, DIO2, LORA_DEVICE)) { Serial.println(F("LoRa Device found")); } @@ -42,17 +43,17 @@ if (LT.begin(NSS, NRESET, DIO0, DIO1, DIO2, LORA_DEVICE)) LT.setupLoRa(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, Optimisation); Serial.println(); - LT.printModemSettings(); //reads and prints the configured LoRa settings, useful check + LT.printModemSettings(); //reads and prints the configured LoRa settings, useful check Serial.println(); - LT.printOperatingSettings(); //reads and prints the configured operating settings, useful check + LT.printOperatingSettings(); //reads and prints the configured operating settings, useful check Serial.println(); - LT.startWriteSXBuffer(0); //initialise buffer write at address 0 - LT.writeFloat(0.0); //add latitude - LT.writeFloat(0.0); //add longitude - LT.writeFloat(0.0); //add altitude - LT.writeUint8(0); //add number of satellites + LT.startWriteSXBuffer(0); //initialise buffer write at address 0 + LT.writeFloat(0.0); //add latitude + LT.writeFloat(0.0); //add longitude + LT.writeFloat(0.0); //add altitude + LT.writeUint8(0); //add number of satellites LT.writeUint8(0); //add tracker status - uint8_t len = LT.endWriteSXBuffer(); //close buffer write + uint8_t len = LT.endWriteSXBuffer(); //close buffer write LT.transmitSXBuffer(0, len, 10000, TXpower, WAIT_TX); } @@ -61,14 +62,14 @@ void sendLocation(int32_t Lat, int32_t Lon) { uint8_t len; - LT.startWriteSXBuffer(0); //initialise buffer write at address 0 - LT.writeInt32(Lat); //add latitude - LT.writeInt32(Lon); //add longitude - LT.writeInt16(fix.alt.whole); //add altitude + LT.startWriteSXBuffer(0); //initialise buffer write at address 0 + LT.writeInt32(Lat); //add latitude + LT.writeInt32(Lon); //add longitude + LT.writeInt16(fix.alt.whole); //add altitude LT.writeInt8(fix.alt.frac); - LT.writeUint8(fix.satellites); //add number of satellites - LT.writeUint8(fix.status); //add tracker status - len = LT.endWriteSXBuffer(); //close buffer write + LT.writeUint8(fix.satellites); //add number of satellites + LT.writeUint8(fix.status); //add tracker status + len = LT.endWriteSXBuffer(); //close buffer write /* Serial.print("Sending data: "); Serial.print(fix.alt.whole); @@ -78,16 +79,19 @@ void sendLocation(int32_t Lat, int32_t Lon) LT.transmitSXBuffer(0, len, 10000, TXpower, NO_WAIT); } - -void loop_lora_pre() { - if (gps.available( gpsPort) && digitalRead(DIO0)) { +void loop_lora_pre() +{ + if (gps.available(gpsPort) && digitalRead(DIO0)) + { fix = gps.read(); sendLocation(fix.latitudeL(), fix.longitudeL()); } } -void loop_lora() { - if (digitalRead(DIO0)) { +void loop_lora() +{ + if (digitalRead(DIO0)) + { sendLocation(fix.latitudeL(), fix.longitudeL()); } } diff --git a/src/main.cpp b/src/main.cpp index c85af92..6e42a71 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#define SEALEVELPRESSURE_HPA (1013.25) +#include #include #include #include @@ -15,14 +15,16 @@ bool deployed = 0; -void deploy() { - digitalWrite(PWM, HIGH); - delay(1000); - digitalWrite(PWM, LOW); - delay(1000); +void deploy() +{ + digitalWrite(PWM, HIGH); + delay(1000); + digitalWrite(PWM, LOW); + delay(1000); } -void TaskDeploycode( void * pvParameters ){ +void TaskDeploycode(void *pvParameters) +{ deploy(); deployed = 1; } @@ -36,7 +38,8 @@ bool save = false; float previous_height; bool altitude = false; -void setup(){ +void setup() +{ Serial.begin(115200); Wire.setClock(800000); setup_sdcard(); @@ -50,20 +53,23 @@ void setup(){ Serial.println("Configuring access point..."); connectToWiFi(networkName, networkPswd); - ESP_ERROR_CHECK(esp_wifi_set_protocol (WIFI_IF_STA, WIFI_PROTOCOL_11B)); + ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B)); wifi_country_t country_info = {"JP", 1, 14, WIFI_COUNTRY_POLICY_MANUAL}; - ESP_ERROR_CHECK(esp_wifi_set_country(&country_info)); + ESP_ERROR_CHECK(esp_wifi_set_country(&country_info)); ESP_ERROR_CHECK(esp_wifi_set_max_tx_power(80)); ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); pinMode(PWM, OUTPUT); uint8_t buffer[50] = "test.txt"; - while(1) { - if (connected) { - bmp.performReading(); - original_height = bmp.readAltitude(SEALEVELPRESSURE_HPA); + while (1) + { + if (connected) + { + bmp.performReading(); + original_height = bmp.readAltitude(SEALEVELPRESSURE_HPA); udp.parsePacket(); - if(udp.read(buffer, 50) > 0){ + if (udp.read(buffer, 50) > 0) + { Serial.print("Server to client: "); Serial.println((char *)buffer); start = millis(); @@ -78,10 +84,11 @@ void setup(){ Serial.println(original_height); } -void loop(){ +void loop() +{ DynamicJsonDocument doc(1024); JsonObject obj = doc.to(); - obj["seconds"] = millis()/1000; + obj["seconds"] = millis() / 1000; obj["deployed"] = deployed; obj["save"] = save; obj["flight"] = flight; @@ -95,32 +102,37 @@ void loop(){ file.print('\n'); file.flush(); //only send data when connected - if(connected){ + if (connected) + { //Send a packet - udp.beginPacket(udpAddress,udpPort); + udp.beginPacket(udpAddress, udpPort); serializeJson(doc, udp); udp.println(); udp.endPacket(); } - if (!save & !deployed & ((obj["bmp388"]["altitude"].as() - original_height) > 50.0)) { + if (!save & !deployed & ((obj["bmp388"]["altitude"].as() - original_height) > ALTITUDE_THRESHOLD)) + { Serial.println("Deploy is save"); save = true; } - if (save & !deployed & (((obj["bmp388"]["altitude"].as() - previous_height) * 5) < 3.0)) { + if (save & !deployed & (((obj["bmp388"]["altitude"].as() - previous_height) * 5) < PRESSURE_THRESHOLD)) + { Serial.println("Start deploy altitude"); altitude = true; deployed = true; digitalWrite(PWM, HIGH); } if (!flight & (max(max(abs(obj["bno055"]["linear_accel_data"]["x"].as()), - abs(obj["bno055"]["linear_accel_data"]["y"].as())), - max(abs(obj["bno055"]["linear_accel_data"]["y"].as()), - abs(obj["bno055"]["linear_accel_data"]["z"].as()))) > 5.0)) { + abs(obj["bno055"]["linear_accel_data"]["y"].as())), + max(abs(obj["bno055"]["linear_accel_data"]["y"].as()), + abs(obj["bno055"]["linear_accel_data"]["z"].as()))) > START_ACCELERATION_THRESHOLD)) + { Serial.println("Timer trigger"); start = millis(); flight = true; } - if(flight & ((millis()-start) > 8*1000) & !deployed) { // Вот таймер + if (flight & ((millis() - start) > TIME_TO_DEPLOY) & !deployed) + { // Вот таймер Serial.println("Start deploy timeout"); deployed = 1; digitalWrite(PWM, HIGH); diff --git a/src/sdcard.h b/src/sdcard.h index 497abcb..04329bb 100644 --- a/src/sdcard.h +++ b/src/sdcard.h @@ -1,166 +1,207 @@ #include "FS.h" #include "SD_MMC.h" -void listDir(fs::FS &fs, const char * dirname, uint8_t levels){ - #ifdef debug +void listDir(fs::FS &fs, const char *dirname, uint8_t levels) +{ +#ifdef debug Serial.printf("Listing directory: %s\n", dirname); - #endif +#endif File root = fs.open(dirname); - if(!root){ - #ifdef debug + if (!root) + { +#ifdef debug Serial.println("Failed to open directory"); - #endif +#endif return; } - if(!root.isDirectory()){ - #ifdef debug + if (!root.isDirectory()) + { +#ifdef debug Serial.println("Not a directory"); - #endif +#endif return; } File file = root.openNextFile(); - while(file){ - if(file.isDirectory()){ - #ifdef debug + while (file) + { + if (file.isDirectory()) + { +#ifdef debug Serial.print(" DIR : "); - #endif +#endif Serial.println(file.name()); - if(levels){ - listDir(fs, file.name(), levels -1); + if (levels) + { + listDir(fs, file.name(), levels - 1); } - } else { - #ifdef debug + } + else + { +#ifdef debug Serial.print(" FILE: "); - #endif +#endif Serial.print(file.name()); - #ifdef debug +#ifdef debug Serial.print(" SIZE: "); - #endif +#endif Serial.println(file.size()); } file = root.openNextFile(); } } -void createDir(fs::FS &fs, const char * path){ - #ifdef debug +void createDir(fs::FS &fs, const char *path) +{ +#ifdef debug Serial.printf("Creating Dir: %s\n", path); - #endif - if(fs.mkdir(path)){ - #ifdef debug +#endif + if (fs.mkdir(path)) + { +#ifdef debug Serial.println("Dir created"); - #endif - } else { - #ifdef debug +#endif + } + else + { +#ifdef debug Serial.println("mkdir failed"); - #endif +#endif } } -void removeDir(fs::FS &fs, const char * path){ - #ifdef debug +void removeDir(fs::FS &fs, const char *path) +{ +#ifdef debug Serial.printf("Removing Dir: %s\n", path); - #endif - if(fs.rmdir(path)){ - #ifdef debug +#endif + if (fs.rmdir(path)) + { +#ifdef debug Serial.println("Dir removed"); - #endif - } else { - #ifdef debug +#endif + } + else + { +#ifdef debug Serial.println("rmdir failed"); - #endif +#endif } } -void readFile(fs::FS &fs, const char * path){ - #ifdef debug +void readFile(fs::FS &fs, const char *path) +{ +#ifdef debug Serial.printf("Reading file: %s\n", path); #endif File file = fs.open(path); - if(!file){ - #ifdef debug + if (!file) + { +#ifdef debug Serial.println("Failed to open file for reading"); - #endif +#endif return; } #ifdef debug Serial.print("Read from file: "); - #endif - while(file.available()){ +#endif + while (file.available()) + { Serial.write(file.read()); } } -void writeFile(fs::FS &fs, const char * path, const char * message){ - #ifdef debug +void writeFile(fs::FS &fs, const char *path, const char *message) +{ +#ifdef debug Serial.printf("Writing file: %s\n", path); #endif File file = fs.open(path, FILE_WRITE); - if(!file){ - #ifdef debug + if (!file) + { +#ifdef debug Serial.println("Failed to open file for writing"); - #endif +#endif return; } - if(file.print(message)){ - #ifdef debug + if (file.print(message)) + { +#ifdef debug Serial.println("File written"); - #endif - } else { - #ifdef debug +#endif + } + else + { +#ifdef debug Serial.println("Write failed"); - #endif +#endif } } -void appendFile(fs::FS &fs, const char * path, const char * message){ +void appendFile(fs::FS &fs, const char *path, const char *message) +{ Serial.printf("Appending to file: %s\n", path); File file = fs.open(path, FILE_APPEND); - if(!file){ + if (!file) + { Serial.println("Failed to open file for appending"); return; } - if(file.print(message)){ + if (file.print(message)) + { Serial.println("Message appended"); - } else { + } + else + { Serial.println("Append failed"); } } -void renameFile(fs::FS &fs, const char * path1, const char * path2){ +void renameFile(fs::FS &fs, const char *path1, const char *path2) +{ Serial.printf("Renaming file %s to %s\n", path1, path2); - if (fs.rename(path1, path2)) { + if (fs.rename(path1, path2)) + { Serial.println("File renamed"); - } else { + } + else + { Serial.println("Rename failed"); } } -void deleteFile(fs::FS &fs, const char * path){ +void deleteFile(fs::FS &fs, const char *path) +{ Serial.printf("Deleting file: %s\n", path); - if(fs.remove(path)){ + if (fs.remove(path)) + { Serial.println("File deleted"); - } else { + } + else + { Serial.println("Delete failed"); } } -void testFileIO(fs::FS &fs, const char * path){ +void testFileIO(fs::FS &fs, const char *path) +{ File file = fs.open(path); static uint8_t buf[512]; size_t len = 0; uint32_t start = millis(); uint32_t end = start; - if(file){ + if (file) + { len = file.size(); size_t flen = len; start = millis(); - while(len){ + while (len) + { size_t toRead = len; - if(toRead > 512){ + if (toRead > 512) + { toRead = 512; } file.read(buf, toRead); @@ -169,20 +210,23 @@ void testFileIO(fs::FS &fs, const char * path){ end = millis() - start; Serial.printf("%u bytes read for %u ms\n", flen, end); file.close(); - } else { + } + else + { Serial.println("Failed to open file for reading"); } - file = fs.open(path, FILE_WRITE); - if(!file){ + if (!file) + { Serial.println("Failed to open file for writing"); return; } size_t i; start = millis(); - for(i=0; i<2048; i++){ + for (i = 0; i < 2048; i++) + { file.write(buf, 512); } end = millis() - start; @@ -190,26 +234,36 @@ void testFileIO(fs::FS &fs, const char * path){ file.close(); } -void setup_sdcard() { - if(!SD_MMC.begin("/sdcard", true, false)){ +void setup_sdcard() +{ + if (!SD_MMC.begin("/sdcard", true, false)) + { Serial.println("Card Mount Failed"); return; } uint8_t cardType = SD_MMC.cardType(); - if(cardType == CARD_NONE){ + if (cardType == CARD_NONE) + { Serial.println("No SD_MMC card attached"); return; } Serial.print("SD_MMC Card Type: "); - if(cardType == CARD_MMC){ + if (cardType == CARD_MMC) + { Serial.println("MMC"); - } else if(cardType == CARD_SD){ + } + else if (cardType == CARD_SD) + { Serial.println("SDSC"); - } else if(cardType == CARD_SDHC){ + } + else if (cardType == CARD_SDHC) + { Serial.println("SDHC"); - } else { + } + else + { Serial.println("UNKNOWN"); } @@ -228,7 +282,7 @@ void setup_sdcard() { renameFile(SD_MMC, "/hello.txt", "/foo.txt"); readFile(SD_MMC, "/foo.txt"); */ - /* + /* xTaskCreate( Task1code, "Task1", diff --git a/src/sht31.h b/src/sht31.h index 61129af..1e898e8 100644 --- a/src/sht31.h +++ b/src/sht31.h @@ -2,26 +2,29 @@ Adafruit_SHT31 sht31 = Adafruit_SHT31(); -void setup_sht() { - if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr - Serial.println("Couldn't find SHT31"); - } - sht31.heater(1); - Serial.print("Heater Enabled State: "); - if (sht31.isHeaterEnabled()) - Serial.println("ENABLED"); - else - Serial.println("DISABLED"); +void setup_sht() +{ + if (!sht31.begin(0x44)) + { // Set to 0x45 for alternate i2c addr + Serial.println("Couldn't find SHT31"); + } + sht31.heater(1); + Serial.print("Heater Enabled State: "); + if (sht31.isHeaterEnabled()) + Serial.println("ENABLED"); + else + Serial.println("DISABLED"); } -void loop_sht(JsonObject &root) { - JsonObject sht = root.createNestedObject("sht31"); +void loop_sht(JsonObject &root) +{ + JsonObject sht = root.createNestedObject("sht31"); float t = sht31.readTemperature(); float h = sht31.readHumidity(); sht["temperature"] = t; - sht["humidity"] = h; + sht["humidity"] = h; -/* + /* if (! isnan(t)) { // check if 'is not a number' Serial.print("Temp *C = "); Serial.print(t); Serial.print("\t\t"); } else { @@ -36,7 +39,6 @@ void loop_sht(JsonObject &root) { // Toggle heater enabled state every 30 seconds // An ~3.0 degC temperature increase can be noted when heater is enabled - /* if (++loopCnt == 30) { enableHeater = !enableHeater; sht31.heater(enableHeater); diff --git a/src/wifi_setup.h b/src/wifi_setup.h index 4a9c1bc..03e6d5d 100644 --- a/src/wifi_setup.h +++ b/src/wifi_setup.h @@ -2,10 +2,10 @@ #include #include "esp_wifi.h" -const char * networkName = "Overlord"; -const char * networkPswd = "justmonika"; +const char *networkName = NETWORK_NAME; +const char *networkPswd = NEWORK_PASSWORD; -const char * udpAddress = "192.168.2.255"; +const char *udpAddress = UDP_ADDRESS; const int udpPort = 3333; boolean connected = false; @@ -13,33 +13,37 @@ boolean connected = false; WiFiUDP udp; //wifi event handler -void WiFiEvent(WiFiEvent_t event){ - switch(event) { - case SYSTEM_EVENT_STA_GOT_IP: - //When connected set - Serial.print("WiFi connected! IP address: "); - Serial.println(WiFi.localIP()); - //initializes the UDP state - //This initializes the transfer buffer - udp.begin(WiFi.localIP(),udpPort); - connected = true; - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - Serial.println("WiFi lost connection"); - connected = false; - break; - default: break; - } +void WiFiEvent(WiFiEvent_t event) +{ + switch (event) + { + case SYSTEM_EVENT_STA_GOT_IP: + //When connected set + Serial.print("WiFi connected! IP address: "); + Serial.println(WiFi.localIP()); + //initializes the UDP state + //This initializes the transfer buffer + udp.begin(WiFi.localIP(), udpPort); + connected = true; + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + connected = false; + break; + default: + break; + } } -void connectToWiFi(const char * ssid, const char * pwd){ +void connectToWiFi(const char *ssid, const char *pwd) +{ Serial.println("Connecting to WiFi network: " + String(ssid)); // delete old config WiFi.disconnect(true); //register event handler WiFi.onEvent(WiFiEvent); - + //Initiate connection WiFi.begin(ssid, pwd);