ESP32. FirebaseDB. Send. Receive. IDE Arduino

Hello friends,

0.- Board D1 R32 ESP32.

esp322b
comparativa
Search images: D1 R32 ESP32

I have done several tutorials with the ESP32:

In this tutorial we are going to see how to send and receive data to/from FireBaseDB with App Inventor and ESP32.

1 Like

1.- Register in FireBase.

  • Add Project.
  • Project settings / Service accounts / Database secrets.
  • Create database.
  • Rules.

You can look this process in:
https://iotdesignpro.com/projects/iot-controlled-led-using-firebase-database-and-esp32

Here in Spanish with many images:
http://kio4.com/arduino/117_Wemos_Firebase.htm

2 Likes

2.- App Inventor sends number and text to ESP32 by Firebase. ESP32 sends to App Inventor random numbers.

p117_wemos_firebase.aia (3.1 KB)

ooooooooooooooo000ooooooooooooo
- CODE ESP32

// Juan A. Villalpando
// http://kio4.com/arduino/117_Wemos_Firebase.htm

#include <FirebaseESP32.h>
#include <WiFi.h>
FirebaseData firebaseData;

const char* ssid = "Nombre_de_tu_Red_Wifi";
const char* password = "Clave_Red_Wifi";

String numero = "";
String texto = "";

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());

  Firebase.begin("https://kio4b-3c240.firebaseio.com/", "1lzi12VLgSecreto_de_la_base_de_datos");
}


void loop() {
  if (Firebase.getString(firebaseData, "/ESP32/numero")) {
      String numero_fb = firebaseData.stringData();
      if (numero_fb != numero) {
        numero = numero_fb;
        Serial.println(numero);
      }
    }

    if (Firebase.getString(firebaseData, "/ESP32/texto")) {
      String texto_fb = firebaseData.stringData();
      if (texto_fb != texto) {
        texto = texto_fb;
        Serial.println(texto);
      }
    }

// Send a random number every 3 seconds.
    if((millis()-tiempo_actual)>= 3000){
          String alea = (String) random(0,100);
          tiempo_actual = millis();
          Serial.println(alea);
          Firebase.setString(firebaseData, "/ESP32/aleatorio", alea);
    }
  
}

3.- App Inventor sends text to on/off LED12 and LED14 in ESP32 by Firebase.

p117_firebase_led.aia (3.0 KB)

ooooooooooooooo000ooooooooooooo
- CODE ESP32

// Juan A. Villalpando
// http://kio4.com/arduino/117_Wemos_Firebase.htm

#include <FirebaseESP32.h>
#include <WiFi.h>
FirebaseData firebaseData;

const char* ssid = "Nombre_de_tu_Red_Wifi";
const char* password = "Clave_Wifi";

#define LED12 12
#define LED14 14

String message12 = "";
String message14 = "";

void setup() {
  pinMode(LED12, OUTPUT);
  pinMode(LED14, OUTPUT);
  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());

  Firebase.begin("https://kio4-3c240.firebaseio.com/", "1lzi12VLgSecreto_de_la_base_de_datos");
}

void loop() {
  if (Firebase.getString(firebaseData, "/ESP32/LED12")) {
      String message12_fb = firebaseData.stringData();
      if (message12_fb != message12) {
        message12 = message12_fb;
        Serial.println(message12);
        if(message12.indexOf("Set LED12 ON") != -1){digitalWrite(LED12, HIGH);}
        if(message12.indexOf("Set LED12 OFF") != -1){digitalWrite(LED12, LOW);}
      }
    }

  if (Firebase.getString(firebaseData, "/ESP32/LED14")) {
      String message14_fb = firebaseData.stringData();
      if (message14_fb != message14) {
        message14 = message14_fb;
        Serial.println(message14);
        if(message14.indexOf("Set LED14 ON") != -1){digitalWrite(LED14, HIGH);}
        if(message14.indexOf("Set LED14 OFF") != -1){digitalWrite(LED14, LOW);}
      }
    }

}

firebasedb23

4.- ESP32 sends status of two PushButtons to App Inventor.

