In this tutorial we saw how we could communicate ESP32, Firebase realtime and an application...
https://community.appinventor.mit.edu/t/esp32-firebasedb-send-receive-ide-arduino/10747
We are going to adapt those codes to ESP8266.
- We can send data from the App to Firebase and display it in the Serial Monitor.
- The ESP8266 code generates two random numbers every 5 seconds, sends them to Firebase and is automatically received by the app.
We will use this library...
We will adapt this code...
https://github.com/mobizt/Firebase-ESP8266/blob/master/examples/Basic/Basic.ino
We will need the address of our project in Firebase realtime...
p117_wemos_firebase_ESP8266.aia (2.9 KB)
We set that FirebaseURL in the Designer.
We will also put a ProjectBucket, in this example ESP8266
When the DataChanged in Firebase, we will get its values.
We can also send data to Firebase.
ESP8266_Firebase.ino
// Juan A. Villalpando
// http://kio4.com/arduino/117_ESP8266_Firebase.htm
#include <ESP8266Firebase.h>
#include <ESP8266WiFi.h>
// SLOW BUT HASTLE-FREE METHOD FOR LONG TERM USAGE.
// DOES NOT REQUIRE PERIODIC UPDATE OF FINGERPRINT
Firebase firebase("kio4b-3C240");
const char* ssid = "Nombre_Red_WiFi";
const char* password = "Clave_WiFi";
float temperatura = 25.0;
float humedad = 87.0;
String texto = "Hola";
String numero = "123";
unsigned long tiempo_actual = 0;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting with ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi conected. IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
// RECEIVE from App
if (firebase.getString("ESP8266/numero")) {
String numero_fb = firebase.getString("ESP8266/numero");
if (numero_fb != numero) {
numero = numero_fb;
Serial.println(numero);
}
}
if (firebase.getString("ESP8266/texto")) {
String texto_fb = firebase.getString("ESP8266/texto");
if (texto_fb != texto) {
texto = texto_fb;
Serial.println(texto);
}
}
///////////////////////////////////////
// SEND to App 2 random numbers every 5 seconds.
if((millis()- tiempo_actual)>= 5000){
temperatura = random(0,10000)/100.0;
humedad = random(0,10000)/100.0;
String datos = (String) temperatura + "|" + (String) humedad;
Serial.println(datos);
//firebase.setFloat("ESP8266/temperatura", temperatura);
//firebase.setFloat("ESP8266/humedad", humedad);
firebase.setString("ESP8266/datos", datos);
tiempo_actual = millis();
}
}
Result.
This tutorial in Spanish.
http://kio4.com/arduino/117B_ESP8266_Firebase.htm