I recently wrote a blog post on writing a temperature logger using Arduino + Python + Orchestrate.io database service. Several readers wrote back to use an alternative database if possible. I have decided to rewire that tutorial but this time with Firebase database. This would also act as a introductory tutorial on Firebase.

Last Update: December 28, 2017

i) Updated to new Firebase site and screens.

What we will build?

We are going to build a Temperature Logger IoT Project that comprises of the following:

Arduino Uno

A temperature sensor (LM35)

A Python application that can read the data from the Arduino Uno

Post that Temperature Data into a Cloud Database (Firebase)

A high level diagram of the process is shown below:

The Hardware Setup

I used the following:

Arduino Uno microcontroller

LM35 Temperature Sensor

Eventually we will have the Raspberry Pi that interfaces with the Arduino to read and transmit off the values but to validate things for now, the Uno was powered via a laptop/desktop with Python installed on it. The communication between the Uno and the PC is via serial port communication.

Arduino Uno + Temperature Sensor Setup

Here is how the LM35 sensor is connected to the Arduino Uno board.

Ofcourse, we used a breadboard to connect all this together but I am simplifying the diagram here so that you know what is connected to which pin. The LM35 has 3 pins. The first one goes to the 5V power pin on Arduino, the 3rd one is the GND and the middle pin is the VOUT where it emits out the values that we need to capture. We connect this to the Analog Pin (A0) on the Arduino. We can then write our Arduino code to read that value, as is shown next.

Arduino Code

The Arduino Code is straight forward as given below:

float temp;

int tempPin = 0; void setup()

{

Serial.begin(9600);

} void loop()

{

temp = analogRead(tempPin);

temp = temp * 0.48828125;

Serial.print(temp);

delay(10000);

}

