Store Temperature Sensor Data in Firebase Real-Time Database through Raspberry Pi

In my first BlogTuts (Blog + Tutorial), we are going to learn about most basic platform process which actually gives the rise to the Internet of Things a.k.a IoT. Here, we are going to send the Temperature and humidity sensor data to IOT cloud — Firebase using Raspberry Pi Controller

Features

  • The Firebase is the actually Intermediate process for monitoring and displaying the different sensors readings through web applications solutions.
  • It works as a handler for connecting Real-Time data of Embedded sensor collaborate with different cross-platform applications(Android, IOS Java SDKs etc) and most importantly it also stores the data online in the Firebase Database, known with the term called Internet Cloud.
  • We can create the Database through secured paths, enable different types of authentication and tasks. With the help of the cloud computing protocols, will be able to exchange the embedded hardware data between local servers.
  • The amazing thing is using the Firebase Real-Time database with Sensors has become easier by using the 2 Wired I2C interfaces with Microcontrollers.
  • The I2C bus, Interface and protocol is the easiest way to create communication between Master Device(Microcontroller) and Slave devices(sensors). Using the Two Wired Pull up, we will be able to transfer 8 Bit or Bytes of input data from the sensors and send it to microcontrollers.

Hardware Setup

OS, IOT, Platforms, programming Language

  • Raspbian(wheezy)
  • Firebase
  • Python

For a newbie enthusiast in this field, I can also gain the knowledge from Setup and installation documentation for Raspberry Pi. And after performing the documentation process, Install the Firebase package using the command

$ sudo pip install python-firebase

Elaboration of I2C Sensor process

Steps to create the compatibility to send the real-time data of sensors to Firebase database through Raspberry Pi is much simple.

  • We are going to import the I2C bus for creating the two wired communication between raspberry pi and SHT31 sensor I2C module.

Note:

To avoid the complex wiring structure, I am going to use the I2C adapter for Raspberry Pi (models — 2,3) designed to connect the I2C sensor.

  • At the same time in starting the process, we will also import firebase from the firebase package.
  • To detect the pull-up and initialize the registers in two wire I2C protocol for I2C sensor module we will be using the commands.
bus = smbus.SMBus(1)
bus.write_i2c_block_data(0x44, 0x2C, [0x06])
  • We are going to reading the data from the 8-bit I2C frame from specified I2C registers.
  • We will be using Shift registers as well as combining the 8-bit individual data (MSB or LSB) and convert the data into 16 bits by the process mentioned
temp = data[0] * 256 + data[1]

Note: In the case of the calculation process we will be using the datasheet in which they specified the exact formula for calculating the temperature in Celcius or Fahrenheit.

cTemp = -45 + (175 * temp / 65535.0) 
fTemp = -49 + (315 * temp / 65535.0)
  • We will be printing the converted values of the sensor (Temperature values in degree Celsius and Fahrenheit and humidity values) in command line interface of Raspberry Pi.

Posting the data in Firebase Database

There are some libraries available in different Computer languages available on GitHub. Ill be using the Python library to communicate with firebase. For getting started with Firebase process do check the documentation on Github available.

Using the python library I am sending the every real-time data of SHT31 sensor data to Firebase as a JSON Object using the attributes:

  • Temperature values in Celcius and Fahrenheit
  • Humidity Values

For sending the sensor values we will be first storing the URL of database mentioned in the snap like

(https://<something>/firebaseio.com) which actually acts as a backend point.

This unique URL of the database is going to help the application to synchronize with Database of sensors further. Also changing the values of “rules” for the reading and writing process in public with the sample as well as snap below

Change the rules from

{
“rules”: {
“read”: “auth != null”
“write”: “auth != null”
}
}

Change the Rules to

{
“rules”: {
“read”: “true”
“write”: “true”
}
}

Caution: Only use this read and write rules process for sample purpose.

After following the I2C process we were able to fetch the values from the sensor as mentioned above. Now we are going to synchronize these values in firebase Real Time database.

  • We are going to declare the URL link in a variable.
firebase= firebase.FirebaseApplication(‘host id mentioned in database of firebase’)
  • Using JSON post method in firebase we will be linking the sensor readings using Firebase.post method.
result = firebase.post(‘host id mentioned’, {‘cTemp’:str(cTemp),’ftemp’:str(fTemp), ‘humidity’:str(humidity)})
  • Finally, when the data of the sensor will be posted to Real-time database of Firebase, then it will share the Random Secured value which is also mentioned as the parent ID for the different values posted in the database as mentioned in the example below with snap
temperature-sensor-bb4bd (PARENT)
{
LGeJ3JC2jj9O8tM6FI6 (Secured Random Authentication Value)
cTemp: “26.8371862364” (CHILD NAME)
ftemp: “80.3069352255” (CHILD NAME)
humidity: “38.1216144045” (CHILD NAME)
}

Now Download (or git pull) the code in pi. Run the program.

$> python SHT31_Firebase.py

After the Execution of program, you will be able to see the readings of the sensor in Real-time database with Random Authentication Values (mentioned below)

Code

You can also check out the code as well as set up documentation from mentioned below links

Temperature Sensor with Firebase Real-Time Database

https://github.com/varul29/SHT31-IOT-Platform/blob/master/SHT31_Firebase.py

Raspberry Pi Basic setup

https://github.com/varul29/RaspberryPI-Basic_Setup

Firebase Setup

https://github.com/varul29/Firebase_Setup

Summary

In the above Blog, we have covered the most basic backend process which helps the I2C based temperature sensor module to connect with Firebase real-time database cloud system using Raspberry Pi using JSON post method in Python programming language.

Will be looking forward to your feedback

Credits

  1. SHT 31: https://github.com/ControlEverythingCommunity/SHT31/blob/master/Arduino/SHT31.ino
  2. Raspberry Pi: http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2
  3. Firebase: https://www.youtube.com/watch?v=BJfVoaifnzc&t=240s
  4. JSON Post method: https://www.geeksforgeeks.org/get-post-requests-using-python/