IOT LAb Manual
IOT LAb Manual
1
1a. Blinking LED
AIM:
COMPONENTS REQUIRED:
1. Raspberry pi
2. Breadboard
3. Jumper wires
4. Resistor
5. LED
PROCEDURE:
STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.
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.
PROGRAM:
2
OUTPUT:
RESULT:
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:
STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.
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.
PROGRAM:
4
led.on()
button.wait_for_release()
led.off()
OUTPUT:
RESULT:
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
PROCEDURE:
STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.
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.
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:
STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.
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.
PROGRAM:
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:
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:
STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.
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.
PROGRAM:
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:
STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.
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.
PROGRAM:
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()
return distance
if __name__ == '__main__':
try:
while True:
dist = distance()
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:
COMPONENTS REQUIRED:
1. Raspberry Pi
2. Jumper wires
3. Servo motor
5. Computer.
PROCEDURE:
STEP3: Connect HDMI to the system to act as monitor for Raspberry pi.
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
PROGRAM:
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:
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
PROCEDURE:
Step 2: Connect micro USB power input HDMI to the system to act as a monitor Step for
Raspberry Pi
Step 5: Open think speak, connect to the browser, click Sign in & create an account.
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.
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(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 3: Connect HDMI to the system to act as monitor for Raspberry pi.
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 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
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)
while True:
dhtDevice = adafruit_dht.DHT11(board.D14)
time.sleep(2)
temperature_c = dhtDevice.temperature
humidity = dhtDevice.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:
COMPONENTS REQUIRED:
1. Raspberry Pi
2. Jumper Wire
3.OLED Screen.
4.DHT11 Sensor.
PROCEDURE:
STEP 3: Connect HDMI to the system to act as monitor for Raspberry pi.
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
PROGRAM:
import time
24
import subprocess
import board
import adafruit_dht
disp . show ()
width=disp. Width
height=disp. 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. true type ("/wm share/ font / true type / dejaire / Deavere
- Py #f', q)
while True:
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 .show()
OUTPUT:
Result:
26
10. MQTT
AIM:
COMPONENTS REQUIRED:
1. Raspberry Pi
2. Bread board.
3. Jumper Wire
4.DHT11 Sensor.
5.Resistor
PROCEDURE:
STEP 3: Connect HDMI to the system to act as monitor for Raspberry pi.
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
27
PROGRAM:
Subscribe.py
Publish.py
28
client.connect("localhost", 1883 , 60)
dhtDevice = adafruit_dht.DHT11(board.D2, use_pulseio=False)
while True:
print("Temperature:", temperature_c)
# publish temperature data to the MQTT broker
client.publish(mqtt_topic, temperature_c)
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 3: Connect HDMI to the system to act as monitor for Raspberry pi.
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
PROGRAM:
30
import RPi.GPIO as GPIO
import time
OUTPUT:
RESULT:
The above program to Detect obstacles using infrared sensor and measure the distance
using ultrasonic sensor is successfully executed.
31
32