DHT22 ThingSpeak IOT

Credit to this website: https://www.hackster.io/adamgarbo/raspberry-pi-2-iot-thingspeak-dht22-sensor-b208f4

For this page, I am going to show the application of IOT by using IOT platform named ThingSpeak. ThingSpeak is open source IOT platform that enable user to collect, store, analysis and visualize sensors data.

For the example, we can use DHT22 as our sensor to read temperature and humidity.

ThingSpeak stores data in Channels, each of which can have up to 8 data fields, as well as location and status fields. Data can be sent to ThingSpeak at a rate of at every 15 seconds or longer.

ThingSpeak Configuration

Step 1. Sign up for new User Account

Step 2. Create a new Channel

  • Then, select “Channels”, “My Channels”, and then “New Channel”.

Step 3: Enter Channel Information

  • Name: DHT22_raspberry_pi
  • Description: You can type any description: Read Temperature and Humidity from DHT22 sensor that integrated with Raspberry pi 4.
  • Field 1: Humidity (%)
  • Field 2: Temperature (°C)

Step 4: Click save

Step 5: Record Write API Key in notepad

  • Go to “APIs keys” tab, and note down to notepad the “Write API Key”. We will use this value later in our python script.

Step 6: Python script creation

  • Create a new python script.
  • Write the code below

#——————————————————————————————#

from time import sleep
from gpiozero import LED
import Adafruit_DHT
from urllib.request import urlopen

myAPI_key = “833PV5FI0BH1L9JE” # your Write API Key

led = LED(17)
led.off()
DHT_pin = 23 # the sensor is connected in GPIO23

baseURL = (“https://api.thingspeak.com/update?api_key=%s” % myAPI_key)

while True:
humidity, temp = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, DHT_pin)
humidity = “%.1f” % humidity # Formatting to two decimal places
temp = “%.1f” % temp
conn = urlopen(baseURL + “&field1=%s&field2=%s” % (humidity, temp)) # Sending the data to thingspeak
print(conn.read())
conn.close() # Closing the connection
print(“Temperature = %s Humidity = %s” % (temp, humidity))
sleep(0.5)

#——————————————————————————————#

Next, to test whether the script runs properly, you can use the following code:

Step 7: Run the script

You will see number beside b’0′ such as b’1′. It means the script will uploads data to ThinkSpeak. A Channel can only be updated every 15s.

Congratulation !!!. You have learn IOT.