[go: up one dir, main page]

0% found this document useful (0 votes)
204 views32 pages

IOT LAb Manual

Internet of Things_ Manual, Lab manual IoT, IoT basic Programs

Uploaded by

THIYAGARAJAN
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)
204 views32 pages

IOT LAb Manual

Internet of Things_ Manual, Lab manual IoT, IoT basic Programs

Uploaded by

THIYAGARAJAN
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/ 32

Lab Manual

Course Title : INTERNET OF THINGS LABORATORY

1
1a. Blinking LED

AIM:

To write a program to Blink an LED using Raspberry pi.

COMPONENTS REQUIRED:

1. Raspberry pi

2. Breadboard

3. Jumper wires

4. Resistor

5. LED

PROCEDURE:

STEP1: Start the process.

STEP2: Connect micro USB power input to Raspberry pi

STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP4: Connect USB port 2.0 to mouse and keyboard.

STEP5: Open Thony →enter coding →save as →file extension python or py.

STEP6: Make the necessary connections of the LED based on the GPIO configuration
specified in the program.

STEP7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP8: The status of the LED is printed in the thony terminal

STEP9: Stop the process.

PROGRAM:

from gpiozero import LED


from time import sleep
led = LED(2)
while True:
led.on()
print('LED ON')
sleep(3)
led.off()
print('LED OFF')
sleep(3)

2
OUTPUT:

RESULT:

The above program to blink an LED using Raspberry Pi is successfully executed.

3
1b. Interfacing Push Button

AIM:

To write a program to interfacing a push button to blink an LED using Raspberry pi.

COMPONENTS REQUIRED:

1. Raspberry pi

2. Breadboard

3. Jumper wires

4. Resistor

5. LED

6.Push button.

PROCEDURE:

STEP1: Start the process.

STEP2: Connect micro-USB power input to Raspberry pi

STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP4: Connect USB port 2.0 to mouse and keyboard.

STEP5: Open Thony →enter coding →save as →file extension python or py.

STEP6: Make the necessary connections of the LED based on the GPIO configuration
specified in the program.

STEP7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP8: The status of the LED is printed in the thony terminal

STEP9: Stop the process.

PROGRAM:

from gpiozero import LED


from gpiozero import Button
led = LED(23)
button = Button(24)
while True:
button.wait_for_press()

4
led.on()
button.wait_for_release()
led.off()

OUTPUT:

RESULT:

The above program to blink an LED using Raspberry Pi is successfully executed.

5
2. Temperature measurement

AIM:

To write a program to find the temperature using temperature and humidity sensor
using Raspberry Pi.

COMPONENTS REQUIRED:

1. Raspberry pi

2. Breadboard

3. Jumper wires

4. Resistor

5. Temperature and humidity

PROCEDURE:

STEP1: Start the process.

STEP2: Connect micro USB power input to Raspberry pi

STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP4: Connect USB port 2.0 to mouse and keyboard.

STEP5: Open Thony →enter coding →save as →file extension python or py.

STEP6: Make the necessary connections of the temperature and humidity sensor
specified in the program.

STEP7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP8: The status of the LED is printed in the thony terminal

STEP9: Stop the process.

PROGRAM:

import time
import board
import adafruit_dht
dhtDevice = adafruit_dht.DHT11(board.D3)
while True:
try:

temperature_c = dhtDevice.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = dhtDevice.humidity

6
print("Temp: {:.1f} F / {:.1f} C Humidity: {}% "
.format(temperature_f, temperature_c, humidity))
except RuntimeError as error:
print(error.args[0])
time.sleep(2.0)

OUTPUT:

RESULT:

The above program to find temperature and humidity sensor using Raspberry Pi is
successfully executed.

7
3. Interfacing LDR to control LED.

AIM:

To write a program to find the temperature using temperature and humidity sensor
using Raspberry Pi.

COMPONENTS REQUIRED:

1. Raspberry pi

2. Breadboard

3. Jumper wires

4. Resistor

5. LED

6.LDR

PROCEDURE:

STEP1: Start the process.

STEP2: Connect micro USB power input to Raspberry pi

STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP4: Connect USB port 2.0 to mouse and keyboard.

STEP5: Open Thony →enter coding →save as →file extension python or py.

