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
- How to make a database in Firebase
- How to upload (download) data to (from) Firebase
- How to use ESP8266 as a connection between Arduino and Firebase
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
Software Apps
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:
- Go to www.arduino.cc/en/Main/Software and download the Arduino software compatible with your OS. Install the IDE software as instructed.
- Run the Arduino IDE and clear the text editor and copy the following code in the text editor.
- Choose the board in: tools > boards, and select your Arduino Board.
- Connect the Arduino to your PC and set the COM port in tools > port.
- Press the Upload (Arrow sign) button.
- 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
Firebase Arduino Library
What’s Next?
- Make a real-time connection between two Arduino based systems
- Add other Google services to your Arduino projects.
Comments (26)
This library is much better and easier
https://github.com/mobizt/Firebase-ESP8266
thanks for sharing
Can I Use a Arduino with an inbuilt Wifi ?
yse, but you should make some changes in the cdoe probably
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.
#include “ESP8266WiFi.h”
#include “FirebaseArduino.h”
This gif is not clear. May I know what are the steps that are used to set up the database in google firebase?
The steps are properly explained in the paragraph above the gif. Check the “Connecting Arduino To Firebase” section.
this cant to be connect with uno
What error do you get?
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.
The download link is provided at the end of the article, in the “Necessary Files & Downloads” section.
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.
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.
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)
Wow, this article is good, my sister is analyzing these
things, thus I am going to inform her.
Hi. We’re so glad to see you’ve liked it. Looking forward to seeing her feedbacks.
Wonderful web page you have right here, i do concur on some items nevertheless, but not all.
Thanks for your generous opinion. We get energy from such comments.
One and Only you bro
You should be a part of a contest for one of
the greatest sites online. I am going to recommend this website!
Thank you so much for your kind word! We get energy from such comments.
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.
Thank you for your generous opinion! We always try to do the best we can.
Thanks for finally talking about > Connecting Arduino to Firebase
to Send & Receive Data [By ESP8266] < Loved it!
So glad you enjoyed it!