SmartDevices Chapitres1 2 3
SmartDevices Chapitres1 2 3
Emir DAMERGI
INSAT 2021/22
2
DAMERGI Emir – INSAT 2021
1
Smart Devices (Smart Embedded Devices)
The definition has evolved over Time. The definition proposed in [1] will be retained:
[1] Silverio-Fernández et al. « What is a smart device? - a conceptualisation within the paradigm of the internet of things
Visualization in Engineering (2018) - Springer
3
DAMERGI Emir – INSAT 2021
Smart Devices
Context-awareness [1]
The ability of a system or system component to gather information about its environment
at any given time and adapt behaviors accordingly.
Connectivity [1]
The ability to establish a connection to a network of any size; Sometimes the main
purpose might be gaining internet access, other times it might be sharing information with
other devices on the network. (Ethernet, Bluetooth, Zigbee, NFC, Wi-Fi, 5G, etc., )
[1] Silverio-Fernández et al. « What is a smart device? - a conceptualisation within the paradigm of the internet of things
Visualization in Engineering (2018) – Springer
4
DAMERGI Emir – INSAT 2021
2
Smart Devices
This definition complies with the main idea of the IoT which is that Any “thing” can be
part of the IoT.
5
DAMERGI Emir – INSAT 2021
Internet Access
The Edge
Gateway
3
Smart Devices
Every thing can become smart and part of the IoT if we add a sensor, a tiny bit of computing
and network connectivity
Smart chair
7
DAMERGI Emir – INSAT 2021
Smart Devices
Smart Shoes
Computing system
Pressure sensors
Accelerometer
Temperature sensor
Battery
BlueTooth Connectivity
8
DAMERGI Emir – INSAT 2021
4
Smart Devices
Sensors Computing
Wired or Wireless
(embedded) Network
Interface
Actuators System
Connectivity
Context Autonomous
Awarness computing
9
DAMERGI Emir – INSAT 2021
Serves as Interface to enable communication between the processing device and external peripherals
P.I *
(display, keyboards), sensors, actuators,
Periph examples: GPIO (Digital), Analog, Ethernet, USB , Serial, etc….
5
Smart Devices :
• Sensors Processing
• Actuators device D
• Display Devices Flash,
Eeprom RAM
BUS
(Code) (Variables)
P.I • Connectivity
P.I
B P.I
P.I
C
P.I
Computing System A
User application
6
Smart Devices :
B A A B
E
Chapter 2,3 and D (Arduino)
D
C
7
Smart Devices : Computing System
single platform and integrates an entire electronic or computer system onto it. It
is, exactly an entire system on a single chip. SoCs are generally designed for a
A microcontroller (µc, or MCU for microcontroller unit) is a small computer on a integrated circuit (IC)
chip. It contains one or more CPUs (processor cores) along with memory and programmable
input/output peripherals.
15
DAMERGI Emir – INSAT 2021
• A SoC, in addition to standard peripherals embedded in a MCU (Serial, SPI, I2C, USB, Timers,
etc..), typically contains a greater number of onboard advanced peripherals that make it specific to a
given applications Field. And it can be used with minimal number of additional external circuits.
Bluetooth connectivity
embedded
UART
SPI
I2C Bluetooth Sensors
Sensors
Module Actuators
Actuators
16
DAMERGI Emir – INSAT 2021
8
Smart Devices : Computing Systems – Socs/MCUs
NodeMCU (ESP8266)
NodeMCU (ESP32)
18
DAMERGI Emir – INSAT 2021
9
Smart Devices : Debug Access Port and In-System Programming (ISP)
IDE PC Target
Source Flash Mem
Code Processing (Code)
device
10
Smart Devices
Development Environment
22
11
Smart Devices : Programming Tools/IDE
Mbed Studio is a free IDE for Mbed OS application and library development (Win, MacOS, Linux).
Supports C and C++ programming Languages.
Mbed supports only MCUs and SoCs based on the Cortex Processors
24
DAMERGI Emir – INSAT 2021
12
Smart Devices : Arduino IDE
25
DAMERGI Emir – INSAT 2021
26
DAMERGI Emir – INSAT 2021
13
Chap 2
Smart Devices : Context Awareness
Emir DAMERGI
• Data Storage INSAT 2021/22
• Digital I/O
• Serial (UART) Communication
• Analog Data Acquisition (Analog Sensors)
• Digital Data Acquisition (Digital Sensors)
Smart Devices
Data Storage / Manipulation
14
Data Storage / Manipulation
In Smart Devices, persistent data storage is necessary to store
• Collected data
Variables are not suitable. They are stored in RAM and are lost when the system is powered off.
Difficulty: Code for Accessing Flash to store Data in file like structures.
Tools* for :
Libraries* (File System) to manipulate Files: read, update, write, create, etc…
*) They are not compatible. For each Board (SoC or MCU), suitable tools/Librairies must be used (if available)
29
DAMERGI Emir – INSAT 2021
30
DAMERGI Emir – INSAT 2021
15
Data Storage / Manipulation : File System for ESP in Arduino
Example 1: Read a txt file and send the Example 2: writes a string (TEST
content (character by char) to serial port SPIFFS) in a file
31
DAMERGI Emir – INSAT 2021
ParamName2 • Parameters that are not the Last node in Hierarchy are
ParamName2_1 objects.
ParamName2_2
ParamName2_2_1
ParamName2_2_2
ParamName3
Rq: There are other formats (expl: XML) but JSON is easier to
read, consumes less characters and easier to parse. 32
DAMERGI Emir – INSAT 2021
16
Data Storage / Manipulation: JSON Format
{ ‘’ParamName1’’ : Value1 ,
{ "firstName": "INSAT",
"lastName": "INSAT",
"age": 25,
},
34
DAMERGI Emir – INSAT 2021
17
Data Storage / Manipulation: JSON Manipulation In Arduino
JsonDoucment
firstName : INSAT
lastName : INSAT
age: 25
address:
streetAddress : Boulevard de la terre
city : Zone Urbaine Nord
postalCode": 1080
phoneNumbers: [ 71800800 , 71800801 ]
35
DAMERGI Emir – INSAT 2021
{
"city":"Tunis",
"weather":"Sunny",
"humidity":[80.5,27.75],
"temp": {
"max":49,
"min":28
}
}
36
DAMERGI Emir – INSAT 2021
18
Data Storage / Manipulation : JSON Deserialization
37
DAMERGI Emir – INSAT 2021
38
DAMERGI Emir – INSAT 2021
19
Data Storage / Manipulation
39
DAMERGI Emir – INSAT 2021
Smart Devices
Digital Input /output
20
Digital I/O: GPIOs
SoC
Flash RAM
Processor
GPIO
GPIOs
P.I pins
pins P.I
GPIO
P.I
Arduino Connector
When programming SoCs Input/output operations,
the used pin(s) must be identified.
For Arduino IDE, the connector pinout identifiers
can be used D0..D15 (Digital) and A0..A5 (Analog)
GPIO4
#define PushButton 4
42
DAMERGI Emir – INSAT 2021
21
Digital I/O: Arduino Programming
Arduino Connector
Example:
An application that continously :
Tests if the PushButton is pressed (Low level).
If yes : The Led is Turned ON
GPIO4
No: the Led is Turend OFF
43
DAMERGI Emir – INSAT 2021
GPIO4
Example:
An application that continously :
Tests if the PushButton is pressed
(Low level).
If yes : The Led is Turned ON
No: the Led is Turend OFF
44
DAMERGI Emir – INSAT 2021
22
Digital I/O: power Interface
~ mA
5V
SoC pins can only support low current (~30 mA max) and Voltage (5V).
45
DAMERGI Emir – INSAT 2021
Smart Devices
UART Communications
23
USART:
Synchronous
Asynchronous
Bluetooth
Zigbee
TX
Wifi
RX
GPS
PC
Terminal
TX USB/Serial Converter
serial USB
(CH340, PL2110, ST-
Link)
RX
24
UART: TX/RX Pins
GPIO
devices must go through GPIO Pins.
It is obviously, the case of UART
UART 1 peripheral. Each UART uses two GPIO
UART1_RX pins: one for sending (TX) and one for
UART 2
GPIO
UART2_TX receiveng serial data (RX) .
UART i UART2_RX
Generally, before configuring and using
UART peripherals, the correspending TX
and RX pins must be configured.
Generally, for each UART, more than one
In Android, this is done automatically
choice (2 or 3) are available for the TX
when invoking the UART to use
and RX pins. It depends on SoC
architecture.
while ( !Serial) {//wait for Serial port Initialization The default Serial object names in arduino are:
} Serial (the UART connected to the USB/Serial
} Converter)
Serial1 for the second UART, Serial2, etc…
50
DAMERGI Emir – INSAT 2021
25
UART Communications: Sending Data
51
DAMERGI Emir – INSAT 2021
char rc;
rc = Serial.read();
receivedChars[nbr] = rc;
nbr++;
}
52
DAMERGI Emir – INSAT 2021
26
UART Communications: GPS (NMEA Messages)
Vcc
TX
TX RX
GND
When activated and configured, he GPS module sends continually NMEA Messages on the TX Pin.
The NMEA Messages are received on the board Serial port and decoded.
53
DAMERGI Emir – INSAT 2021
When only 1 satellite is in View : only the date and Time can be decoded
1 satellite in View
Lattitude and Longitude positions are decoded when many Satellites are in View
A = Available
N: Lattitude
E: Longitude
11 satellite sin
View
54
DAMERGI Emir – INSAT 2021
27
UART Communications: Decoding GPS NMEA Messages with TinyGPSPlus
55
DAMERGI Emir – INSAT 2021
Vcc
TX
TX RX
GND
To configure and control communication Modules (GPRS, Zigbee, Wifi, etc... ) AT commands are used on Serial
communication: AT+….. (Some AT commands are standardized, others are proprietary)
28
Smart Devices
Analog Sensors
Sampling
Analog Digital
Conversion
Quantization
29
Analog to Digital Conversion
Processor
Analog Digital Temperature, Speed,
Binary Data Voltage Sensor
Digital Converter
Processing Analog Quantity Humidity, light
ADC
Sampled signal
Quantized signal
Vmax V111
max
Quantization Error (max = ΔQ/2)
110
101
ΔQ: Quantum
100
011
010
V001
min x
Vmin 000x
100 111 011 011 101 000
Sampling Period
time
Fourier Transform
frequency
Fmax
30
Conversion Result
2 = 00..010
1 = 00..001
0 = 00..000 Vin
Vref- Vref- Vref +
Vref- Vref+
- ΔQ Emir – INSAT 2021
+ΔQ +2*ΔQ DAMERGI 61
Conversion Result
Processor
Analog Digital Temperature, Speed,
Binary Data Voltage Sensor
Digital Converter
Processing Analog Quantity Humidity, light
Nin ADC Vin
G
Δy slope
Vmes
Δx = Δy/Δx
31
Analog I/O: Arduino programming
63
DAMERGI Emir – INSAT 2021
Vmoy = 0
Vmoy = 2,5 V
PWM Signal
T = Period
Vmoy = 3,75V
T=Ton + Toff
V
Vmoy = 5 V
Ton Toff
Vmoy = Ton/T * V
64
DAMERGI Emir – INSAT 2021
32
Analog I/O: Arduino programming (PWM - DC Motor Control)
DC* Motors are drived by PWM signals Motor Speed = fct (Vmoy) = fct (Value)
DC Moror : Bidirectionnal
DC Moror : One Direction
Digital out
PWM signal
Digital out
PWM signal
(Speed)
H-Bridge
+V
PWM
66
DAMERGI Emir – INSAT 2021
33
Smart Devices
Digital Sensors
Analog signals are much affected by external noise and create errors in the output signal.
Digital signals have higher noise Immunity. So, digital sensors are preferred over analog ones.
However, given that all measured quantities are analog, Analog to Digital Conversion is Inevitable. But it is
Registers
Digital Sensors contain some Registers used to store
ADC
• A/D Converter and Communication configuration
Analog
• Conversion Results (binary data)
Sensor
Digital Sensor
68
DAMERGI Emir – INSAT 2021
34
Digital Sensors: I2C/SPI communications
Generally, Digital Sensors support both SPI (Serial Peripheral Interface) and I2C (Inter-Integrated Circuit)
SoC
Registers
SPI SPI
or I2C or I2C
ADC
Analog
Sensor
The SoC, through Peripheral Interfaces (SPI/I12C),
Communication Analog/Digital
has access to the Digital Sensor Registers.
Interface Converter
Register(s).
69
DAMERGI Emir – INSAT 2021
Smart Devices
Digital Sensors : SPI
35
Serial Connectivity: SPI
SPI MASTER SPI SLAVE
SPC
SDI
SDO
/SS
36
Serial Connectivity: SPI
SDO /
time
Generally, the sent data are written to Reg @i, @i+1, @i+2, etc…
But the slave can be configured to write to the same Register @i
DAMERGI Emir – INSAT 2021 73
SDO / Data from slave …………… Data from slave Data from slave
(READ) (READ) (READ)
time
To read Data, The master must continue to send dummy data to activate the clock signal
Generally, the data are READ from Reg @i, @i+1, @i+2, etc…
But the slave can be configured to read the same Register @i
DAMERGI Emir – INSAT 2021 74
37
SPI: Arduino Programming - Configuration/Initilaization
Flash RAM Processing The Initialization of the SPI object is put in the setup
device
SPI1_SCK section (begin method).
GPIO
SPI1_MOSI
The Slave Select pin will be configured in OUTPUT
mode and set HIGH to disable SPI Slave device at
SPI 1
beginning
SPI1_MISO
SPI 2
GPIO
SS
Rq: Register adresses are generally <=63 (0111 1111), so that the MSB (bit 7) is 0 (Write operation)
DAMERGI Emir – INSAT 2021 76
38
SPI: Arduino Programming - Write to slave
digitalWrite(MySPI_SS, HIGH);
Rq: Generally, the Slave Device will increment the address after each byte reception. If the Regsiter
address is X, the sent values will be written to registers @X, @X+1, @X+2, ….@X+n.
Read 1 Byte:
digitalWrite(MySPI_SS, LOW);
Uint8_t Rcv_Data = SPI.transfer(0x00); //Send dummy Data to activate clk signal and read Data from Slave
digitalWrite(MySPI_SS, HIGH);
39
SPI: Arduino Programming - Read From slave
Read 2 Bytes:
digitalWrite(MySPI_SS, LOW);
digitalWrite(MySPI_SS, HIGH);
RQ: It is possible to read multiple bytes from successive Address registers @X, @X+1, @X+2, ….@X+n.
Smart Devices
Digital Sensors : I2C (Inter-Integrated Circuits)
40
Serial Connectivity: I2C
Invented
by philips
Serial Clock (SCL)
1982 Generated by Master
• A device can act a Master and Slave. But not at the same time.
• Max Number of Slaves is 128 (7 bits addressing) and 1024 (10 bits addressing)
41
I2C: Write to slave
1 1 1 1 0 A9 A8 R /W A7 A6 A5 A4 A3 A2 A1 A0
10 bit slave SLAVE Addr R/ SLAVE Addr
address S Part 1 W A Part 2
A
42
I2C: Read from slave (I2C Peripheral Interfaces)
Before reading, the Matser must start by sending (write) the register address
Restart Condition
Without Stop
1 Fixed 7 bits
Address:
2 configurable 7
1001 100 bits Addresses: 8 configurable 7
bits Addresses:
0011 1xx
43
I2C: Arduino Programming - Configuration/Initilaization
GPIO
passed as parameters.c
# 𝑖𝑛𝑐𝑙𝑢𝑑𝑒 𝒘𝒊𝒓𝒆. 𝒉
I2C 1
I2C1_SCL
I2C 0
GPIO I2C0_SDA
I2C0_SCL
Reg Value to be
S SLAVE Addr /W A
Addr
A
written
A P
I2C Master
I2C Slave (Digital Sensor)
SoC Value
@ Reg
I2C I2C
i
ADC
i+1
Analog
i+2
MySensors_I2C.beginTransmission (Slave_Addr); Sensor
…
MySensors_I2C.write(Reg_Addr);
I2C Interface Analog/Digital
MySensors_I2C.write(Value); 8 bits
Converter
MySensors_I2C. endTransmission(true);
44
I2C: Arduino Programming - Read From slave
MySensors_I2C.beginTransmission (Slave_Addr);
MySensors_I2C.write(Reg_Addr);
MySensors_I2C. endTransmission(false);
MySensors_I2C. endTransmission(true);
I2C/SPI Devices
Additinally to sensors, many other devices have I2C and SPI Interfaces:
Bluetooth BLE
GPS Module
Module 4G/LTE
LORA Module Module
45
Chap 3
Smart Devices : Connectivity and IoT
Emir DAMERGI
• IoT Context INSAT 2021/22
• Binary Stacks for wireless protocols
• Wifi connectivity
• HTTP and Web Browing
• MQTT
• Bluetooth Connectivity (BLE)
Smart Devices
Connectivity - IoT Context
46
IoT Applications
47
IoT Connectivity Technologies
Cellular Network
2G/3G/4G
10MBps
WIFI
1 MBps
Bluetooth BLE
Zigbee
100 KBps
Lora
Zwave
10 KBps
LPWAN: Low
Power Wide
Area Network
1 KBps
Sigfox
actuators Internet
sensors
IP connectivity
actuators
IoT Platform
Data Storage
Message
broker
End user
Application
Data Analysis
48
IoT Connectivity : Cellular Solution for LPWA
e: enhanced
NB: NarrowBand
1 MBps
Bluetooth BLE MTC
Zigbee NB
100 KBps
Lora
Zwave
10 KBps
LPWA: Low
Power Wide
Area Network
1 KBps
Sigfox
49
The IoT Architecture – LPWA with 5G
sensors
IP connectivity
actuators
IoT Platform
Data Storage
Message
broker
End user
Application
Data Analysis
Smart Devices
Connectivity
50
Wireless Connectivity with MCUs
For connectivity purposes, MCU were used with external wireless communication modules through
Serial (UART) Interface
Serial (UART)
Connection BlueTooth
TX
UART Modules
Zigbee
RX
Wifi
The configuration and parameters settings are done using the AT Commands (AT = ATtention)
Each module has its own AT Commands List. Example (ESP8266 Wifi Module)
AT+CIPSTART=0,"TCP", Start TCP or UDP connection. Here, the first parameter (0) is the id of the connection, “TCP” means
we’re using TCP instead of UDP, then followed by the address (or ip) of the web server then the port
"www.teachmemicro.com","80” number.
10
1
DAMERGI Emir – INSAT 2021
Compilation
High Level Layers
(Host) are provided as
software Libraries Binary File
Flash
Low Level Layers (Controller) RAM Processing
are Hardware implemented device Analog
GPIO
as Peripheral Interface RF
BLE Controller
GPIO
Disadvantages (Libraries):
• Compatibility issues with IDEs and programming languages.
• Compatiblity issues with compilers versions.
• Resulting code not optimized ( Size, Power consumption)
• Source code must be given by stack developper. 10
2
DAMERGI Emir – INSAT 2021
51
Wireless Connectivity with SoCs – New approach
Example: BLE Protocol Stack
Compilation
GPIO
as Peripheral Interface RF
BLE Controller
GPIO
Binary Stacks are also called « Software Devices »
« Dispositif logiciel » 10
3
DAMERGI Emir – INSAT 2021
User App
Communication between User Application and
Binary the Binary Stack (Function Calls and Responses)
Stack
are done through message written to and read
Flash Mailbox Processor from RAM (Mailbox).
RAM
GPIO
BLE Controller
GPIO
Advantages:
• No more Compatibility issues with IDEs, programming languages and compilers versions. (Less support)
• Very optimized code.
• Source code remains confidential.
10
4
DAMERGI Emir – INSAT 2021
52
Wireless Connectivity with SoCs – ESP32
BlueTooth Low level Wifi Low level Layers as
Layers as P.I (HW) P.I (HW)
Wifi and Bluetooth
high Level Layers
as Binary Stacks
User application
• BLE Stack
Cortex-M0 processor • Zigbee Stac
Accessing the BLE and • Thread Stack
802.15.4 Periph Interface • Dual BLE/Zigbee Stack
and executing the Binary
Stack
10
6
DAMERGI Emir – INSAT 2021
53
Smart Devices
Connectivity
10
8
DAMERGI Emir – INSAT 2021
54
Programming ESP SoCs with Arduino : ESP8266/32 as Wifi Access point
10
9
DAMERGI Emir – INSAT 2021
Programming ESP SoCs with Arduino : ESP8266/32 as Wifi Client and Access point
11
0
DAMERGI Emir – INSAT 2021
55
Programming ESP SoCs with Arduino : ESP8266/32 as Web Server
same time.
processing.
11
1
DAMERGI Emir – INSAT 2021
• Routes are created and added to the server before it starts listening for requests.
• A route refers to , an path (URI) , an HTTP method and handler combination sent by an HTTP
Actions executed
when a HTTP
GET Request
(with URI ‘/’) is
sent by the client
Rq: An URI can identify a resource such as a web page, a file, an Action to be executed
11
2
DAMERGI Emir – INSAT 2021
56
Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – HTTP response
1)
Example: request->send (200, "text/plain", "Hello World!");
request->send (404);
2)
11
3
DAMERGI Emir – INSAT 2021
Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – HTTP response
3)
• In this case, the content of the html page will be processed by the function htmlprocessor
(user defined). Its role is to replace placeholders in html code with appropriate strings before
Example : A html page containing a placeholder : %temperature%, the htmlprocessor function will
replace the string ‘’%temperature%’’ with a string corresponding to the temp value ‘’29’’
11
4
DAMERGI Emir – INSAT 2021
57
Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – Application
- When a Client (Web Browser) accesses the Smart Device (Root), The Index.html is sent. It contains
two buttons GREEN and BLUE: When the Blue button is pressed the ESP Blue Led is set ON and the
GPIO state is displayed (BLUE). When the GREEN Button is pressed , the GREEN Led is turned ON
11
1) https://randomnerdtutorials.com/esp32-web-server-spiffs-spi-flash-file-system/ 5
DAMERGI Emir – INSAT 2021
1) The html page contains a variable element (the string to be displayed after GPIO state). It must be a
placeholder
Index.html
58
Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – Application
Setup continued…..
11
8
DAMERGI Emir – INSAT 2021
59
Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – Application
11
9
DAMERGI Emir – INSAT 2021
When The access is locally and the 2 Devices are connected to same AP: IP Addresses are known
For Remote Access through Internet: The Smart Device IP Address is unknown and variable !!!!!
Web Client
IP ???
AP
12
0
DAMERGI Emir – INSAT 2021
60
Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – Limits
2
3
IP
AP
1- The Server reservers a Fixed IP Address (Reference@) to the communication between the 2 entitites.
2- The Device publishes periodically the IP address (of the edge AP) to the Server.
3- The 2 entities use the (Reference@) for sending Data. The server plays a gateway role.
Disadvantages:
• The AP must be configured to route all Packets to a Device (TCP Port).
• Not scalable: If many Devices are used, each one must have use a different TCP Port and the AP
configured for each Device.
12
1
DAMERGI Emir – INSAT 2021
1 2
IP
AP
1- The Device stores Data (State, Value) in the Database and gets Client Commands and Requests.
2- The Client Stores Commands ans Requests in the Database and gets Data (State, Value).
Disadvantages:
• Complicated Approach. SQL Request must be sent !!! (Field Names and Types must be known)
• Database Access Time is Long Not Scalable (When Important Nbr of Devices)
• Adding New Data Measurements Update DB, Client code and Device Code.
12
2
DAMERGI Emir – INSAT 2021
61
Smart Devices
Connectivity
MQTT Protocol
12
MQ: An IBM Product (90s)
4
DAMERGI Emir – INSAT 2021
62
MQTT : the IoT Messaging protocol
MQTT is a Client Server messaging transport protocol relying on a publish/subscribe model.
• An MQTT Sever is called Broker. It receives all messages from the clients and then route them to the
appropriate destination clients.
• An MQTT Clients is any device (IoT Thing, smart device, PC, Server) running MQTT protocol, that
connects to the MQTT Broker (over a network) and sends Data (Publish) or request Data (Subscribe)
MQTT : Topics
MQTT topics are a form of addressing that allows MQTT clients to share information.
MQTT Topics are structured in a hierarchy similar to folders and files in a file system using the forward
slash ( / )as a delimiter. So, you can create a self descriptive naming structures of you own choosing.
Rq: Topic are Case sensitive and Must consist of at least one character to be valid.
12
6
DAMERGI Emir – INSAT 2021
63
MQTT : Message Format
Message Type DUP QoS Retained 0: At most-once Delivery (Best Effort, Fire and Forget)
PUBLISH yes Yes 1*) Yes 1: At least-once delivery (Messages are guaranteed to be
delivered. Duplicate Messages are tolerated.
SUBSCRIBE 0 Yes 2*) X
Retain:
1: Order the Broker to retain the last
received PUBLISH Message and deliver it as
a first message to new subscribers
Example (Using retain=1):
Temperature, Light or Door On/OFF Status 12
8
DAMERGI Emir – INSAT 2021
64
MQTT : QoS
12
9
DAMERGI Emir – INSAT 2021
13
0
DAMERGI Emir – INSAT 2021
65
MQTT : Keep Alive Timer & Session Lifetime
13
1
DAMERGI Emir – INSAT 2021
MQTT : Subscription
66
MQTT: Clean session Bit & Session Persistance.
13
3
DAMERGI Emir – INSAT 2021
13
4
DAMERGI Emir – INSAT 2021
67
MQTT: ESP32 Programming (with arduino)
Declarations
13
5
DAMERGI Emir – INSAT 2021
Connect to AP
13
6
DAMERGI Emir – INSAT 2021
68
MQTT: ESP32 Programming (with arduino)
Loop
https://pubsubclient.knolleary.net/api 13
8
DAMERGI Emir – INSAT 2021
69
MQTT: ESP32 Programming (with arduino) – PubSubClient Methods
13
https://pubsubclient.knolleary.net/api 9
DAMERGI Emir – INSAT 2021
14
https://pubsubclient.knolleary.net/api 0
DAMERGI Emir – INSAT 2021
70
Oasis Official Document
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.pdf
MQTT essentials
https://www.hivemq.com/mqtt-essentials/
14
1
DAMERGI Emir – INSAT 2021
Smart Devices
Connectivity
71
BLE: vs Classic Bluetooth
Bluetooth Low Energy is used for applications that do not need to exchange large amounts
of data and can run on battery power for years at a cheaper cost.
It uses short Frames and remains in sleep mode most of the time.
Connection times only last a few milliseconds,
14
3
DAMERGI Emir – INSAT 2021
14
4
DAMERGI Emir – INSAT 2021
72
BLE: vs Classic Bluetooth
*) BLE intended for devices that only need to exchange small amounts
of data periodically, extending battery life by months or even years.
14
5
DAMERGI Emir – INSAT 2021
BLE: profiles/Protocols
Bluetooth profiles are additional protocols that build upon the basic Bluetooth standard
(protocol) to more clearly define how the underlying standard must behave.
While Bluetooth specifications (Protocols) define how the technology works, profiles define
how it's used.
14
6
DAMERGI Emir – INSAT 2021
73
BLE: Protocol Stack
14
8
DAMERGI Emir – INSAT 2021
74
BLE: Control Plane (GAP profile/Link layer)
• GAP : This layer directly interfaces with application layer
and/or profiles on it. It provides Access to the Link layer
operations related to:
- Device Discovery.
- Connection Timing Control
- Connection establishment and termination
• Link Layer :. It is responsible for :
- Advertising
- scanning,
- Creating/maintaining connections.
The operation of BLE Link Layer can be described in terms
of state diagram:
14
9
DAMERGI Emir – INSAT 2021
- Contact tracing
- Indoor Localization
- Smart Watch
A peripheral : A device that advertises its presence so central devices can establish a connection. After
connecting, peripherals no longer broadcast data to other central devices and stay connected to the device.
They are low-power. [expl: Smart Watch]
A Central: A device that initiates a connection with a peripheral device by first listening to the advertising
packets. Then, it sends a request connection data packet to the peripheral device.
If the peripheral device accepts , a connection is established [expl: Smart Phone]
15
0
DAMERGI Emir – INSAT 2021
75
BLE: Link Layer Roles & Connection establishment
Depending on the desired GAP Role, the Link Layer will move through the LL States to establish the
connection and reach the Master Role (Peripheral) or the Slave role (Central)
3
1 2 1
4
3
Central Peripheral
15
1
DAMERGI Emir – INSAT 2021
BLE: Advertising/Scanning
The Scanner (S) scans the three Advertising channels periodically with a period = scan Interval (50 ms
default) . Each channel is scanned for a time = scan window (25ms default).
BLE devices in 3 ms
can scan, connect,
send data, confirm
validate reception
and terminate
Each Advertising Interval (20 ms default), The Advertiser (A) transmits 3 Advertising Packets on the 3
available Advertising channels (37, 38 and 39).
The Scanner can then receive the Advertising Packet an send a Connect Request
15
2
DAMERGI Emir – INSAT 2021
76
BLE: Connection Parameters
The connection request packet includes:
• Frequency hop increment (channel_i, channel_j, channel_k, … ) with j=i+hop increment , k=j+hop
increment.
• Connection interval : The time between the beginning of two consecutive connection events. This value
ranges from 7.5 ms to 4 s.
15
3
DAMERGI Emir – INSAT 2021
Connection
Interval
• Connection supervision timeout: The maximum time between two received valid data packets
before a connection is considered lost. (max 30 sec)
15
4
DAMERGI Emir – INSAT 2021
77
BLE: Data Plane (GATT/ATT)
The GATT profile and ATT protocol represent
together the data Plane in the BLE protocol Stack
An Attribute is :
• A generic term for any data element that is exposed by a server .
• The smallest data entity defined by ATT (and GATT)
• Addressable pieces of information.
15
5
DAMERGI Emir – INSAT 2021
Identifier for each of the data that the server attribute can be read or
15
6
DAMERGI Emir – INSAT 2021
78
BLE: GATT Services
19B10010-E8F2-537E-4F6C-D104768A1214 RD
19B10011-E8F2-537E-4F6C-D104768A1214 0/1 RD | WR
19B10012-E8F2-537E-4F6C-D104768A1214 0/1 RD
15
7
DAMERGI Emir – INSAT 2021
15
8
DAMERGI Emir – INSAT 2021
79
BLE: GATT/ATT Roles
Request / Command
15
9
DAMERGI Emir – INSAT 2021
16
0
DAMERGI Emir – INSAT 2021
80
BLE: ESP32 BLE Server Programming - 1
Include and Declarations
Setup
Initialize Smart Device BLE
Create GATT Server
Create a Service
Create Characteristics
16
1
DAMERGI Emir – INSAT 2021
16
2
DAMERGI Emir – INSAT 2021
81
Smart Devices
Connectivity
It is recommended to have :
16
4
DAMERGI Emir – INSAT 2021
82
Wireless Link Budget : example
PTX= 8mW
Receiver sensitivity = - 61 dBm
SOM = 10 dBm
Q1) D max in Free Space link ?
Q2) P RX,mW for D= Dmax/2 ? 16
5
DAMERGI Emir – INSAT 2021
Power Consumption
WH = V * I * T W=V*I
V
GND
16
6
DAMERGI Emir – INSAT 2021
83
Output Power/Power Consumption (ESP32 and UBLOX)
16
7
DAMERGI Emir – INSAT 2021
16
8
DAMERGI Emir – INSAT 2021
84
16
DAMERGI Emir – INSAT 2021
9
85