Project tutorial
Connecting ESP8266 to Firebase to Send & Receive Data

Connecting ESP8266 to Firebase to Send & Receive Data © GPL3+

In this tutorial, you will learn how to upload and download data to/from a Firebase Real time database with Arduino UNO and ESP8266.

  • 18,179 views
  • 16 comments
  • 5 respects

Components and supplies

Apps and online services

About this project

Overview

In this tutorial, you will learn how to upload and download data to/from a Firebase database with Arduino UNO and ESP8266 (ESP-01) module.

This tutorial include every steps for this project.(especially good for beginners)

What You Will Learn

  • How to make a database in Firebase.
  • How to upload and download data from Firebase.
  • How to use ESP8266 as a connection between Arduino and Firebase.

Step 1 : Download Arduino IDE

download Arduino IDE from following link.

https://www.arduino.cc/en/software

Step 2 : Install ESP8266 Board to Arduino IDE

Start Arduino and open the Preferences window.

Enter https://arduino.esp8266.com/stable/package_esp8266com_index.json into the Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.

After that open Boards Manager from Tools > Board menu and install esp8266 platform.

Select ESP8266 board from Tools > Board menu after installation.

Step 3 : Adding the Firebase library.

download firebase library from following link.

https://github.com/FirebaseExtended/firebase-arduino

Open Sketch > include library > add.ZIP library and add ZIP library to Arduino IDE.

After that go to Sketch > Include Library Verify that the library you just added available in the list.

Step 4 : Adding the ArduinoJson library.

Open Tools > Manage Library and install ArduinoJson version 5.13.5 library.

Step 5 : Setup Firebase realtime database.

Go to firebase console from following link.

https://firebase.google.com/

click Add project.

(If you haven't already, create a Firebase project: In the Firebase console, click Add project, then follow the on-screen instructions to create a Firebase project or to add Firebase services to an existing GCP project).

Enter Project name.

Google Analytics for your Firebase project is optional.

Create a Realtime Database.

Use test mode as a Security rule.

Firebase Realtime Database.

Step 6 : Code explanation.

Open File > Examples > FirebaseArduino > FirebaseDemo_ESP8266.

Make Sure you Select ESP8266 board from Tools > Board menu.

Set your FIREBASE_HOST.

Open your Realtime Database and copy your Firebase host to in to your arduino code (FIREBASE_HOST).

When your copy Firebase host, remove " https:// "and"/" from the Firebase host.

#define FIREBASE_HOST "your FIREBASE HOST"

for example

#define FIREBASE_HOST "werr-f374b.firebaseio.com"

Set your FIREBASE_AUTH.

Open your Realtime Database and go to project overview > project settings > Service accounts > Database secrets and copy your Secret and paste in to your FIREBASE_AUTH.

#define FIREBASE_AUTH "Your secret"

Set your WIFI_SSID and WIFI_PASSWORD.

#define WIFI_SSID " your WIFI SSID"
#define WIFI_PASSWORD " your WIFI PASSWORD"

Connect to WIFI code.

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

Set Float value to database.

// set Float  value
 Firebase.setFloat("number", 42.0);
 // handle error
 if (Firebase.failed()) {
     Serial.print("setting /number failed:");
     Serial.println(Firebase.error());  
     return;
 }

Update Float value.

// update Float  value
 Firebase.setFloat("number", 43.0);
 // handle error
 if (Firebase.failed()) {
     Serial.print("setting /number failed:");
     Serial.println(Firebase.error());  
     return;
 }

Get Float value from database.

 // get value  
Firebase.getFloat("number");

Remove value from database.

// remove value
 Firebase.remove("number");

Set string value to database.

// set string value
 Firebase.setString("message", "hello world");
 // handle error
 if (Firebase.failed()) {
     Serial.print("setting /message failed:");
     Serial.println(Firebase.error());  
     return;
 }

Set bool value to database.

// set bool value
 Firebase.setBool("truth", false);
 // handle error
 if (Firebase.failed()) {
     Serial.print("setting /truth failed:");
     Serial.println(Firebase.error());  
     return;
 }

Append a new value to database.

// 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;
 }

Firebase Arduino API Reference (more details about the Library).

https://firebase-arduino.readthedocs.io/en/latest/

Step 7 : Connections for uploading code.

Connect the pins.

The ESP8266 works with 3.3V and not 5V.

ESP8266:-------------- >Arduino:

GND -------------------------- GND

GPIO-2 (IO2) -------------------- Not connected (open)

GPIO-0 (IO0)---------------------GND

RX --------------------------------RX

TX ---------------------------------TX

CHPD (EN) ------------------------ 3.3V

RST -------------------------------- Not connected (open)

VCC (3v3) -------------------------- 3.3V

connect Arduino Uno RESET Pin to Uno's Ground/GND Pin.

Step 8 : upload the code.

Before you upload the code check following instructions.

  • Select ESP8266 board from Tools > Board menu.
  • Select right port from Tools > Port menu.
  • Select 115200 as a upload speed from Tools > upload speed menu.
  • Recheck your pins.

If you fail to upload code, remove the serial Arduino cable and plug it again and retry to upload code. (retry more than three times)

If you want to upload new code, remove the serial Arduino cable and plug it again and upload the code.

For errors, please check out this video.

Step 9 : Final step.

After uploading the code, remove the serial Arduino cable and take out

GPIO-0 (IO0) from GND and plug serial Arduino cable.

That's it, you are done!!!

Open serial monitor of Arduino IDE where you see the ESP is connected and ready.

Finally check your database.

https://github.com/FirebaseExtended/firebase-arduino

https://firebase-arduino.readthedocs.io/en/latest/

https://medium.com/@cgrant/using-the-esp8266-wifi-module-with-arduino-uno-publishing-to-thingspeak-99fc77122e82

https://room-15.github.io/blog/2015/03/26/esp8266-at-command-reference/#AT+CWJAP

https://create.arduino.cc/projecthub/ahmedibrrahim/iot-using-esp8266-01-and-arduino-afa35e

Code

FirebaseDemo_ESP8266.inoArduino
//
// Copyright 2015 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// FirebaseDemo_ESP8266 is a sample that demo the different functions
// of the FirebaseArduino API.

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

// Set these to run example.
#define FIREBASE_HOST "your FIREBASE HOST"
#define FIREBASE_AUTH "your secret"
#define WIFI_SSID "your WIFI SSID"
#define WIFI_PASSWORD "your WIF 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);
}

Schematics

Circuit diagram for upload code to ESP8266 using Arduino.
Screenshot 2020 11 17 115854 qzmuu9ko0e
Circuit diagram for after uploading code to ESP8266 using Arduino.
Screenshot 2020 11 17 211321 r5b2okovwc

Comments

Similar projects you might like

ESP8266 ESP-01 Webserver

by Robin Kanattu Thomas

  • 145,398 views
  • 22 comments
  • 175 respects

Control Arduino Uno Using ESP8266 WiFi Module and Blynk App

Project tutorial by Adithya TG

  • 78,988 views
  • 18 comments
  • 64 respects

AI-Thinker AI-Cloud Inside ESP8266 Update Firmware(REVIEWED)

Project tutorial by luciorocha

  • 54,688 views
  • 24 comments
  • 29 respects

Telegram Bot with ESP8266

Project tutorial by Team CodersCafe

  • 54,371 views
  • 53 comments
  • 88 respects

WiFi ESP8266 and DHT22 Sensor

Project tutorial by hbolanos2001

  • 34,180 views
  • 7 comments
  • 29 respects
Add projectSign up / Login