0% found this document useful (0 votes)
27 views36 pages

Unit 4

The document discusses the use of JSON and XML as data interchange formats in IoT applications, highlighting their structures, advantages, and how they facilitate communication between clients and servers. It also covers Python libraries such as httplib, urllib, and smtplib for handling HTTP requests, URL manipulations, and sending emails, respectively. Additionally, it introduces Raspberry Pi as a versatile platform for building IoT devices, emphasizing its capabilities in interfacing with sensors and actuators.

Uploaded by

keerthanacse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views36 pages

Unit 4

The document discusses the use of JSON and XML as data interchange formats in IoT applications, highlighting their structures, advantages, and how they facilitate communication between clients and servers. It also covers Python libraries such as httplib, urllib, and smtplib for handling HTTP requests, URL manipulations, and sending emails, respectively. Additionally, it introduces Raspberry Pi as a versatile platform for building IoT devices, emphasizing its capabilities in interfacing with sensors and actuators.

Uploaded by

keerthanacse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Unit 4

BUILDING IoT WITH RASPBERRY PI


Python Packages of Interest for IoT
JSON(Java Script Object Notation):
 Easy to read and write data – interchange

format
 JSON is used as an alternative to XML
 Easy for machines to parse and generate

 ▪Built is two structures :


 i) Collection of name – value pairs ( Dictionaries in Python)
 ii) Ordered list of values (List in Python)

 JSON format is used for serializing and transmitting


structured data over a N/W connection Example,
transmitting data b/w server and web application.
 JSON (JavaScript Object Notation) is a lightweight data interchange
format that is easy for both humans and machines to read and
write. It is commonly used for serializing and transmitting
structured data over a network.

 JSON in Network Communication


◦ When data needs to be exchanged between a client (such as a
web browser or mobile app) and a server, JSON is often used as
the format for request and response messages.

 JSON Communication Flow in a Network

HTTP Request (JSON)


Web Browser Server
(Client) (Backend API)
HTTP Response (JSON)
 Example of JSON in Client-Server Communication
◦ Client Sends a Request (JSON Format):
The client (e.g., a web application) sends a request to the
server in JSON format.
 json
{
"username": "john_doe",
"password": "secure123"
}

◦ Server Processes the Request and Sends a Response:


The server processes the request and responds with a JSON
object containing the requested data.
 json
{
"status": "success",
"message": "Login successful",
"user_id": 101
}
 Note that,
exchange of
information
encoded as
JSON involves
encoding and
decoding
steps Python
Package JSON
has encoding
and decoding
JSON.
 XML (Extensible Markup Language) is used to store and transport data in a
structured format.
It is human-readable and machine-friendly, similar to HTML but used for data
exchange.
 1. Basic Structure of an XML File
 <?xml version="1.0" encoding="UTF-8"?>
 <college>
 <student>
 <firstname>Sumalatha</firstname>
 <lastname>Aradhya</lastname>
 <contact>9945599554</contact>
 <email>[email protected]</email>
 <address>
 <city>Tumakuru</city>
 <state>Karnataka</state>
 <pin>572103</pin>
 </address>
 </student>
 </college>
 Creating an XML File Using Python
 Step-by-Step Process:
 1️⃣Import the required module

2️⃣Create the root element


3️⃣Add child elements
4️⃣Save the XML to a file
XML (Extensible Markup
Language)
 XML (Extensible Markup Language) is a markup
language designed to store, transport, and structure
data. It is both human-readable and machine-
readable and is widely used for data exchange
between systems.
 Features of XML
◦ Self-Descriptive: Tags define the data and structure (e.g.,
<name>John</name>).
◦ Hierarchical Structure: Uses a tree-like format to represent
relationships.
◦ Platform-Independent: Can be used across different
programming languages and systems.
◦ Extensible: Users can create their own custom tags.
◦ Widely Used: Found in APIs, web services, configurations, and
databases.
 Basic xml Structure
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student>
<id>101</id>
<name>John Doe</name>
<email>[email protected]</email>
</student>
</students>

 Advantages of XML
