Iot Final
Iot Final
Name :
Subject / Code : Internet Of Things Applications Laboratory – U20CCP609
Branch : Computer and Communication Engineering
Year/Semester : III / VI
Certified that this is the record for Practical Work done by the above student in the Internet of Things
11 TCP Communication
12 UDP Communication
INTRODUCTION:
ETS IoT kit is an all-in-one prototyping platform for sensor based IoT projects. It is packed
with state-of-the-art sensor technology and ready-to-use software applications, capable of fulfilling all
your IoT application needs. It has inbuilt raspberry pi3 with all-in-one sensor platform to enable infinite
IoT application prototype possibilities.
HARDWARE SPECIFICATION:
WIRELESS RADIO
Inbuilt wireless radio is very small, its markings can only be properly seen through a
microscope or magnifying glass, the Broadcom BCM43438 chip provides 2.4GHz 802.11n wireless
LAN, Bluetooth Low Energy, and Bluetooth 4.1 Classic radio support. Cleverly built directly onto the
board to keep costs down, rather than the more common fully qualified module approach, its only
unused feature is a disconnected FM radio receiver.
ANTENNA
There is no need to connect an external antenna to the Raspberry Pi 3. Radios are connected to
this chip antenna soldered directly to the board, to keep the size of the device to a minimum. Despite
its diminutive stature, this antenna should be more than capable of picking up wireless LAN and
Bluetooth signals – even through walls.
SD CARD
The Raspberry Pi should work with any compatible SD card.
SPI DEVICES
MEMORY SIZE:
For installation of NOOBS, the smallest recommended card size is 8GB. For image
installations, we recommend a minimum of 4GB. Some distributions, specifically Libre ELEC and
Arch, can run on much smaller cards.
CARD CLASS:
The card class determines the sustained write speed for the card; a class 4 card will be able to
write at 4MB/s, while a class 10 should be able to attain 10 MB/s. However, it should be noted that this
does not mean a class 10 card will outperform a class 4 card for general usage, because often this write
speed is achieved at the cost of read speed and increased seek times.
I2C DEVICES:
It has six i2c devices.
MCP23017-IC1
MCP23017-IC2
AXDL345
SHT31
OLED
PIN configuration details of ETS IoT KIT, it has an IC called MCP3008 10Bit ADC converter
connected with Chip Enable 0 (CE0). ETS IoT KIT consists of two MCP23017 and one MCP3008
which are used for I/O expander and on-board component connectivity. Below tables describes PIN
mapping details of the same.
HOW TO CONFIGURE THE ETS IOT KIT
1. Format the memory card in FAT32 format.
2. Install Raspberry Pi OS in the SD card.
3. Insert the OS installed SD card in the Raspberry Pi slot.
4. Connect HDMI screen / VGA screen, Keyboard, Mouse with ETS IoT KIT
5. Connect the appropriate power supply (5 V) to ETS IoT KIT.
6. Once the booting process is over the Raspberry Pi screen will be appeared.
7. Enable the settings in the Raspberry Pi (Annexure V)
8. Install the necessary libraries in the Raspberry Pi (Annexure-I)
Step 1: Open terminal
Step 2: Enter the command
Step 3: press “Enter”
PYTHON 3 IDLE LIBRARY INSTALLATION
sudo apt updt.
sudo apt install python3 idle3
ANY DESK LIBRARY INSTALLATION
wget https://download.anydesk.com/rpi/anydesk_6.1.1-1_armhf.deb
sudodpkg -i anydesk_6.1.1-1_armhf.deb
sudo apt-get install –f
MCP23017 LIBRARY INSTALLATION
pip3 install adafruit-circuitpython-mcp230xx sudo pip3 install Adafruit
circuitpython-mcp230xx
ADXL345 library Installation
sudo pip3 install adafruit-circuitpython-adxl34x
SHT31 library Installation
pip3 install adafruit-circuitpython-sht31d sudo pip3 install Adafruit circuitpython-
sht31d
MCP3008 LIBRARY INSTALLATION
pip3 install adafruit-circuitpython-mcp3xxx sudo pip3 install Adafruit circuitpython
mcp3xxx
OLED (SSD1306) LIBRARY INSTALLATION
pip3 install adafruit-circuitpython-ssd1306 sudo pip3 install Adafruit circuitpython-
ssd1306
DHT11 LIBRARY INSTALLATION
sudo pip3 install Adafruit_DHT
MQTT BROKER LIBRARY INSTALLATION
sudo pip3 install paho-mqtt
PI CAMERA LIBRARY INSTALLATION
sudo apt-get install python3-picamera
BMI160 LIBRARY INSTALLATION
python3 -m pip install BMI160-i2c
BLUETOOTH LIBRARY INSTALLATION
pip3 install pybluez
sudo apt-get install Bluetooth bluez lib bluetooth-dev sudo python3 -m pip install
pybluez
REBOOT THE SYSTEM AFTER LIBRARY INSTALLATION
sudo reboot
Open the “Demo_code” folder in the Pi directory and check the codes one by one.
Ex.No.2
INTRODUCTION TO RASPBERRY PI PLATFORM AND PYTHON
Date: PROGRAMMING
AIM
To write a python program to read and print the given input data.
To write a python program to perform the arithmetic operations on the given two numbers and
print their sum, difference, product and division.
PROGRAM
Python program to read name with hello message
name = input ("Enter your data:")
print ("Hello "+ data)
PROCEDURE
1. Start the process
2. Connect micro-USB power input to Raspberry pi.
3. Connect HDMI to the system to act as monitor for Raspberry pi.
4. Connect USB port to mouse and keyboard.
5. Enter the coding in terminal for installing Python.
6. Stop the process.
PROGRAM:
num1 = int (input ("Enter First Number: "))
num2=int (input ("Enter Second Number:"))
print ("Enter which operation would you like to perform?")
ch=input ("Enter any of these char for specific operation +, -, *, /:")
result=0
ifch=='+':
result = num1+num2
elifch =='-':
result = num1-num2
elifch =='*':
result = num1 * num2
elifch =='/':
result = num1 / num2
else:
print ("Input character is not recognized!")
print (num1,ch ,num2, ":",result)
Output1: Addition
Enter First Number: 100
Enter Second Number:5
Enter which operation would you like to perform?
Enter any of these char for specific operation +, -, *, /: + 100 +5: 105
Output 2: Division
Enter First Number: 20
Enter Second Number:5
Enter which operation would you like to perform?
Enter any of these char for specific operation +, -, *, /: / 20 / 5: 4.0
Output 3: Subtraction
Enter First Number: 8
Enter Second Number:7
Enter which operation would you like to perform?
Enter any of these char for specific operation +, -, *, /: -8 -7: 1
Output4: Multiplication
Enter First Number: 6
Enter Second Number:8
Enter which operation would you like to perform?
Enter any of these char for specific operation +, -, *, /: *6 * 8: 48
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
Thus, a python program to perform the arithmetic operations on the given two numbers and
print their sum, difference, product and division is executed and verified successfully
Ex.No. 3a
LED INTERFACE WITH RASPBERRY PI
Date:
AIM:
To Design a Python program to control the state of an LED, allowing for both turning it on and
off programmatically.
COMPONENTS REQUIRED:
1. RASPBERRY PI 3
2. 3.3K resistor
3. Breadboard
4. Led
5. Connecting wire.
PROGRAM:
import RPi.GPIO as GPIO
import time.
led=8 GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led,GPIO.OUT)
while True:
GPIO.output(led,GPIO.HIGH)
time.sleep(1)
GPIO.output(led,GPIO.LOW)
time.sleep(1)
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
The Python program successfully controls the state of an LED on a Raspberry Pi. It integrates
necessary libraries, configures GPIO pins, and provides a user- friendly interface for turning the LED
on and off based on user inputs.
Ex.No. 3b
BUZZER INTERFACE WITH RASPBERRY PI
Date:
AIM:
To Develop a Python program to effectively control the activation and deactivation of a buzzer.
The program aims to provide users with the capability to turn the buzzer on and off through a well-
structured script.
COMPONENTS REQUIRED:
1. RASPBERRY PI 3
2. 3.3K resistor
3. Bread board
4. Buzzer
5. Connecting wire.
PROGRAM:
import RPi.GPIO as GPIO
import time
buzzer=13 GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led,GPIO.OUT)
while True:
GPIO.output(buzzer,GPIO.HIGH)
time.sleep(1)
GPIO.output(buzzer,GPIO.LOW)
time.sleep(1)
CIRCUIT DIAGRAM:
OUTPUT:
Particulars Max Marks Mark secured
Viva 10
Total 25
RESULT:
Upon successful execution, the program will demonstrate the capacity to manage the state
of the buzzer, allowing users to turn it on or off based on specified conditions or user inputs. The user-
friendly interface will enable seamless interaction with the buzzer's state, showcasing the practical
control achieved through the Python script.
Ex.No. 4a
PUSH BUTTON INTERFACE WITH RASPBERRY PI AND TO TURN
Date: ON / OFF LED
AIM:
To Develop a Python program to interface a push button with a Raspberry Pi, allowing users to
turn an LED on and off based on the state of the push button. The program aims to provide a responsive
and user-friendly interface for controlling the LED.
COMPONENTS REQUIRED:
1. RASPBERRY PI 3
2. 3.3K resistor
3. Breadboard
4. Led
5. Push Button
6. Connecting wire.
PROGRAM:
import RPi.GPIO as GPIO
switch=16
led=18
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led,GPIO.OUT)
GPIO.setup(switch,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while True:
if GPIO.input(switch) == 0:
print ("LED ison")
GPIO.output(led,True)
else:
print ("LED is off")
GPIO.output(led,False)
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
The python program successfully interfaces a push button with a Raspberry Pi, enabling
users to turn an LED on and off. Users can interact with the push button, toggling the LED state and
demonstrating effective control through the program.
Ex.No. 4b
PUSH BUTTON INTERFACE WITH RASPBERRY PI AND TO TURN
Date: ON / OFF THE BUZZER
AIM:
To Develop a Python program to interface a push button with a Raspberry Pi, providing
users with the ability to turn a buzzer on and off based on the state of the push button. The program
aims to create an intuitive and responsive interface for controlling the buzzer.
COMPONENTS REQUIRED:
1. Raspberry PI 3
2. 3.3K resistor
3. Bread board
4. Buzzer
5. Push Button
6. Connecting wire.
PROGRAM:
import RPi.GPIO as GPIO
switch=16
buzzer=18
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led,GPIO.OUT)
GPIO.setup(switch,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while True:
if GPIO.input(switch) == 0:
print ("Buzzer is on")
GPIO.output(buzzer,True)
else:
print ("Buzzer is off")
GPIO.output(buzzer,False)
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
The Python program interfaces a push button with a Raspberry Pi, allowing users to turn a
buzzer on and off. Interaction with the push button toggles the buzzer state, showcasing effective control
through the program.
Ex.No. 5a
OLED INTERFACE WITH RASPBERRY PI
Date:
AIM:
To Develop a Python program to interface an OLED display with a Raspberry Pi, aiming
to display text or shapes on the OLED screen. The program's goal is to facilitate effective
communication between the Raspberry Pi and the OLED display.
COMPONENTS REQUIRED:
1. RASPBERRY PI 3
2. OLED Display
3. Connecting Wire
PROGRAM:
import board
import digitalio
from PIL import Image, ImageDraw
import adafruit_ssd1306
oled_reset = digitalio.DigitalInout(board.D4)
width = 128
height= 64
i2c= board.I2C()
disp = adafruit_ssd1306.SSD1306_I2C (WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_ reset)
disp.fill(0)
disp.show()
#create text
image = Image.new('1”, (disp.width,disp.height))
draw=ImageDraw.Draw(imag)
text = input ("Enter a text:”)
font =ImageFont.load_default()
draw.text((0,0),text,font=font,fill=255)
disp.image(image)
disp.show()
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
Upon successful execution, the program will demonstrate the ability to interact with the
OLED display, displaying specified text or shapes as per the program's logic.
Ex.No. 5b
INTERFACE A CAMERA WITH RASPBERRY PI
Date:
AIM:
To Develop a Python program to interface a camera with a Raspberry Pi, with the goal of
activating the camera and capturing an image. The program aims to create a functional interface for
camera control and image capture.
COMPONENTS REQUIRED:
1. RASPBERRY PI 3
2. Pi-camera
PROGRAM:
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
Upon successful execution, the program will demonstrate the Raspberry Pi's capability to
interact with the camera, activating it and capturing an image as per the program's logic.
Ex.No. 6a
SERVO MOTOR INTERFACE WITH RASPBERRY PI
Date:
AIM:
To Develop a Python program to interface a servo motor with a Raspberry Pi, focusing on
controlling the motor's operation. The aim is to establish an effective interface for manipulating the
servo motor's movements.
MATERIAL REQUIRED:
1 Raspberry Pi
2 Servo Motor
3 Jumper Wires
PROGRAM:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
pwm=GPIO.PWM(11,50)
pwm.start(0)
while True:
pwm.ChangeDutyCycle(5) # left -90 degposition
sleep (1)
pwm.ChangeDutyCycle(7.5) # neutral position
sleep (1)
pwm.ChangeDutyCycle(10) # right degposition
sleep (1)
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
The program demonstrates the Raspberry Pi's capability to communicate with the servo
motor, resulting in controlled motor operation.
Ex.No. 6b
RAIN SENSOR INTERFACE WITH RASPBERRY PI
Date:
AIM:
To write a Python program to interface a rain sensor with a Raspberry Pi, with the objective
of generating an alert message when the rain sensor detects rainfall. The aim is to establish an effective
communication link between the Raspberry Pi and the rain sensor for real-time monitoring.
COMPONENTS REQUIRED:
1. RASPBERRY PI 3
2. Raindrop sensing board
3. Control board
4. Buzzer
5. Connecting Wires
6. Breadboard
PROGRAM:
# raindrop sensor DO connect to GPIO18
# HIGH = no rain, LOW = rain detected
# Buzzer on GPIO13
from time import sleep
from gpiozero import Buzzer, InputDevice
buzz = buzzer (13)
no_rain = InputDevice(18)
def buzz_now(iterations):
for x in range(iterations):
buzz.on()
sleep (0.1)
buzz.off ()
sleep (0.1)
while True:
if not no_rain.is_active:
print ("It's raining!")
buzz_now (5)
sleep (1)
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Marks Mark secured
Viva 10
Total 25
RESULT:
Upon successful execution, the program will showcase the Raspberry Pi's ability to
interface with the rain sensor, detecting rainfall and generating an alert message as per the program's
logic.
Ex.No. 7a
ULTRASONIC SENSOR INTERFACE WITH RASPBERRY PI.
Date:
AIM:
To write a Python program to interface an ultrasonic sensor with a Raspberry Pi, focusing
on accurately detecting and displaying the distance measured by the sensor. The aim is to establish
effective communication and control between the Raspberry Pi and the ultrasonic sensor.
COMPONENTS REQUIRED:
1. Raspberry Pi
2. Ultrasonic sensor (HC-SR04)
3. 1k Resistor
4. 1.5k Resistor
5. Breadboard
6. Connecting Wires
PROGRAM:
import RPi.GPIO as GPIO
import time
TRIG=21
ECHO=20
GPIO.setmode(GPIO.BCM)
while True:
print ("distance measurement in progress")
GPIO.setup (TRIG, GPIO.OUT)
GPIO.setup (ECHO, GPIO.IN)
GPIO.output (TRIG,False)
print("waiting for sensor to settle")
time.sleep (0.2)
GPIO.output (TRIG,True)
time.sleep (0.00001)
GPIO.output (TRIG,False)
while GPIO.input (ECHO)==0:
pulse_start=time.time()
while GPIO.input (ECHO)==1:
pulse_end=time.time()
pulse_duration=pulse_end-pulse_start
distance=pulse_duration*17150
distance=round (distance,2)
print"distance:",distance,"cm" time.sleep (2)
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
Upon successful execution, the program will demonstrate the Raspberry Pi's capability to
interface with the ultrasonic sensor, accurately measure distances, and display the results as per the
program's logic.
Ex.No. 7b
IR SENSOR INTERFACE WITH RASPBERRY PI
Date:
AIM:
To Write a Python program to interface an IR sensor with a Raspberry Pi, aiming to detect
the presence of an obstacle. The objective is to establish effective communication between the
Raspberry Pi and the IR sensor for obstacle detection.
MATERIAL REQUIRED:
1. Raspberry Pi
2. IR Module
3. Breadboard
4. Connecting Wires
PROGRAM:
import RPi.GPIO as GPIO import time
sensor = 16
buzzer = 18
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor,GPIO.IN)
GPIO.setup(buzzer,GPIO.OUT)
GPIO.output(buzzer,False)
print ("IR Sensor Ready ")
print (" ")
try:
while True:
if GPIO.input(sensor):
GPIO.output(buzzer,True)
print "Object Detected”.
while GPIO.
input(sensor):
time.sleep(0.2)
else:
GPIO.output(buzzer,False)
except KeyboardInterrupt:
GPIO.cleanup()
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Marks Mark secured
Viva 10
Total 25
RESULT:
The program showcases the Raspberry Pi's ability to interface with the IR sensor, detecting
obstacles and potentially generating an alert or response.
Ex.No. 8a
THINKGSPEAK HUMIDITY AND TEMPERATURE USING
Date: RASPBERRY PI
AIM:
To develop a Python program to interface a DHT1/DHT22 sensor with a Raspberry Pi,
with the goal of verifying and uploading humidity and temperature data to Thinkgspeak. The aim is to
establish communication between the Raspberry Pi and the sensor for real-time monitoring.
COMPONENTS REQUIRED
1. Raspberry Pi
2. DHT11/22
3. Connecting wires
PROGRAM:
while True:
try:
humi, temp = DHT22_data()
print(humi,temp)
if isinstance(humi, float) and isinstance(temp, float):
humi = '%.2f' % humi
temp = '%.2f' % temp
conn = urllib2.urlopen(baseURL + '&field1=%s&field2=%s' % (temp, humi))
print(conn.read())
# Closing the connection conn.close()
else:
print('Error')
sleep (10)
except:
break
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
Upon successful execution, the program will demonstrate the Raspberry Pi's ability to
interface with the DHT sensor, read humidity and temperature data, and upload the information to
ThingSpeak
Ex.No. 8b
HUMIDITY AND TEMPERATURE SENSING IN USING
Date: ESP8266
AIM:
To Write a C program to interface a DHT11 / DHT22 sensor with an ESP8266 module,
with the goal of verifying and obtaining humidity and temperature readings. The aim is to establish
effective communication between the ESP8266 module and the sensor for data retrieval.
COMPONENTS REQUIRED:
1. ESP8266 NODE MC
2. DHT11/22
3. Connecting wires
PROGRAM:
#include <DHT.h>
#include <ESP8266WiFi.h>
String apiKey = "83KCN4VCEGD6FCR5”;
const char *ssid = "Vivo iq z3”; // replace with your wifi ssid and wpa2 key //
SMVEC_CONFERANCE1 // Xperia XZ
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
Upon successful execution, the program will demonstrate the ESP8266 module's ability to
interface with the DHT sensor, retrieve humidity and temperature data, and potentially display or
transmit the information as desired. The result showcases the integration of sensor data with the
ESP8266 module, providing a practical solution for humidity and temperature sensing in IoT
applications
Ex.No. 9
DATABASE CREATION USING MYSQL
Date:
AIM:
Create a Python program to establish a connection with a MySQL database on a Raspberry
Pi and perform basic SQL queries. The aim is to demonstrate the Raspberry Pi's capability to interact
with a MySQL database and execute fundamental SQL operations.
CONFIGURATION / INSTALLATIONS:
1. Before we get started with installing MySQL to our Raspberry Pi, we must first updt. our
package list and all installed packages.
sudo apt updt.
sudo apt upgrade
2. The next step is to install the MySQL server software on your Raspberry Pi.
sudo apt install mariadb-server
3. With the MySQL server software installed to the Raspberry Pi, we will now need to secure it
by setting a password for the “root” user.
sudo mysql_secure_installation
4. if you want to access your Raspberry Pi’s MySQL server and start making changes to your
databases, you can enter the following command.
sudo mysql -u root -p
5. You will be prompted to enter the password that we just created in step 3 for MySQL’s root
user.
SQL COMMAND:
Start by creating a MySQL database using the following command:
1. Creating Database
CREATE DATABASE exampledb;
2. Creating Table
CREATE TABLE ECE (fname varchar (10),degn varchar(10),empid int(10));
3. Inserting Values
INSERT INTO ECE VALUES(‘RAJA’,’Prof’,1923);
4. Showing ECE table
Select*from ECE
OUTPUT:
fname Degn empid
CHETHAN PRAKASH Asst. Professor 1923
RAJA Professor 1239
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
Upon successful execution, the program will showcase the Raspberry Pi's ability to
connect to a MySQL database. The result demonstrates the integration of MySQL with the Raspberry
Pi, providing a foundation for database interaction in various applications
Ex.No. 10
MQTT PUBLISHER AND SUBSCRIBER
Date:
AIM:
Develop a Python program to act as an MQTT (Message Queuing Telemetry Transport)
publisher. The objective is to create a script that can publish messages or data to an MQTT broker,
facilitating communication with other MQTT- enabled devices or subscribers.
COMPONENT REQUIRED:
• 1 x Raspberry Pi
• Stable internet access
PROGRAM:
mqtt_publisher.py:
# MQTT Settings for HiveMQ Public Broker
mqtt_broker_address = "broker.hivemq.com"
mqtt_port = 1883
mqtt_topic = "temperature"
# Create MQTT client instance
mqtt_client = mqtt.Client()
# Connect to MQTT broker
mqtt_client.connect(mqtt_broker_address, mqtt_port, 60)
while True:
temperature = random.uniform(20, 30) # Simulating temperature data
print (f"Publishing Temperature: {temperature:.2f} °C")
# Publish temperature to MQTT broker
mqtt_client.publish(mqtt_topic, str(temperature))
time.sleep(5)
mqtt_subscriber.py:
import paho.mqtt.client as mqtt
# MQTT Settings for HiveMQ Public Broker
mqtt_broker_address = "broker.hivemq.com"mqtt_port = 1883
mqtt_topic = "temperature"
# Callback when the client connects to the broker
def on_connect(client, userdata, flags, rc):
print (f"Connected to MQTT Broker with result code {rc}")
# Subscribe to the temperature topic
client.subscribe(mqtt_topic)
# Callback when a message is received
def on_message(client, userdata, msg):
print (f"Received message on topic {msg.topic}: {msg.payload} °C")
# Create MQTT client instance
mqtt_client = mqtt.Client()
# Set callback functions
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
# Connect to MQTT broker
mqtt_client.connect(mqtt_broker_address, mqtt_port, 60)
# Start a background thread for handling MQTT messages
mqtt_client.loop_start()
# Keep the script running
while True: pass
OUTPUT:
MQTT_publisher.py
MQTT_subscriber.py
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
Upon successful execution, the program will demonstrate the Raspberry Pi's ability to act
as an MQTT subscriber, receiving and handling messages from specific topics on the MQTT broker.
The result showcases the integration of MQTT communication on the Raspberry Pi, providing a flexible
and responsive platform for real-time data consumption in IoT (Internet of Things) scenarios.
Ex.No. 11
TCP COMMUNICATION
Date:
AIM:
To Develop a Python program on the Raspberry Pi to establish TCP (Transmission Control
Protocol) communication. The objective is to create a script that enables the Raspberry Pi to act as either
a TCP server or client, facilitating data exchange over a TCP connection.
COMPONENT REQUIRED:
• 1 x Raspberry Pi
• Stable internet access
PROGRAM:
Client.py
import socket
host = ' ' #enter host address of the server
#example INET 192.168.43.2
port = 9090
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((host,port))
server.send("Hello server !".encode('utf-8'))
print(server.recv(1024).decode('utf-8'))
Server.py
import socket
host = ' ' #find the ip address from
#in terminal type:
# and check find the INET address
#example INET 192.168.43.2
port = 8080
server=socket.socket(socket.AF.INET,socket.SOCK_STREAM)
server.bind((host,port))
server.listen(5)
while True:
com_soc, address = server.accept()
print (f"connted to {address}")
message = com_soc.recv(1024).decode('utf-8')
print (f"Message from client is: {message}")
com_soc.send(f"Message recived ! Thank you!".encode('utf-8'))
com_soc.close()
print (f"connection with {address} ended!")
OUTPUT:
Client.py
Server.py
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT:
Upon successful execution, the program will demonstrate the Raspberry Pi's ability to
establish and participate in TCP communication. The result show cases the integration of TCP
functionality, providing a means for the Raspberry Pi to send and receive data over a TCP network,
fostering versatile communication capabilities for various applications
Ex.No. 12
UDP COMMUNICATION
Date:
AIM:
To write a Python program on the Raspberry Pi to implement UDP (User Datagram
Protocol) communication. The goal is to develop a script that allows the Raspberry Pi to function as
either a UDP server or client, facilitating data exchange over a UDP connection.
COMPONENT REQUIRED:
• 1 x Raspberry Pi
• Stable internet access
PROGRAM:
Client.py
import socket
host = ' ' #enter host address of the server
port = 8080
server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server.sendto("Hello server !".encode('utf-8'),(host,port))
print(server.recvfrom(1024)[0].decode('utf-8'))
Server.py
import socket
host = ' ‘ #find the ip address from
#in terminal type:
#ifconfig and check find the INET address
#example INET 192.168.43.2
port = 8080
server = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
server.bind((host,port))
message, address = server.recvfrom(1024)
print(message.decode('utf-8'))
server.sendto("Message from server".encode('utf-8'), address)
OUTPUT:
Client.py
Server.py
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT
Upon successful execution, the program will showcase the Raspberry Pi's capability to
establish and engage in UDP communication. The result demonstrates the integration of UDP
functionality, enabling the Raspberry Pi to transmit and receive data over a UDP network, providing a
lightweight and connectionless communication method for diverse applications.
Ex.No. 13
LED CONTROL USING ESP8266 WITH BLYNK APP
Date:
AIM:
To Develop a Python program on the Raspberry Pi to create a web interface that allows
users to check the status of an LED remotely over a Local Area Network (LAN). The objective is to
provide a user-friendly web page that displays whether the LED is currently turned on or off.
COMPONENT REQUIRED:
• 1 x Raspberry Pi
• 1 x USB cable
• 1 x SD Card and adapter (4GB minimum)
• 1 x Led Light
• 1 x Resistor
• Jumper Wires
Creating the python server with requests to handle LED ON or LED OFF
PROGRAM:
import BlynkLib
import RPi.GPIO as GPIO
BLYNK_AUTH_TOKEN = 'wmXhipe5qkT89okQkjsN0yCgGa1gjeGd'
led1 = 18
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(led1,GPIO.OUT)
x = 20
# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN)
# Led control through V0 virtual pin
@blynk.on("V0")
def v0_write_handler(value):
# global led_switch
if int(value[0])!= 0:
GPIO.output(led1, GPIO.HIGH)
print('LED1 HIGH')
else:
GPIO.output(led1, GPIO.LOW)
print('LED1 LOW')
# Led control through V0 virtual pin
@blynk.on("V1")
def v1_write_handler(value):
# global led_switch
if int(value[0]) != 0:
GPIO.output(led2, GPIO.HIGH)
print('LED2 HIGH')
else:
GPIO.output(led2, GPIO.LOW)
print('LED2 LOW')
#function to sync the data from virtual pins
@blynk.on("connected")
def blynk_connected():
print("Raspberry Pi Connected to New Blynk")
while True:
blynk.run()
CIRCUIT DIAGRAM:
OUTPUT:
Max
Particulars Mark secured
Marks
Program and Execution 15
Viva 10
Total 25
RESULT
Upon successful execution, the program will enable users to access a web page hosted on
the Raspberry Pi from a remote location on the LAN. The web page should display the status of the
LED, indicating whether it is on or off. This result demonstrates the integration of web-based control,
offering a convenient way to monitor the LED's status remotely