SEC134 Lab Manual
SEC134 Lab Manual
31.03.22
Problem: Write the python programs for finding the factorial and the prime numbers within the
given range. And do the headless raspberrypi setup with ethernet connection.
Do the headless Raspberry pi board setup with Ethernet connection and VNC viewer
Factorial:
Main Function()
Factorial_Function( n)
def factorial(n):
# Driver Code
SEC134 Hands-On Python with Raspberry Pi - Lab
num = 5;
print("Factorial of",num,"is",
factorial(num))
Input/Output
Input: None
Output: 120
prime_list = []
if i == 0 or i == 1:
continue
else:
if i % j == 0:
break
else:
prime_list.append(i)
return prime_list
# Driver program
starting_range = 2
ending_range = 7
if len(lst) == 0:
else:
Input/Output
SEC134 Hands-On Python with Raspberry Pi - Lab
Input: None
Output: [2 3 5]
Procedure:
Raspberry pi 4 OS setup:
Here we are setting up a headless raspberry pi with Ethernet and VNC viewer.
network={
ssid="NETWORK_NAME"
psk="NETWORK_KEY"
}
9. Login to raspberry pi using ssh. Use putty and use the host name raspberrypi.local (default
port:22)
SEC134 Hands-On Python with Raspberry Pi - Lab
10. Login with the username “pi” and the password “raspberry”
11. Install the VNC server in raspberry pi with the following commands
sudo apt update
sudo apt install realvnc-vnc-server realvnc-vnc-viewer
Conclusion: Thus the python programs for finding the factorial of a given number and prime
numbers within the given range has been written and the headless raspberry pi setup has been done
with ethernet connectivity.
SEC134 Hands-On Python with Raspberry Pi - Lab
Problem: Write the Python program to blink the LED connected to the GPIO pin of a Raspberry Pi
Board
Aim: Write the Python program to blink the LED connected to the GPIO 14 of Raspberry-Pi board.
The LED will be ON for 1 Second and OFF for 2 Seconds.
Schematic:
Raspberry Pi 4 R = 100
Algorithm:
Program:
from time import sleep # Import the sleep function from the time module
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial value to
low (off)
Conclusion: Thus, the python program for blinking the LED connected to the 8th pin of GPIO
connector of Raspberry PI has been executed and witnessed the LED blinking.
SEC134 Hands-On Python with Raspberry Pi - Lab
Ex No 3: Write a program to get the input from the push button and actuate the LED accordingly
with Raspberry Pi
Problem: Write the python program to write a program to turn ON LED when push button is pressed
Aim: Write the Python program to turn ON the LED connected to the GPIO 14 (8th pin) of Raspberry-
Pi board when the push button connected to GPIO 21( 40th pin) is pressed.
Schematic: 3.3V
Raspberry Pi 4 R = 100
Algorithm:
Program:
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial value to
low (off)
inp = GPIO.input(40)
print(inp)
if (inp == 0):
else:
Conclusion: Thus the python program for turning ON the LED connected to the 8th pin of GPIO
connector when the push button connected to 40th pin of GPIO connector of Raspberry PI has been
executed and witnessed the LED blinking.
SEC134 Hands-On Python with Raspberry Pi - Lab
Ex No 4: Write a program to interface DHT11 sensor with Raspberry Pi and write a program to print
temperature and humidity readings.
Aim: Write a program to interface DHT11 sensor with Raspberry Pi and write a program to print
temperature and humidity readings for every 1 second.
Schematic:
GPIO21(40th Pin)
3.3V(1st Pin)
GND(6th Pin)
Raspberry Pi 4
Algorithm:
Program:
import dht11
import time
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
SEC134 Hands-On Python with Raspberry Pi - Lab
#GPIO.cleanup()
GPIO.setup(21, GPIO.IN) # Set pin 8 to be an output pin and set initial value to low (off)
instance = dht11.DHT11(pin=21)
while True:
result = instance.read()
if result.is_valid():
time.sleep(1)
Conclusion: Thus the python program for interfacing DHT11 has been written for getting the
temperature and humidity values and the same is printed every 1 second.
SEC134 Hands-On Python with Raspberry Pi - Lab
Ex No 5: Write python programs to make raspberry pi board as web server and give the sensor
data to client and get the command from the user to turn ON/OFF the LED from the client. interface
DHT11 sensor with Raspberry Pi and write a program to print temperature and humidity readings.
Aim: Write a program to interface DHT11 sensor with Raspberry Pi and use the Flask package to
make the raspberry pi as web sever to serve the web page with temperature and humidity data and
to get the user data to control the led ON/OFF.
Schematic:
GPIO21(40th Pin)
3.3V(1st Pin)
GND(6th Pin)
Raspberry Pi 4
Algorithm:
Program:
Web Page:
body {
background: blue;
color: yellow;
}
SEC134 Hands-On Python with Raspberry Pi - Lab
<html>
<head>
</head>
<body>
</body>
</html>
<html>
<head>
</head>
<body>
</body>
</html>
import dht11
import time
import datetime
SEC134 Hands-On Python with Raspberry Pi - Lab
global temper
global humidi
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#GPIO.cleanup()
GPIO.setup(14, GPIO.OUT, initial=GPIO.LOW) # Set GPIO 14 to be an output pin and set initial value
to low (off)
instance = dht11.DHT11(pin=21)
app = Flask(__name__)
@app.route('/')
def index():
now = datetime.datetime.now()
while True:
result = instance.read()
if result.is_valid():
temper = result.temperature
humidi = result.humidity
break
templateData = {
'title' : 'HELLO!',
'time': timeString,
'Temp': temper,
'Humid': humidi
@app.route('/ledctrl', methods=['GET'])
def ledctrl():
cmd = request.args.get('led')
print(cmd)
if cmd == 'ON':
LedStatusString ='ON'
else:
LedStatusString = 'OFF'
templateData = {
'title' : 'LED_STATUS',
'ledstatus': LedStatusString
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0')
[ To get the output, run this python app and in your PC, open the browser and type the following
Output:
LED control:
LED ON
LED OFF
SEC134 Hands-On Python with Raspberry Pi - Lab
Conclusion: Thus, the python program using Flask has been executed which acts as a web server
with two web pages; index.html and ledstat.html. The application has been tested with the browser
and the sensor data is read and the user command is taken from client and given to the application
which controls the LED accordingly.
SEC134 Hands-On Python with Raspberry Pi - Lab
Ex No 6: Write a program to create UDP server on Raspberry Pi and respond with Temperature
and Humidity data to UDP client.
Aim: Write a python program to create UDP server on Raspberry Pi and respond with
Temperature and Humidity data to UDP client.
Hardware/Software Tools required: Hardware: Raspberry Pi 4 with power adapter, Hercules TCP
Utility
Schematic:
GPIO21(40th Pin)
3.3V(1st Pin)
GND(6th Pin)
Raspberry Pi 4
Algorithm:
Program:
import dht11
import time
import socket
localPort = 20001
SEC134 Hands-On Python with Raspberry Pi - Lab
bufferSize = 1024
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#GPIO.cleanup()
GPIO.setup(21, GPIO.IN) # Set pin 8 to be an output pin and set initial value to low (off)
instance = dht11.DHT11(pin=21)
UDPServerSocket.bind(('', localPort))
while(True):
print(clientMsg)
print(clientIP)
if message == b'read':
while True:
result = instance.read()
if result.is_valid():
temper = result.temperature
humidi = result.humidity
SEC134 Hands-On Python with Raspberry Pi - Lab
break
bytesToSend = str.encode(msgFromServer)
UDPServerSocket.sendto(bytesToSend, address)
[ To get the output, run this python app and in your PC, open the Hercules Utility which will act as a
UDP Client.]
Output:
Conclusion: Thus, a python program has been written to create UDP server on Raspberry Pi and
respond with Temperature and Humidity data to UDP client.
SEC134 Hands-On Python with Raspberry Pi - Lab
Ex No 7: Write a program to create TCP server on Raspberry Pi and respond with Temperature
and Humidity data to TCP client.
Aim: Write a python program to create TCP server on Raspberry Pi and respond with
Temperature and Humidity data to TCP client.
Hardware/Software Tools required: Hardware: Raspberry Pi 4 with power adapter, Hercules TCP
Utility
Schematic:
GPIO21(40th Pin)
3.3V(1st Pin)
GND(6th Pin)
Raspberry Pi 4
Algorithm:
Program:
import dht11
import time
import socket
localPort = 20001
bufferSize = 1024
SEC134 Hands-On Python with Raspberry Pi - Lab
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#GPIO.cleanup()
GPIO.setup(21, GPIO.IN) # Set pin 8 to be an output pin and set initial value to low (off)
instance = dht11.DHT11(pin=21)
TCPServerSocket.bind(('', localPort))
TCPServerSocket.listen()
print(f"Connected by {addr}")
while True:
message = conn.recv(bufferSize)
if not message:
break
if message == b'read':
while True:
result = instance.read()
if result.is_valid():
temper = result.temperature
humidi = result.humidity
break
SEC134 Hands-On Python with Raspberry Pi - Lab
bytesToSend = str.encode(msgFromServer)
conn.sendall(bytesToSend)
[ To get the output, run this python app and in your PC, open the Hercules Utility which will act as a
UDP Client.]
Output:
Conclusion: Thus, a python program has been written to create TCP server on Raspberry Pi and
respond with Temperature and Humidity data to TCP client.
SEC134 Hands-On Python with Raspberry Pi - Lab
Hardware/Software Tools required: Hardware: Raspberry Pi 4 with power adapter, 5MP Camera,
Schematic:
Algorithm:
Program:
button = Button(14)
camera = PiCamera()
camera.rotation = 180
camera.start_preview()
button.wait_for_press()
print("Button pressed")
sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()
#!/usr/bin/python
SEC134 Hands-On Python with Raspberry Pi - Lab
import cv2
Conclusion: Thus, a Python program has been written to interface the camera with Raspberry Pi.
An Edge detection using OpenCV is done in Raspberry Pi for a given image.
SEC134 Hands-On Python with Raspberry Pi - Lab
Source: https://www.tutorialspoint.com/raspberry_pi/raspberry_pi_gpio_connector.htm
Appendix B:
Raspberry pi 4 OS setup:
Here we are setting up headless raspberry pi with Ethernet and VNC viewer.
network={
ssid="NETWORK_NAME"
psk="NETWORK_KEY"
}
23. Login to raspberry pi using ssh. Use putty and use the host name raspberrypi.local (default
port:22)
24. Login with the username “pi” and the password “raspberry”
25. Install the VNC server in raspberry pi with the following commands
sudo apt update
sudo apt install realvnc-vnc-server realvnc-vnc-viewer