◦ Data Storage: Used for storing structured data (e.g., configuration
files, databases).
◦ Data Exchange: Commonly used in APIs (SOAP, REST) to exchange
data between different applications.
◦ Configuration Files: Used in Android apps (AndroidManifest.xml),
software settings, and web services.
◦ IoT & Web Services: Helps IoT devices and web applications
communicate.
 Creating an XML File in Python
1. Import the Required Module
import xml.etree.ElementTree as ET
2. Create the Root Element
college = ET.Element("college")
3. Add a Child Element (student)
student = ET.SubElement(college, "student")
4. Add Sub-elements with Text Values
ET.SubElement(student, "firstname").text = "Sumalatha"
ET.SubElement(student, "lastname").text = "Aradhya"
ET.SubElement(student, "contact").text = "9945599554"
ET.SubElement(student, "email").text = [email protected]
5. Write the XML Data to a File
tree = ET.ElementTree(college)
tree.write("output.xml", encoding="utf-8", xml_declaration=True)
6. Output: Generated output.xml File
<?xml version="1.0" encoding="utf-8"?>
<college>
<student>
<firstname>Sumalatha</firstname>
<lastname>Aradhya</lastname>
<contact>9945599554</contact>
<email>[email protected]</email>
</student>
</college>
 httplib is a Python 2 module used to handle
HTTP requests.
However, in Python 3, httplib was replaced
with http.client.
 If you're using Python 3, you should use
http.client instead of httplib.
 1. Making a Simple HTTP GET Request
 How It Works
 ✅ HTTPConnection("www.example.com") →
Connects to the website
✅ request("GET", "/") → Sends a GET request
✅ getresponse() → Receives the response
 2. Making an HTTPS Request (Secure Connection)
 If the website uses HTTPS, you need HTTPSConnection
instead of HTTPConnection.
 3. Se
 How It Works
 ✅ request("POST", "/posts", data, headers) → Sends
data
✅ headers = {"Content-type": "application/json"} →
Sets headers
 nding Data (POST Request)
 4. Handling Errors in HTTP Requests
 How It Works
 ✅ HTTPException → Catches HTTP errors
✅ Handles connection issues
httplib2
 httplib2 is a lightweight HTTP client library
for Python that supports HTTP and HTTPS
requests. It provides caching,
authentication, and compression features.
 Supports GET, POST, PUT, DELETE
 Handles authentication (Basic, Digest,

OAuth)
 Provides caching for performance
 Works with HTTPS
URLLib
 urllib is a Python module used to work with URLs (Uniform Resource
Locators).
It allows us to fetch data from the internet, send data to websites,
and handle errors.
 1. Fetching Data from a Website (GET Request)
◦ import urllib.request

◦ url = "https://jsonplaceholder.typicode.com/posts/1"

◦ # Open the URL and fetch data


◦ response = urllib.request.urlopen(url)

◦ # Print status and response


◦ print("Status Code:", response.status)
◦ print("Response:", response.read().decode('utf-8'))
 How It Works
 ✅ urlopen(url): Opens the URL
✅ response.read(): Reads the webpage content
✅ .decode('utf-8'): Converts bytes to text
 2. Sending Data to a Website (POST Request)
 import urllib.request
 import urllib.parse

 url = "https://jsonplaceholder.typicode.com/posts"

 # Data to send (like filling a form)


 data = urllib.parse.urlencode({
 "title": "My Post",
 "body": "This is a test post.",
 "userId": 1
 }).encode('utf-8')

 # Create a request
 req = urllib.request.Request(url, data=data, method="POST")

 # Send the request and get the response


 response = urllib.request.urlopen(req)

 print(response.read().decode('utf-8'))
 How It Works
 ✅ urllib.parse.urlencode(): Converts data to URL format