You will notice in the loop that every 10 seconds, we are printing out the temperature value that was read from the Analog Pin (#0)

If you run the Serial Port Monitor that comes with the Arduino IDE and if the Arduino is powered up and connected as per the diagram shown, then you will find the Temperature value being printed on the Serial Monitor as given below:

Once this happens, we know that the Arduino setup is looking good and all we need to do now is to write a client program on the PC that interfaces with this Arduino, read the values via the Serial port and then push them to Firebase.

Introduction to Firebase

Firebase is a real-time database in the cloud that provides an API to store and sync data in real-time. Granted that we do not need the real-time features over here but the idea of this post is to demonstrate that you could use Firebase as a solution to storing your data in the cloud. It could act as a great aggregator of data from various devices and possibly for different use-cases even sync to other clients where you are monitoring stuff from.

Getting started with Firebase is free and it comes with the Spark plan by default that has a generous free quota that you can use today. There are other plans too and the screenshot below shows the plans/pricing/features at the time of updating this blog post.

So, the first thing that you should do is to login to the Firebase console at https://console.firebase.google.com. Once you are logged in successfully, the next thing to do is to create a new Firebase project. Let’s get going on that.

Creating our Firebase Project

A Firebase project gives you a unique name space for your data. Currently, Firebase is not just a real-time database but it is an entire suite of services that makes it easy for us to write mobile applications. It offers a database, notifications, development tools, authentication, crash reporting, analytics and more.

In our case, however we will be focused on the Database only i.e. a storage area in the cloud for logging our temperature values. In other words, you can think of the project as a Temperature Logger where I want to log the Temperature being collected from various weather stations (each setup of the Arduino + Temperature Sensor would be a single station).

In the Welcome screen, you will see a button to Add project as shown below:

Click on Add project. This will bring up the Create a project screen as shown below, where you will need to provide a project name. Note that Firebase will generate a unique Project ID (your Google Cloud Platform project ID) if your name is not unique across all projects. Select a Country/region where you would like your database to be hosted. Click on Create Project.

Be Patient. This will create a Firebase database under the Spark Plan.

To iterate again, we are only going to look at the Database feature of Firebase and not other services for now. Click on the Database link on the left and then click on GET STARTED button as shown below. This will show up some details about your database as shown below:

The database details screen is shown below:

Important: Please note down your database URL (https://<something>/firebaseio.com) in the screen above. This is the unique database url that is available to the outside world for integration. In our case, the Python application that we shall be writing will be using this unique URL as our backend database. Simple!

Another point to note here is that it shows the database name and a null next to it. This means that currently there is no data in the database. Keep this screen handy, you will be coming back to it and can see the database live here as it comes in. Even if not live, you can see what the data currently is in the database over here.

Click on the Rules tab. You will notice that it has the following rules by default as shown below. Let’s not get too much into rules for now but it is sufficient to understand that why the magic text indicates is that to do database reads and writes, you will need to be authenticated. Firebase supports multiple authentication mechanisms, but we will keep it simple for now and not use any authentication. Note that what we are going to do is not good practice but it is ok for our tutorial here. What we are going to do is open up access to this Firebase database to allow anyone (everyone) to both read and write.

To do that, simply start editing the text to the one that is shown below:

Click on Publish. This will publish (associate) the rules with your database. It also indicates clearly (and which is good by the way) that we have defined our database to be accessed by the public (read and write). Boys and Girls, don’t try this at home (sorry … don’t try this in a live database).

That’s it. We have a Firebase database in the cloud, all ready for read and write.

Let us go back to what I want to do in the application. I am setting up various weather stations with their own unique names and each Weather Station is going to collect the Temperature + Date + Time (per reading).

So, what I want to do is collect these readings under each Location. My tree structure looks something like this:

Root | — — Location 1

| — — Temperature Record 1 (Temperature Reading, Date, Time)

| — — Temperature Record 2 (Temperature Reading, Date, Time)

| — — …..

| — — Temperature Record N (Temperature Reading, Date, Time)

| — — Location 2

...

| — — Location N

Sounds ok?

It should be easy to now imagine how the Firebase REST API would function? First up, we will need 3 pieces of information:

App URL : Remember, we noted down the Firebase URL. That is the URL that we will post our data to. Data to post i.e. a JSON object that encapsulates the temperature reading, date and time. For e.g. {“date”:some_date,”time”:some_time,value=temperature_value} The path into which I should log this data. Remember that I wanted to each temperature record under its respective location. Hence I will need the location name and then the full path to this branch would be the APP_URL + “/” + “Temperature_Location_Name”.

Keep in mind that Firebase supports various Client Libraries, so you need not use the raw REST API to perform your client side operations. I usually prefer to understand the REST API since it helps to understand a little bit better on what is going on . Check out the documentation for a list of client side libraries that are available.

Python Code

Now, let us move on to the Python code that interfaces over Serial port to read the temperature values from the Arduino setup and posts that data into Firebase.

The steps are simple:

1) We initialize the serial port communication via which we will be reading the Temperature values that the Arduino unit will be emitting every 10 seconds. You need to figure out which Serial Port on your machine is interfaced to the Arduino.

2) Every 10 seconds, the code will read the value from the Serial Port. We can obviously build in more validations in the code, but this is good for now to demonstrate how all the pieces come together.

3) There are a few Python libraries available for Firebase but since they are not on the official list and hence I did not use them. I went with the requests package in Python and that serves the purpose well when dealing with a REST API.

Each Temperature record is being sent to Firebase as a JSON object with the following attributes:

Temperature in Centigrade

Date of the recording

Time of the recording

The URL to which we are posting is as explained above i.e.

APP_URL + “/” + “Temperature_Location_Name”

and to that we are appending the “temperature.json” to indicate that it is a temperature reading.

Take a look at the Python client program below:

Checking our data

Assuming that all the hardware is wired up and you have your Python code ready, executing the code is straightforward. A sample run of the Python code is shown below:

27.55,16:50:38,09/03/2015,Mumbai-Kandivali

Record inserted. Result Code = 200,{‘name’:’-JjyFyyJDgm9Y82oOZM9'}

The final step was to validate if our data was being transmitted successfully and into Firebase.

Visit the Dashboard for your Firebase Database and you should start seeing some entries over there i.e. the temperature records.

Summary

This blog post covered several things. First up, setting up a simple Arduino & temperature sensor to collect temperature readings. The next step was an introduction to Firebase, the real-time database available in the cloud and how we could use that as our database for the temperature logger system.

I look forward to your feedback. Till then, Happy Firebasing!

Note: If you are not interested in the Arduino part and simply want to work with Firebase, then I suggest that you could take the Python code and modify it by providing your own values or data records that you wish to persist in Firebase. Simply remove out the serial port piece of the code in the Python and substitute it with data and you are good to go.