Connecting Arduino to Firebase to Send & Receive Data

Connecting Arduino to Firebase to Send & Receive Data © CC BY

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

  • 5,481 views
  • 6 comments
  • 17 respects

Components and supplies

Apps and online services

About this project

You can read this and other amazing tutorials on ElectroPeak's official website

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.

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 thenmake 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 <<span class="TextRun Highlight BCX0 SCXW174472232" lang="EN-US" xml:lang="EN-US" data-contrast="auto"><span class="SpellingError BCX0 SCXW174472232">FirebaseArduino.h></span></span>
// 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); 
} 

What’s Next?

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

Code

codeArduino
/*   
#include <ESP8266WiFi.h">
#include <<span class="TextRun Highlight BCX0 SCXW174472232" lang="EN-US" xml:lang="EN-US" data-contrast="auto"><span class="SpellingError BCX0 SCXW174472232">FirebaseArduino.h></span></span>
 
// 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); 
}

Comments

Similar projects you might like

Send MKR1000 Data to Google Sheets

Project tutorial by Stephen Borsay

  • 11,499 views
  • 29 comments
  • 34 respects

Post Data to a Google Spreadsheet with Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,222 views
  • 0 comments
  • 7 respects

Pushing Data to Google Docs

by Gustavo Gonnet

  • 27,283 views
  • 60 comments
  • 83 respects

Export Data from Arduino to Excel Sheet

Project showcase by ahmed ismail

  • 18,063 views
  • 1 comment
  • 10 respects

Bike Route Data Gatherer

Project tutorial by Paul Trebilcox-Ruiz

  • 9,621 views
  • 0 comments
  • 37 respects

Rube Goldberg Weather Station with Internet Data Storage

Project in progress by randtekk

  • 7,532 views
  • 6 comments
  • 43 respects
Add projectSign up / Login