Raspberry Pi IoT Integration Guide
Raspberry Pi IoT Integration Guide
RAM
ETHERNET USB
Comparison of various models:
About the Board Status LEDs
Programming
Default installed :
• Python
• C
• C++
• Java
• Scratch
• Ruby
Note : Any language that will compile for ARMv6 can be used with raspberry pi.
Linux on Raspberry Pi
NOOBS (or “New Out Of the Box Software”) is a unique installation image. With
NOOBS loaded on an SD Card you can install a wonderful range of operating systems for
your Raspberry Pi.
• Raspbian- Raspbian Linux is a Debian Wheezy port optimized for Raspberry Pi.
• Arch- Arch is an Arch Linux port for AMD (Advance micro devices) 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.
Raspbian Linux Desktop
Raspberry Pi GPIO
Act as both digital output and digital input.
2. SPI:
Serial Peripheral Interface (SPI) is a synchronous serial data protocol used for communicating with one or
more peripheral devices.
In an SPI connection, there is one MASTER device and one or more Peripheral devices. There are 5 pins in
raspberry pi for SPI interface:
• MISO- Master In Slave Out : Master line for sending data to the peripherals.
• MOSI- Master Out Slave In : Slave line for sending data to the master.
• SCK (Serial Clock) : Clock generated by master to synchronize data transmission
• CE0 (Chip Enable 0)
• CE1 (Chip Enable 1)
3. I2C:
The I2C (Inter-IC) interface pins on Raspberry Pi allows to connect hardware modules. I2C interface allows
synchronous data transfer with just two pins - SDA (data line) and SCL (clock line).
As well as simple input and output devices, the GPIO pins can be used with a variety of
alternative functions, some are available on all pins, others on specific pins:
• PWM (pulse-width modulation)
– Software PWM available on all pins
– Hardware PWM available on GPIO12, GPIO13, GPIO18, GPIO19
• SPI
– SPI0: MOSI (GPIO10); MISO (GPIO9); SCLK (GPIO11); CE0 (GPIO8), CE1 (GPIO7)
– SPI1: MOSI (GPIO20); MISO (GPIO19); SCLK (GPIO21); CE0 (GPIO18); CE1 (GPIO17);
CE2 (GPIO16)
• I2C
– Data: (GPIO2); Clock (GPIO3)
– EEPROM Data: (GPIO0); EEPROM Clock (GPIO1)
• Serial
– TX (GPIO14); RX (GPIO15)
Fully hardware PWM
• This type of PWM is generated by the Pi's PWM peripheral.
• The timing of the pulses is controlled by the PWM peripheral.
• It is the most accurate and the most flexible.
• It can be generated on GPIO 12/13/18/19. However there are only two channels, so only two different
PWM streams can be generated at a time. GPIO 12/18 are on one channel, GPIO 13/19 on the other.
• Suitable for jitter free servos, glitch free LED brightness control, motor speed control.
• SPI is a common communication protocol used by many different devices. For example, SD card
modules, RFID card reader modules, and 2.4 GHz wireless transmitter/receivers all use SPI to
communicate with microcontrollers.
3. The master sends the data one bit at a time to 4. If a response is needed, the slave returns
the slave along the MOSI line. The slave reads the data one bit at a time to the master
bits as they are received: along the MISO line. The master reads the
bits as they are received:
Advantages and Disadvantages of SPI Protocol
ADVANTAGES
• No start and stop bits, so the data can be streamed continuously without
interruption
• No complicated slave addressing system like I2C
• Higher data transfer rate than I2C (almost twice as fast)
• Separate MISO and MOSI lines, so data can be sent and received at the same
time.
DISADVANTAGES
• Uses four wires (I2C and UARTs use two)
• No acknowledgement that the data has been successfully received (I2C has this)
• No form of error checking like the parity bit in UART
• Only allows for a single master
I2C (Inter IC Interface)
• With I2C, you can connect multiple slaves to a single master (like SPI) and you can have
multiple masters controlling single, or multiple slaves.
• This is really useful when you want to have more than one microcontroller logging data to a
single memory card or displaying text to a single LCD.
• I²C is a multi-master protocol that uses 2 signal lines.
– The two I²C signals are called ‘serial data’ (SDA) and ‘serial clock’ (SCL). There is no need
of chip select (slave select) or arbitration logic.
– Virtually any number of slaves and any number of masters can be connected onto these
2 signal lines and communicate between each other using a protocol that defines:
• 7-bits slave addresses: each device connected to the bus has got such a unique
address.
• Data divided into 8-bit bytes.
• A few control bits for controlling the communication start, end, direction and for an
acknowledgment mechanism.
• The data rate has to be chosen between 100 kbps, 400 kbps and 3.4 Mbps, respectively called
standard mode, fast mode and high speed mode. Some I²C variants include 10 kbps (low speed mode)
and 1 Mbps (fast mode +) as valid speeds.
• Physically, the I²C bus consists of the 2 active wires SDA and SCL and a ground connection. The active
wires are both bi-directional.
• The I2C protocol specification states that the IC that initiates a data transfer on the bus is considered
the Bus Master. Consequently, at that time, all the other ICs are regarded to be Bus Slaves.
HOW I2C WORKS
1. First, the master will issue a START condition. This acts as an ‘Attention’ signal to all of the connected devices. All
ICs on the bus will listen to the bus for incoming data.
2. Then the master sends the ADDRESS of the device it wants to access, along with an indication whether the
access is a Read or Write operation (Write in our example).
3. Having received the address, all IC’s will compare it with their own address. If it doesn’t match, they simply wait
until the bus is released by the stop condition. If the address matches, however, the chip will produce a
response called the ACKNOWLEDGE signal.
4. Once the master receives the acknowledge, it can start transmitting or receiving DATA.
5. When all is done, the master will issue the STOP condition. This is a signal that states the bus has been released
and that the connected ICs may expect another transmission to start any moment.
6. When a master wants to receive data from a slave, it proceeds the same way, but sets the RD/nWR bit at a
logical one. Once the slave has acknowledged the address, it starts sending the requested data, byte by byte.
After each data byte, it is up to the master to acknowledge the received data.
ADVANTAGES AND DISADVANTAGES OF I2C
ADVANTAGES
• Only uses two wires
• Supports multiple masters and multiple slaves
• ACK/NACK bit gives confirmation that each frame is transferred successfully
• Hardware is less complicated than with UARTs
DISADVANTAGES
• Slower data transfer rate than SPI
• The size of the data frame is limited to 8 bits
• More complicated hardware needed to implement than SPI
WAMP - AutoBahn for IoT
WAMP (Web Application Messaging Protocol)
➢ WAMP (Web Application Messaging Protocol) is a sub-protocol of
WebSocket which provides the messaging patterns:
• Publish-Subscribe, and
• Remote Procedure call (RPC)
Components Description
Activities They dictate the UI and handle the user interaction to the smart
phone screen
Services They handle background processing associated with an
application.
Broadcast Receivers They handle communication between Android OS and
applications.
Content Providers They handle data and database management issues.
Feature of WAMP protocol- Loosely coupled:
WAMP provides unified Application Routing for application communication:
• routing of events in the Publish & Subscriber pattern and
• routing of calls in the Remote Procedure Call pattern
between applications components in one protocol.
Strong coupled:
Lets take the old “client-server” model. In the client-server model, a remote procedure call goes
directly from the Caller to the Callee:
Caller Callee
• A Router is capable of routing both calls and events, and hence can support flexible,
decoupled architectures that use both RPC and PubSub.
Example:
WAMP Router
Imagine to have a small embedded
device like an Arduino uno with Dealer Broker
sensors (like a temperature sensor) and
actuators (like a light or motor)
connected. 1 2
We want to integrate the device into
an overall system with
• user facing frontend to control the UI or UI or
Actuator Sensor
actuators, and Backend Backend
• continuously process sensor values in a
Caller Callee Publisher Subscriber
backend component.
Embedded Device
Using WAMP, we can have:
WAMP in an IoT application
a) a browser-based UI,
b) the embedded device and Fig. Switching on a light on the device from the browser-based UI is
c) our backend talk to each other in real- naturally done by calling a remote procedure on the device (1) and the
time sensor values generated by the device continuously are naturally
transmitted to the backend component (and possibly others) via publish
& subscribe (2).
WAMP - Concepts
• Transport: Transport is channel that connects two peers.
• Session: Session is a conversation between two peers that runs over a transport.
• Client: Clients are peers that can have one or more roles. In publish-subscribe model client can have
following roles:
▪ Publisher: Publisher publishes events (including payload) to the topic maintained by the Broker.
▪ Subscriber: Subscriber subscribes to the topics and receives the events including the payload.
In rRPC model client can have following roles:
▪ Caller: Caller issues calls to the remote procedures along with call arguments.
▪ Callee: Callee executes the procedures to which the calls are issued by the caller and returns the
results back to the caller.
• Router: Routers are peers that perform generic call and event routing.
In publish-subscribe model Router has the role of a Broker:
▪ Broker: Broker acts as a router and routes messages published to a topic to all subscribers subscribed
to the topic.
In RPC model Router has the role of a Dealer:
▪ Dealer: Dealer acts a router and routes RPC calls from the Caller to the Callee and routes results from
Callee to Caller.
• Application Code: Application code runs on the Clients (Publisher, Subscriber, Callee or Caller).
WAMP Session between Client and Router
Client Router
Roles Roles
Publisher
Transport Dealer
Subscriber
Session
Caller
Broker
Callee
WAMP WebSocket Protocol
Publish-Subscribe messaging using WAMP
Desired features
▪ Easily connect devices to the cloud
▪ Store/retrieve sensor data in the cloud
▪ Make decisions in the cloud and analyze data
▪ Visualize/share data
▪ Actuate from the cloud
Cloud Services
SaaS PaaS IaaS
Provid Complete software Ability to develop Ability to provision
es the application or UI to and deploy computing and
user application itself. application in the storage resources.
cloud using tools, These resources are
API, software provided to user in
libraries and the form of virtual
services provided machine instances
by CSP. and virtual storage.
Cloud Underlying cloud Underlying cloud Underlying cloud
service infrastructure infrastructure infrastructure
provid including servers, including servers,
er networks, OS, networks, OS and
(CSP) storage and storage.
manag application
es software.
Cloud Platforms for IoT
• Microsoft Azure IoT Suite
• Google Cloud's IoT Platform
• IBM Watson IoT Platform
• AWS IoT Platform
• Cisco IoT Cloud Connect
• Salesforce IoT Cloud
• Kaa IoT Platform
• Thingspeak IoT Platform
• Xively Cloud
Xively Cloud for IoT
• Created in 2007, acquired in 2011 by LogMeln
• Recently acquired by Google.
• Xively is a commercial Platform-as-a-Service which provides tools and
services for developing IoT solutions.
• IoT developers can focus on front-end infrastructures and devices for IoT
that generates the data, while the backend data collection infrastructure is
managed by Xively.
Triggers
1. For each channel, one or more Triggers can be created.
2. A trigger specification includes:
a) a channel to which the trigger corresponds,
b) Trigger condition and
c) an HTTP POST URL.
3. Triggers are used for integration with third party applications.
Example: Post a tweet when temperature is greater than 30°. Minimum interval
between two posts: 5 seconds
Creating Feeds and Channels
• Feeds and channels can be created directly from the web
platform
• Alternatively, they can be created with an HTTP POST request
– Headers and body of the HTTP requests specify information on the
feed to be created
Updating channels
• Similarly, channels may be updated with fresh data in
different ways:
– Directly, with HTTP requests
– Indirectly, with libraries
Device
Product Template Metadata
Device
Device
Device
Basic operations
• Xively libraries supports RESTful APIs, Sockets and MQTT
protocols for connecting IoT devices to Xively cloud.
• Xively API enables to create/modify/retrieve feeds and
datastreams.
• All operations are done via RESTful HTTP commands
– All resources are uniquely addressable through URL
– All resources can be manipulated (create, read, update, delete) via
HTTP requests (POST, GET, PUT, DELETE)
– The data for/from resources is transferred through well-known
representations (HTML, JSON)
• Create/delete/update a Feed
• Create/delete/update a Channel
• List/view a Feed
• Operations may be performed either with HTTP requests or
by using the provided libraries.
Django Framework
A High Level Python Application
Framework
Django Framework:
• Django Framework is an open source web application framework for developing Web
Applications in Python.
• Framework in general is a collection of solutions, packages and best practices that allows
development of web apps and dynamic websites.
• It provides generic things that almost all applications need.
It's like building a house. First you put up a wooden frame that outlines the structure of the
house. That's the framework. Then you build the house on the framework: you add walls,
windows, doors, etc.
Why Django?
• With Django, one can take Web applications from concept to launch in a matter of hours.
• Django takes care of much of the hassle of Web development, so that one can focus on
writing the app without needing to reinvent the wheel.
• It’s free and open source.
Features of Django:
• Ridiculously fast.
Django was designed to help developers take applications from concept to completion as quickly
as possible.
• Fully loaded.
Django handles all the common Web development tasks. Django takes care of user authentication,
content administration, site maps, and many more tasks — right out of the box.
• Reassuringly secure.
Django takes security seriously and helps developers avoid many common security mistakes, such
as SQL injection, cross-site scripting, cross-site request forgery and click-jacking. Its user
authentication system provides a secure way to manage user accounts and passwords.
• Exceedingly scalable.
Some of the busiest sites on the planet use Django’s ability to quickly and flexibly scale to meet
the heaviest traffic demands.
• Incredibly versatile.
Companies, organizations and governments have used Django to build all sorts of things — from
content management systems to social networks to scientific computing platforms.
• Frontend web designing:
– e.g. HTML, CSS (Cascading Style Sheets), Javascript
• Backend development:
– e.g. Servlet, Javascript, PHP (Personal Home Page), ASP (Active Server
Page), Python
Django
MVT
2. Template:
✓ Template is simply an HTML Page.
✓ Templates are used to specify a structure for an output. Data can be populated in a
template using placeholders. It defines how the data is presented.
✓ An example is a Generic list view that we can use to display a set of records from the
database.
3. View:
✓ View ties the model to the template.
✓ The view is where you write the code that actually generates the web pages.
✓ View determines what data is to be displayed, retrieves the data from the database
and passes the data to the template.
MVC- Model- View- Controller
Model View Controller, known as MVC separates the code as three components. MVC
separates the logic and presentation layer from each other.
MVC architecture has been used for designing web applications as well as mobile apps.
1. Model – This layer deals with data-related logic. For example, it can retrieve, change
and save data to the database.
2. View – It can be called as the presentation layer. It is responsible for collecting data from
the model or user and presenting it. In a web application, everything that is displayed in
the browser falls under View.
3. Controller – It controls the data flow and interaction between view and model. For
example, a controller, based on a request or action, will collect data from a database
with the help of Model and send it to the user through Views.
Difference between MVC and MVT
1. The main difference between MVC and MVT is that in a Model View Controller
pattern, we have to write all the control specific code. But in an MVT, the
controller part is taken care of by the framework itself.
2. The classic MVC pattern works by managing the state of an application. When a
user performs an action or makes a request, an action in the Controller is called.
The Controller then either tells Model to make changes and update the View or
returns a View based on a Model. Hence we can say that the View is controlled
by the Controller and Model.
However, MVT takes a slightly different approach. When a user makes an HTTP
request, the corresponding view performs a query on the Model and collects
the result set from the Model. The View then fills the result in a template and
sends it to the user.
3. Unlike in MVC, view is not coupled with a Model. This makes MVT loosely
coupled and easy-to-modify.
MVT Pattern: Model-View-Template
MVT Pattern:
When a user requests for a resource, Django acts as a controller and checks if it is available. If
the URL maps, the View interacts with the Model and renders a Template. Django sends back
a Template to the user as a response.
Install Django
/> py -m pip install Django
/> py -m pip install Django==3.1.3
Creating a Project
polls/views.py
To call the view, we need to map it to a URL - and for this we need a URLconf.
To create a URLconf in the polls directory, create a file called urls.py.
mysite/urls.py
urlpatterns = [
path('polls/', include('polls.urls')), path('admin/',
admin.site.urls),
]
The include() function allows referencing other URLconfs. Whenever Django encounters include(),
it chops off whatever part of the URL matched up to that point and sends the remaining string to
the included URLconf for further processing.
http://localhost:8000/polls/