Iot - Assignment - 03
Iot - Assignment - 03
Sciences
Submitted to: Dr.M Tariq Khan
Muhammad Abdullah Khan
Roll no:19p-0361
Muhammad Junaid
Roll no:19P-0321
Assignment: 03
Subject:InternetofThings(IOT)
Sec: A
Q1 – Consider the following example, what will be the output?
Code:
Import time #import time library
Import websocket #IMPORT WEBSOCKET API to enable exclusive paircommunication
Import date time #import python's datetime module
Import sys #import SYS module for direct communication with interpreter
sys.path.append('/home/pi/rpi/code/Package') #use append, the built-in function of sys to
look
for the given file named Package in the path mentioned in its argument
import grovepi # import the library for Grovepi - the connections with Rpi will
be made
via Grovepi
from grove_rgb_lcdimport* # import the modules for Grove's RGB LCD backlight that is to be used
websocket.enableTrace(True)
ws = websocket.create_connection("ws://wot.city/object/57cad2809453b2446f007e/send")
#start a websocket based exclusive connection between sensor and Rpi
while True:
[temp, humidity] = grovepi.dht(sensor,blue)
# use grovepi's Digital Humidity Temperature sensor's basic version so default value is
blue. save its current output to temp and humidity variables
print("temp = %0.2f C humidity = %0.2f%%"&(temp, humidity))
setText("temp = %0.2f C \nhumidity = %.02f%%" &(temp,
humidity)) t = time.time()
date = datetime.datetime.fromtimestamp(t).strftime('%YhkmbkjbkS')
# convert the current date time to string so that it can be displayed
vals = {\"date\”:
\""+date+"\",\"temperature"\:"+str(temp)+","h"\":"+str(humidity)+"}"
time.sleep(1);
ws.send(vals)
; print(vals);
In the above code there is infinite loop is used there for it will be continues unless when the code is
stop. We used loop in the program to repeat a statement or sets of statement.
Q2 – write a similar program to get result on your raspberry pi?
SOL:
When we will run the above similar program on Resbarri pi we must need to install some packages on Resbarri
pi the following are some important packages that I install before..
❖ Adaffruit
❖ Git-core
❖ Python_DHT
❖ Setup.py
When I installed the above important packages then we need to set online server for that we shall use the
THINKSPEAK for online server which is shown in the following fig no 1 and it cloud base we can be easily
access and connect everywhere.
import Adafruit_DHT
import time
def getData(sensor:int, pin:int):
'''
Input DHT sensor type and RPi GPIO pin to collect a sample of data
Parameters:
sensor: Either 11 or 22, depending on sensor used (DHT11 or DHT22)
pin: GPIO pin used (e.g. 4)
'''
try:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
return humidity, temperature
except:
Exception("Error reading sensor data")
return False
if __name__ == "__main__":
sensor = 22 # Change to 11 if using DHT11
pin = 4 # I used GPIO pin 4
while True:
print(getData(sensor, pin))
time.sleep(5)