Interacting with a Cloud Web Service - Internet of Things - ثاني ثانوي

Lesson 3 Interacting with a Cloud Web Service Interacting with Web Services We will extend the project from the previous lesson to enable it to send data via a web service. Binary loT Cloud is an educational online cloud platform that allows you to view the environmental data that is collected by the Arduino circuit you created in the previous lesson. وزارة التعليم 2073-1445 190 binarylogic Link to digital lesson www.len.edil.sa Binary loT Cloud Binary loT Cloud Welcome sharif73 Z P 14 1- - " Figure 4 25 Binary IoT platform with collected data from Arduino

Lesson 3: Interacting with a Cloud Web Service

Interacting with Web Services

Firstly, you will need to register and create a new user in the Binary loT Cloud online platform. To register on the Binary loT Cloud online platform: > Go to the Binary loT Cloud online platform at https://ksa-iot.azurewebsites.net/Login.aspx and on the welcome page click on Register. > Enter a Username and a Password of your choosing and the PIN code 174563 > Click the Register button. binarylogic 1 Username Password LORIN Binary loT Cloud <Login Username sharif73 Password 2 3 Confirm Password Figure 4:26 logisting in the Binary for Cloud form Pin وزارة التعليم 2173-1445 REGISTER 5 4

Lesson 3: Interacting with a Cloud Web Service

Firstly, you will need to register and create a new user in the Binary IoT Cloud online platform.

Calling a Web API with Python You will create a JSON object containing the environment data and send it to Binary loT Cloud through a function that interacts with a Web API. Install the Python requests package through pip. Open the terminal on your working directory and enter the following command: pip install requests At the start of your Python code, import the requests package with the line: import requests Get the current date and time and convert it to a string by importing the datetime module and calling the following method: from datetime import datetime date time str(datetime.now()) Create 2 variables usemame and password that are the credentials you used previously to register and login to Binary loT Cloud. username = "your_useume heur" password = "your_password_here" Create a new function called send_data() which takes the following arguments: def send_data(username, password, temperature, moisture): Web API A Web API (Application Programming Interface) is an endpoint that allows a program to access a service from another program that resides on a server on the Internet. Javascript Object Notation (SON) JSON (JavaScript Object Notation) is an open data format type that is used to transfer data between services Objects in JSON are key-value pairs that can store data types such as strings, integers, floats, arrays, and even other objects. D وزارة التعليم 2673-1985 182 Figure 4.27: An application communicates with a bud plattor through a welx API

Lesson 3: Interacting with a Cloud Web Service

Calling a Web API with Python

Create a new variable named api_url with the following link which is the cloud platform's API web endpoint: api_url = "https://ksa-iot-api.azurewebsites.net/api/readings" Create an object with the method variables which will be passed to the API as a JSON request body: reading = { "username": usernanie, "password": password, "temperature": temperature, "Moisture": moisture, "datetime" date_time } Call the API with the requests.post() method which has as arguments an API URL and a JSON body object: response = requests.post(api_url, json-reading) Web API وزارة التعليم 103-1485 Python во {json Software Figure 4 28 JSON is an open data format type to transter dua berween services Data

Lesson 3: Interacting with a Cloud Web Service

Create a new variable named api_url with the following link which is the cloud platform's API web endpoint:

