iot_file
iot_file
Objective:
Familiarize yourself with basic Linux commands on the Raspberry Pi.
Materials Needed:
• Raspberry Pi with Raspbian OS installed
• Monitor
• Keyboard and mouse
Steps:
1. Booting Up the Raspberry Pi:
• Connect your Raspberry Pi to the monitor, keyboard, and mouse.
• Power on the Raspberry Pi.
• Log in with your username and password.
2. Opening the Terminal:
• Open the Terminal application from the desktop environment.
3. Executing Basic Commands:
• ls: List directory contents
sh
Copy code
ls
This command lists all files and directories in the current directory.
• cd: Change directory
sh
Copy code
cd /path/to/directory
This opens the cron table for editing, allowing you to schedule tasks.
• chown: Change file owner
sh
Copy code
sudo chown newuser myfile.txt
Materials Needed:
• Raspberry Pi with Raspbian OS installed
• Monitor
• Keyboard and mouse
Steps:
1. Boot Up the Raspberry Pi and Open Terminal:
• Power on your Raspberry Pi.
• Open the Terminal application.
2. Run the Python Programs:
• a) Read your name and print Hello message with name
python
Copy code
# hello_name.py
name = input("Enter your name: ")
print(f"Hello, {name}!")
• b) Read two numbers and print their sum, difference, product, and division
python
Copy code
# arithmetic_operations.py
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
print(f"Sum: {sum_result}")
print(f"Difference: {diff_result}")
print(f"Product: {prod_result}")
print(f"Division: {div_result}")
word_count = len(input_string.split())
char_count = len(input_string)
• d) Area of a given shape (rectangle, triangle, and circle) reading shape and
appropriate values from standard input
python
Copy code
# area_of_shapes.py
import math
if shape == "rectangle":
length = float(input("Enter the length: "))
width = float(input("Enter the width: "))
area = length * width
elif shape == "triangle":
base = float(input("Enter the base: "))
height = float(input("Enter the height: "))
area = 0.5 * base * height
elif shape == "circle":
radius = float(input("Enter the radius: "))
area = math.pi * (radius ** 2)
else:
area = None
print("Invalid shape entered.")
Materials Needed:
• Raspberry Pi with Raspbian OS installed
• Monitor
• Keyboard and mouse
Steps:
1. Boot Up the Raspberry Pi and Open Terminal:
• Power on your Raspberry Pi.
• Open the Terminal application.
2. Run the Python Programs:
• a) Print a name 'n' times, where name and n are read from standard input,
using for and while loops
Using For Loop:
python
Copy code
# print_name_for.py
name = input("Enter your name: ")
n = int(input("Enter the number of times to print your name: "))
for i in range(n):
print(name)
count = 0
while count < n:
print(name)
count += 1
for i in range(10):
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"Current time: {current_time}")
time.sleep(10)
• d) Read a file line by line and print the word count of each line
Creating a Sample File: Create a file named sample.txt with some text in it.
sh
Copy code
echo "Hello world\nThis is a sample file\nIt has multiple lines\
nEach with words" > sample.txt
Materials Needed:
• Raspberry Pi with Raspbian OS installed
• Monitor
• Keyboard and mouse
• Breadboard
• Jumper wires
• LEDs (at least 2)
• Resistors (220Ω)
• Push-button switches (at least 2)
Steps:
1. Boot Up the Raspberry Pi and Open Terminal:
• Power on your Raspberry Pi.
• Open the Terminal application.
2. Install Required Python Library:
• Ensure the RPi.GPIO library is installed. You can install it using:
sh
Copy code
sudo apt-get update
sudo apt-get install python3-rpi.gpio
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setmode(GPIO.BCM)
# Setup switches
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Setup LEDs
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
try:
while True:
if GPIO.input(18) == GPIO.HIGH:
GPIO.output(24, GPIO.HIGH)
else:
GPIO.output(24, GPIO.LOW)
if GPIO.input(23) == GPIO.HIGH:
GPIO.output(25, GPIO.HIGH)
else:
GPIO.output(25, GPIO.LOW)
time.sleep(0.1)
except KeyboardInterrupt:
pass
GPIO.cleanup()
Python Program:
python
Copy code
# flash_led_from_file.py
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
try:
while True:
GPIO.output(17, GPIO.HIGH)
time.sleep(on_time)
GPIO.output(17, GPIO.LOW)
time.sleep(off_time)
except KeyboardInterrupt:
pass
GPIO.cleanup()