Hace 2 años creé una aplicación en ReactJS con Firebase y leía esos campos de FireBase desde un ESP32 así podía prender y apagar luces remotamente, leer sensores. #### [Aplicación de ReactJS](/files/reactjs_ESP32logger-main.zip) ```bash sudo apt remove cmdtest sudo npm install -g yarn unzip reactjs_ESP32logger-main.zip cd reactjs_ESP32logger-main rm -rf node_modules rm -rf yarn.lock yarn add caniuse-lite@1.0.30001632 ### Agregá en package.json "resolutions": { "caniuse-lite": "1.0.30001632" }, ### Borrá todos los babels de yarn.lock The solution was to manually delete all @babel/* entries from yarn.lock and running: yarn cache clean && yarn install && yarn dedupe to let Yarn regenerate those with the latest applicable version and hopefully compatible manner. This helped the parsing issue, but a new issue that's mentioned by OP popped up. yarn install yarn build export NODE_OPTIONS=--openssl-legacy-provider yarn start ``` ##### Para arreglar el problema de 404: https://stackoverflow.com/questions/45412014/how-do-i-set-the-start-url-of-a-manifest-json-to-be-the-root-of-the-site https://stackoverflow.com/questions/78856773/requires-babel-7-16-0-but-was-loaded-with-7-12-3 ### Código que usé en el Arduino o FanIOT. Para este código usé librerías que ahora las voy a escribir. Como crear el usuario firebase y obtener la `API_KEY`: https://www.youtube.com/watch?v=cm-Qe2HMJGk #### Temp Logger WifiManager -> acceder Wifi usando AP temporal para no hardcodear credenciales. Abrir servidor web en Arduino (crear) * Crear página web para loggear y registrar cuentas en el servidor de Arduino usando Firebase. * Pasar el token de login a Arduino. * Usar mDNS para no utilizar IP dinámica. * Guardar en la EEPROM su identificación de FireBase (opcional). * Usar SNIFFS para servir archivos HTML y un servidor asíncrono para parsear argumento tipo GET y POST. * Instalar Firebase client esp32. * Guardar datos de humedad en el respectivo usuario logueado. ##### Bibliotecas usadas: * WifiManager * ESPmDNS * AsyncWebServerESP32 * SNIFFS * FirebaseESP32 * AsyncTCP * ArduinoJson * ds18b20 (dallas temp.) * OneWire (Max 31850) * Adafruit ssd1306 * adafruit GFX Probablemente no necesito usar todas esas librerías pero eso es lo que usé. **Todo este código lo cargás en el ESP32 o FanIOT y levantará un servidor web en donde tendrás que ingresar el mismo usuario que ingresás en la app de reactjs, y ahí podrás leer los datos, aparte es mejor que cambies la base de datos de firebase por una tuya** ```c #include // https://github.com/tzapu/WiFiManager // // A simple server implementation showing how to: // * serve static messages // * read GET and POST parameters // * handle missing pages / 404s // #include #include #include #include #include #include #include #include #include #include #include "time.h" #include //https://github.com/FastLED/FastLED #define LED_PIN 27 #define NUM_LEDS 4 #define BRIGHTNESS 75 #define LED_TYPE WS2812B #define COLOR_ORDER GRB//RGB CRGB leds[NUM_LEDS]; #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); #define Pagina_Login R"====(

Login Form