STEP6: Make the necessary connections to interface a LDR to control LED using
Raspberry Pi.

STEP7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP8: The status of the LED is printed in the thony terminal

STEP9: Stop the process.

PROGRAM:

import RPi.GPIO as GPIO


import time
GPIO.setmode(GPIO.BOARD)
delayt = .1
value = 0
ldr = 7
led = 11
GPIO.setup(led, GPIO.OUT)

8
GPIO.output(led, False)
def rc_time (ldr):
count = 0
GPIO.setup(ldr, GPIO.OUT)
GPIO.output(ldr, False)
time.sleep(delayt)
GPIO.setup(ldr, GPIO.IN)
while (GPIO.input(ldr) == 0):
count += 1
return count
try:
# Main loop
while True:
print("Ldr Value:")
value = rc_time(ldr)
print(value)
if ( value <= 100):
print("Lights are ON")
GPIO.output(led, True)
if (value > 100):
print("Lights are OFF")
GPIO.output(led, False)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()

RESULT:

The above program to interfacing LDR to control ED using Raspberry Pi is successfully


executed.

9
4. Controlling LED from web server.

AIM:

To write a program to control a LED from web server using Raspberry Pi.

COMPONENTS REQUIRED:

1. Raspberry pi

2. Breadboard

3. Jumper wires

4. Resistor

5. LED

PROCEDURE:

STEP1: Start the process.

STEP2: Connect micro USB power input to Raspberry pi

STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP4: Connect USB port 2.0 to mouse and keyboard.

STEP5: Open Thony →enter coding →save as →file extension python or py.

STEP6: Make the necessary connections of the LED to control it from web server using
Raspberry Pi.

STEP7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP8: The status of the LED is printed in the thony terminal

STEP9: Stop the process.

PROGRAM:

import RPi.GPIO as GPIO


import os
from http.server import BaseHTTPRequestHandler, HTTPServer
host_name = '192.168.1.6' # IP Address of Raspberry Pi
host_port = 4900

def setupGPIO():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)

10
class MyServer(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def _redirect(self, path):
self.send_response(303)
self.send_header('Content-type', 'text/html')
self.send_header('Location', path)
self.end_headers()
def do_GET(self):
html = '''
<html>
<body
style="width:960px; margin: 20px auto;">
<h1>Welcome to my Raspberry Pi</h1>
<form action="/" method="POST">
Turn LED :
<input type="submit" name="submit" value="On">
<input type="submit" name="submit" value="Off">
</form>
</body>
</html>
'''
self.do_HEAD()
self.wfile.write(html.format().encode("utf-8"))
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length).decode("utf-8")
post_data = post_data.split("=")[1]
setupGPIO()
if post_data == 'On':
GPIO.output(18, GPIO.HIGH)
else:
GPIO.output(18, GPIO.LOW)
print("LED is {}".format(post_data))
self._redirect('/')

# # # # # Main # # # # #
if __name__ == '__main__':
http_server = HTTPServer((host_name, host_port), MyServer)
print("Server Starts - %s:%s" % (host_name, host_port))
try:
http_server.serve_forever()
except KeyboardInterrupt:
http_server.server_close()

11
OUTPUT:

RESULT:

The above program to control LED from web server using Raspberry Pi is successfully
executed.

12
5. Distance Measurement.

AIM:

To write a program to measure the distance using ultrasonic sensor using Raspberry Pi.

COMPONENTS REQUIRED:

1. Raspberry pi

2. Breadboard

3. Jumper wires

4. Register

5. Sensor

PROCEDURE:

STEP1: Start the process.

STEP2: Connect micro USB power input to Raspberry pi

STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP4: Connect USB port 2.0 to the mouse and keyboard.

STEP5: Open Thony →enter coding →save as →file extension python or py.

STEP6: Make the necessary connections of the ultrasonic sensor to control it using
Raspberry Pi.

STEP7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP8: The distance is measured in printed in the thony terminal

STEP9: Stop the process.

PROGRAM:

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BCM)

GPIO_TRIGGER = 17

GPIO_ECHO = 27 6

GPIO.setup(GPIO_TRIGGER, GPIO.OUT)

13
GPIO.setup(GPIO_ECHO, GPIO.IN)

