Skip to content

Commit

Permalink
close #37 close #35 close #28 close #25
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-8 committed Jul 21, 2021
1 parent a4a0c17 commit d7a8386
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

*.ini
desktop.ini
scmc/secret.h
30 changes: 19 additions & 11 deletions scmc/firebase.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
85 changes: 41 additions & 44 deletions scmc/scmc.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
Made by members of Brown University club "Scientists for a Sustainable World" ([email protected]) 2021 https://github.com/brown-SSW/brown-solar-charger
*/

#include <Arduino.h>
#include <WiFi.h> //used for sending stuff over the wifi radio within the esp32
#include <CircularBuffer.h> //used for having fixed size buffers that discard the oldest data https://github.com/rlogiacco/CircularBuffer
#include <PubSubClient.h> //https://github.com/knolleary/pubsubclient
#include <ESP_Mail_Client.h> //library for sending email https://github.com/mobizt/ESP-Mail-Client
#include <Dusk2Dawn.h> //sunrise sunset lookup https://github.com/dmkishi/Dusk2Dawn remove "static" from library as needed to fix compiling error
#include <Dusk2Dawn.h> //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_ESP_Client.h> //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.
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit d7a8386

Please sign in to comment.