Slide 1

Slide 1 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 1 Iwan Setiawan IoT Cloud Platform: ThingSpeak

Slide 2

Slide 2 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 2 Iwan Setiawan Previous Week’s Material ● Temperature monitoring – CPU w/ GPIO Zero – DHT11 w/ Adafruit DHT ● Writing data to a CSV file ● Displaying the data to a live graph with matplotlib ● Actions: LED and buzzer Analog signal Compute Storage Digital signal Stallings, 2016 1 2 CSV +Humidity * https://components101.com/sensors/dht11-temperature-sensor DHT11* 3

Slide 3

Slide 3 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 3 Iwan Setiawan Previous Week’s Material ● Temperature monitoring – CPU w/ GPIO Zero – DHT11 w/ Adafruit DHT ● Writing data to a CSV file ● Displaying the data to a live graph with matplotlib ● Actions: LED and buzzer Analog signal Compute Storage Digital signal Stallings, 2016 Communications +Humidity 1 2 3 5 6 CSV +Notification Simple * https://components101.com/sensors/dht11-temperature-sensor DHT11*

Slide 4

Slide 4 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 4 Iwan Setiawan Previous Week’s Material ● Temperature monitoring – CPU w/ GPIO Zero – DHT11 w/ Adafruit DHT ● Writing data to a CSV file ● Displaying the data to a live graph with matplotlib ● Actions: LED and buzzer Analog signal Compute Storage Digital signal Stallings, 2016 Communications +Humidity 1 2 5 6 +Notification Cloud Compute Storage Simple * https://components101.com/sensors/dht11-temperature-sensor DHT11* 3 CSV

Slide 5

Slide 5 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 5 Iwan Setiawan Objectives ● Learn how to setup channels on an IoT platform ● Learn how to send collected data from an IoT device to the channel ● Learn how to display the data on a dashboard

Slide 6

Slide 6 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 6 Iwan Setiawan Contents ● Setting up a channel on ThingSpeak ● Getting temperature and humidity data from DHT11 and sending the data to the channel ● Displaying the data using a dashboard using Private and Public Views

Slide 7

Slide 7 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 7 Iwan Setiawan Cloud Computing Stallings, 2016

Slide 8

Slide 8 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 8 Iwan Setiawan Cloud Computing Stallings, 2016

Slide 9

Slide 9 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 9 Iwan Setiawan IoT Cloud Platforms Cloud IoT Core IoT Azure IoT Losant ThingsBoard

Slide 10

Slide 10 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 10 Iwan Setiawan IoT Cloud Platform ● Data storing or aggregation – Live data streams via APIs – Time series database ● Data visualization ● Analytics ● Apps – Transforming and/or visualizing data – Triggering actions

Slide 11

Slide 11 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 11 Iwan Setiawan Preparation ● Raspberry Pi 4 set +Ethernet cable ● Breadboard ● Jumper wires ● DHT11 sensor ● MathWorks account Internet connection

Slide 12

Slide 12 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 12 Iwan Setiawan Creating an Account

Slide 13

Slide 13 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 13 Iwan Setiawan License Types

Slide 14

Slide 14 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 14 Iwan Setiawan Creating a Channel ● Login, and click My Channels – New Channel ● Set the channel settings – Name: ACNT-X Monitoring, e.g. ACNT-10 Monitoring – Description: Temperature and humidity monitoring – Field 1: Temperature – Field 2: Humidity ● Click Save Channel Channel ID

Slide 15

Slide 15 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 15 Iwan Setiawan Protocols for IoT ● REST API over HTTPS ● MQTT – Considered lighweight – Publish-subscribe method

Slide 16

Slide 16 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 16 Iwan Setiawan API Keys and Write/Read URLs ● Click to API Keys tab ● Take notes – Write API Key to write data to a channel (WAPIK) – Read API Key to read data from a channel (RAPIK) – API Requests: Write Feeds https://api.thingspeak.com/update?api_key=WAPIK&field1=A&field2=B – API Requests: Read Feeds https://api.thingspeak.com/channels/[Channel ID]/feeds.json?api_key=RAPIK&results=2 Temperature data Humidity data