def distance():

GPIO.output(GPIO_TRIGGER, True)

time.sleep(0.00001)

GPIO.output(GPIO_TRIGGER, False)

start_time = time.time()

stop_time = time.time()

while GPIO.input(GPIO_ECHO) == 0:

start_time = time.time()

while GPIO.input(GPIO_ECHO) == 1:

stop_time = time.time()

time_elapsed = stop_time - start_time

distance = (time_elapsed * 34300) / 2

return distance

if __name__ == '__main__':

try:

while True:

dist = distance()

print("Distance: %.1f cm" % dist)

time.sleep(1)

except KeyboardInterrupt:

GPIO.cleanup()

14
OUTPUT:

RESULT:

The above program to measure the distance using ultrasonic sensor using Raspberry Pi
is successfully executed.

15
6. Servo Motors.

AIM:

To write a program to control servo motors using Raspberry Pi.

COMPONENTS REQUIRED:

1. Raspberry Pi

2. Jumper wires

3. Servo motor

5. Computer.

PROCEDURE:

STEP1: Start the process.

STEP2: Connect micro USB power input to Raspberry pi

STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP4: Connect USB port 2.0 to the mouse and keyboard.

STEP5: Open Thony →enter coding →save as →file extension python or py.

STEP6: Make the necessary connections to control servo motors using Raspberry Pi.

STEP7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP8: The status of the Servo motor is printed in the thony terminal

STEP9: Stop the process.

PROGRAM:

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11,GPIO.OUT)

servo1 = GPIO.PWM(11,50)

servo1.start(0)

try:

while True:

16
angle = float(input('Enter angle between 0 & 180: '))

servo1.ChangeDutyCycle(2+(angle/18))

time.sleep(0.5)

servo1.ChangeDutyCycle(0)

finally:

servo1.stop()

GPIO.cleanup()

OUTPUT:

Enter Angle between 0 and 180:- 45°

RESULT:

The above program to control servo motors using Raspberry Pi is successfully executed.

17
7. Temperature and Humidity Data in Thingspeak cloud.

AIM:

To write a program to measure temperature and humidity over thingspeak cloud with
Raspberry Pi.

COMPONENTS REQUIRED:

1. Raspberry Pi

2. Bread board.

3. Jumper Wire

4.Temperature and Humidity sensor.

PROCEDURE:

Step 1: Start the process

Step 2: Connect micro USB power input HDMI to the system to act as a monitor Step for
Raspberry Pi

Step 3: Connect HDMI to the system.

Step 4: Connect USB port 2.0 to mouse, keyboard.

Step 5: Open think speak, connect to the browser, click Sign in & create an account.

Step 6: Create a new channel acquire temperature & humidity data.

Step 7: Select the required fields for this program to data.

Step 8: Rename the fields to temperature & humidity & save the changes.

Step 9: New channel has been created, note down & write API key from API key table.

Step 10: Open thony - enter coding - Save as file tension as python or py

Step 11: Make the necessary connection the temperature & humidity sensor specified
program.

Step 12: Load the program into Raspberry Pi using program y the load tab and run it.

Step 13: The temperature and humidity reading will be reflected in think speak.

18
step 14: The statue of the temperature & humidity, measurement will be printed in Stop
the process. thony terminal.

step 15:Stop the process.

PROGRAM:

import time

import board

import adafruit_dht

import requests

import random

dhtDevice = adafruit_dht.DHT11(board.D26)

url = "https://api.thingspeak.com/update.json"

while True:

try:

api_key = "14W8WKW7GW9N4KWX"

temperature_c = dhtDevice.temperature

temperature_f = temperature_c * (9 / 5) + 32

humidity = dhtDevice.humidity

print("Temp: {:.1f} F / {:.1f} C Humidity: {}% "

.format(temperature_f, temperature_c, humidity))

payload = {'api_key': api_key, 'field1': temperature_c, 'field2': humidity}

response = requests.post(url, json=payload)

except RuntimeError as error:

print(error.args[0])

time.sleep(2.0)

19
OUTPUT:

RESULT:

The above program to measure temperature and humidity over the thingspeak cloud is
successfully executed.

20
8. Interfacing Server data over Bluetooth.

AIM:

