Component list
- Raspberry Pi
- DHT11/22 or AM2302 temperature and humidity sensor
- 4.7K or 10K resister
- Breadboard
- Jumper wires
1) Raspberry Pi
2) DHT11/22 or AM2302 temperature and humidity sensor
DHT22 DHT11 AM2302
3) 4.7K or 10K resister
4.7K resister 10K resister
4) Breadboard
5) Few jumper wires (Male to female)
1 Prepare the RPI
Install the OS on Raspberry Pi. Follow the instruction on the link below.
Raspbian is good choice. http://www.raspberrypi.org/quick-start-guide
2 Setup the hardware
Prepare the Raspberry Pi hardware as follows:
DH11 | Raspberry Pi |
---|---|
VCC | 3.3v |
GND | GND |
DATA | GPIO#4 |
Connect 4.7K - 10KΩ resistor between the Data pin and the VCC pin of DHT11 or DHT22 (try 10k resister if 4.7k didn't work)
3. Download the library from GitHub
Download the library from Github
Test the C code as follows
(type 11 or 22 or 2302 pin# is the data pin connected GPIO pin number)
For example, we'll have a DHT11 connected to GPIO #4 so the command should be
If everything is fine, you should get something like this.
Sample possible error outputs
-
The DHT series of sensors will only respond every 2 seconds so if you are not getting data, be sure to wait 3 seconds before trying again. If not it will return something like this:
-
If you try wrong GPIO pin number or if the sensor is not responding it will show something as below:
4. Prepare the scripts
Now we have basic C file to read the sensor value. This section describes how to read the sensor value from a shell script. First create a directory in the /root directory as follows
Now copy the Adafruit_DHT file from the Adafruit-Raspberry-Pi-Python-Code/Adafruit_DHT_Driver to gadgetkeeper directory. Then run the following command and create a new file.
Now paste the following shell script and save the file.
Set the execute permission with the following command. (Please uncomment the relevant command line in the file. This code works for DHT11)
Finally you can run the command as follows and read the output.
How to read only the temperature value from the sensor
Use the following shell script to read only the temperature value. Create new file called read_temperature.sh
Now paste the following shell script and save the file.
How to read only the Humidity value from the sensor
Use the following shell script to read only the humidity value. Create new file called read_humidity.sh
Now paste the following shell script and save the file.
5. Uploading data using API
Now we can read the temperature and humidity value from the sensor. It’s time to upload the sensor reading to Gadgetkeeper.
- First create your account here: http://console.gadgetkeeper.com/#login
- Then you need to create thermometer thing and create the temperature property. You can follow step by step guide.
6. API interface scripting
Now we can upload the temperature values to Gadgetkeeper using the following script. You can create new property for humidity and upload that value too. First let’s see how to upload the temperature through the API.
Then paste the following code to the file and save.
You must set the execute permission by running the following command.
Now run following command and open new file.
Then paste the following code in it and save:
Set execute permission as follows. (Make sure to replace API_KET, THING_ID and PROPERTY_ID with your values)
Now the /root/gadgetkeeper folder should contain following files in it.
It’s time to test the code. Run the following command and test it.
If your API settings are wrong, then script will show something like this:
If the sensor has some problem, then the script will retry to read the data from the sensor maximum 5 times and give the following output. You can easily configure the maximum retry count and retry interval in the send_temperature.sh script.
7. Read value from API
Now we can update the temperature value through the API. Following script can be used to read the current value from the API.
Open new file and save the following script in it:
Then set the permission for the file:
8. Automate Script
Now the script can be scheduled to upload data to API periodically (every 2 min). Add the following cronjob using sudo crontab –e command.