From d7a838637f85f3b0016ca07c91dc9be6712fc9bc Mon Sep 17 00:00:00 2001 From: joshua-8 <59814881+joshua-8@users.noreply.github.com> Date: Tue, 20 Jul 2021 20:44:33 -0700 Subject: [PATCH] close #37 close #35 close #28 close #25 --- .gitignore | 2 +- scmc/firebase.ino | 30 +++++++++++------ scmc/scmc.ino | 85 +++++++++++++++++++++++------------------------ 3 files changed, 61 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index 823e0f1..3798629 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -*.ini +desktop.ini scmc/secret.h diff --git a/scmc/firebase.ino b/scmc/firebase.ino index e6edcf4..5c2ce80 100644 --- a/scmc/firebase.ino +++ b/scmc/firebase.ino @@ -20,12 +20,12 @@ boolean firebaseGetSettings() { if (Firebase.RTDB.getJSON(&fbdo, "/settings")) { fjson = fbdo.jsonObject(); - // fjson.get(jsonData, "/OTAEnable"); - // if (jsonData.typeNum == FirebaseJson::JSON_BOOL) { - // otaEnable = jsonData.boolValue; - // } else { - // Serial.println("error: OTAEnable type wrong"); - // } + fjson.get(jsonData, "/liveDataUpdateIntervalMillis"); + if (jsonData.type == "int") { + liveDataUpdateMillisInterval = jsonData.intValue; + } else { + Serial.println("ERROR! Firebase (settings) liveDataUpdateIntervalMillis type wrong"); + } fjson.clear(); return true; @@ -38,8 +38,14 @@ boolean firebaseGetSettings() { boolean firebaseSendDebug() { fjson.clear(); fjson.set("local ip", WiFi.localIP().toString()); - fjson.set("OTAEnabled", otaEnable); + fjson.set("OTAEnabled (read)", otaEnable); fjson.set("time", int(timestampEpoch)); + fjson.set("minAfterMidnight", minutesAfterMidnight()); + fjson.set("sunrise", sunriseTime()); + fjson.set("sunset", sunsetTime()); + fjson.set("liveDataUpdateMillisInterval (read)", int(liveDataUpdateMillisInterval)); + fjson.set("seconds since boot", int(millis() / 1000)); + if (Firebase.RTDB.updateNodeSilent(&fbdo, "/debug", &fjson)) { return true; } else { @@ -53,10 +59,10 @@ boolean firebaseRecvDebug() { fjson = fbdo.jsonObject(); fjson.get(jsonData, "/EnableOTA"); - if (jsonData.typeNum == FirebaseJson::JSON_BOOL) { + if (jsonData.type == "bool") { otaEnable = jsonData.boolValue; } else { - Serial.println("error: EnableOTA type wrong"); + Serial.println("ERROR! Firebase (RecvDebug) EnableOTA type wrong"); } //add debug data to recieve @@ -66,9 +72,11 @@ boolean firebaseRecvDebug() { if (jsonData.boolValue) { fjson.set("/REBOOT", false); Firebase.RTDB.updateNodeSilent(&fbdo, "/debug", &fjson); - Serial.println("REBOOTING (settings/REBOOT equaled true in firebase)"); + Serial.println("REBOOTING! (settings/REBOOT equaled true in firebase)"); rebootESP32(); } + } else { + Serial.println("ERROR! Firebase (RecvDebug) REBOOT type wrong"); } } else { @@ -119,7 +127,7 @@ boolean firebaseDeleteOldData(String path, unsigned long interval, byte num) { if (!Firebase.RTDB.deleteNode(&fbdo, nodeToDelete.c_str())) { Serial.println("ERROR! Firebase (delete delete)"); Serial.println(fbdo.errorReason()); - // report = false; deleteNode seems to work even though error is thrown + //report = false; deleteNode seems to work even though error is thrown TODO: uncomment this } } fjson.iteratorEnd(); diff --git a/scmc/scmc.ino b/scmc/scmc.ino index 3956519..7a8ee04 100644 --- a/scmc/scmc.ino +++ b/scmc/scmc.ino @@ -6,12 +6,10 @@ Made by members of Brown University club "Scientists for a Sustainable World" (s4sw@brown.edu) 2021 https://github.com/brown-SSW/brown-solar-charger */ - +#include #include //used for sending stuff over the wifi radio within the esp32 -#include //used for having fixed size buffers that discard the oldest data https://github.com/rlogiacco/CircularBuffer -#include //https://github.com/knolleary/pubsubclient #include //library for sending email https://github.com/mobizt/ESP-Mail-Client -#include //sunrise sunset lookup https://github.com/dmkishi/Dusk2Dawn remove "static" from library as needed to fix compiling error +#include //sunrise sunset lookup https://github.com/dmkishi/Dusk2Dawn DOWNLOAD ZIP OF MASTER AND INSTALL MANUALLY SINCE THE RELEASE (1.0.1) IS OUTDATED AND DOESN'T COMPILE! #include //firebase library https://github.com/mobizt/Firebase-ESP-Client #include "secret.h" //passwords and things we don't want public can be kept in this file since it doesn't upload to github (in gitignore) The file should be kept somewhat secure. @@ -22,10 +20,19 @@ boolean timeAvailable = false; boolean firebaseAvailable = false; boolean firebaseStarted = false; -boolean muteAllAlerts = false; +boolean firebaseAvailSettings = true; +boolean firebaseAvailSendDebug = true; +boolean firebaseAvailRecvDebug = true; +boolean firebaseAvailSendLive = true; +boolean firebaseAvailDeleteDay = true; +boolean firebaseAvailDay = true; +boolean firebaseAvailDeleteMonth = true; +boolean firebaseAvailMonth = true; + + +boolean muteAllAlerts = true; boolean firebaseAvailableAlerted = true; -boolean firebaseAvailableHelper = true; boolean firebaseRanSomething = false; boolean otaEnable = true; @@ -50,19 +57,19 @@ float dayGenWh = 0.0; float dayHoursUsed = 0; unsigned long lastLiveUpdateMillis = 0; -long lastLiveUpdateMillisInterval = 30000; +long liveDataUpdateMillisInterval = 30000; unsigned long lastDayDataUpdateMillis = 0; -long lastDayDataUpdateMillisInterval = 30000; +long dayDataUpdateMillisInterval = 30000; unsigned long lastMonthDataUpdateMillis = 0; -long lastMonthDataUpdateMillisInterval = 100000; +long monthDataUpdateMillisInterval = 100000; //change to daily timer unsigned long lastCalcUpdateMillis = 0; -long lastCalcUpdateMillisInterval = 1000; +long calcUpdateMillisInterval = 1000; unsigned long lastLoadSettingsMillis = 0; -long lastLoadSettingsMillisInterval = 10000; +long loadSettingsMillisInterval = 10000; long wifiCheckIntervalMillis = 5000; @@ -90,13 +97,20 @@ void loop() { connectFirebase(); firebaseStarted = true; } - firebaseAvailableHelper = (timeAvailable && firebaseStarted && wifiAvailable); runLiveUpdate(); runDayDataUpdate(); runMonthDataUpdate(); runSettingsDebugUpdate(); - if (firebaseRanSomething) { - firebaseAvailable = firebaseAvailableHelper; + if (firebaseRanSomething && firebaseStarted) { + firebaseAvailable = + firebaseAvailSettings && + firebaseAvailSendDebug && + firebaseAvailRecvDebug && + firebaseAvailSendLive && + firebaseAvailDeleteDay && + firebaseAvailDay && + firebaseAvailDeleteMonth && + firebaseAvailMonth; } if (firebaseAvailable) { @@ -121,7 +135,7 @@ void setSafe() { } void runCalc() { - if (millis() - lastCalcUpdateMillis > lastCalcUpdateMillisInterval) { + if (millis() - lastCalcUpdateMillis > calcUpdateMillisInterval) { cumuWhGenHelper += 1.0 * liveGenW * (millis() - lastCalcUpdateMillis) / (1000 * 60 * 60); cumulativeWhGen += long(cumuWhGenHelper); cumuWhGenHelper -= long(cumuWhGenHelper); @@ -130,45 +144,32 @@ void runCalc() { } void runLiveUpdate() { - if (millis() - lastLiveUpdateMillis > lastLiveUpdateMillisInterval) { + if (millis() - lastLiveUpdateMillis > liveDataUpdateMillisInterval) { lastLiveUpdateMillis = millis(); firebaseRanSomething = true; if (timeAvailable) { // don't want to give inaccurate timestamps - if (!firebaseSendLiveData()) { - firebaseAvailableHelper = false; - } + firebaseAvailSendLive = firebaseSendLiveData(); } } } void runSettingsDebugUpdate() { - if (millis() - lastLoadSettingsMillis > lastLoadSettingsMillisInterval) { + if (millis() - lastLoadSettingsMillis > loadSettingsMillisInterval) { lastLoadSettingsMillis = millis(); firebaseRanSomething = true; - if (!firebaseGetSettings()) { - firebaseAvailableHelper = false; - } - if (!firebaseRecvDebug()) { - firebaseAvailableHelper = false; - } - if (!firebaseSendDebug()) { - firebaseAvailableHelper = false; - } + firebaseAvailSettings = firebaseGetSettings(); + firebaseAvailRecvDebug = firebaseRecvDebug(); + firebaseAvailSendDebug = firebaseSendDebug(); } } void runDayDataUpdate() { - - if (millis() - lastDayDataUpdateMillis > lastDayDataUpdateMillisInterval) { + if (millis() - lastDayDataUpdateMillis > dayDataUpdateMillisInterval) { lastDayDataUpdateMillis = millis(); firebaseRanSomething = true; if (timeAvailable) { - if (!firebaseSendDayData()) { - firebaseAvailableHelper = false; - } - if (!firebaseDeleteOldData("/data/dayData/", 24 * 60 * 60, 2)) { - firebaseAvailableHelper = false; - } + firebaseAvailDay = firebaseSendDayData(); + firebaseAvailDeleteDay = firebaseDeleteOldData("/data/dayData/", 24 * 60 * 60, 2); } liveGenW = 10000; @@ -179,16 +180,12 @@ void runDayDataUpdate() { void runMonthDataUpdate() { - if (millis() - lastMonthDataUpdateMillis > lastMonthDataUpdateMillisInterval) { + if (millis() - lastMonthDataUpdateMillis > monthDataUpdateMillisInterval) { lastMonthDataUpdateMillis = millis(); firebaseRanSomething = true; if (timeAvailable) { - if (!firebaseSendMonthData()) { - firebaseAvailableHelper = false; - } - if (!firebaseDeleteOldData("/data/monthData/", 31 * 24 * 60 * 60, 2)) { - firebaseAvailableHelper = false; - } + firebaseAvailMonth = firebaseSendMonthData(); + firebaseAvailDeleteMonth = firebaseDeleteOldData("/data/monthData/", 31 * 24 * 60 * 60, 2); } //after testing, these variables should actually be reset to 0 for the next day dayGenWh = random(1400, 2000);