p117_firebase_pulsador.aia (3.6 KB)

  • PushButtons have no resistance because they are configured as pinMode INPUT_PULLUP

ooooooooooooooo000ooooooooooooo
- CODE ESP32

// Juan A. Villalpando
// http://kio4.com/arduino/117_Wemos_Firebase.htm

#include <FirebaseESP32.h>
#include <WiFi.h>
FirebaseData firebaseData;

const char* ssid = "Nombre_de_tu_Red_Wifi";
const char* password = "Clave_Red_Wifi";

#define LED12 12
#define LED14 14
#define Pulsa16 16
#define Pulsa17 17

String message12 = "";
String message14 = "";
String message16 = "";
String message17 = "";
boolean P16;
boolean P17;
boolean P16_old;
boolean P17_old;

void setup() {
  pinMode(LED12, OUTPUT);
  pinMode(LED14, OUTPUT);

  pinMode(Pulsa16, INPUT_PULLUP); // Pin16 and Gnd.
  pinMode(Pulsa17, INPUT_PULLUP); // Pin17 and Gnd.
  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());

  Firebase.begin("https://kio4-3c240.firebaseio.com/", "1lzi12VLgSecreto_de_la_base_de_datos");
}

void loop() {
  if (Firebase.getString(firebaseData, "/ESP32/LED12")) {
      String message12_fb = firebaseData.stringData();
      if (message12_fb != message12) {
        message12 = message12_fb;
        Serial.println(message12);
        if(message12.indexOf("Set LED12 ON") != -1){digitalWrite(LED12, HIGH);}
        if(message12.indexOf("Set LED12 OFF") != -1){digitalWrite(LED12, LOW);}
      }
    }

  if (Firebase.getString(firebaseData, "/ESP32/LED14")) {
      String message14_fb = firebaseData.stringData();
      if (message14_fb != message14) {
        message14 = message14_fb;
        Serial.println(message14);
        if(message14.indexOf("Set LED14 ON") != -1){digitalWrite(LED14, HIGH);}
        if(message14.indexOf("Set LED14 OFF") != -1){digitalWrite(LED14, LOW);}
      }
    }

// Send messages PushButtons
    P16 = digitalRead(Pulsa16);
    if (P16 != P16_old) {
      P16_old = P16;
      if (P16 == HIGH){message16 = "'NO Pulsado 16'";} else {message16 = "'Pulsado 16'";}
      Serial.println(message16);
      Firebase.setString(firebaseData, "/ESP32/P16", message16);
    }

    P17 = digitalRead(Pulsa17);
    if (P17 != P17_old) {
      P17_old = P17;
      if (P17 == HIGH){message17 = "'NO Pulsado 17'";} else {message17 = "'Pulsado 17'";}
      Serial.println(message17);
      Firebase.setString(firebaseData, "/ESP32/P17", message17);
    }
   
}

5.- From web.

@Juan_Antonio,

Seeing this tutorial of yours is like I am having a birthday gift or christmas present :rofl: :rofl: :rofl:

Thanks keep it coming,

Ragna

Hi @Juan_Antonio,
Could you please complete the tutorial? In step 12 of the tutorial, “Start in test mode” was chosen for testing, in this mode there was “Your security rules are defined as public, so anyone can steal, modify, or delete data in your database.”
Screenshot 2020-06-11 at 4.43.30 PM
Once the testing is done and the user wants to change mode in “Start in locked mode”, do you have any steps, this could be a couple of instructions bur for the new guys, this seems to be a show stopper.

Thanks,
Ragna

hi Ragne ..
did you get update or a link to authentication steps i can use in mit app and arduino ?
than you

Hello Juan
I want to get numeric value from app inventor to esp..But couldn't.
stringValue=Firebase.RTDB.getString(&fbdo, "test/set");
set=stringValue.toInt();

Can you help me

This works

String stringValue = " 12.34 ";
int my_value = stringValue.toInt(); // 12

What do you get?
stringValue=Firebase.RTDB.getString(&fbdo, "test/set");
Serial.println(stringValue);