Forgot password?
)====" const char *Pagina = R"====(Panel

Cargando...

Panel


Guardando cada 3 minutos valores de los sensores en el usuario:

Reloj Arduino (GMT):

Sensor Temperatura

Sensor LDR
Intensidad de Luz:
Sensor TILT
Buzzer
LED 1
LED 2
LED 3
LEDs Inteligentes WS2812b
OLED 128X32
Ingresa el Mensaje:
)===="; // Provide the token generation process info. #include // Provide the RTDB payload printing info and other helper functions. #include #define API_KEY "AIzaSyAXZzTwC_Nqh-ZCepllSi8XKTnOcd6UtY4" /* 3. If work with RTDB, define the RTDB URL */ //#define DATABASE_URL "http://35.201.97.85/" #define DATABASE_URL "http://esp32-bd971-default-rtdb.firebaseio.com/" // GPIO where the DS18B20 is connected to const int oneWireBus = 19; // Setup a oneWire instance to communicate with any OneWire devices OneWire oneWire(oneWireBus); // Pass our oneWire reference to Dallas Temperature sensor DallasTemperature sensors(&oneWire); bool logged = false; FirebaseJson json; FirebaseJsonArray WS2812b_; FirebaseJsonData result; /* 4. Define the Firebase Data object */ FirebaseData fbdo; /* 5. Define the FirebaseAuth data for authentication data */ FirebaseAuth auth; /* 6. Define the FirebaseConfig data for config data */ FirebaseConfig config; void signIn(String email, String password) { /* Assign the user sign in credentials */ auth.user.email = email; auth.user.password = password; /* Initialize the library with the Firebase authen and config */ logged = true; Firebase.begin(&config, &auth); } int one_light_pin= 32; int two_light_pin = 33; int three_light_pin = 25; // Variable to save USER UID String uid; // Database main path (to be updated in setup with the user UID) String databasePath; String databasePath_write; // Database child nodes const char *tempPath_c = "/temperature_c"; const char *tempPath_f = "/temperature_f"; const char *ldrPath = "/ldr"; const char *timePath = "/timestamp"; const char *tiltPath = "/tilt"; const char *gmtTimePath = "/gmtTime"; // Parent Node (to be updated in every loop) //char parentPath[100]; #define BUZZER_PIN 14 // ESP32 pin GIOP18 connected to piezo buzzer int timestamp; const char* ntpServer = "pool.ntp.org"; float temperature; // Timer variables (send new readings every three minutes) unsigned long sendDataPrevMillis = 0; unsigned long timerDelay = 1800000; // 180000 //unsigned long timerDelay = 3600; int sensorPinTILT = 26; int sensorPinLDR = 39; // select the input pin for LDR int sensorValue = 0; // variable to store the value coming from the sensor // Function that gets current epoch time unsigned long getTime() { time_t now; struct tm timeinfo; if (!getLocalTime(&timeinfo)) { //Serial.println("Failed to obtain time"); return(0); } time(&now); return now; } char timeStringBuff[50]; char *printLocalTime() { time_t rawtime; struct tm timeinfo; if(!getLocalTime(&timeinfo)) { Serial.println("Failed to obtain time"); sprintf(timeStringBuff,"Error al obtener fecha"); return timeStringBuff; } //char timeStringBuff[50]; //50 chars should be enough //strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo); strftime(timeStringBuff, 50, "%A, %B %d %Y %H:%M:%S", &timeinfo); //print like "const char*" Serial.println(timeStringBuff); //Optional: Construct String object return timeStringBuff; } bool signupOK = false; AsyncWebServer server(1234); void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); } // ... void RGB_color(int red_light_value, int green_light_value, int blue_light_value) { analogWrite(one_light_pin, red_light_value); analogWrite(two_light_pin, green_light_value); analogWrite(three_light_pin, blue_light_value); } void setup() { // put your setup code here, to run once: Serial.begin(115200); configTime(-1*3600*3, 0, ntpServer); WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP // it is a good practice to make sure your code sets wifi mode how you want it. FastLED.addLeds(leds, NUM_LEDS); FastLED.setBrightness(BRIGHTNESS); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 Serial.println(F("SSD1306 allocation failed")); for(;;); } display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); // Display static text display.println("Ingrese con su cuenta de Firebase."); display.display(); display.startscrollleft(0x00, 0x0F); pinMode(one_light_pin, OUTPUT); pinMode(two_light_pin, OUTPUT); pinMode(three_light_pin, OUTPUT); pinMode(BUZZER_PIN, OUTPUT); pinMode(sensorPinTILT, INPUT); digitalWrite(sensorPinTILT, HIGH); //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around WiFiManager wm; // settings - wipe stored credentials for testing // these are stored by the esp library //wm.resetSettings(); // Automatically connect using saved credentials, // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"), // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect()) // then goes into a blocking loop awaiting configuration and will return success result bool res; // res = wm.autoConnect(); // auto generated AP name from chipid // res = wm.autoConnect("AutoConnectAP"); // anonymous ap res = wm.autoConnect("AutoConnectAP","password"); // password protected ap if(!res) { Serial.println("Failed to connect"); //return; ESP.restart(); } else { //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); Serial.print("Connected with IP: "); Serial.println(WiFi.localIP()); Serial.println(); Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION); /* Assign the API key (required) */ config.api_key = API_KEY; /* Assign the RTDB URL */ config.database_url = DATABASE_URL; Firebase.reconnectWiFi(true); fbdo.setResponseSize(2048); // If the signupError.message showed "ADMIN_ONLY_OPERATION", you need to enable Anonymous provider in project's Authentication. /* Assign the callback function for the long running token generation task */ config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h /** The id token (C++ string) will be available from config.signer.tokens.id_token * if the sig-up was successful. * * Now you can initialize the library using the internal created credentials. * * If the sign-up was failed, the following function will initialize because * the internal authentication credentials are not created. */ //config.max_token_generation_retry = 5; //Firebase.begin(&config, &auth); display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); // Display static text display.println(WiFi.localIP().toString()); display.startscrollleft(0x00, 0x0F); /*server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ sensors.requestTemperatures(); request->send(200, "text/html", "

HOLA USUARIO "+String(uid)+"

