RaspberryPi-DHT22-Ubidots
This project uses a Raspberry Pi connected to a DHT22 sensor to read the temperature and humidity of its surroundings to a website called Ubidots, this site then plots these readings as charts and allows real-time analytics to be performed.
Components
- Raspberry Pi B+ [or later]
- Gobbler
- Breadboard
- Connecting wires
- DHT22 [or DHT11] Digital Humidity and Temperature Sensor
- Power Source for Raspberry Pi
- An ethernet cable or a Wifi Module [USB ones work fine]
Requirements
- Router with DHCP capability
- Wired or wireless Router
Circuit Diagram
Download Fritzing from here and open the Fritzing project to have a better idea about the connections.
Pre Connection Procedure
- Flash the Raspbian Jessie OS into the MicroSD card of your Raspberry Pi using the Win32 Disk Imager Software
- Insert the MicrSD card into your Rapsberry Pi
- Download and run Putty, a SSH Client.
- Power up the Raspberry Pi and connect it to your router using an ethernet cable
- Determine the IP Address of your Raspberry pi from the router and enter that IP Address as hostname in the Hostname text field in putty
- Connect to the Pi using putty, even if the connection refuses once or twice, its okay, try again, it will connect.
- The default username is "pi" and password is "raspberry", login to your pi using these credentials
- Expand the file system in Raspberry pi
sudo raspi-config
- Set the time zone of the system in Raspberry pi
sudo raspi-config
- Run the udpate a few times
sudo apt-get update
- Install the necessary softwares
sudo apt-get install git-core python-dev python-pip python-smbus
.These will come in handy later - Then reboot,
sudo reboot
Preparing the Pi for DHT22 / DHT11
- Connect the sensor to the Pi as shown in the circuit diagram
-
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
to clone the ADafruit DHT repository into your Pi -
cd Adafruit_Python_DHT
-
sudo apt-get install build-essential python-dev python-openssl
to install the necessary packages needed to install external python libraries -
sudo python setup.py install
to install the external library cd examples
-
sudo ./AdafruitDHT.py 2302 4
to run the example and check if the sensor is working or not
Installing Ubidots package
This is a simple three step procedure, just run the following commands
sudo apt-get install python-setuptools
sudo easy_install pip
sudo pip install ubidots
The Python Script
- Sign up at Ubidots
- Go to Sources and create a new source
- Click on the source you just created
- Add two variables, one called Humidity and other called Temperature, honestly you can create as many variables as you want and call them whatever you want, but for this project what we need are these two.
- In the right bottom of the screen, select the Device wizard and select Raspberry Pi, then give the units for each variable, the number of seconds you want the delay between each reading to be and hit generate code.
- The code needed to send the data to the Ubidots source is generated, copy it and create a python script.
- This script sends random data and it is not the actual data we want to send, now we need to use the Adafruit Python DHT library and create the rest of the script needed.
Add these lines of code in the appropriate places
import sys
import Adafruit_DHT
#Set the type of sensor and the pin for sensor
sensor = Adafruit_DHT.DHT22
pin = 4
Within the try block inside the While(1): add this line
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
And replace the variables value1 with humidity and value2 with temperature
Remove the lines that assign values to value1 and value2. - Run the code using
sudo python fileName.py
- Check the values in the variable, they keep updating as and when the Pi sends the value
- You can create widgets with the values, download the data, or send that data as a public link.
Snapshots
The data when viewed in the Data Soruce
The data when viewed as widgets