Unit IV Raspberry Pi
Unit IV Raspberry Pi
IoT Communication Models and APIs – IoT Communication Protocols – Bluetooth – WiFi –
ZigBee – GPS – GSM modules – Open Platform (like Raspberry Pi) – Architecture –
Programming – Interfacing – Accessing GPIO Pins – Sending and Receiving Signals Using
GPIO Pins – Connecting to the Cloud.
"Internet of Things is the network of physical objects or ‘things’ embedded with electronics,
software, sensors and connectivity to enable it to achieve greater value and service by
exchanging data with the manufacturer, operator and/or other connected devices.
Each thing is uniquely identifiable through its embedded computing system but is able to
interoperate within the existing Internet infrastructure.
• An effective communication models shows how the process works and helps one
understand how communication can be done.
• Internet of things enables People and things (devices)to be connected wherever they
are, using any network or service they like.
• IoT allows to connect multiple devices (things) to communicate with each other.
There are four basic types of communication models as given are as follows
1.Request response Model
2.Publisher subscriber Model
3.Push pull model
4.Exclusive pair model
Request & Response Model:
• In the request Response Model client requests information from the server and waits
till the response is served from the server.
• HTTP protocol is used by request Response Model.
• For example, a browser client may request a web page from the server through a
request and the corresponding web page will be served by the server as a response.
• The client and the server can communicate one to one or one to many with more
requests.
• This model is stateless communication model and each Request Response pair is
independent of others.
The communication takes place between a client and a server.
• Whenever required, the client will request information from the server.
• This request is usually in the encoded format.
• So in this model, basically a client sends requests to the server and the server responds
to the requests.
• That is why it is called as Request-Response model.
• After receiving the request from the client, the server decides how to respond, fetches
the data from the database and its resource representation, prepares a response and
ultimately sends the response to the client.
Publish-Subscribe Model(Pub-Sub):
• In this model, you will find three main entities:-
• Publisher, Broker and Consumer.
• Let us see the roles of each of these 3 entities.
• Publishers, send the data to the topics that are managed by the broker.
• They are the source of data.
• The Man in the Middle, the Broker, has the responsibility to accept the data sent by the
publisher and deliver that data to the consumers.
• What is the task of the Consumers? Consumers will subscribe to the broker-managed
topics.
• Once the data is published on a topic, the broker sends this message to all consumers
who have subscribed to the specific topic.
• It works a bit like YouTube. When you subscribe to a channel and tap the Bell icon,
you'll get notifications if the YouTube channel posts a video.
Push-Pull Model:
The push-pull model constitutes data publishers, data consumers, and data queues.
Publishers and Consumers are not aware of each other.
Publishers publish the message/data and push it into the queue. The consumers, present on the
other side, pull the data out of the queue. Thus, the queue acts as the buffer for the message
when the difference occurs in the rate of push or pull of data on the side of a publisher and
consumer.
Queues help in decoupling the messaging between the producer and consumer. Queues also act
as a buffer which helps in situations where there is a mismatch between the rate at which the
producers push the data and consumers pull the data.
Exclusive Pair Model:
Exclusive Pair Model This communication model is full duplex, bi directional
communication model.
It uses persistent connection between client and server.
Client sends request to server for opening the connection.
This connection is open till the client sends a request for closing the connection.
It's a bi-directional, full duplex communication model in which a dedicated
communication link is set between the client and the server.
The connection remains open until the client sends a request to close the connection.
The client and server can send messages to one another after configuring the
connection.
As soon as the connection is terminated, no exchange of messages would take place
between the client and the server.
*************
IoT Communication APIs :
• An API is an interface used by programs to access an application. It enables a program
to send commands to another program and receive replies from the app.
• IoT APIs are the interface points between an IoT device and the Internet and/or
other network components.
• Here we will talk about the REST-based API and the Websocket based API.
The Internet of Things (IoT) has changed how we interact with devices and created new
opportunities for businesses to gather and analyze data. However, this has created a demand for
device−to−device communication that is seamless. This is where IoT Communications APIs
come to play a major role in communication systems.
IoT Communications APIs are a set of protocols and interfaces that allow IoT devices and
platforms to communicate with one another.
Internet of Things Communications APIs standardizes how IoT devices communicate with one
another and with other systems. They enable developers to design applications and services that
can access and operate IoT devices without worrying about underlying communication protocols
and interfaces.
An API is an interface used by programs to access an application. It enables a program
to send commands to another program and receive replies from the app.
IoT APIs are the interface points between an IoT device and the Internet and/or other
network components.
Here we will talk about the REST-based API and the Websocket based API.
4.MQTT:
• MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol
used in IoT Communications APIs to link devices to the cloud. MQTT was created to
be efficient, dependable, and simple to use, making it an excellent choice for IoT
applications.
• MQTT is a publish or subscribe messaging model that allows devices to submit
messages to particular topics and receive messages from topics to which they are
subscribed.
******************
BLUETOOTH:
Bluetooth is a wireless LAN technology designed to connect devices of different functions
such as telephones, notebooks, computers (desktop and laptop), cameras, printers, coffee
makers, and so on.
• A Bluetooth LAN is an ad hoc network, which means that the network is
formed spontaneously.
• Bluetooth was originally started as a project by Ericsson Company.
• It is named for Harald Blaatand, the king of Denmark (940-981) who united
Denmark and Norway.
• Blaatand translates to Bluetooth in English.
• Today, Bluetooth technology is the implementation of a protocol defined by the
IEEE
802.15 standard.
• The standard defines a wireless personal-area network (PAN) operable in an
area the size of a room or a hall.
General Characteristics
Universal wireless interface
Ad-hoc networking architecture
80 MHz in unlicenced ISM band at 2.45 GHz
Short range (10 m), low power consumption
Gross bitrate 1 Mbps
Simultaneous voice and high speed data support
Evolves from cable replacement -> networking solution
Architecture
• Bluetooth defines two types of networks:
• Piconet
• Scatternet.
Piconets
• A Bluetooth network is called a piconet, or a small net.
• A piconet can have up to eight stations, one of which is called the primary; the
rest are called secondaries.
• All the secondary stations synchronize their clocks and hopping sequence with
the primary.
• Note that a piconet can have only one primary station.
• The communication between the primary and the secondary can be one-to-one or
one-to-many.
• Although a piconet can have a maximum of seven secondaries, an additional
eight secondaries can be in the parked state.
• A secondary in a parked state is synchronized with the primary, but cannot take
part in communication until it is moved from the parked state.
• Because only eight stations can be active in a piconet, activating a station from
the parked state means that an active station must go to the parked state
Scatternet
• Combined form of piconet->scatternet.
• A secondary station in one piconet can be the primary in another piconet.
• This station can receive messages from the primary in the first piconet (as a
secondary) and, acting as a primary, deliver them to secondaries in the second
piconet.
• A station can be a member of two piconets
Bluetooth Devices
• A Bluetooth device has a built-in short-range radio transmitter.
• The current data rate is 1 Mbps with a 2.4-GHz bandwidth.
• This means that there is a possibility of interference between the IEEE 802.11b
wireless LANs and Bluetooth LANs.
Bluetooth Layers
Bluetooth uses several layers that do not exactly match those of the Internet model
Radio Layer
• The radio layer is roughly equivalent to the physical layer of the Internet model.
• Bluetooth devices are low-power and have a range of 10 m.
• Bluetooth devices will be integrated into typical mobile devices and rely on battery
• Tpohw
isere.quires small, low power chips which can be built into handheld devices.
• The combined use for data and voice transmission has to be reflected in the
design, i.e., Bluetooth has to support multi-media data.
Band
• Bluetooth uses a 2.4-GHz ISM band divided into 79 channels of 1 MHz each.
FHSS
• Bluetooth uses the frequency-hopping spread spectrum (FHSS) method in the
physical layer to avoid interference from other devices or other networks.
• Bluetooth hops 1600 times per second, which means that each device changes its
modulation frequency 1600 times per second.
• A device uses a frequency for only 625 µs (1/1600 s) before it hops to another
frequency; the dwell time is 625 µs.
Modulation
• To transform bits to a signal, Bluetooth uses a sophisticated version of FSK,
called GFSK (FSK with Gaussian bandwidth filtering;
• GFSK has a carrier frequency. Bit 1 is represented by a frequency deviation
above the carrier; bit 0 is represented by a frequency deviation below the carrier.
• The frequencies, in megahertz, are defined according to the following formula for
each channel:
• For example, the first channel uses carrier frequency 2402 MHz (2.402 GHz),
and the second channel uses carrier frequency 2403 MHz (2.403 GHz).
Baseband Layer
• The baseband layer is roughly equivalent to the MAC sublayer in LANs.
• The access method is TDMA .
• The primary and secondary communicate with each other using time slots.
• The length of a time slot is exactly the same as the dwell time, 625 µs.
• This means that during the time that one frequency is used, a sender sends a
frame to a secondary, or a secondary sends a frame to the primary.
• Note that the communication is only between the primary and a secondary;
• secondaries cannot communicate directly with one another.
**********
WiFi :
WiFi stands for Wireless Fidelity. It is based on the IEEE 802.11 family of standards and is
primarily a local area networking (LAN) technology designed to provide in- building
broadband coverage.
Current WiFi systems support a peak physical-layer data rate of 54 Mbps and typically
provide indoor coverage over a distance of 100 feet.
WiFi has become the de facto standard for last mile broadband connectivity in homes, offices,
and public hotspot locations. Systems can typically provide a coverage range of only about
1,000 feet from the access point.
A wireless network uses radio waves, just like cell phones, televisions and radiosdo. In
fact, communication across a wireless network is a lot like two-way radio communication.
Here's what happens:
1. A computer's wireless adapter translates data into a radio signal and transmits it using
an antenna.
2. A wireless router receives the signal and decodes it. The router sends the information
to the Internet using a physical, wired Ethernet connection.
The process also works in reverse, with the router receiving information from the Internet,
translating it into a radio signal and sending it to the computer's wireless adapter.
The radios used for WiFi communication are very similar to the radios used for walkie-
talkies, cell phones and other devices. They can transmit and receive radio waves, and they can
convert 1s and 0s into radio waves and convert the radio waves back into 1s and 0s.
But WiFi radios have a few notable differences from other radios:
• MAC Layer: This layer is responsible for reliable transmission of data by accessing
different networks with the carrier sense multiple access collision avoidance (CSMA).
This also transmits the beacon frames for synchronizing communication.
• Application Support Sub-Layer: This layer enables the services necessary for Zigbee
device object and application objects to interface with the network layers for data
managing services. This layer is responsible for matching two devices according to their
services and need.
1.Star Topology
In a star topology, the network consists of one coordinator which is responsible for
initiating and managing the devices over the network. All other devices are called end
devices that directly communicate with coordinator. This is used in industries where all the
end point devices are needed to communicate with the central controller, and this topology is
simple and easy to deploy.
2. Mesh Topology ;
In mesh, the Zigbee network is extended with several routers where coordinator is responsible
for staring them. These structures allow any device to communicate with any other adjacent node
for providing redundancy to the data. If any node fails, the information is routed automatically to
other device by these topologies. As the redundancy is the main factor in industries, hence mesh
topology is mostly used.
3.Cluster-tree Network :
• In a cluster-tree network, each cluster consists of a coordinator with leaf nodes, and these
coordinators are connected to parent coordinator which initiates the entire network.
Applications:
• The ZigBee Alliance targets applications Across consumer, commercial, industrial and
government markets worldwide
• Home networking
• Industrial control and management
GSM (Global System for Mobile communications) :
GSM (Global System for Mobile communications or Groupe Speciale Mobile)
communications, initiated by the European Commission, is the second-generation mobile
cellular system aimed at developing a Europe-wide digital cellular system.
Base station controller (BSC): The main function of the BSC is to look over a certain number
of BTSs to ensure proper operation. It takes care of handoff from one BTS to the other,
maintains appropriate power levels of the signal, and administers frequency among BTSs.
Mobile switching center (MSC): The MSC basically performs the switching functions of the
system by controlling calls to and from other telephone and data systems. It also does functions
such as network interfacing and common channel signaling. If the MSC has an interface to the
PSTN, then it is called a gateway MSC. GSM uses two important databases called HLR and
VLR, to keep track of the current location of a MS.
Authentication center (AUC): AUC unit provides authentication and encryption parameters
that verify the user’s identity and ensure the confidentiality of each call. The AUC protects
network operators from different types of frauds and spoofing found in today’s cellular world.
Equipment identity register (EIR): EIR is a database that contains information about the
identity of mobile equipment
Home Location Register (HLR):
The HLR is a central database that stores subscriber information for users registered to
a particular mobile network operator.
It contains permanent subscriber data such as the subscriber's service profile, current
location area, supplementary services, and authentication information.
When a subscriber initiates a call or sends a message, the network consults the HLR to
retrieve necessary subscriber information for call setup or message delivery.
The HLR also handles subscriber authentication and authorization processes.
Visitor Location Register (VLR):
The VLR is a temporary database that stores information about subscribers who are
currently located in a particular area served by a Mobile Switching Center (MSC).
When a mobile subscriber roams into a new location area, the VLR associated with the
serving MSC of that area temporarily stores subscriber information from the HLR.
The VLR allows the network to quickly access subscriber data without needing to
repeatedly query the HLR for each call or message.
It plays a key role in call routing by providing the necessary subscriber information to
the MSC for call setup and management.
Frequency Bands and Channels :
GSM has been allocated an operational frequency from 890MHz to 960 MHz.
To reduce possible interference, the MS and the BS use different frequency ranges (i.e., MSs
employ 890MHz to 915MHz and BS operates in 935MHz to 960 MHz).
Frames in GSM :
The GSM system uses the TDMA scheme shown in Figure. With a 4.615 ms– long frame,
divided into eight time slots each of 0.557 ms. Each frame measured in terms of time is 156.25
bits long, of which 8.25 period bits are guard bits for protection.
Raspberry Pi :
Raspberry Pi is a small single-board computer (SBC). It is a credit card-sized computer that
can be plugged into a monitor. It acts as a minicomputer by connecting the keyboard, mouse,
and display. Raspberry Pi has an ARM processor and 512MB of RAM. The architecture of
Raspberry Pi is discussed in this article.
Voltages:
Two 5V pins and two 3.3V pins are present on the board, as well as a number of ground pins
(GND), which can not be reconfigured. The remaining pins are all general-purpose 3.3V pins,
meaning outputs are set to 3.3V and inputs are 3.3V-tolerant.
Outputs:
A GPIO pin designated as an output pin can be set to high (3.3V) or low (0V).
Inputs :
A GPIO pin designated as an input pin can be read as high (3.3V) or low (0V). This is made
easier with the use of internal pull-up or pull-down resistors. Pins GPIO2 and GPIO3 have fixed
pull-up resistors, but for other pins this can be configured in software.
Other functions :
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):
Pulse width modulation (PWM) is a modulation technique that generates variable-width
pulses to represent the amplitude of an analog input signal.
Software PWM available on all pins
Hardware PWM available on GPIO12, GPIO13, GPIO18, GPIO19
SPI:
A serial peripheral interface (SPI) is an interface commonly used in computers and
embedded systems to facilitate short-distance communication between a microcontroller
and one or more peripheral integrated circuits (ICs). Serial Peripheral Interface (SPI) is a full-
duplex serial protocol for communicating with high-speed peripherals.
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 :
I2C stands for Inter-Integrated Circuit. It is a bus interface connection protocol incorporated into
devices for serial communication. I2C stands for Inter-Integrated Circuit. It is a bus interface
connection protocol incorporated into devices for serial communication.
Data: (GPIO2); Clock (GPIO3)
EEPROM :
Data: (GPIO0); EEPROM Clock (GPIO1)
Serial:
TX (GPIO14); RX (GPIO15)
GPIO pinout :
A GPIO reference can be accessed on your Raspberry Pi by opening a terminal window and
running the command pinout. This tool is provided by the GPIO Zero Python library, which is
installed by default in Raspberry Pi OS.
Serial interface:
The serial interface on Raspberry Pi has received (Rx) and transmit (Tx) pins for
communication with serial peripherals.
Raspberry Pi has multiple serial ports. The first two are called primary and secondary. By
default, primary appears on GPIO 14 (transmit) and 15 (receive) of the J8. Secondary is used by
bluetooth, on Raspberry Pi that support bluetooth. Each port is driven by a UART. On every
Raspberry Pi there are at least two of them: UART0 and UART1. The assignment of which
UART drives which port depends on the model and its configuration.
***********
Interfacing:
Introduction to UART:
UART (Universal Asynchronous Receiver/Transmitter) is a serial communication
protocol in which data is transferred serially i.e. bit by bit. Asynchronous serial communication
is widely used for byte oriented transmission. In Asynchronous serial communication, a byte of
data is transferred at a time.
UART serial communication protocol uses a defined frame structure for their data bytes. Frame
structure in Asynchronous communication consists:
• START bit: It is a bit with which indicates that serial communication has started and it is
always low.
• Data bits packet: Data bits can be packets of 5 to 9 bits. Normally we use 8-bit data
packet, which is always sent after the START bit.
• STOP bit: This usually is one or two bits in length. It is sent after data bits packet to
indicate the end of frame. Stop bit is always logic high.
MOSI (Master Out Slave In): Pin 19 of the GPIO connector strip
MISO (Master In Slave Out): Pin 21 of the GPIO connector strip
SCLK (Serial Clock): Pin 23 of the GPIO connector strip
CS / CE (Chip Select or Chip Enable): Pins 24 and 26 or any free one
The output of the GPIO connector strip
MOSI and MISO:
The MOSI and MISO lines are the data lines where MOSI is responsible for transmitting
data from the master to the slave and MISO is responsible to transmit data from the slave
to the master.
When the slave needs to send a response back to the master, the master will first generate
a prearranged number of clock cycles first before the slave transmits data on the MISO
line.
Compared to asynchronous serial where data is sent in either direction at any amount, the
master knows how much data is being returned and when in SPI.
SCLK:
SCLK which is the clock signal is generated by the master to the slave to signal it to look
at the data line to read data bits incoming.
In SPI interfaces, there is only one master who is in charge of generating the clock signal
to one or more slaves.
SS / CSS:
SS/CSS has the same functionality as chip select which is used as an addressing function. This
signal is normally an active-low signal which is then pulled high to disconnect a slave from the
SPI bus.
SPI Data Transmission:
An SPI communication will typically start with:
Master sending a clock signal and selecting a slave through the SS line.
Master will send a logic 0 signal to select a slave as this line is on an active low signal.
If a waiting period is required (eg. Analog to digital conversion) the master will be
required to wait for a period of time before being able to send the clock signal.
SPI is a full-duplex interface so both master and slave can send data to each other at the
same time via the MOSI and MISO lines respectively and data are being simultaneously
transmitted between the master and slave. This will be synchronized by the serial clock.
For example, these two operations will happen at the same time:
Master sends data one bit at a time on the MOSI line = Slave Reads it
(If a response is needed) Slave returns the data one bit at a time to the master on
the MISO line = Master reads it
SPI provides you with the flexibility to select the rising or falling edge of the clock to
sample or shift the data.
For the number of data bits to be transmitted, please do refer to your device datasheet as
every device differs.
• I2C bus/interface is used to communicate with the external devices like RTC, MPU6050,
Magnetometer, etc with only 2 lines. We can connect more devices using I2C interface if
their addresses are different.
• To access I2C bus in Raspberry Pi, we should make some extra configuration. Raspberry
Pi has I2C pins which are GPIO 2 and GPIO 3 (Refer Raspberry Pi Pin diagram)
• SDA
– This signal is known as Serial Data. Any data sent from one device to another
goes on this line
• SCL
– This is the Serial Clock signal. It is generated by the master device and controls
when data is sent and when it is read.
I2C Bus operation:
• SDA line Transmits/Receives data bits (MSB is sent first)
• Data in SDA line is stable during clock (SCL) high
• Serial clock is driven by the master
• Acknowledgment bit is driven by the receiver after the end of reception
• If the receiver does not acknowledge, SDA line remains high
**********
myAPI = 'IUKV2ZRBQW9MV407Q'
ThingsURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
def DHT22_data():
humidity, temperature = dht.read_retry(dht.DHT22, 23)
return humi, temp