To write a program to transfer sensor data over Bluetooth using Raspberry Pi.

COMPONENTS REQUIRED:

1. Raspberry Pi

2. Bread board.

3. Jumper Wire

4.DHT11 Sensor.

PROCEDURE:

STEP 1: Start the process.

STEP 2: Connect micro USB power input to Raspberry pi

STEP 3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP 4: Connect USB port 2.0 to the mouse and keyboard.

STEP 5: Open Thony →enter coding →save as →file extension python or py.

STEP 6: Make the necessary connections to control servo motors using Raspberry Pi.

STEP 7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP 8: Install phone P13 bluetooth manager in the smart

Step 9: pair the smartphone with raspberry pi using Bluetooth

Step 10: Open the bluetooth manager in the mobile phone and conned the device with the
appropriate device.

Step 11: Open the connection is established, the temperature & humidity data is
transferred to the phone through the application

STEP 12: Stop the process.

21
PROGRAM:

import bluetooth

import time

import board

import adafruit_dht

server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)

port = 1

server_sock.bind(("", port))

server_sock.listen(1)

print("Waiting for connection on RFCOMM channel %d" % port)

client_sock, address = server_sock.accept()

print("Accepted connection from ", address)

while True:

dhtDevice = adafruit_dht.DHT11(board.D14)

time.sleep(2)

temperature_c = dhtDevice.temperature

humidity = dhtDevice.humidity

print("Temp: {:.1f} C Humidity: {}% ".format(temperature_c, humidity))

data = "Temperature={}*C;Humidity={}%".format(temperature_c, humidity)

client_sock.send(data)

client_sock.close()

server_sock.close()

22
OUTPUT:

23
RESULT:

The above program to transfer sensor data over Bluetooth is successfully executed.

9. Interfacing OLED .

AIM:

To write a program for interfacing OLED using Raspberry Pi.

COMPONENTS REQUIRED:

1. Raspberry Pi

2. Jumper Wire

3.OLED Screen.

4.DHT11 Sensor.

PROCEDURE:

STEP 1: Start the process.

STEP 2: Connect micro USB power input to Raspberry pi

STEP 3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP 4: Connect USB port 2.0 to the mouse and keyboard.

STEP 5: Open Thony →enter coding →save as →file extension python or py.

STEP 6: Make the necessary connections to temperature and humidity sensor using
Raspberry Pi.

STEP 7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP 8: The status of the temperature and humidity reducing will be displayed in the
thony terminal

STEP 9: Stop the process.

PROGRAM:

import time

24
import subprocess

import board

import adafruit_dht

from board import SLL, SDA import bus 10

from PIL import Image, Image Draw, Image fort

import adafruit - ssd 1306.

12c = bus 10. T2C (SCL, SDA)

dip adafruit-sed 1206. SSD1306 - I2C (128, 33, 120)

disp fill (0)

disp . show ()

width=disp. Width

height=disp. height

image=Image new ("." (width, height))

draw =Image Draw Draw(image) draw rectangle (10,0, width, height). culture = 0, fill=0)

padding=2

top =padding

bottom =height-padding

font= Image Font load- default ()

font = Image font. true type ("/wm share/ font / true type / dejaire / Deavere

- Py #f', q)

dht Device adafruit -dht. DHT!! (board DIT, we-pulse io=False)

while True:

draw.rectangle (10,0, width, height), outline =0,fill=0)

temperature-c= dht. Device temperature

humidity= dht Device humidity

draw.text (.top+io), "IP: "+IP,

draw.text ((x, topt+8), "CPU load:" file 255) +CPU, front font, fill-225)

25
draw.text (12, top + 10)," Temperature: " + str temperature -c),

print ("Temp : {:1F } C Humidity: {}/." format fill size: 225) (temperature -c, humidity)) ,

disp. image (image)

disp .show()

time .sleep (0.1)

OUTPUT:

Temperature: 30.0 C Humidity: 57%

Result:

The above program for interfacing OLED is successfully executed.

26
10. MQTT

AIM:

To Write a program on Arduino/Raspberry Pi to publish temperature data to MQTT


broker.

COMPONENTS REQUIRED:

1. Raspberry Pi

2. Bread board.

3. Jumper Wire

4.DHT11 Sensor.

5.Resistor