Slide 17

Slide 17 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 17 Iwan Setiawan Python Libraries ● Adafruit DHT, a library for DHT11/DHT22 ● urllib.request, a library for opening URLs * https://docs.python.org/3/library/urllib.request.html

Slide 18

Slide 18 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 18 Iwan Setiawan Code (1) ● Type the code, save, and run import Adafruit_DHT as dht import urllib.request import csv sensor = dht.DHT11 pin = 21 CHAN_ID = Y0URCH4N1D API_WKEY = "Y0UR4P1WK3Y" # Change to your actual Write API Key API_WREQ = "https://api.thingspeak.com/update?api_key=" # Write API Key API_RKEY = “Y0UR4P1RK3Y” API_RREQ = "https://api.thingspeak.com/channels/{0}/feeds.json?".format(CHAN_ID) CSV_FILE = "/home/pi/acnt/iot-c2d.csv" [...] iot-d2c.py

Slide 19

Slide 19 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 19 Iwan Setiawan Code (2) [...] def sensor_read(): humi, temp = dht.read_retry(sensor, pin) if humi is not None and temp is not None: print("Temperature: {0:0.1f}*C Humidity: {1:0.1f}%".format(temp, humi)) else: print("Cannot read data from the sensor") return humi, temp def cloud_send(): send_req = urllib.request.urlopen(API_WREQ + '{0}&field1={1:0.1f}&field2={2:0.1f}'\ .format(API_WKEY, temp, humi)) [...] iot-d2c.py

Slide 20

Slide 20 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 20 Iwan Setiawan Code (3) [...] def cloud_receive(): receive_req = urllib.request.urlretrieve(API_RREQ + 'api_key={0}&results=2'\ .format(API_RKEY), CSV_FILE) with open(CSV_FILE) as file: csv_reader = csv.reader(file) for row in csv_reader: msg = row[2] if __name__ == "__main__": while True: try: humi, temp = sensor_read() cloud_send() cloud_receive() except KeyboardInterrupt: exit() iot-d2c.py

Slide 21

Slide 21 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 21 Iwan Setiawan Observation and Sharing ● Observe the dashboard on Private View tab for 5 minutes and make sure that the channel on ThingSpeak received data from your IoT device ● Click Sharing tab, select “Share channel view with everyone” ● Click Public View tab and make sure that there is a dashboard with two charts shown

Slide 22

Slide 22 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 22 Iwan Setiawan IoT Assignment 4 ● Simulate a scenario similar to Assignment 3 number 3. Please observe: – your dashboard for the sent data to the channel on ThingSpeak – CSV file output via read data from the channel ● Continue to run data collecting and storing to the cloud for 12 2 hours ● Optional: Send an email on heat alert, temperature > 35°C – Add Apps – MATLAB Analysis, or – Add Apps – React

Slide 23

Slide 23 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 23 Iwan Setiawan Results to be Uploaded 1. Experiment results: photos and explanations 2. Assignment 4 results – Attach you group’s Channel ID and CSV file – Photos and a demo video showing the results – Source code, screenshots/figures, and explanations Upload your experiment and assignment 4 results to Moodle ● Using one PDF file ● One group, one document ● Upload your demo video somewhere, only attach the URL to the doc. 06/16 9pm code experiment

Slide 24

Slide 24 text

Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 24 Iwan Setiawan Resources ● Circuit Basics, “Logging Sensor Data to the Cloud Using the Raspberry Pi,” 2020. [Online]. Available: https://circuitbasics.com/logging-sensor-data-to-the- cloud-using-the-raspberry-pi/ ● N. Vemu, “The Naivest Way to Send and Retrieve Data from Cloud (Using Python),” 2021. [Online]. Available: https://medium.com/geekculture/the-naivest-way-to- send-and-retrieve-data-from-cloud-using-python- 52a3e4b5fe24