My First IOT (Internet Of Things) project
Even though IOT has been around for a while, it’s still mystery to a large group of people. How do you make devices talk and derive intelligence and actionable and useful data out of it.
I have been working with one of the IOT project Nube for past 6 months which gives you full control and comfort, so you never run out of gas. You can manage and monitor your consumption, create alerts and place orders in one click! I helped architected the cloud application which consumes lot of data reported by sensors on gas tanks and provide relevant information for gas companies and end consumers. It’s a complete end to end IOT solution targeting 10 million tanks to start with.
More I worked with Nube IOT solution, it became clear to me to on how to design and architect end to end solution but I thing that wasn’t clear to me was on how these actual sensors work and what kind of programming and skills are needed to develop IOT hardware and firmwares.
Out of the curiosity I got access to Raspberry Pi 2 and I decided to build a simple POC which reports temperature and humidity and sends data to the cloud.
Here I am describing the process to build IOT solution which reports temperature and humidity.
- Raspberry PI — Small single board computer
- DHT11 temperature & humidity sensor
- Jumper Wires and Breadboard (3 Male to Female)
- External Mouse and Keyboard ( I used wireless keyboard and mouse)
- Internet — You can use LAN connection or WiFi. My Raspberry Pi 2 didn’t had WiFi and I didn’t liked the idea to adding more LAN wires. I ordered Wireless Nano USB Adapter, but this is completely optional.
- ThingSpeak — A server side platform for collecting IoT device data.
- A Python Program — It will run on Raspberry PI
Understanding Raspberry PI GPIO Pins
GPIO (General purpose input/output) pings along the edge of the board.
These pins are physical interface between the Pi and the outside world.
Read more about these pins from here.
Connecting Sensor DHT11 to Raspberry PI
The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed).
DHT11 can have three or four pins. If it is a 4 pin version, then a pin is unused[NC]. PIN 1 is VCC, PIN 2 is data, if you have 4 PIN version then PIN3 is unused[NC], PIN4 is ground. [ In case of 3 PIN version PIN 3 is ground]
Note — I used 10K 0hm resistor as shown in the Diagram 2 — Connection diagram above but it didn’t worked for me and once I removed the resistor my sensor started reporting data. Some tutorials will include instructions for using resistor but it didn’t worked for me.
Step 1 — Insert DHT11 in the breadboard
1. VCC connected to +3.3V~5V
2. DATA connected to the microcontroller IO port
3. GND connected to ground
Step 2 — Now make below connections using Jumper Wire
- Connect 3.3v P1 pin from Raspberry Pi to VCC (V) of DHT11 using Jumper wire.
- Connect Pi ground P6(Black dot from Diagram 1 above) to PIN 3 which is GND pin in DHT11 (Or PIN4 if that exists)
- Connect Pi GPIO2 P3 to DHT11 Data pin
Server Side Setup
Now lets setup server side to receive the data. We will use ThingSpeak as it provides server side infrastructure to collect the data and process it but you can use any other service as well.
- Create a free account on ThingSpeak.
- Create a channel as shown below with 3 fields for Temp-C (in celsius), Temp-F (in Fahrenheit), Humidity
3. Once you save the Channel, go to API Key section of channel & note the Write API key. This key will be needed to send data to ThingSpeak channel from device.
Making Raspberry Pi ready to read data from sensor
- If Install updates
sudo apt-get update
2. Install Python if not already there. You can check the python version by running command
3. If Python doesn’t exists. Install it using
sudo apt-get install python-dev
4. Install Rpi.GPIO (Library used to interact with GPIO pins)
sudo apt-get install python-rpi.gpio
5. Install library for ADH11 sensor — Download library
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
6. Install library
sudo apt-get update
sudo apt-get install build-essential python-dev python-openssl
sudo python setup.py install
You can download the code by using following command
Update your API key here in the code
# Define GPIO pin to which DHT11 is connected
DHTpin = 2
#Setup our API and delay
myAPI = "GET_YOUR_KEY" # API Key from thingSpeak.com
myDelay = 60 #number of seconds between posting data
sudo python temperaturesensor.py
Once you run the python script, you should start seeing the data on ThingSpeak as shown in the below image.