FIoT - Unit-3
FIoT - Unit-3
What is Python?
Python is a high-level, interpreted, general-purpose programming language created by
Guido van Rossum and released in 1991. It is known for its readable syntax, simplicity, and
versatility, making it a popular choice for beginners and professionals alike. Python
supports multiple programming paradigms, including procedural, object-oriented, and
functional programming.
print("Hello, World!")
x = 10 # Integer
name = "Alice" # String
pi = 3.14 # Float
is_valid = True # Boolean
Conditional Statement:
if x > 5:
print("x is greater than 5")
else:
print("x is 5 or less")
Loops:
For Loop:
for i in range(5):
print(i)
While Loop:
count = 0
while count < 5:
print(count)
count += 1
Function:
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Applications of Python
- Web Development – using Django, Flask
- Data Science & Analytics – using Pandas, NumPy, Matplotlib
- Machine Learning & AI – using TensorFlow, scikit-learn, PyTorch
- Scripting & Automation – automating repetitive tasks
- Game Development – with Pygame
- Internet of Things (IoT) – Python on Raspberry Pi
- Cybersecurity – penetration testing and scripting
Why Learn Python?
- Easy for beginners with clean syntax
- Massive online support and community
- High demand in the job market
- Used in both industry and academia
- Flexible and powerful for a wide range of applications
2. Choose an IDE:
Options include:
- IDLE (comes with Python)
- VS Code
- PyCharm
- Jupyter Notebook
3. Start Practicing:
Use online platforms like:
- HackerRank
- LeetCode
- Codecademy
- Replit
Introduction to Raspberry Pi
What is Raspberry Pi?
The Raspberry Pi is a small, affordable, single-board computer developed by the Raspberry
Pi Foundation in the UK. Originally designed to promote computer science education in
schools and developing countries, the Raspberry Pi has evolved into a powerful platform
used in a wide variety of applications including IoT, robotics, home automation, and
embedded systems.
It is about the size of a credit card, yet powerful enough to perform tasks such as word
processing, programming, and even multimedia playback.
- Python
- C/C++
- Java
Tools such as Thonny IDE and VS Code are commonly used for software development.
Applications of Raspberry Pi
1. **Education** – Teaching programming, electronics, and robotics.
2. **Install the OS** – Use Raspberry Pi Imager to write Raspberry Pi OS onto the microSD
card.
3. **Boot and Configure** – Connect peripherals (monitor, keyboard, mouse) and power it
on.
1. Essential Hardware
Raspberry Pi: The core computer.
Micro SD Card: Stores the operating system (Raspberry Pi OS).
Monitor: For displaying output (use HDMI).
Keyboard and Mouse: For input and control (use USB).
Power Supply: To power the Raspberry Pi.
Optional: Ethernet cable for internet access, headphones or speakers.
2. Connecting the Peripherals
Monitor: Connect the monitor to the Raspberry Pi's HDMI port.
Keyboard and Mouse: Connect the keyboard and mouse to the Raspberry Pi's USB
ports.
Power Supply: Connect the power supply to the Raspberry Pi.
SD Card: Insert the micro SD card into the Raspberry Pi.
Optional: Connect ethernet cable to the ethernet port.
3. Getting Started
Power On: Plug in the power supply and the Raspberry Pi will boot up from the SD card.
Operating System: The Raspberry Pi will boot into the Raspberry Pi OS.
Basic Functionality: You can now use the keyboard and mouse to interact with the
Raspberry Pi OS.
4. Additional Peripherals
Cameras:
The Raspberry Pi can interface with cameras, including the Pi Camera module.
Sensors:
You can connect various sensors (temperature, light, etc.) to the Raspberry Pi's GPIO
pins.
Other Devices:
You can connect other devices like RFID readers, LCD displays, and more.
SPI:
Serial Peripheral Interface (SPI) can be used to communicate with high-speed
peripherals.
Implementation of IoT with Raspberry Pi
Introduction
The Internet of Things (IoT) connects physical devices to the internet, enabling data
exchange and remote control. Raspberry Pi—a low-cost, credit card-sized computer—is a
widely used platform for IoT due to its affordability, versatility, and GPIO (General
Purpose Input Output) capabilities.
Hardware Requirements
- Raspberry Pi (3/4 or newer)
- MicroSD Card with Raspberry Pi OS
- Power supply
- Sensors (e.g., DHT11 for temperature/humidity, PIR for motion)
- Actuators (LEDs, Relays)
- Wi-Fi or Ethernet
- Jumper wires and breadboard
Software Requirements
- Operating System: Raspberry Pi OS
- Programming Language: Python
- Libraries:
- RPi.GPIO: Control GPIO pins
- Adafruit_DHT: Read from DHT sensors
- requests, paho-mqtt: For network communication
- Cloud Platforms: Thingspeak, Blynk, AWS IoT, Azure IoT Hub
1. Setting Up Raspberry Pi
Install Raspberry Pi OS using Raspberry Pi Imager. Enable SSH and connect to Wi-Fi.
Update packages using `sudo apt update && sudo apt upgrade`.
2. Connecting Sensors and Actuators
Connect sensors (e.g., DHT11) to GPIO pins. Write Python code to read sensor values.
Test output via terminal or LED indication.
import Adafruit_DHT
import requests
sensor = Adafruit_DHT.DHT11
pin = 4
requests.get(f"https://api.thingspeak.com/update?api_key=YOUR_K
EY&field1={temperature}")
4. Cloud Integration
Create a channel on a cloud platform like ThingSpeak or Blynk. Use HTTP or MQTT
protocols for communication. Visualize real-time data using dashboards.
Applications
- Smart Home Automation (lights, security, climate control)
- Health Monitoring Systems (temperature, heartbeat sensors)
- Agriculture (soil moisture, irrigation control)
- Industrial IoT (predictive maintenance, monitoring)
Security Considerations
- Use secure APIs (HTTPS)
- Change default Raspberry Pi credentials
- Enable firewalls and disable unused ports
Examples
1. Blinking an LED
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
while True:
time.sleep(1)
time.sleep(1)
sensor = Adafruit_DHT.DHT11
print(f"Temp={temperature}°C, Humidity={humidity}%")
else:
import time
GPIO.setmode(GPIO.BCM)
PIR_PIN = 17
GPIO.setup(PIR_PIN, GPIO.IN)
print("Waiting for motion...")
while True:
if GPIO.input(PIR_PIN):
print("Motion detected!")
time.sleep(2)
while True:
button_state = GPIO.input(23)
GPIO.output(24, not button_state)