PROCEDURE:

STEP 1: Start the process.

STEP 2: Connect micro USB power input to Raspberry pi

STEP 3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP 4: Connect USB port 2.0 to the mouse and keyboard.

STEP 5: Open Thony →enter coding →save as →file extension python or py.

STEP 6: Make the necessary connections of the temperature and the humidity sensor
using Raspberry Pi.

STEP 7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP 8: The status of the temperature and humidity reducing will be displayed in the
thony terminal

STEP 9: Stop the process.

27
PROGRAM:

Subscribe.py

import paho.mqtt.client as mqtt

# MQTT broker settings


mqtt_broker = "localhost"
mqtt_port = 1883
mqtt_topic = "Temperature"

# create an MQTT client object


client = mqtt.Client()

# callback function to handle incoming messages


def on_message(client, userdata, message):
# print temperature data
print("Temperature", message.payload.decode())

# set the on_message callback function


client.on_message = on_message

# connect to the MQTT broker and subscribe to the temperature topic


client.connect(mqtt_broker, mqtt_port)
client.subscribe(mqtt_topic)

# start the MQTT client loop to receive incoming messages


client.loop_forever()

Publish.py

import paho.mqtt.client as mqtt


import time
import random
import board
import adafruit_dht
# MQTT broker settings
#broker_address = "192.168.1.4:1883"
mqtt_broker = "1ocalhost"
mqtt_port = 1883
mqtt_topic = "Temperature"

# create an MQTT client object


client = mqtt.Client()

# connect to the MQTT broker

28
client.connect("localhost", 1883 , 60)
dhtDevice = adafruit_dht.DHT11(board.D2, use_pulseio=False)
while True:

# simulate temperature reading between 20 and 30 degrees Celsius


temperature_c = dhtDevice.temperature
#break
#except RuntimeError as e:
# print("Error reading temperature data: ", e)
# print("Retrying...")

print("Temperature:", temperature_c)
# publish temperature data to the MQTT broker
client.publish(mqtt_topic, temperature_c)

# wait for 1 second before publishing the next temperature reading


time.sleep(3)
# continue

# disconnect from the MQTT broker


client.disconnect()

OUTPUT:

Publisher:

Temperature : 32

Subscriber:

Temperature : 32

RESULT:

The above program to publish temperature data to MQTT broker is successfully executed.

29
11. Detection of obstacle.

AIM:

To Detect obstacles using infrared sensor and measure the distance using ultrasonic
sensor

COMPONENTS REQUIRED:

1. Raspberry Pi

2. Bread board.

3. Infrared Sensor

4.Ultrasonic Sensor.

5.Resistor

6.Jumper Wire

PROCEDURE:

STEP 1: Start the process.

STEP 2: Connect micro USB power input to Raspberry pi

STEP 3: Connect HDMI to the system to act as monitor for Raspberry pi.

STEP 4: Connect USB port 2.0 to the mouse and keyboard.

STEP 5: Open Thony →enter coding →save as →file extension python or py.

STEP 6: Make the necessary connections of the temperature and the humidity sensor
using Raspberry Pi.

STEP 7: Load the program into raspberry pi using the Load Tab and Run the program.

STEP 8: Set GP10 pin numbering mode & set up to IR sensor module

STEP 9: Set up The IR sensor pin as an input.

STEP 10: Read IR sensor input and obstacle detected,

STEP 11: Stop the process.

PROGRAM:

30
import RPi.GPIO as GPIO
import time

# Set GPIO pin numbering mode


GPIO.setmode(GPIO.BCM)

# Define GPIO pins for the IR sensor module


IR_SENSOR_PIN = 21

# Set up the IR sensor pin as an input


GPIO.setup(IR_SENSOR_PIN, GPIO.IN)

# Main program loop


while True:
# Read the IR sensor input
ir_input = GPIO.input(IR_SENSOR_PIN)

# If an obstacle is detected, print a message to the terminal


if ir_input == GPIO.HIGH:
print("Obstacle detected!")

# Wait for 0.5 seconds before taking another reading


time.sleep(0.5)

OUTPUT:

RESULT:

The above program to Detect obstacles using infrared sensor and measure the distance
using ultrasonic sensor is successfully executed.

31
32

You might also like