The complete code for the send_data() method is shown bellow: def send_data(username, password, temperature, moisture): api_url = "https://ksa-iot-api.azurewebsites.net/api/readings" date_time = str(datetime.now()) reading = { "username": username, "password": password, "temperature: temperature, "moisture" moisture, "datetime" date_time 1 response requests.post(api_url, json-reading) In the main program, add the send_data() method to send the environment data you collected every 30 seconds. send_data(username, password, temperature, moisture) time.sleep(30) Complete Code from datetime import datetime import time import requests import pyfirmata وزارة البت البدر J173-175

Lesson 3: Interacting with a Cloud Web Service

The complete code for the send_data() method is shown bellow:

Complete Code1

board = pyfirmata. Arduino ('COM4*) it pyfirmata.util.Iterator(board) it.start() dc_motor_pin board.get_pin('d:3:0') temperature sensor_pin = board get pin('a:2:1') moisture sensor_pin = board.get pin( 8:4:1') username "your_username_here" password = "your_password_here" def send_data(username, password, temperature, moisture): api_url="https://ksa-iot-api.azurewebsites.net/api/readings" date_time = str(datetime.now()) reading { "username" username, } "password" password, "temperature": temperature, "moisture": moisture, "datetime" date_time response = requests.post(api_url, json-reading) def water plant(dc_motor_pin): print("Watering plant ---") de motor pin.write(1) time.sleep(5) de notorin.write(0) مرارة اليك البدر 123-1785

Lesson 3: Interacting with a Cloud Web Service

Complete Code2

while True: temperature_value = temperature sensor_pin.read() moisture_value moisture sensor pin.read() if (temperature value is not None) and (moisture value is not None): temperature value = float(temperature_value) * 1000 = voltage (temperature value / 1024) * 5 temperature - (voltage 0.5) 100 moisture (1.0 float(moisture_value)) *108 if (temperature >= 24.0) and (moisture <= 40.0): water plant(de_motor_pin) " temperature report "Temperature: + str(temperature) + "C" moisture report = "Moisture: " str(round (moisture, 2)) "*" print(temperature_report) print(moisture_report) send_data(username, password, temperature, moisture) 00 درارة التعليم 123-1465 time.sleep(30) Firmata Web API Figure 4 29 Arduino communicating well PL and Binary IoT Cloud Binery of Cloud

Lesson 3: Interacting with a Cloud Web Service

Complete Code3

وزارة التعليم 127-1975 18 Complete Project After you have written the program place the soil moisture sensor in a plant, run the Python code from your PC and you will see environmental readings being outputted to the terminal. Figure 4.3D: Smart garden with Arduino Assume that the DC motor Is connected to a watering pump or valve.

Lesson 3: Interacting with a Cloud Web Service

Complete Project

Viewing the Smart Garden Data Run the complete Python program and leave it for a few minutes to collect data. Then it will be uploaded to the Binary loT Cloud platform. Log in to the platform with your credentials and you can view the data that you have collected for your plant. To log into the Binary loT Cloud online platform: > Go to the Binary lot Cloud online platform at https://ksu-iot.azurewebsites.net/Login.aspx. > Enter the Username and the Password that you created. > Click the Login button. وزارة التعليم binarylogic 1 Usarmame sharf73 Password LOGIN 3 Binary IoT Cloud 2 Figure 4 21 Logging into the Binary joT Cloud online platform;

Lesson 3: Interacting with a Cloud Web Service

Viewing the Smart Garden Data

binarylogic Binary loT Cloud Welcome sharif73 Date The data that you view in this table are the environmental readings that are collected by the Arduino and sent to Temperature Mon 284 2:40 15/14/30124 2842 78401 1521125 25 A the platform via the Python program running on your PC 21.41 0 1011126 2540 99.50 29.40 13 240 2942 2541 1941 2945 2840 43.34 وزارة التعليم EXPORT TO XLSX EXPORT TO CSV LOGOUT Export your data into .xlsx or.csv files Comma Separated Values (CSV) A CSV (Comma Separated Values) file is a text file whose values are separated by commas and every line in the file represents a data record. Figure 4 32 Viewing environment readings in the Binary IoT Cloud online platform

Lesson 3: Interacting with a Cloud Web Service

The data that you view in this table are the environmental

160 Using Sensor Data and Big Datasets for Data-Driven Decision Making The project we have just built implements basic functionality for monitoring a smart garden. However, this was just a small example with only one device collecting and sending data to an online platform. Suppose we had a large garden that required more devices to cover the various plants and environment, with each plant species and variety requiring a different environment configuration for its monitoring. Collecting different datasets would reveal patterns in the behavior of the garden as a whole system. Now suppose that there were even more gardens in numerous locations across the city that the municipality parks management has to maintain with completely different systems. All these gardens collect vast amounts of data, which, when analyzed, can provide thorough insights and patterns about the operation and behavior of all those gardens compared to one another. 100 Arduino Plant ++ 0101 0101 Q10± Figure 4.33: Scaling the smart garden prod These insights are valuable assets to guide the smart gardens' optimization and evolution theresel each garden system's effectiveness, longevity, and balance. 2173-1445

Lesson 3: Interacting with a Cloud Web Service

Using Sensor Data and Big Datasets for Data-Driven Decision Making

Exercises 1 Explain the use of a Web API. 2 Why are the username and the password required to connect to the Web API? Think of two reasons and write them down. وزارة الكلية

Lesson 3: Interacting with a Cloud Web Service

Explain the use of a Web API.

Why are the username and the password required to connect to the Web API? Think of two reasons and write them down.

3 Which code lines do you have to change to modify the conditions required to start watering the plant and the duration of the watering? 4 Describe what readings other than temperature and humidity can be collected in a smart garden environment. مرارة التعليم

Lesson 3: Interacting with a Cloud Web Service

Which code lines do you have to change to modify the conditions required to start watering the plant and the duration of the watering?

Describe what readings other than temperature and humidity can be collected in a smart garden environment.

5 Suppose you could also read past data from an online cloud platform. You could not only have current environmental readings to use, but data from the past as well. Analyze how this could lead to more efficient watering of your smart gardens and present your ideas below. 6 Compare the JSON and CSV data format types. مرارة التعليم

Lesson 3: Interacting with a Cloud Web Service

Suppose you could also read past data from an online cloud platform. You could not only have current environmental readings to use, but data from the past as well.

Compare the JSON and CSV data format types.