String is small square like this :white_large_square:. When I convert it to int zero 0.
I want to get this

Check my tutorial

....
 Firebase.begin("https://kio4-3c240.firebaseio.com/", "1lzi12VLgSecreto_de_la_base_de_datos");
}

void loop() {
  if (Firebase.getString(firebaseData, "/ESP32/LED12")) {
      String message12_fb = firebaseData.stringData();
      if (message12_fb != message12) {
        message12 = message12_fb;
        Serial.println(message12);
        if(message12.indexOf("Set LED12 ON") != -1){digitalWrite(LED12, HIGH);}
        if(message12.indexOf("Set LED12 OFF") != -1){digitalWrite(LED12, LOW);}
      }
    }
...

if(Firebase.RTDB.getString(&fbdo, "test/set")){
stringValue = fbdo.stringData();
if(stringValue_fb != stringValue)
{
stringValue=stringValue_fb;
Serial.print("stringValue =");
Serial.println(stringValue);
}

And result is this

if(Firebase.RTDB.getString(&fbdo, "/test/set")){
if (fbdo.dataType() == "string") {
stringValue = fbdo.stringData();
set=stringValue.toInt();
Serial.println(stringValue);
}

It s done. Thank you brother

Now I need help with another problem. I want to change the "set" value with rotary encoder that I got from Firebase and change it with slider on app inventor .
So I want to change this "set" value with both rotary encoder and slider in app inventor. Whichever changed it last, it will be the set value.
I'm trying to do it with this piece of code but I couldn't.
if(millis() > Time_set){
Serial.print("Point1 ");
State = digitalRead(Apin); // Reads the "current" state of the clock pin
// If the previous and the current state of the clock are different, that means a step has occured
if (State != LastState){
Serial.print("Point2 ");
// If the data state is different to the clock state, that means the encoder is rotating clockwise
if (digitalRead(Bpin) != State) {
counter ++;
} else {
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
LastState = State; // Updates the previous state of the clock with the current state
Time_set = getStartTime(2000);
}

Hi Rustem_Mam and Juan
I did all your tutorial but it is not working now. Can you check again help me.
Thank You!

#include "FirebaseESP32.h"
#include <WiFi.h>
#include "DHTesp.h"
#include <WiFiClient.h>
#include <Arduino.h>
#define FIREBASE_HOST 
#define FIREBASE_AUTH
#define WIFI_SSID 
#define WIFI_PASSWORD 
#define DHT_PIN 5
#define LEDR 23
#define bt 0
DHTesp dht;
int b = 0;
String Statusvalue;
FirebaseData fbdo;
// set up
void setup() {
Serial.begin(9600);
delay(1000);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
dht.setup(DHT_PIN, DHTesp::DHT11);
dht.getPin();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
pinMode(LEDR, OUTPUT);
pinMode(bt, INPUT);
}
void loop() {
float h = dht.getHumidity();
float t = dht.getTemperature();
Firebase.setFloat(fbdo, "Sensors/DHT11/Nhietdokk", t);
Firebase.setFloat(fbdo, "Sensors/DHT11/Doam", h);
Serial.print("nhiet do =");
Serial.println(t);
Serial.print("do am =");
Serial.println(h);
if (digitalRead(bt) == LOW) {
delay(20);
if (digitalRead(bt) == LOW) {
b = b + 1;
if (b == 2)
b = 0;
if (b == 1) {
digitalWrite(LEDR, HIGH);
delay(200);
}
if (b == 0) {
digitalWrite(LEDR, LOW);
delay(200);
}
}
while (digitalRead(bt) == LOW)
;
}
if (Firebase.RTDB.getString(fbdo, "LED/Status")) {
if (fbdo.dataType() == "string") {
Statusvalue = fbdo.stringData();
b = Statusvalue.toInt(); // have called int b
if (b == 1) {
digitalWrite(LEDR, HIGH);
delay(200);
} else if (b == 0) {
digitalWrite(LEDR, LOW);
delay(200);
}
Serial.print("bt");
Serial.println(bt);
Serial.print("b");
Serial.println(b);
}
}
}