0% found this document useful (0 votes)
5 views8 pages

Iot - Assignment - 03

The document contains a programming assignment related to the Internet of Things (IoT) using Raspberry Pi, focusing on reading temperature and humidity data from sensors and sending it to a server. It includes code examples for setting up the sensors, establishing a WebSocket connection, and pushing data to ThingSpeak. Additionally, it outlines the necessary packages to install and provides sample output from the code execution.

Uploaded by

eastern.farhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Iot - Assignment - 03

The document contains a programming assignment related to the Internet of Things (IoT) using Raspberry Pi, focusing on reading temperature and humidity data from sensors and sending it to a server. It includes code examples for setting up the sensors, establishing a WebSocket connection, and pushing data to ThingSpeak. Additionally, it outlines the necessary packages to install and provides sample output from the code execution.

Uploaded by

eastern.farhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

FAST National University of Computer and Emerging

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

sensor=6 # set pin 6 as the output tosensor


blue= 0 # Low/False/Off is being called bluehere
white=1 # High/True/On is being called Whitehere

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);

Output of the Code:


temp =24.50 humidity =34.50%
+27-11-21+ +24.5+ +33.5%
temp =24.50C humidity =54.66%
+27-11-21+ +24.5+ +40.2%
temp =24.50C humidity =43.50%
+27-11-21+ +24.5+ +32.56%
temp =24.50C humidity =43.50%
+27-11-21+ +24.5+ +42.54%
temp =24.50C humidity =42.70%
+27-11-21+ +24.5+ +42.7%
temp =24.50C humidity =42.70%
+27-11-21+ +24.5+ +42.7%
temp =24.50C humidity =42.70%
+27-11-21+ +24.5+ +42.7%
temp =24.50C humidity =42.70%
+27-11-21+ +24.5+ +42.7%
temp =24.50C humidity =42.70%
+27-11-21+ +24.5+ +42.7%
temp =24.50C humidity =41.90%
+27-11-21+ +24.5+ +41.9%

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.

Figure 1: Setting of Thinkspeak Channels


When it is done we will use Temperature and Humidity as a two sensors and then connect these two sensors
with Resbarri pi in which the Temperature sensor is called BMP180 and for Humidity sensor called DHT11 which
shown in following fig 2.

Figure 2: Temperature and Humidity Sensor details


Running the code on Raspberry Pi:
import requests
import time
import random
KEY = 'MY_KEY'
def pushData(temp:float, humidity:float):
'''Takes temperature and humidity readings and pushes data to ThingSpeak server'''
#Set up request url and parameters
url = 'https://api.thingspeak.com/update'
params = {'key': KEY, 'field1': temp, 'field2': humidity}
#Publish values to ThingSpeak
res = requests.get(url, params=params)
if __name__ == "__main__":
while True:
temp = random.randint(0, 100) # Randomly generate a temp value
humidity = random.randint(0, 100) # Randomly generate a humidity value
print(temp, humidity)
pushData(temp,humidity)
time.sleep(10) # Wait 10 seconds

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)

from common import getData, pushData


import time
if __name__ == "__main__":
while True:
sensor = 22
pin = 4
humidity, temp = getData(sensor,pin)
pushData(temp,humidity)
time.sleep(10)
Output:
Below figure show the data of Temperature and Humidity as shown in field chart .

You might also like