SENSOR LDR: " + String(analogRead(sensorPinLDR)) + "
TEMPERATURA EN CELSIUS: "+String(sensors.getTempCByIndex(0))+"
Temp en Fahrenheit: "+String(sensors.getTempFByIndex(0))+"
TILT: "+ String(digitalRead(sensorPinTILT)) +"

"); });*/ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(200, "text/html", Pagina); }); server.on("/buzzer", HTTP_GET, [](AsyncWebServerRequest *request){ AsyncResponseStream *response = request->beginResponseStream("application/json"); DynamicJsonDocument json(1024); json["status"] = "ok"; digitalWrite(BUZZER_PIN, HIGH); delay(2000); digitalWrite(BUZZER_PIN, LOW); serializeJson(json, *response); request->send(response); }); server.on("/login", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(200, "text/html", Pagina_Login); }); server.on("/_login", HTTP_GET, [] (AsyncWebServerRequest *request) { AsyncResponseStream *response = request->beginResponseStream("application/json"); DynamicJsonDocument json(1024); String usuario; String clave; bool error = false; if (request->hasParam("uname") && request->hasParam("psw")) { usuario = request->getParam("uname")->value(); clave = request->getParam("psw")->value(); signIn(usuario, clave); display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); // Display static text display.println("OK."); display.display(); json["status"] = "ok"; serializeJson(json, *response); request->send(response); } else { error = true; json["status"] = "error"; serializeJson(json, *response); request->send(response); } }); server.on("/api/led/apagar", HTTP_GET, [] (AsyncWebServerRequest *request) { AsyncResponseStream *response = request->beginResponseStream("application/json"); DynamicJsonDocument json(1024); String led; bool error = false; if (request->hasParam("led")) { led = request->getParam("led")->value(); if (led.compareTo("1") == 0) analogWrite(one_light_pin, 0); else if (led.compareTo("2") == 0) analogWrite(two_light_pin, 0); else if (led.compareTo("3") == 0) analogWrite(three_light_pin, 0); //else if (led.compareTo("4") == 0) {FastLED.clear();FastLED.show();} } else { error = true; } if (error) json["status"] = "ok"; else{json["status"] = "error";json["message"] = "El parametro led no puede ser nulo.";} serializeJson(json, *response); request->send(response); }); server.on("/api/led/encender", HTTP_GET, [] (AsyncWebServerRequest *request) { AsyncResponseStream *response = request->beginResponseStream("application/json"); DynamicJsonDocument json(1024); String led; bool error = false; if (request->hasParam("led")) { led = request->getParam("led")->value(); Serial.println("el doctor muerte a la yuta uno por uno"); Serial.println(led); if (led.compareTo("1") == 0) analogWrite(one_light_pin, 255); if (led.compareTo("2") == 0) analogWrite(two_light_pin, 255); if (led.compareTo("3") == 0)analogWrite(three_light_pin, 255); } else { error = true; } if (error) json["status"] = "ok"; else{json["status"] = "error";json["message"] = "Falta el parametro led.";} serializeJson(json, *response); request->send(response); }); server.on("/api/display", HTTP_GET, [] (AsyncWebServerRequest *request) { AsyncResponseStream *response = request->beginResponseStream("application/json"); DynamicJsonDocument json(1024); bool error = false; String text; if (request->hasParam("message")) { text = request->getParam("message")->value(); display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); // Display static text display.println(text); display.display(); display.startscrollleft(0x00, 0x0F); } else { error = true; } if (error) json["status"] = "ok"; else{json["status"] = "error";json["message"] = "Falta el parametro message.";} serializeJson(json, *response); request->send(response); }); // Send a GET request to /get?message= server.on("/api/sensors", HTTP_GET, [] (AsyncWebServerRequest *request) { AsyncResponseStream *response = request->beginResponseStream("application/json"); DynamicJsonDocument json(1024); //json["localtime"] = String(printLocalTime()); json["localtime"] = printLocalTime(); json["temp_celsius"] = sensors.getTempCByIndex(0); json["temp_fahrenheit"] = sensors.getTempFByIndex(0); json["tilt"] = digitalRead(sensorPinTILT); json["ldr"] = analogRead(sensorPinLDR); json["user"] = uid; serializeJson(json, *response); request->send(response); }); server.onNotFound(notFound); DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*"); server.begin(); sensors.begin(); // Getting the user UID might take a few seconds Serial.println("Getting User UID"); while ((auth.token.uid) == "") { Serial.print('.'); delay(1000); } // Print user UID uid = auth.token.uid.c_str(); //sprintf(uid, "%s", auth.token.uid); Serial.print("User UID: "); Serial.println(uid); // Update database path databasePath = "/UsersData/" + uid + "/readings"; //sprintf(databasePath, "/UsersData/%s/readings", uid); databasePath_write = "/UsersData/" + uid + "/writtings/tempLogger"; //sprintf(databasePath_write, "/UsersData/%s/writtings/tempLogger", uid); } } // String path = auth.token.uid.c_str(); //<- user uid void loop() { // Send new readings to database if (Firebase.ready() && logged && (millis() - sendDataPrevMillis > timerDelay || sendDataPrevMillis == 0)){ sendDataPrevMillis = millis(); //Get current timestamp //timestamp = getTime(); time_t now; struct tm timeinfo; if (!getLocalTime(&timeinfo)) { //Serial.println("Failed to obtain time"); timestamp = 0; } time(&now); timestamp = now; //timestamp = 1234; Serial.print ("time: "); Serial.println (timestamp); //String parentPath; //parentPath = databasePath.c_str() + "/" + String(timestamp); char parentPath[100]; sprintf(parentPath,"%s/%d",databasePath.c_str(),timestamp); sensors.requestTemperatures(); float celsius = sensors.getTempCByIndex(0); float fahrenheit = sensors.getTempFByIndex(0); while (celsius == -127.00 || fahrenheit == -196.60){ sensors.requestTemperatures(); celsius = sensors.getTempCByIndex(0); fahrenheit = sensors.getTempFByIndex(0); delay(5000); } json.set(tempPath_c, String(celsius)); json.set(tempPath_f, String(fahrenheit)); json.set(ldrPath, String(analogRead(sensorPinLDR))); json.set(tiltPath, String(digitalRead(sensorPinTILT))); json.set(timePath, timestamp); json.set(gmtTimePath, printLocalTime()); Serial.printf("Set json... %s\n", Firebase.RTDB.setJSON(&fbdo, parentPath, &json) ? "ok" : fbdo.errorReason().c_str()); Serial.printf("Getting json...\n"); char aux[100]; int led_rojo,led_amarillo,led_verde,_buzzer; char _oled[200]; sprintf(aux,"%s/led_rojo",databasePath_write.c_str()); Serial.println(aux); Firebase.RTDB.getInt(&fbdo, aux, &led_rojo); sprintf(aux,"%s/led_amarillo",databasePath_write.c_str()); Serial.println(aux); Firebase.RTDB.getInt(&fbdo, aux, &led_amarillo); sprintf(aux,"%s/led_verde",databasePath_write.c_str()); Serial.println(aux); //Serial.println('led verde:'); Serial.println(led_verde); Firebase.RTDB.getInt(&fbdo, aux, &led_verde); sprintf(aux,"%s/buzzer",databasePath_write.c_str()); Serial.println(aux); Firebase.RTDB.getInt(&fbdo, aux, &_buzzer); int on_off; for (int i=1; i<5; i++){ sprintf(aux,"%s/WS2812b%d",databasePath_write.c_str(),i); //Serial.println('working?'); Serial.println(aux); Firebase.RTDB.getArray(&fbdo, aux, &WS2812b_); WS2812b_.get(result,0); String colorString = result.to(); Serial.println(colorString); long color_hex = strtol(colorString.c_str(), NULL, 16); leds[i-1] = color_hex; WS2812b_.get(result,1); on_off = result.to(); //Serial.println('reckful'); Serial.println(on_off); if(on_off) FastLED.show(); else{ leds[i-1] = strtol("0x000000", NULL,16) ;FastLED.show(); } } sprintf(aux,"%s/oled",databasePath_write.c_str()); Firebase.RTDB.getString(&fbdo, aux, _oled); /*if (_led1 == 1) analogWrite(one_light_pin, 255); if (_led2 == 1) analogWrite(two_light_pin, 255); if (_led3 == 1) analogWrite(three_light_pin, 255); if (_led1 == 0) analogWrite(one_light_pin, 0); if (_led2 == 0) analogWrite(two_light_pin, 0); if (_led3 == 0) analogWrite(three_light_pin, 0);*/ if (_buzzer){ sprintf(aux,"%s/buzzer",databasePath_write.c_str()); Firebase.RTDB.set(&fbdo, aux, false); digitalWrite(BUZZER_PIN, HIGH); delay(2000); digitalWrite(BUZZER_PIN, LOW); } if (!led_rojo) analogWrite(one_light_pin, 0); if (!led_amarillo) analogWrite(two_light_pin, 0); if (!led_verde) analogWrite(three_light_pin, 0); if (led_rojo) analogWrite(one_light_pin, 255); if (led_amarillo) analogWrite(two_light_pin, 255); if (led_verde) analogWrite(three_light_pin, 255); Serial.println("Contenido de _oled: "); Serial.println(_oled); if(strcmp(_oled,"") != 0){ display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); // Display static text display.println(_oled); display.display(); display.startscrollleft(0x00, 0x0F); } } } ``` Más información: https://redirect.invidious.io/search?q=firebase+esp32