Connecting to Firebase Realtime Databse from Arduino using ESP8266 module
By: Anga Koko
Tags: Cloud, Firebase, Arduino, IoT, Esp8266

Being able to read inputs from an arduino board is great, but what’s better is being able to save the data on the cloud so other applications can access the data. This post will show how to do just that.

To be able to archive this, you will need:

Arduino IDE. You can download from here https://www.arduino.cc/en/main/software Arduino Board and ESP8266 module A firebase project

Step 1: Create a firebase project

First thing you need to do is create a firebase project and activate Realtime Database. Start with test mode to enable read and write in firebase rules. You will need your Firebase App Id and Secrete key for the code. You can use this link to get started with Firebase https://firebase.google.com/

Step 2: Writing the code

Paste this code in your Auduino IDE. You will need your firebase app id and secrete key.

#include <ESP8266WiFi.h>

#include <FirebaseArduino.h>

#include <ArduinoJson.h>

// Set these to run example.

#define FIREBASE_HOST "AppId.firebaseio.com"

#define FIREBASE_AUTH "secrete key"

#define WIFI_SSID "SSID"

#define WIFI_PASSWORD "password"

void setup() {

Serial.begin(9600);

// connect to wifi.

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

Serial.print("connecting");

while (WiFi.status() != WL_CONNECTED) {

Serial.print(".");

delay(500);

}

Serial.println();

Serial.print("connected: ");

Serial.println(WiFi.localIP());

Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

}

int n = 0;

void loop() {

// set value

Firebase.setFloat("number", 42.0);

// handle error

if (Firebase.failed()) {

Serial.print("setting /number failed:");

Serial.println(Firebase.error());

return;

}

delay(1000);

// update value

Firebase.setFloat("number", 43.0);

// handle error

if (Firebase.failed()) {

Serial.print("setting /number failed:");

Serial.println(Firebase.error());

return;

}

delay(1000);

// get value

Serial.print("number: ");

Serial.println(Firebase.getFloat("number"));

delay(1000);

// remove value

Firebase.remove("number");

delay(1000);

// set string value

Firebase.setString("message", "hello world");

// handle error

if (Firebase.failed()) {

Serial.print("setting /message failed:");

Serial.println(Firebase.error());

return;

}

delay(1000);

// set bool value

Firebase.setBool("truth", false);

// handle error

if (Firebase.failed()) {

Serial.print("setting /truth failed:");

Serial.println(Firebase.error());

return;

}

delay(1000);

// append a new value to /logs

String name = Firebase.pushInt("logs", n++);

// handle error

if (Firebase.failed()) {

Serial.print("pushing /logs failed:");

Serial.println(Firebase.error());

return;

}

Serial.print("pushed: /logs/");

Serial.println(name);

delay(1000);

}

Step 3: Download and Install required libraries

If you don’t have required libraries you will get error while trying to run the code. You will need to install FirebaseArduino and ArduinoJson Library

For FirebaseArduino library:

Download zip file from this link https://github.com/FirebaseExtended/firebase-arduino/blob/master/src/Firebase.h On the arduino IDE click on Sketch — Include Library — Add .Zip Library.. and select the downloaded zip file

For ArduinoJson library:

On the arduino IDE click on Sketch — Include Library — Mange Libraries… Search for ArduinoJson library Select version 5 and click install. Note that FirebaseArduino library works with Version 5 of this library at the time of making this post.

Another option for installing ArduinoJson library is to use same steps for FirebaseArduino library using this link https://github.com/bblanchon/ArduinoJson

Final Step: Uploading your code

You can now upload your code to the board and check your database for data.

If you find this post helpful you can give it a clap