From 4d5da38801ef09c0602946c5e6109f14013b9b64 Mon Sep 17 00:00:00 2001 From: LloydRichardsIntern <59728961+LloydRichardsIntern@users.noreply.github.com> Date: Wed, 15 Apr 2020 10:43:46 +0200 Subject: [PATCH 1/4] feature: Added Custom Param Added custom parameters to the WiFiManager to set and save the device. Currently there is an issue where the string copy is overwritting the User and UserID with the Device ID, will need to debug --- lib/MimirTesting/MimirTesting.cpp | 152 +++++++++++++++++++++++++++++- lib/MimirTesting/MimirTesting.h | 13 ++- src/main.cpp | 7 +- 3 files changed, 166 insertions(+), 6 deletions(-) diff --git a/lib/MimirTesting/MimirTesting.cpp b/lib/MimirTesting/MimirTesting.cpp index e22e2af..1e8be5c 100644 --- a/lib/MimirTesting/MimirTesting.cpp +++ b/lib/MimirTesting/MimirTesting.cpp @@ -1,4 +1,6 @@ #include "MimirTesting.h" +#include //this needs to be first, or it all crashes and burns... +#include "SPIFFS.h" #include #include #include @@ -14,7 +16,6 @@ //Sensor Libraries #include "Adafruit_SHT31.h" //https://github.com/adafruit/Adafruit_SHT31 #include "SparkFun_VEML6030_Ambient_Light_Sensor.h" //https://github.com/sparkfun/SparkFun_Ambient_Light_Sensor_Arduino_Library -//https://github.com/ControlEverythingCommunity/BH1715/blob/master/Arduino/BH1715.ino #include #include "ccs811.h" @@ -162,7 +163,16 @@ void MimirTesting::DisplaySensors() void MimirTesting::initWIFI(bool _display) { + WiFiManagerParameter custom_USER("User Name", "User Name", _USER, 40); + WiFiManagerParameter custom_USER_ID("User ID", "User ID", _USER_ID, 40); + WiFiManagerParameter custom_DEVICE_ID("Device ID", "Device ID", _DEVICE_ID, 40); + WiFiManager wifiManager; + + wifiManager.addParameter(&custom_USER); + wifiManager.addParameter(&custom_USER_ID); + wifiManager.addParameter(&custom_DEVICE_ID); + strip.setPixelColor(2, strip.Color(255, 255, 0)); strip.show(); wifiManager.autoConnect("mimirAP"); @@ -181,8 +191,66 @@ void MimirTesting::initWIFI(bool _display) strip.show(); } + //Update Device Info with Params + strcpy(_USER, custom_USER.getValue()); + strcpy(_USER_ID, custom_USER_ID.getValue()); + strcpy(_DEVICE_ID, custom_DEVICE_ID.getValue()); + + saveConfig(); + WiFi_OFF(); } +void MimirTesting::initConfig() +{ + if (SPIFFS.begin()) + { + if (SPIFFS.exists("/config.json")) + { + //file exists, reading and loading + Serial.println("reading config file"); + fs::File configFile = SPIFFS.open("/config.json", "r"); + if (configFile) + { + size_t size = configFile.size(); + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + + configFile.readBytes(buf.get(), size); + DynamicJsonDocument configJson(1024); + DeserializationError error = deserializeJson(configJson, buf.get()); + if (error) + { + Serial.println("failed to load json config"); + return; + } + Serial.println("\nparsed json"); + serializeJson(configJson, Serial); + + strcpy(_USER, configJson["User"] | "N/A"); + strcpy(_USER_ID, configJson["UserID"] | "N/A"); + strcpy(_DEVICE_ID, configJson["DeviceID"] | "N/A"); + } + } + } +} + +void MimirTesting::saveConfig() +{ + DynamicJsonDocument newConfigJson(1024); + newConfigJson["User"] = _USER; + newConfigJson["UserID"] = _USER_ID; + newConfigJson["DeviceID"] = _DEVICE_ID; + + fs::File configFile = SPIFFS.open("/config.json", "w"); + if (!configFile) + { + Serial.println("failed to open config file for writing"); + } + + serializeJson(newConfigJson, Serial); + serializeJson(newConfigJson, configFile); + configFile.close(); +} void MimirTesting::DisplayWiFiCredentials() { @@ -221,12 +289,49 @@ void MimirTesting::initTimer() void MimirTesting::forceStartWiFi() { + WiFiManagerParameter custom_USER("User Name", "User Name", _USER, 40); + WiFiManagerParameter custom_USER_ID("User ID", "User ID", _USER_ID, 40); + WiFiManagerParameter custom_DEVICE_ID("Device ID", "Device ID", _DEVICE_ID, 40); + + WiFiManager wifiManager; + + wifiManager.addParameter(&custom_USER); + wifiManager.addParameter(&custom_USER_ID); + wifiManager.addParameter(&custom_DEVICE_ID); + display.fillScreen(GxEPD_WHITE); display.setCursor(2, 20); display.println("*WiFi Config*"); display.println("****Mode*****"); display.update(); + + if (!wifiManager.startConfigPortal("OnDemandAP")) + { + Serial.println("failed to connect and hit timeout"); + delay(3000); + //reset and try again, or maybe put it to deep sleep + ESP.restart(); + delay(5000); + } + delay(5000); + + Serial.println(custom_USER.getValue()); + Serial.println(custom_USER_ID.getValue()); + Serial.println(custom_DEVICE_ID.getValue()); + + //Update Device Info with Params + strcpy(_USER, custom_USER.getValue()); + strcpy(_USER_ID, custom_USER_ID.getValue()); + strcpy(_DEVICE_ID, custom_DEVICE_ID.getValue()); + + Serial.println(_USER); + Serial.println(_USER_ID); + Serial.println(_DEVICE_ID); + + saveConfig(); + + WiFi_OFF(); } void MimirTesting::WiFi_ON() @@ -289,6 +394,51 @@ void MimirTesting::readSensors(bool _display) if (_display) DisplayReadings(); } +void MimirTesting::DisplayDeviceInfo() +{ + display.fillScreen(GxEPD_WHITE); + display.setCursor(0, 20); + display.print("User: "); + display.println(_USER); + display.print("UserID: "); + display.println(_USER_ID); + display.print("DeviceID: "); + display.println(_DEVICE_ID); + display.print("IP: "); + display.println(_IP_ADDRESS); + display.println("____________"); + !SHT31D_L_STATUS ? display.println("SHT31_L: X ") + : display.println("SHT31_L: O"); + + !SHT31D_H_STATUS ? display.println("SHT31_H: X") + : display.println("SHT31_H: O"); + + !VEML6030_STATUS ? display.println("VEML6030: X") + : display.println("VEML6030: O"); + + !CCS811B_STATUS ? display.println("CCS811B: X") + : display.println("CCS811B: O"); + + !BMP280_STATUS ? display.println("bmp280: X") + : display.println("bmp280: O"); + + !TEMT600_STATUS ? display.println("TEMT600: O") + : display.println("TEMT600: X"); + + display.println("____________"); + display.print("Battery: "); + display.println(_BATTERY); + display.print("Sensors: "); + display.println(_SENSOR); + display.print("WiFi: "); + display.println(_WIFI); + display.print("Server: "); + display.println(_SERVER); + display.print("MicroSD: "); + display.println(_MICROSD); + + display.update(); +} void MimirTesting::DisplayReadings() { diff --git a/lib/MimirTesting/MimirTesting.h b/lib/MimirTesting/MimirTesting.h index 2599eba..b179037 100644 --- a/lib/MimirTesting/MimirTesting.h +++ b/lib/MimirTesting/MimirTesting.h @@ -37,6 +37,8 @@ class MimirTesting void readSensors(bool display = false); void readBattery(bool display = false); + void DisplayDeviceInfo(); + void sendData(bool display = false); void WiFi_ON(); void WiFi_OFF(); @@ -53,9 +55,9 @@ class MimirTesting int _MICROSD = 0; String _IP_ADDRESS; - String _USER = "Lloyd Richards"; - String _USER_ID = "P2003_PROTOTYPE"; - String _DEVICE_ID = "PROTOTYPE3_03"; + char _USER[]; + char _USER_ID[]; + char _DEVICE_ID[]; String TimeStr, DateStr, ErrorMessage; // strings to hold time and date const char *TZ_INFO = "CET-1CEST,M3.5.0,M10.5.0/3"; @@ -87,8 +89,12 @@ class MimirTesting void writeFile(fs::FS &fs, const char *path, const char *message); void appendFile(fs::FS &fs, const char *path, const char *message); + void initConfig(); + void saveConfig(); + void printValue(float value, const char *type, const char *unit, int decimel = 2); void getIPAddress(); + void DisplayWiFiIcon(int x, int y); void DisplayBatteryIcon(int x, int y); void DisplaySensors(); @@ -96,6 +102,7 @@ class MimirTesting void DisplayWiFiSetup(); void DisplayWiFiCredentials(); void DisplaySentData(int httpResponseCode, String response); + void drawString(int x, int y, String text, alignment align); void blinkPixel(int pixel, int R = 255, int G = 0, int B = 0, int repeat = 1); diff --git a/src/main.cpp b/src/main.cpp index 0fd6a72..760f9b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,13 +8,14 @@ void setup() { if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_TIMER) { - + Serial.begin(115200); Wire.begin(); mimirTesting.initDisplay(115200); mimirTesting.initNeoPixels(50); mimirTesting.initSensors(); mimirTesting.initWIFI(); } + pinMode(39, INPUT); mimirTesting.initTimer(); mimirTesting.initDash(); mimirTesting.readBattery(); @@ -22,9 +23,11 @@ void setup() mimirTesting.WiFi_ON(); mimirTesting.sendData(true); mimirTesting.WiFi_OFF(); - mimirTesting.SLEEP(); + mimirTesting.forceStartWiFi(); } void loop() { + mimirTesting.DisplayDeviceInfo(); + delay(5000); } \ No newline at end of file From 116022d3fda90233a3af4384fce1a304bca772e4 Mon Sep 17 00:00:00 2001 From: LloydRichardsIntern <59728961+LloydRichardsIntern@users.noreply.github.com> Date: Wed, 15 Apr 2020 11:20:15 +0200 Subject: [PATCH 2/4] fix: strcpy() Overwrite Fixed a bug where the DeviceID was overwritting all the other device info by setting the byte size of the the strings. Might need to adjust these in the future --- lib/MimirTesting/MimirTesting.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/MimirTesting/MimirTesting.h b/lib/MimirTesting/MimirTesting.h index b179037..c87828d 100644 --- a/lib/MimirTesting/MimirTesting.h +++ b/lib/MimirTesting/MimirTesting.h @@ -55,9 +55,9 @@ class MimirTesting int _MICROSD = 0; String _IP_ADDRESS; - char _USER[]; - char _USER_ID[]; - char _DEVICE_ID[]; + char _USER[40]; + char _USER_ID[40]; + char _DEVICE_ID[40]; String TimeStr, DateStr, ErrorMessage; // strings to hold time and date const char *TZ_INFO = "CET-1CEST,M3.5.0,M10.5.0/3"; From 563947c523a4420cc15da4ba81faf3ccb602da87 Mon Sep 17 00:00:00 2001 From: LloydRichardsIntern <59728961+LloydRichardsIntern@users.noreply.github.com> Date: Wed, 15 Apr 2020 11:49:18 +0200 Subject: [PATCH 3/4] fix: SPIFFS Config.json There was a problem with the config file not writting the params. Forgot to initialize the SPIFFS and so there were errors later on. Have fixed now but needs cleaning --- lib/MimirTesting/MimirTesting.cpp | 11 +++++++++++ lib/MimirTesting/MimirTesting.h | 2 +- src/main.cpp | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/MimirTesting/MimirTesting.cpp b/lib/MimirTesting/MimirTesting.cpp index 1e8be5c..b618663 100644 --- a/lib/MimirTesting/MimirTesting.cpp +++ b/lib/MimirTesting/MimirTesting.cpp @@ -204,6 +204,8 @@ void MimirTesting::initConfig() { if (SPIFFS.begin()) { + + Serial.println("mounted file system"); if (SPIFFS.exists("/config.json")) { //file exists, reading and loading @@ -211,6 +213,7 @@ void MimirTesting::initConfig() fs::File configFile = SPIFFS.open("/config.json", "r"); if (configFile) { + Serial.println("opened config file"); size_t size = configFile.size(); // Allocate a buffer to store contents of the file. std::unique_ptr buf(new char[size]); @@ -230,8 +233,16 @@ void MimirTesting::initConfig() strcpy(_USER_ID, configJson["UserID"] | "N/A"); strcpy(_DEVICE_ID, configJson["DeviceID"] | "N/A"); } + else + { + Serial.println("failed to load json config"); + } } } + else + { + Serial.println("failed to mount FS"); + } } void MimirTesting::saveConfig() diff --git a/lib/MimirTesting/MimirTesting.h b/lib/MimirTesting/MimirTesting.h index c87828d..c70dd8b 100644 --- a/lib/MimirTesting/MimirTesting.h +++ b/lib/MimirTesting/MimirTesting.h @@ -30,6 +30,7 @@ class MimirTesting void initWIFI(bool display = false); void initDash(); void initTimer(); + void initConfig(); void i2cScanner(); void testNeoPixels(int repeat = 3, int delay = 500); @@ -89,7 +90,6 @@ class MimirTesting void writeFile(fs::FS &fs, const char *path, const char *message); void appendFile(fs::FS &fs, const char *path, const char *message); - void initConfig(); void saveConfig(); void printValue(float value, const char *type, const char *unit, int decimel = 2); diff --git a/src/main.cpp b/src/main.cpp index 760f9b9..c7d9d1f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ void setup() Wire.begin(); mimirTesting.initDisplay(115200); mimirTesting.initNeoPixels(50); + mimirTesting.initConfig(); mimirTesting.initSensors(); mimirTesting.initWIFI(); } From ff1aaee515ee6e684f72dcc369751588e88640b8 Mon Sep 17 00:00:00 2001 From: LloydRichardsIntern <59728961+LloydRichardsIntern@users.noreply.github.com> Date: Wed, 15 Apr 2020 12:38:07 +0200 Subject: [PATCH 4/4] fix: Remove Debug Removed some debugging --- lib/MimirTesting/MimirTesting.cpp | 13 ++++--------- src/main.cpp | 2 +- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/MimirTesting/MimirTesting.cpp b/lib/MimirTesting/MimirTesting.cpp index b618663..1580803 100644 --- a/lib/MimirTesting/MimirTesting.cpp +++ b/lib/MimirTesting/MimirTesting.cpp @@ -326,20 +326,11 @@ void MimirTesting::forceStartWiFi() } delay(5000); - - Serial.println(custom_USER.getValue()); - Serial.println(custom_USER_ID.getValue()); - Serial.println(custom_DEVICE_ID.getValue()); - //Update Device Info with Params strcpy(_USER, custom_USER.getValue()); strcpy(_USER_ID, custom_USER_ID.getValue()); strcpy(_DEVICE_ID, custom_DEVICE_ID.getValue()); - Serial.println(_USER); - Serial.println(_USER_ID); - Serial.println(_DEVICE_ID); - saveConfig(); WiFi_OFF(); @@ -409,6 +400,10 @@ void MimirTesting::DisplayDeviceInfo() { display.fillScreen(GxEPD_WHITE); display.setCursor(0, 20); + display.print("Time: "); + display.println(TimeStr); + display.println(DateStr); + display.println("____________"); display.print("User: "); display.println(_USER); display.print("UserID: "); diff --git a/src/main.cpp b/src/main.cpp index c7d9d1f..847869f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,7 +24,7 @@ void setup() mimirTesting.WiFi_ON(); mimirTesting.sendData(true); mimirTesting.WiFi_OFF(); - mimirTesting.forceStartWiFi(); + //mimirTesting.forceStartWiFi(); } void loop()