The previous tutorial showed how to build a web interface to control your ESP32 over a Local Area Network. If you want to talk to your ESP32 from outside your LAN, you'll need to do a bit more work. There are many options:
Note: The following tutorial is a bit outdated. For working with Firebase, I recommend starting with this tutorial from RandomNerd. There are additional tutorials on that site that explore other Firebase features as well.
The first step is to head to Firebase and sign in with your Google account. Click "Get Started" and then "Add Project". Next, specify your project name (I called mine ESP32-LED), which will be used to generate a url (like https://esp32-led-9a0ab.firebaseio.com) where you can access your project database. You can opt in to Google Analytics if you wish. Click OK; you should be taken to the Firebase console page for your project. Click "Realtime Database" from the panel on the left.
Scroll down to Realtime Database and click "Create database". For the Security rules options, select "Start in test mode" (you can change this later). Your Database console should now look something like this:
Write down the URL that points to your database (https://esp32-led-9a0ab.firebaseio.com) . You are looking at the root of your database, which is "null" because there isn't anything there yet. First let's change permissions (click "Rules" and set both read and write to "true"). Next we need to create a secret key to give to our Huzzah board so it can access this database. Click the gear icon next to "Project Overview" and select "Project settings". On the "Project settings" page, click "Service accounts" and then "Database secrets":
Hover over the secret key, and select "Show". Copy this and save it somewhere to use in later steps. Also make sure to write down the auto-generated URL where you can access your database (something like esp32-led-9a0ab.firebaseio.com).
Set up your circuit so that there is an LED attached to pin 5. In Arduino IDE, make sure you've installed the necessary libraries for ESP32 (see previous step if not).
Additionally, we need to add a couple libraries in order to talk to Firebase. Search and install the Firebase ESP32 Client by Mobizt, as well as the ArduinoJson library (version 6).
In Arduino IDE, we can start with some of the WiFi library boilerplate code that we used in the last example (no need to start a server instance this time). Also, paste in your key and URL from Firebase. To learn more about how to use the FirebaseESP32 library, check the documentation.
#include <WiFi.h> // esp32 library
#include <FirebaseESP32.h> // firebase library
#define FIREBASE_HOST "esp32-led-9a0ab.firebaseio.com" // the project name address from firebase id
#define FIREBASE_AUTH "" // the secret key generated from firebase
#define WIFI_SSID "" // input your home or public wifi name
#define WIFI_PASSWORD "" // password of wifi ssid
String fireString = ""; // led status received from firebase
int ledpin = 5;
//Define FirebaseESP32 data object
FirebaseData firebaseData;
void setup() {
Serial.begin(115200);
delay(1000);
pinMode(ledpin, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); // try to connect with wifi
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address is : ");
Serial.println(WiFi.localIP()); // print local IP address
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // connect to firebase
Firebase.reconnectWiFi(true);
Firebase.set(firebaseData, "/LED_STATUS", "OFF"); // set initial string of "OFF"
}
void loop() {
Firebase.get(firebaseData, "/LED_STATUS"); // get led status input from firebase
fireString = firebaseData.stringData(); // change to e.g. intData() or boolData()
Serial.println(fireString);
if (fireString == "ON") { // compare the input of led status received from firebase
Serial.println("Led Turned ON");
digitalWrite(ledpin, HIGH); // make output led ON
}
else if (fireString == "OFF") { // compare the input of led status received from firebase
Serial.println("Led Turned OFF");
digitalWrite(ledpin, LOW); // make output led OFF
}
else {
Serial.println("Please send ON/OFF");
}
delay(1000); // not strictly necessary
}
After modifying and uploading the above code, open your Serial monitor. In your browser, go to the Realtime Database console, where you should see a new field called "LED_STATUS" that is set to "OFF". Try changing it to "ON" (case sensitive) and observe the results. You can now turn on this LED from anywhere in the world where you have internet access!
While we can now talk to our device remotely, directly meddling with the database isn't exactly a smooth UI. Let's make a simple web interface to do this.
Save the code below as a new HTML file (e.g., in your Week 10 folder). On your Firebase console page, look under where it says "Get started by adding Firebase to your app" and click the Web icon (</>). On the next page, make a name for your app (like LED Toggle), and then copy the auto-generated configuration code into the firebaseConfig variable below.
```