DS18B20 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 DS18B20 as our sensor to read temperature.

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 minimum 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: DS18B20_raspberry_pi
  • Description: You can type any description: Example: “This will read a temperature sensor from DS18B20 sensor that integrated with Raspberry pi 4”.
  • Field 1: 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. Example: DS18B20_thingspeak.py
  • Write the code below

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

import time
from w1thermsensor import W1ThermSensor
from urllib.request import urlopen

sensor = W1ThermSensor()

myAPI_key = “GGAOEJOZF5VINSJF” #your Write API key
baseURL = (“https://api.thingspeak.com/update?api_key=%s” %myAPI_key)

while True:
temp = sensor.get_temperature()
#temp = “%0.1f” % temp
print(“Temperature = %0.1f celcius” % temp)
conn = urlopen(baseURL + “&field1=%s” % temp)
print(conn.read()) #print data entry
conn.close() #closing the connection
time.sleep(1)

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

Step 7: Run the script

You will see number beside b’0′ such as b’1′.

b’0′ =  There is no data upload to.

b’1′. = Date Entries: 1 . It means the script will uploads data to ThinkSpeak. A Channel can only be updated every 15s.

Congratulation !!!. You have learned IOT.