Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <WebServer.h>
- #include <HTTPClient.h>
- #include <ArduinoJson.h>
- #include <EEPROM.h>
- const char* ssidAP = "facerecognition";
- const char* passwordAP = "123456789";
- WebServer server(80);
- String wifiSSID;
- String wifiPassword;
- String firebaseURL;
- String firebaseAPIKey;
- String firebaseNode;
- const int EEPROM_SIZE = 512;
- const int ledPin = 2;
- unsigned long startTime = 0;
- bool ledOn = false;
- int duration = 0;
- String globalSerialData = "";
- void processJsonAndSendSerial2(const String &jsonString) {
- StaticJsonDocument<200> jsonDoc;
- DeserializationError error = deserializeJson(jsonDoc, jsonString);
- if (error) {
- Serial.print("deserializeJson() failed: ");
- Serial.println(error.f_str());
- return;
- }
- Serial.print("json send to serial => ");
- Serial.println(jsonString);
- Serial2.write(jsonString.toInt());
- }
- bool connectToWiFi() {
- if (wifiSSID.length() > 0) {
- WiFi.softAPdisconnect();
- WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
- int attempts = 0;
- while (WiFi.status() != WL_CONNECTED && attempts < 20) {
- delay(500);
- Serial.print(".");
- attempts++;
- }
- Serial.println();
- return WiFi.status() == WL_CONNECTED;
- }
- return false;
- }
- void setupAccessPoint() {
- WiFi.softAP(ssidAP, passwordAP);
- server.on("/", handleRoot);
- server.on("/submit", handleFormSubmission);
- server.begin();
- Serial.println("Web server started.");
- }
- void saveToEEPROM() {
- EEPROM.writeString(0, wifiSSID);
- EEPROM.writeString(100, wifiPassword);
- EEPROM.writeString(200, firebaseURL);
- EEPROM.writeString(300, firebaseAPIKey);
- EEPROM.writeString(400, firebaseNode);
- EEPROM.commit();
- }
- void handleRoot() {
- server.send(200, "text/html", "<h1>ESP32 Configuration</h1>");
- }
- void handleFormSubmission() {
- if (server.method() == HTTP_POST) {
- wifiSSID = server.arg("ssid");
- wifiPassword = server.hasArg("password") ? server.arg("password") : "";
- firebaseURL = server.arg("firebaseURL");
- firebaseAPIKey = server.arg("firebaseAPIKey");
- firebaseNode = server.arg("firebaseNode");
- saveToEEPROM();
- connectToWiFi() ? Serial.println("Connected to WiFi") : setupAccessPoint();
- }
- }
- void readFromEEPROM() {
- wifiSSID = EEPROM.readString(0);
- wifiPassword = EEPROM.readString(100);
- firebaseURL = EEPROM.readString(200);
- firebaseAPIKey = EEPROM.readString(300);
- firebaseNode = EEPROM.readString(400);
- }
- String getFirebaseTotalSum() {
- if (WiFi.status() == WL_CONNECTED) {
- HTTPClient http;
- String url = firebaseURL + "/" + firebaseNode + "/totalSum/value.json?auth=" + firebaseAPIKey;
- http.begin(url);
- int httpResponseCode = http.GET();
- if (httpResponseCode > 0) {
- String jsonData = http.getString();
- http.end();
- return jsonData;
- }
- http.end();
- }
- return "0";
- }
- String getFirebaseData() {
- if (WiFi.status() == WL_CONNECTED) {
- HTTPClient http;
- String url = firebaseURL + "/" + firebaseNode + "/RX.json?auth=" + firebaseAPIKey;
- http.begin(url);
- int httpResponseCode = http.GET();
- if (httpResponseCode > 0) {
- String jsonData = http.getString();
- http.end();
- return jsonData;
- }
- http.end();
- }
- return "Wi-Fi not connected";
- }
- void setup() {
- Serial.begin(115200);
- Serial2.begin(9600, SERIAL_8N1, 16, 17);
- EEPROM.begin(EEPROM_SIZE);
- readFromEEPROM();
- if (!connectToWiFi()) setupAccessPoint();
- server.on("/", handleRoot);
- server.on("/submit", handleFormSubmission);
- server.begin();
- Serial.println("Web server started.");
- }
- void loop() {
- server.handleClient();
- if (WiFi.status() == WL_CONNECTED) {
- String totalSumStr = getFirebaseTotalSum();
- Serial.println("TotalSum from Firebase: " + totalSumStr);
- int totalSumValue = totalSumStr.toInt();
- if (totalSumValue > 0) {
- String binaryString = String(totalSumValue, BIN);
- Serial.println("Binary totalSum: " + binaryString);
- Serial2.println(binaryString);
- }
- String jsonRXData = getFirebaseData();
- Serial.println("Received from Firebase (RX Data) = " + jsonRXData);
- processJsonAndSendSerial2(jsonRXData);
- delay(500);
- } else {
- setupAccessPoint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement