├── DHT22-Firebase-Code.py └── README.md /DHT22-Firebase-Code.py: -------------------------------------------------------------------------------- 1 | # Author: Ahmed M. Hussain, All rights reserved 2 | 3 | import pyrebase 4 | import sys 5 | import Adafruit_DHT 6 | import time 7 | 8 | config = { 9 | "apiKey": "AIzaSyD68dO5eqaIPqa4KBm9DE0kt5U-aee2ooA", 10 | "authDomain": "tempapp-a0c0e.firebaseapp.com", 11 | "databaseURL": "https://tempapp-a0c0e.firebaseio.com", 12 | "storageBucket": "tempapp-a0c0e" 13 | } 14 | 15 | firebase = pyrebase.initialize_app(config) 16 | db = firebase.database() 17 | while 1: 18 | humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4) 19 | time_hhmmss = time.strftime('%H:%M:%S') 20 | date_mmddyyyy = time.strftime('%d/%m/%Y') 21 | data = {"Date": date_mmddyyyy,"Time": time_hhmmss, "Temperature": temperature, "Humidity": humidity} 22 | db.child("/message").push(data) 23 | print("Temp: %f -- Date: %s -- Time: %s" %(x,date_mmddyyyy,time_hhmmss)) 24 | time.sleep(60) 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Connecting-Raspberry-Pi-with-Firebase-Database-Using-Python 2 | The following project was developed to collect the humidity and the temperature using the DHT22 sensor and Raspberry Pi v3 and store these data on the Firebase database. 3 | ## The following instructions should work with any types of projects, since the basic idea has pretty much the same logic. 4 | ### My project setup: 5 | 1. Raspberry Pi v3 model B, running Raspbian 4.9.35-v7. 6 | 2. DHT22 sensor with 3 pins: GND, 5V and Data. 7 | #### First, you must configure your Raspberry Pi with the following: 8 | 9 | 1. sudo apt-get update 10 | 2. sudo apt-get install python-dev 11 | 3. wget https://bootstrap.pypa.io/get-pip.py 12 | 4. sudo python get-pip OR sudo apt-get install python-pip (new Raspian versions) 13 | 5. sudo pip install pyrebase 14 | #### Note that you may need to update your pyasn modules (pip install --upgrade pyasn1-modules) #### 15 | 6. I also had to download some files for the DHT22 sensor from github: https://github.com/adafruit/Adafruit_Python_DHT 16 | 17 | ##### If you are done with the previous steps, you are now ready to start writing the python script. Take a look at the following link for the pyrebase: https://github.com/thisbejim/Pyrebase. 18 | 19 | ``` 20 | # This code should be used with every script that you will be using to connect to the Firebase database. 21 | import pyrebase 22 | from firebase import firebase 23 | config = { 24 | # You can get all these info from the firebase website. It's associated with your account. 25 | "apiKey": "apiKey", 26 | "authDomain": "projectId.firebaseapp.com", 27 | "databaseURL": "https://databaseName.firebaseio.com", 28 | "storageBucket": "projectId.appspot.com" 29 | } 30 | 31 | firebase = pyrebase.initialize_app(config) 32 | . 33 | . 34 | . 35 | . 36 | ``` 37 | ##### Look at the Pyrebase https://github.com/thisbejim/Pyrebase to look at the functions and how to use them. 38 | --------------------------------------------------------------------------------