Introduction: Industry 4.0: Arduino IoT

COMPONENTS AND SUPPLIES

  • Arduino UNO R3
  • ElectroPeak ESP8266-12N WiFi Module

APPS AND ONLINE SERVICES

  • Arduino IDE

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 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.

Step 1: Setting Up Arduino IDE

If it’s the first time you are using an Arduino board, just follow these steps otherwise you can just jump to the next step:

  • 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!

Step 2: ​Connecting Arduino to Firebase

First, you should create an account in Firebase. It’s quite easy to create an account; Go to the Firebase website, 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 video for more detailed 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.

Step 3: Circuit

Step 4: 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); }
Arduino Contest 2019

Participated in the
Arduino Contest 2019