✅ urllib.request.Request(): Creates a request object
✅ urlopen(req): Sends the request
 3. Handling Errors (When Something Goes Wrong!)
 This helps catch errors like wrong URLs or no internet.
 import urllib.request
 import urllib.error

 url = "https://invalid-url.com"

 try:
 response = urllib.request.urlopen(url)
 print(response.read().decode('utf-8'))
 except urllib.error.HTTPError as e:
 print("HTTP Error:", e.code) # Example: 404 Not Found
 except urllib.error.URLError as e:
 print("URL Error:", e.reason) # Example: No internet connection
 How It Works
 ✅ HTTPError: Catches server errors (e.g., 404 Not Found)
✅ URLError: Catches connection errors
 4. Downloading a File from the Internet
 This is like saving an image, PDF, or any file.
 import urllib.request

 url = "https://www.example.com/sample.pdf"

 # Download and save the file


 urllib.request.urlretrieve(url, "downloaded_file.pdf")

 print("File downloaded successfully!")


 How It Works
 ✅ urlretrieve(url, filename): Saves the file locally
 5. Parsing URLs
 If you need to extract parts of a URL, use urllib.parse.
 from urllib.parse import urlparse

 url = "https://example.com/path?name=John&age=25"

 parsed_url = urlparse(url)

 print("Scheme:", parsed_url.scheme) # https


 print("Netloc:", parsed_url.netloc) # example.com
 print("Path:", parsed_url.path) # /path
 print("Query:", parsed_url.query) # name=John&age=25
 How It Works
 ✅ urlparse(url): Breaks URL into parts
✅ scheme: Protocol (http, https)
✅ netloc: Website domain
✅ path: Page path
✅ query: Parameters
 Why Use urllib?
 ✅ Fetch Web Data Easily
✅ Send Data to Websites
✅ Download Files
✅ Parse URLs
 It’s built into Python (No need to install
anything!)
 Can open web pages, send data, and
handle errors
 Useful for web scraping, APIs, and file
downloads
smtplib
 smtplib is a Python module that helps send emails using SMTP
(Simple Mail Transfer Protocol).
 Think of it like a virtual postman that delivers your email to the
recipient.
 How It Works (Step-by-Step)
1. Connect to the SMTP Server
Gmail: smtp.gmail.com (Port 587)
Outlook: smtp.office365.com (Port 587)
Yahoo: smtp.mail.yahoo.com (Port 465 with SSL)
2. Start a Secure Connection
server.starttls() ensures a secure email transmission.
3. Login to Your Email
Use your email and App Password (not your real password).
For Gmail, enable "Less Secure Apps" or use an App Password.
4. Send the Email
server.sendmail(sender, receiver, message) sends the email.
5. Close the Connection
server.quit() disconnects from the email server.
Common Errors & Fixes
 Error: SMTPAuthenticationError

Fix: Use an App Password instead of your real


password.
 Error: smtplib.SMTPException: STARTTLS extension not

supported by server
Fix: Use SSL (465) instead of TLS (587)
 server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
 Error: smtplib.SMTPRecipientsRefused
Fix: Check if the recipient email is correct.
Use smtplib
 Automate Emails (Alerts, Reports, Notifications)

Send Bulk Emails


Attach Files & Send Secure Emails
Python Packages of Interest for IoT
 1. JSON: JavaScript Object Notation (JSON) is an easy to read and write
data- interchange format. JSON is used as an alternative to XML and is an
easy for machines to parse and generate. JSON is built on two structures -
a collection of name-value pairs (e.g. a Python dictionary) and ordered lists
of values (e.g.. a Pythonlist).

 2. XML: XML (Extensible Markup Language) is a data format for structured


document interchange. The Python minidom library provides a minimal
implementation of the Document Object Model interface and has an API
similar to that in other languages.

 3. HTTPLib & URLLib: HTTPLib2 and URLLib2 are Python libraries used in
network/internetprogramming

 4. SMTPLib: Simple Mail Transfer Protocol (SMTP) is a protocol which


handles sending email and routing e-mail between mail servers. The
Python smtplib module provides an SMTP client session object that can be
used to send email.
 5. NumPy: NumPy is a package for
scientific computing in Python. NumPy
provides support for large multi-dimensional
arrays and matrices

 6. Scikit-learn: Scikit-learn is an open


