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.- 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