Connecting Arduino to Firebase to Send & Receive Data [By ESP8266]

Table of Contents

Overview

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

Storing data (like sensors data) to a database that can be accessed from anywhere by the internet may be very useful. Firebase makes storing and retrieving data easy.

What You Will Learn

What is Firebase?

Firebase is a mobile and web application development platform developed by Firebase, Inc. in 2011, then acquired by Google in 2014. As of October 2018, the Firebase platform has 18 products which are used by 1.5 million apps.

  • Firebase provides multiple services as following:
  • Firebase Analytics which is a free application measurement solution providing insight into app usage and user engagement.
    Firebase Cloud Messaging (FCM) which is a cross-platform solution for messages and notifications for Android, iOS, and web applications, which is cost-free as of 2016.
  • Firebase Auth which is a service that can authenticate users using only client-side code. It supports social login providers Facebook, GitHub, Twitter and Google (and Google Play Games). Moreover, it includes a user management system whereby developers can enable user authentication with email and password login stored with Firebase.

Required Materials

Hardware Components

Arduino UNO R3 × 1
ESP8266 WiFi Module with PCB Antenna × 1

Software Apps

Arduino IDE

Connecting Arduino To Firebase

First, you should create an account in Firebase. It’s quite easy to create an account; Go to “firebase.google.com”, click on Console, sign in by your Google account, and then make a new project. After creating a new project, add a name and enable the test mode. You can add some value manually in the realtime database part. You can get JSON form of your data by adding “.json” at the end of the database URL. Watch the following video for more information:

You can read or transfer data from your database by Arduino and ESP8266. You need a Host name and an Auth key of your firebase project. Then, you should add the Firebase Arduino library and upload the code. If it’s the first time you are using an Arduino board, just follow these steps:

  1. Go to www.arduino.cc/en/Main/Software and download the Arduino software compatible with your OS. Install the IDE software as instructed.
  2. Run the Arduino IDE and clear the text editor and copy the following code in the text editor.
  3. Choose the board in: tools > boards, and select your Arduino Board.
  4. Connect the Arduino to your PC and set the COM port in tools > port.
  5. Press the Upload (Arrow sign) button.
  6. You’re all set!

Circuit

Code

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
 
// Set these to run example. 
#define FIREBASE_HOST "example.firebaseio.com" 
#define FIREBASE_AUTH "token_or_secret" 
#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); 
}

Download this file and check other examples of connecting Arduino to Firebase

What’s Next?

  • Make a real-time connection between two Arduino based systems 
  • Add other Google services to your Arduino projects. 

Liked What you see?

Get updates and learn from the best

More To Explore

Comments (26)

  • Joe Reply

    This library is much better and easier
    https://github.com/mobizt/Firebase-ESP8266

    November 4, 2019 at 1:40 am
    • Saeed Hosseini Reply

      thanks for sharing

      November 7, 2019 at 4:47 am
  • Asimo Reply

    Can I Use a Arduino with an inbuilt Wifi ?

    November 27, 2019 at 6:09 pm
    • Saeed Hosseini Reply

      yse, but you should make some changes in the cdoe probably

      November 28, 2019 at 8:18 am
  • Sreenu Reply

    I am getting this error
    Firebase.h:24:10: fatal error: memory: No such file or directory

    #include

    ^~~~~~~~

    compilation terminated.

    exit status 1
    Error compiling for board Arduino Uno.

    February 3, 2020 at 9:00 am
    • Saeed Hosseini Reply

      #include “ESP8266WiFi.h”
      #include “FirebaseArduino.h”

      March 1, 2020 at 6:16 am
  • M A Reply

    This gif is not clear. May I know what are the steps that are used to set up the database in google firebase?

    February 16, 2020 at 11:49 am
    • Mehran Maleki Reply

      The steps are properly explained in the paragraph above the gif. Check the “Connecting Arduino To Firebase” section.

      December 5, 2020 at 11:24 am
  • ck Reply

    this cant to be connect with uno

    April 11, 2020 at 6:01 am
    • Amir Mogoeir Reply

      What error do you get?

      May 30, 2020 at 1:20 pm
  • Roberto Reply

    Where can I download this #include library? When I tried to compile this, I got the error that I don’t have such file or directory. Thanks in advance.

    July 2, 2020 at 4:03 pm
    • Mehran Maleki Reply

      The download link is provided at the end of the article, in the “Necessary Files & Downloads” section.

      December 5, 2020 at 11:18 am
  • wan Reply

    i have try the code using the arduino uno and ESP-01 WiFi Serial Transceiver Module (ESP8266)…it dont work …why iget get an this error:

    ..\esp8266\2.7.2/tools/sdk/include/ESP8266WiFi.h”: Invalid argument

    i already follow the exact connection as the schematic given above.

    July 29, 2020 at 4:05 pm
    • wan Reply

      did somebody successfully run the code using schematic given??….as far as i know the #include library only can be executed using the board with built-in ESP8266.

      July 29, 2020 at 4:09 pm
  • wan Reply

    did anybody successfully run the code ?
    why i get this error:

    esp8266\2.7.2/tools/sdk/include/ESP8266WiFi.h”: Invalid argument

    i am using the arduino uno and ESP-01 (ESP8266)

    July 29, 2020 at 4:11 pm
  • movies reviews blog Reply

    Wow, this article is good, my sister is analyzing these
    things, thus I am going to inform her.

    April 16, 2021 at 1:30 pm
    • Mehran Maleki Reply

      Hi. We’re so glad to see you’ve liked it. Looking forward to seeing her feedbacks.

      April 17, 2021 at 4:42 am
  • life insurance no medical exam Reply

    Wonderful web page you have right here, i do concur on some items nevertheless, but not all.

    May 11, 2021 at 8:27 am
    • Mehran Maleki Reply

      Thanks for your generous opinion. We get energy from such comments.

      May 12, 2021 at 4:52 am
  • http://1114.adminka.cc/ Reply

    One and Only you bro

    June 4, 2021 at 5:40 pm
  • more info Reply

    You should be a part of a contest for one of
    the greatest sites online. I am going to recommend this website!

    June 8, 2021 at 9:42 pm
    • Mehran Maleki Reply

      Thank you so much for your kind word! We get energy from such comments.

      June 9, 2021 at 7:30 am
  • check here Reply

    Excellent goods from you, man. I have understand your stuff previous to and you are
    just extremely excellent. I really like what
    you’ve acquired here, certainly like what you are saying and the way in which you say it.
    You make it entertaining and you still care for to keep it smart.
    I can’t wait to read far more from you. This
    is actually a great website.

    June 8, 2021 at 11:48 pm
    • Mehran Maleki Reply

      Thank you for your generous opinion! We always try to do the best we can.

      June 9, 2021 at 7:03 am
  • choisir-sa-guitare.com Reply

    Thanks for finally talking about > Connecting Arduino to Firebase
    to Send & Receive Data [By ESP8266] < Loved it!

    July 23, 2021 at 5:19 pm
    • Mehran Maleki Reply

      So glad you enjoyed it!

      July 24, 2021 at 5:48 am

Leave a Reply

Your email address will not be published. Required fields are marked *