Advertisement
solielios

totalsum arduino

Feb 4th, 2025
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>
  2. #include <WebServer.h>
  3. #include <HTTPClient.h>
  4. #include <ArduinoJson.h>
  5. #include <EEPROM.h>
  6.  
  7. const char* ssidAP = "facerecognition";
  8. const char* passwordAP = "123456789";
  9.  
  10. WebServer server(80);
  11.  
  12. String wifiSSID;
  13. String wifiPassword;
  14. String firebaseURL;
  15. String firebaseAPIKey;
  16. String firebaseNode;
  17.  
  18. const int EEPROM_SIZE = 512;
  19. const int ledPin = 2;
  20.  
  21. unsigned long startTime = 0;
  22. bool ledOn = false;
  23. int duration = 0;
  24. String globalSerialData = "";
  25.  
  26. void processJsonAndSendSerial2(const String &jsonString) {
  27.     StaticJsonDocument<200> jsonDoc;
  28.     DeserializationError error = deserializeJson(jsonDoc, jsonString);
  29.     if (error) {
  30.         Serial.print("deserializeJson() failed: ");
  31.         Serial.println(error.f_str());
  32.         return;
  33.     }
  34.     Serial.print("json send to serial => ");
  35.     Serial.println(jsonString);
  36.     Serial2.write(jsonString.toInt());
  37. }
  38.  
  39. bool connectToWiFi() {
  40.     if (wifiSSID.length() > 0) {
  41.         WiFi.softAPdisconnect();
  42.         WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
  43.         int attempts = 0;
  44.         while (WiFi.status() != WL_CONNECTED && attempts < 20) {
  45.             delay(500);
  46.             Serial.print(".");
  47.             attempts++;
  48.         }
  49.         Serial.println();
  50.         return WiFi.status() == WL_CONNECTED;
  51.     }
  52.     return false;
  53. }
  54.  
  55. void setupAccessPoint() {
  56.     WiFi.softAP(ssidAP, passwordAP);
  57.     server.on("/", handleRoot);
  58.     server.on("/submit", handleFormSubmission);
  59.     server.begin();
  60.     Serial.println("Web server started.");
  61. }
  62.  
  63. void saveToEEPROM() {
  64.     EEPROM.writeString(0, wifiSSID);
  65.     EEPROM.writeString(100, wifiPassword);
  66.     EEPROM.writeString(200, firebaseURL);
  67.     EEPROM.writeString(300, firebaseAPIKey);
  68.     EEPROM.writeString(400, firebaseNode);
  69.     EEPROM.commit();
  70. }
  71.  
  72. void handleRoot() {
  73.     server.send(200, "text/html", "<h1>ESP32 Configuration</h1>");
  74. }
  75.  
  76. void handleFormSubmission() {
  77.     if (server.method() == HTTP_POST) {
  78.         wifiSSID = server.arg("ssid");
  79.         wifiPassword = server.hasArg("password") ? server.arg("password") : "";
  80.         firebaseURL = server.arg("firebaseURL");
  81.         firebaseAPIKey = server.arg("firebaseAPIKey");
  82.         firebaseNode = server.arg("firebaseNode");
  83.         saveToEEPROM();
  84.         connectToWiFi() ? Serial.println("Connected to WiFi") : setupAccessPoint();
  85.     }
  86. }
  87.  
  88. void readFromEEPROM() {
  89.     wifiSSID = EEPROM.readString(0);
  90.     wifiPassword = EEPROM.readString(100);
  91.     firebaseURL = EEPROM.readString(200);
  92.     firebaseAPIKey = EEPROM.readString(300);
  93.     firebaseNode = EEPROM.readString(400);
  94. }
  95.  
  96. String getFirebaseTotalSum() {
  97.     if (WiFi.status() == WL_CONNECTED) {
  98.         HTTPClient http;
  99.         String url = firebaseURL + "/" + firebaseNode + "/totalSum/value.json?auth=" + firebaseAPIKey;
  100.         http.begin(url);
  101.         int httpResponseCode = http.GET();
  102.         if (httpResponseCode > 0) {
  103.             String jsonData = http.getString();
  104.             http.end();
  105.             return jsonData;
  106.         }
  107.         http.end();
  108.     }
  109.     return "0";
  110. }
  111.  
  112. String getFirebaseData() {
  113.     if (WiFi.status() == WL_CONNECTED) {
  114.         HTTPClient http;
  115.         String url = firebaseURL + "/" + firebaseNode + "/RX.json?auth=" + firebaseAPIKey;
  116.         http.begin(url);
  117.         int httpResponseCode = http.GET();
  118.         if (httpResponseCode > 0) {
  119.             String jsonData = http.getString();
  120.             http.end();
  121.             return jsonData;
  122.         }
  123.         http.end();
  124.     }
  125.     return "Wi-Fi not connected";
  126. }
  127.  
  128. void setup() {
  129.     Serial.begin(115200);
  130.     Serial2.begin(9600, SERIAL_8N1, 16, 17);
  131.     EEPROM.begin(EEPROM_SIZE);
  132.     readFromEEPROM();
  133.     if (!connectToWiFi()) setupAccessPoint();
  134.     server.on("/", handleRoot);
  135.     server.on("/submit", handleFormSubmission);
  136.     server.begin();
  137.     Serial.println("Web server started.");
  138. }
  139.  
  140. void loop() {
  141.     server.handleClient();
  142.     if (WiFi.status() == WL_CONNECTED) {
  143.         String totalSumStr = getFirebaseTotalSum();
  144.         Serial.println("TotalSum from Firebase: " + totalSumStr);
  145.         int totalSumValue = totalSumStr.toInt();
  146.         if (totalSumValue > 0) {
  147.             String binaryString = String(totalSumValue, BIN);
  148.             Serial.println("Binary totalSum: " + binaryString);
  149.             Serial2.println(binaryString);
  150.         }
  151.         String jsonRXData = getFirebaseData();
  152.         Serial.println("Received from Firebase (RX Data) = " + jsonRXData);
  153.         processJsonAndSendSerial2(jsonRXData);
  154.         delay(500);
  155.     } else {
  156.         setupAccessPoint();
  157.     }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement