라즈베리파이 realtime database - lajeubelipai realtime database

Introduction

This article demonstrates how to store sensor data on the Firebase real-time database through Raspberry Pi.

Real-Time Database

  • Store and synchronize data with NoSQL cloud DB. 
  • Data is synced across all clients in real-time
  • NoSQL databases are increasingly used in big data and real-time web applications.
  • The Firebase real-time database is a cloud-hosted database.
  • Data is stored as JSON

Prerequisite

  • Raspberry Pi
  • Firebase account (Google Account) 
  • DHT 11
  • Putty 

Step 1

 

Create a new project and fill in the necessary details.

 

Step 2

  • Next, go to select Develop and then click on Database in your project. 
  • After that, the Database appears and select the "Real-time Database Get Started" button. 

 

Step 3

Next, go to rules >> change rules. Just replace that with the following code.

Default Rules

  1.   
  2. {  
  3.   "rules": {  
  4.     ".read""auth != null",  
  5.     ".write""auth != null"  
  6.   }  
  7. }  

Change to these rules

This is for the first test. Anyone can read and write your database without authentication. 

  1.   
  2. {  
  3.   "rules": {  
  4.     ".read"true ,  
  5.     ".write"true  
  6.   }  
  7. }  

  • Apply the above code to Firebase rule, then click the publish button to deploy the rules. After the rules are published anyone can read and write to your data.
  • Go to database >> real-time database >> data

 

"//raspberrypi-3d41f.firebase.com"  This link is used to access our RT database. Click add icon then you can create your own data. This link is used to communicate between Python and Firebase RT.

Step 4

We use the DHT 11 sensor. Send the temperature and humidity data to firebase. Python code uses a putty tool to access the Raspberry Pi remote connection.

Refer to my articles 

  • IoT Remote Access Pi Login Using Raspberry PI 3 Model B 
  • How To Send A Captured Image Through Email Using Raspberry Pi, Pi Camera, And Python 

Wiring Diagram 

 

Step 5

Open the python IDE, File >> New. Save the name as dht-firebase.py.py, then follow the given code. 

  1. import urllib2, urllib, httplib  
  2. import json  
  3. import os   
  4. from functools import partial  
  5.   
  6. GPIO.setmode(GPIO.BCM)  
  7. GPIO.cleanup()  
  8. GPIO.setwarnings(False)  
  9.   
  10. sensor = Adafruit_DHT.DHT11  
  11. pin = 4  
  12. humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)  
  13.   
  14. firebase = firebase.FirebaseApplication('//YOUR_FIREBASE_URL.firebaseio.com/'None)  
  15.   
  16.   
  17. def update_firebase():  
  18.   
  19.     humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)  
  20.     if humidity is not None and temperature is not None:  
  21.         sleep(5)  
  22.         str_temp = ' {0:0.2f} *C '.format(temperature)    
  23.         str_hum  = ' {0:0.2f} %'.format(humidity)  
  24.         print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))    
  25.               
  26.     else:  
  27.         print('Failed to get reading. Try again!')    
  28.         sleep(10)  
  29.   
  30.     data = {"temp": temperature, "humidity": humidity}  
  31.     firebase.post('/sensor/dht', data)  
  32.       
  33.   
  34. while True:  
  35.         update_firebase()  
  36.           
  37.           
  38.         sleep(5)  

Next, we need to update the Raspberry Pi. So, install the latest packages. 

  1. sudo apt-get update    
  2. sudo apt-get upgrade    
  3. sudo apt-get install python3    

Run the program.

  1. sudo python python-fire/dht-firebase.py  

Console Output

 

Firebase Output 

Summary

In this article, you learned how to send IOT data to Firebase.

If you have any questions/ feedback/ issues, please write in the comment box.


Toplist

최신 우편물

태그