source machine learning library for Python
that provides implementations of various
machine learning algorithms for
classification, clustering, regression and
dimension reduction problems.
IoT Physical Devices &
Endpoints
 Building blocks
 Raspberry Pi Board
 Linux on Raspberry Pi
 Raspberry Pi Interfaces
IoT Devices
• The "Things" in IoT usually refers to IoT devices
which have unique identities and can perform
remote sensing, actuating and monitoring
capabilities.
• IoT devices can:
• Exchange data with other connected devices and
applications (directly or indirectly), or
• Collect data from other devices and process the data
locally or
• Send the data to centralized servers or cloud-based
application back-ends for processing the data, or
• Perform some tasks locally and other tasks within the IoT
infrastructure, based on temporal and space constraints
Basic building blocks of an IoT Device

 Sensing : Sensors can be either on-board the IoT device or


attached to the device.

 Actuation : IoT devices can have various types of actuators


attached that allow taking actions upon the physical entities
in the vicinity of the device.

 Communication: Communication modules are responsible


for sending collected data to other devices or cloud-based
servers/storage and receiving data from other devices and
commands from remote applications.

 Analysis & Processing Analysis and processing modules


are responsible for making sense of the collected data
Block diagram of an IoT
Device
Exemplary Device:
Raspberry Pi
 Raspberry Pi is a low-cost mini-computer with
the physical size of a credit card.

 Raspberry Pi runs various flavors of Linux and


can perform almost all tasks that a normal
desktop computer can do.

 Raspberry Pi also allows interfacing sensors


and actuators through the general purpose
I/O pins.

 Since Raspberry Pi runs Linux operating


system, it supports Python "out of the box".
Raspberry Pi
Key Components and Their
Functions
 GPIO Headers: General Purpose
Input/Output (GPIO) pins used for
connecting sensors, modules, and
other peripherals.

 RCA Video(Radio Corporation of


America): Composite video output for
connecting older display devices.

 Audio Jack: 3.5mm audio output for


speakers or headphones

 Status LEDs: Indicate power and


activity status of the board.

 USB 2.0 Ports: Used for connecting


peripherals like keyboards, mice, and
storage devices.
 Ethernet Port: For wired network
connectivity.

 CSI Connector (Camera Serial


Interface): Used to connect a Raspberry
Pi camera module.

 HDMI Port: Used to connect a monitor or


TV for display output.

 Micro USB Power: The power input for


the Raspberry Pi board.

 SD Card Slot: Holds the SD card, which


contains the Raspberry Pi OS.

 DSI Connector (Display Serial


Interface): Used for connecting an official
Raspberry Pi touchscreen display.
Linux on Raspberry Pi
 The Raspberry Pi runs Linux-based operating systems, making it a powerful and flexible
tool for projects like IoT, automation, and programming.

 Raspbian
 Raspbian Linux is a Debian Wheezy port optimized for Raspberry Pi.

 Arch
 Arch is an Arch Linux port for AMD devices.

 Pidora
 Pidora Linux is a Fedora Linux optimized for Raspberry Pi.

 RaspBMC
 RaspBMC is an XBMC media-center distribution for Raspberry Pi.

 OpenELEC
 OpenELEC is a fast and user-friendly XBMC media-center distribution.

 RISC OS
 RISC OS is a very fast and compact operating system.
 1. Raspbian (Now Raspberry Pi OS)
 Based on Debian Wheezy, optimized for Raspberry Pi.
 Comes with pre-installed software for programming and general use.
 Recommended for beginners and general Raspberry Pi users.

 2. Arch
 A port of Arch Linux for ARM(Advanced RISC Machine)-based
devices like Raspberry Pi.
 Minimalist and lightweight but requires manual setup.
 Best for advanced users who want full control over their OS.

 3. Pidora
 A Fedora Linux version optimized for Raspberry Pi.
 Offers a user-friendly interface with Fedora’s package ecosystem.
 Discontinued but was once an alternative to Raspbian.

You might also like