0% found this document useful (0 votes)
30 views85 pages

SmartDevices Chapitres1 2 3

Uploaded by

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

SmartDevices Chapitres1 2 3

Uploaded by

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

Chap 1

Smart Devices : Introduction

Emir DAMERGI
INSAT 2021/22

Smart Devices (Smart Embedded Devices)


• An embedded device is an object that contains a special-purpose computing (processing)
system : Generally, an embedded device's Firmware (Software) will only run a single
application which helps the device to do its job.

• An embedded system is a processor-based computer hardware system with software that is


designed to perform a dedicated function

• Embedded processors (Proessors


for embedded systems) represent
98% of processors market in 2019.
• 2% for PC/Mac processors.

2
DAMERGI Emir – INSAT 2021

1
Smart Devices (Smart Embedded Devices)

A Smart Device is an embedded device made intelligent ???

The definition has evolved over Time. The definition proposed in [1] will be retained:

“A smart device is a context-aware electronic device capable of

performing autonomous computing and connecting to other devices

wire or wirelessly for data exchange”

[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., )

Autonomous computing [1]


The ability of a device or multiple devices performing tasks autonomously without the
direct command of the user.

[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.

What Are Smart Devices? (www.arm.com/glossary/smart-devices)


Smart devices are all of the everyday objects made intelligent with advanced compute, including AI
and machine learning (not necessary), and networked to form the internet of things (IoT).
Smart devices can operate at the edge of the network or on very small endpoints, and are powerful
enough to process data without having to report back into the cloud

What is IoT? (www.oracle.com/internet-of-things/what-is-iot/)


The Internet of Things (IoT) describes the network of physical objects—“things”—that are
embedded with sensors, software, and other technologies for the purpose of connecting and
exchanging data with other devices and systems over the internet.

Thing (IoT) = Smart Device

5
DAMERGI Emir – INSAT 2021

Smart Devices: IoT Edge and Endpoint Devices

Internet Cloud (Servers)

Internet Access

The Edge

Gateway

Edge Device Network Entry point to the IoT Cloud.


Bluetooth, Zigbee, Lorawan, …
Endpoint Device
The most remote element in an IoT communication channel
(thing)

• An endpoint can also be an Edge device.


• In addition to the Gateway function, Edge devices can perform complex tasks
(Data agregation, processing , filtering, secutity, etc..) 6
DAMERGI Emir – INSAT 2021

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

 A smart device is an embedded  An embedded system is a


system with: processor-based computer
• context awareness hardware system with
• Autonomous Computing software that is designed to
• Connectivity perform a dedicated function

Sensors Computing
Wired or Wireless
(embedded) Network
Interface
Actuators System
Connectivity
Context Autonomous
Awarness computing

9
DAMERGI Emir – INSAT 2021

Smart Devices : computing System

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….

* : Peripheral Interface ( Interface de périphérique)


10
DAMERGI Emir – INSAT 2021

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

A (following in this chapter): Computing Systems for Smart Devices

B Chapter 2: Input/Output Operations in Smart Devices (context Awareness)


B
C Chapter 3: Smart Devices and Connectivity
3
D Chapter : Smart Devices and edge Computing
DAMERGI Emir – INSAT 2021 11

Smart Devices : Programming

User application

High Level (Object Oriented Library)

MiddleWare (Protocols, File System, Scheduler)

Firmware or HARDWARE ABSTRACTION LAYER


(HAL or Firmware Library)

Smart Device (Computing System + Sensors + Connectivity +..)

DAMERGI Emir – INSAT 2021 12

6
Smart Devices :

B A A B
E
Chapter 2,3 and D (Arduino)

D
C

C Chapter 4: Low level Input/output – Access (Cortex-M bassed SOC)

D Chapter 5: Firmware Library

E Chapter 6: Object Oriented Layer Programming


13
DAMERGI Emir – INSAT 2021

Computing System for


Smart Devices

7
Smart Devices : Computing System

A Sytem on a chip, also known as an SoC, is an integrated circuit that takes a

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

specific application (SoCs for Smartphones, for Digital Receivers, etc..)

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

Smart Devices : Computing Systems


The two terms (MCU, SoC) are often used interchangeably. The main difference is that

• 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

STM32F4 MCU STM32WB55 SOC

Using ST MCU (STM32F4) in a Smart Device Using ST SOC (STM32WB55) in a Smart


with Bluetooth connectivity Device with Bluetooth connectivity

16
DAMERGI Emir – INSAT 2021

8
Smart Devices : Computing Systems – Socs/MCUs

SOC / Processor Flash Wifi BLE Std Zigbee OO IDE Company


MCU (MB) BL
ESP8266 Tensilica Standard 1 to 4 Yes No No No Arduino Espressif (CN)
106Micro @80Mhz

ESP32 Tensilica Xtensa LX6 1 to 4 Yes Yes Yes No Espressif (CN)


@160MHz Arduino
(1/ 2 Cores )
NINA- 1 No Yes No No Arduino U-blox (CH)
B306 ARM Cortex-M4 +
ARM MBED
STM32 ARM Cortex-M4 1 No Yes No Yes Arduino ST-
WB55 + + Microelectronics
ARM Cortex-M0 ARM MBED (FR/ITA)
STM32F4 1 No No No No Arduino ST-
(MCU) ARM Cortex-M4 + Microelectronics
ARM MBED (FR/ITA)

BLE: BlueTooth Low Energy (5.0)


Std BL: Standard BlueTooth (up to 4.
OO IDE: Object Oriented Integrated development environment
17
DAMERGI Emir – INSAT 2021

Smart Devices : Computing Systems - Boards

NodeMCU (ESP8266)
NodeMCU (ESP32)

Nano 33 BLE Sense P-Nucleo-WB55 Nucleo-F401


(Nina-B306) (STM32WB55) (STM32F401 MCU)

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

Compiler RAM Flash,


(Variables) BUS User code
(Code)
(Application)
Binary P.I
File
P.I Bootloader
Debugger/Programmer P.I
Formats: SWD P.I
.bin DAP
.hex
.elf

The Flash memory of a SoC conatians User code


User Code can be updated by using a
and Bootloader code (company).
Debugger/programmer probe that communicates
Both are called Firmware (a specific class of software
with the DAP peripheral (Debug Access Port).
that provides the low-level control for a device's specific
The DAP has access to Flash memory and can
hardware)
update the user code The Bootloader is the first code executed after system
Reset. It jumps to the Application.
19
DAMERGI Emir – INSAT 2021

Smart Devices : Bootloader and In Application Programming (IAP)

Bootloader) is the code executed just after


SoC Reset. It is the BIOS (PC) equivalent Update User
code from UART
in embedded systems. It :
• Initializes some peripherals parameters.
Update User
• Polls for update user code update code from CAN
request ?
 If yes, the user code is updated through
Update User
one of the available peripherals (UART,
code from USB
SPI; I2C, CAN, USB).
And the SoC is reset.
Update User
code from I2C
 If not, a jump to user code is executed.
Jump to user
code
Update User
For some SoCs, Code update wirelessly is code from SPI

possible. It is called OTA (Over the Air)


RESET
code update or FOTA (Firmware Over The SoC
Air).
STM32F4x Bootloader Activity Diagram
Examples: STM32WB55 and Nano 33
support FOTA (Bluetooth).
20
DAMERGI Emir – INSAT 2021

10
Smart Devices
Development Environment

Smart Devices : Programming Tools/IDE

22

DAMERGI Emir – INSAT 2021

11
Smart Devices : Programming Tools/IDE

Low Level (Register API based High Level


Access) (ST HAL) (OO: Arduino /MBED)

User Expertrise Very high High Low to medium

Dev Time Very Long Long Very short

Code Size Very Short Short Large

Performance Optimized ~Optimized ~ poor

Code Easy Easy Difficult


Control/modification

Users Professionals Professionals Hobbyists


Professionals *)

*)- For Fast Prototyping


- For products without Performance/memory Size
23
constraints (Expl: Sonoff – ESP8266/Arduino)

DAMERGI Emir – INSAT 2021

Smart Devices : ARM MBED 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

The Arduino IDE is an open-source cross-platform application (Windows, Linux and


MacOS), written in JAVA language, and developed by Arduino Company.
• For Programming Languages, It supports the C and C++.
• Originally, it was used for only the boards developed by the Arduino company.
• Many other Microcontrollers and Boards Manufacturer, have developed their own
Libraries (Arduino compatible) and added them to the IDE.
 ESP8266 and ESP32 SOCs (Espressif)
 STM32 MCUs (ST Microelectronics) “ARM Cortex-M”
 Nordic MCUs (Nordic). “ARM Cortex-M”
 MSP MCUs (Texas Instruments). “ARM Cortex-M”
 NINA SoCs (UBLOX). “ARM Cortex-M”

25
DAMERGI Emir – INSAT 2021

Smart Devices : Arduino Sketchs Structure

The minimal Arduino project (sketch) contains:

1- Global Variables declarations, defines and


library including will be put at the beginning.

2- Setup function : This function will be


executed once at program start. Contains the
code to initialize the variables, pin modes, start
using libraries, etc.

3- Loop function: will be run continiuously as


the board is powered. The code controlling the
application behavior must be added here.

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

• Configuration parameters (AP SSID, AP Pwd, etc…)

• Collected data

Variables are not suitable. They are stored in RAM and are lost when the system is powered off.

A part of the code memory (Flash) is used for DATA storage.

Difficulty: Code for Accessing Flash to store Data in file like structures.

Managing Code and Data Zones.

Tools* for :

- splitting the Flash memory in Code and data zone


+ uploading Files to data zone . 1M of a total size of 4 MByte are reserved for data

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

Data Storage / Manipulation : File System for ESP in Arduino

For ESP8266 and ESP32, SPIFFS* File System :

1) Include the Library: #include « SPIFFS.h » (« FS.h » for ESP8266)

2) Create and Initialize SPIFFS Object: SPIFFS.begin();

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

Data Storage / Manipulation: JSON Format


Manipulating txt files to store, recuperate, update Data and Connexion parameters (structured Data) is
complicated.
JSON (JavaScript Object Notation) is an open standard file format and data interchange
format that uses human-readable text to store and transmit data.

• Objects are described in a hierarchical structure.


Object
• The hierarchy level is thetheoretically unlimited.
ParamName1
• Each parameter has a value or a value Array or is an
Object itself.

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 ,

• Strings are placed between


‘’ParamName2’’ : {’ParamName2_1’’ : Value2_1 , ‘’.
‘’ParamName2_2’’ : {‘’ParamName2_2_1’’ : value2_2_1 ,
‘’ParamName2_2_2’’ : value2_2_2 }, • Parameter Names are
Strings.
},
‘’ParamName3’ : [ value3a , value3b, value3c ] • Arrays are grouped between
} [ and separated by , .
33
DAMERGI Emir – INSAT 2021

Data Storage / Manipulation: JSON Format

Example of a JSON File describing a Person (Object)

{ "firstName": "INSAT",
"lastName": "INSAT",

"age": 25,

"address": { "streetAddress": "Boulevard de la terre",


"city": "Zone Urbaine Nord",
"postalCode": 1080

},

"phoneNumbers": ["71800800" , "71800801" ]

34
DAMERGI Emir – INSAT 2021

17
Data Storage / Manipulation: JSON Manipulation In Arduino

JSON File : serie of characters(Text File)


{"firstName": "INSAT", "lastName": "INSAT", "age": 25, "address": { "streetAddress": "Boulevard de la terre", "city": "Zone Urbaine
Nord","postalCode": 1080 },"phoneNumbers": ["71800800" , "71800801" ]}

Deserialization: extract Serialization: Convert json


objects, parameter structure in a json formatted text
names and values file (brackets, etc…

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

Data Storage / Manipulation: JSON Manipulation In Arduino

{
"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

3- Get data from JsonDocument  Structure


1- Open the json file

2- Deserialize the json file  to jsonDocument

4- Close File and ends SPIFFS

37
DAMERGI Emir – INSAT 2021

Data Storage / Manipulation : JSON Serialization

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

All SoC pins (except Power/Gnd pins) are connected to the


GPIO Peripherals «General Purpose Input Output».
Pins provide an electric connection
between the SoC peripherals and The Processor has direct access (Read/Write) to the

external circuits. GPIO pins.

All Communications between a SoC Perpiheral and


external devices must go through the GPIOs.
*) Generally 16 pins for each GPIO Peripheral
24
DAMERGI Emir – INSAT 2021

Digital I/O : GPIO Pins & Ardunio Numbering

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 GreenLed D12

For Pins that are not tied to the Arduino Connector,


the ardunio pin numbers must be known. For each
Example for Arduino Pin
installed board, a pins_arduino.h file contains the
numbering (ESP8266)
pin mapping.

#define PushButton 4

It is possible to use Arduino pin Numbers for connector pins.

42
DAMERGI Emir – INSAT 2021

21
Digital I/O: Arduino Programming

Pin Configuration Write a Value to Pin Read Pin Value

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

Digital I/O: Arduino Programming


Arduino Connector

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).

To control High Current/Voltage Devices, a Power Interface must be used.

Example: Relay, power Transistor.

45
DAMERGI Emir – INSAT 2021

Smart Devices
UART Communications

23
USART:

Synchronous

Asynchronous

STM32F4: USART1, USART2, USART3, UART4, UART5, USART6


DAMERGI Emir – INSAT 2021

UART: Communication with modules / PC

Bluetooth

Zigbee
TX

Wifi
RX

GPS
PC
Terminal

TX USB/Serial Converter
serial USB
(CH340, PL2110, ST-
Link)
RX

DAMERGI Emir – INSAT 2021

24
UART: TX/RX Pins

As stated, all communications between


Flash RAM Processing
device Peripheral interfaces and external
UART1_TX

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.

DAMERGI Emir – INSAT 2021

UART Communications : Serial Object


Serial Object Creation and Communication parameters configuration:

If config is not specified, the default value is used :

SERIAL_8N1: 8 data bits, No Parity bits and 1 Stop bit

Setup() { Generally, the UART Pins are specified and


pre-configured.
Serial.begin (115200); //initialize Serial Port with 115200 bits/sec

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

The Arduino Default Total


Buffer size is 64 bytes.
If more bytes are sent, the
write operation can be blocked. Examples:
Serial.print (" HELLO !!") ;
int X=17; Example:
Serial.print (X);  17 Char Msg[ ] = " HELLO !!";
Serial.print (X,BIN);  10001 Serial.print (Msg , 7) ;
Serial.print(X,HEX);  11

Float Accel = 35,26548


Serial.print (Accel);  35,26
Serial.print (Accel,4);  35,2654

51
DAMERGI Emir – INSAT 2021

UART Communications; Receiving Data

const byte numChars = 32;


char receivedChars [numChars];
Byte nbr;

while (Serial.available() > 0 && nbr < numChars)


{

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

UART Communications: GPS (NMEA Messages)

When only 1 satellite is in View : only the date and Time can be decoded

V = Void (No GPS position can be calculated)

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

Read Data (NMEA) from serial


Port (Connected to GPS
Module) and decode with gps
object

Read decoded Data (Date,


Time, Longitude and Lattitude)

55
DAMERGI Emir – INSAT 2021

UART Communications: AT Commands (GPRS Communication Modules)

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)

Example (SIM800L GSM/GPRS Module):

AT+SAPBR=3,1,Contype,"GPRS“ (GPRS Mode, Response = “OK” if success else


“ERROR”)

AT+SAPBR=3,1,Contype,“CSD“ (Data Call mode, Response “OK” if success


else “ERROR”)

at+cmgs="+XXXXXXXXX" Response “+CMGS: 1” if sms is sent


else “ERROR”
> The message………. [Ctrl+Z]
56
DAMERGI Emir – INSAT 2021

28
Smart Devices
Analog Sensors

Processing Analog quantities ?


Processor

Binary Data Voltage Temperature, Speed,


Digital
Processing Analog Quantity Sensor Humidity, light

Sampling
Analog Digital
Conversion
Quantization

DAMERGI Emir – INSAT 2021 58

29
Analog to Digital Conversion
Processor
Analog Digital Temperature, Speed,
Binary Data Voltage Sensor
Digital Converter
Processing Analog Quantity Humidity, light
ADC

Quantized Quantization Sampled


Coding Sampling
signal signal

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

DAMERGI Emir – INSAT 2021 59

Sampling Period

time
Fourier Transform
frequency

Fmax

Sampling Frequency min= 2* Fmax

 Sampling Period max =1/2*Fmax

Tconv :Conversion Time

Sampling Quantization Sampling Quantization


+ Coding + Coding

Sampling Period min= Tconv


Tconv < Sampling Period < 1/(2*Fmax)

DAMERGI Emir – INSAT 2021 60

30
Conversion Result

Nin (N bits) Vin = Vref - + Nin * ΔQ


2N-1=111…1

Quantization Step (Quantum)


=
ΔQ = (Vref+ - Vref-) / 2N

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

Vin = Vref - + Nin * ΔQ


Vin (Voltage)

Δy slope
Vmes
Δx = Δy/Δx

G = ( Vref - + (Nin * ΔQ) – VMES ) /slope + Gmes


Gmes G
(quantity to
measure)
ΔQ = (Vref+ - Vref-) / 2N
G = ( (Vin – Vmes )/ slope ) + Gmes

DAMERGI Emir – INSAT 2021 62

31
Analog I/O: Arduino programming

It specifies the Vref+


source (It depends on Returns a value from 0 to 1023
the board). Generally, (for a 10 bits resolution:
default value is 5V default).
Vref- is tied to ground To exact voltage can be
(0V) calculated (see last diapo)

63
DAMERGI Emir – INSAT 2021

Analog I/O: Arduino programming (PWM)

Vmoy = 0

Generates a Periodic ON/OFF Digital


signal with duty cycle: Ton / T = value Vmoy = 1,25V

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

*) Direct Current : courant continu 65


DAMERGI Emir – INSAT 2021

Analog I/O: Arduino programming (PWM - Servo Motor Control)


#include <ServoESP32.h> for
ESP32

+V

PWM

66
DAMERGI Emir – INSAT 2021

33
Smart Devices
Digital Sensors

Digital Sensors: I2C/SPI communications

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

executed inside the Digital sensor itself.

Registers
Digital Sensors contain some Registers used to store
ADC
• A/D Converter and Communication configuration
Analog
• Conversion Results (binary data)
Sensor

Each Register has its address. Communication Analog/Digital


Interface Converter

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)

serial communication protocols

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

The SoC, is so able to configure the Sensor (ADC) by Digital Sensor

writing to configuration registers and to read the

conversion result by reading the corresponding

Register(s).
69
DAMERGI Emir – INSAT 2021

Smart Devices
Digital Sensors : SPI

35
Serial Connectivity: SPI
SPI MASTER SPI SLAVE

SPC
SDI
SDO
/SS

• Developped by Motorola (1980s)


• Synchronous and Full Duplex communication (Clock generated By Matser)
• High Speed (100 Mbps @ 0,1m)
• 4 wires*:
SCLK: Serial CLOCK  SPC: Serial Peripheral Clock
MOSI: Master Output Slave Input  SDI : Serial Data In
MISO: Master Input Slave Output  SDO: Serial Data Output
/SS: Slave Select (Active Low Level)

* : 3 Wires Communication is possible (SDO=SDI)  Half Duplex


DAMERGI Emir – INSAT 2021 71

Serial Connectivity: SPI


SPI MASTER SPI SLAVES

A point (Master) to Multipoint


(Slaves) communication is
possible:

Slave Select Signal • 3+N Wires are needed for


for each Slave controlling N Slaves
• At a given time, only one
unique Slave must be active.

DAMERGI Emir – INSAT 2021 72

36
Serial Connectivity: SPI

SDI / WR + Address i Data to Slave ……… Data to Slave Data to Slave


(WRITE) (WRITE) (WRITE)

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

Serial Connectivity: SPI

SDI / Rd + Address Dummy Data ………… Dummy Data Dummy Data

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

For installed Boards, SPI Peripherals are


declared and the 3 pins (SCK, MOSI and MISO)
are defined.
The user can choose any free pin for the Slave
Select signal that will be controlled by the user
code (LOW, HIGH)

DAMERGI Emir – INSAT 2021 75

SPI: Arduino Programming - Write to slave

Write 1 Byte in Reg


(@Reg=i+1)

SPI Master 0 @Reg = *@Reg= Value (DI7..DI0)


i+1
SoC
SCK SPI Slave (Digital Sensor)
MOSI Value
SPI SPI
MISO @ Reg
MySPI_SS /CS
i
ADC
i+1
Analog
digitalWrite (MySPI_SS, LOW); i+2
Sensor

SPI.transfer (i+1); //send address …


I2C Interface Analog/Digital
SPI.transfer (Value); //send value 8 bits Converter

digitalWrite (MySPI_SS, HIGH);

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

Write many Bytes (2) in Reg


(@Reg=i+1, i+2)

0 @Reg = *@Reg i+1= Value1 (DI 7..DI 0)


i+1
*@Reg i+2= Value2 (DI 15..DI 8)

digitalWrite (MySPI_SS, LOW);

SPI.transfer (i+1); //Send address

SPI.transfer(Value1); //Send first value  @ i+1

SPI.transfer(Value2); //Send second value  @i+2

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.

DAMERGI Emir – INSAT 2021 77

SPI: Arduino Programming - Read From slave

Read 1 Byte:

Var = *@Reg = DO7..DO0


1

Const int Reg_Addr = …

digitalWrite(MySPI_SS, LOW);

SPI.transfer(Reg_Addr | 0x80 ); //Send address with MSB (bit 7 = 1): Read

Uint8_t Rcv_Data = SPI.transfer(0x00); //Send dummy Data to activate clk signal and read Data from Slave

digitalWrite(MySPI_SS, HIGH);

DAMERGI Emir – INSAT 2021 78

39
SPI: Arduino Programming - Read From slave

Read 2 Bytes:

Var = *@Reg = DO7..DO0


1

Const int Reg_Addr = …

digitalWrite(MySPI_SS, LOW);

SPI.transfer(Reg_Addr | 0x80 ); //Send address with MSB (bit 7 = 1): Read

Uint8_t Rcv_Data1 = SPI.transfer(0x00); // Read Data from Slave (@Reg_Addr)

Uint8_t Rcv_Data2 = SPI.transfer(0x00); // Read Data from Slave (@Reg_Addr + 1)

digitalWrite(MySPI_SS, HIGH);

RQ: It is possible to read multiple bytes from successive Address registers @X, @X+1, @X+2, ….@X+n.

DAMERGI Emir – INSAT 2021 79

Smart Devices
Digital Sensors : I2C (Inter-Integrated Circuits)

40
Serial Connectivity: I2C
Invented
by philips
Serial Clock (SCL)
1982 Generated by Master

Serial Data (SDA)

I2C is a 2-Wire Half Duplex Bus (Clock+Data) :

• Multiple Masters can be connected to the Bus.

• Only Masters can start /Stop a communication.

• Each Slave must have a unique Address.

• 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)

• Collision Detection Mechanism


DAMERGI Emir – INSAT 2021 81

Serial Connectivity: I2C

 Data transfer between ICs and systems at relatively low rates


 “Classic” I2C is rated to 100K bits/second

 “Fast Mode” devices support up to 400K bits/second

 A “High Speed Mode” is defined for operation up to 3.4M bits/second

DAMERGI Emir – INSAT 2021 82

41
I2C: Write to slave

Byte 1 Byte 2 Byte n


b7 b6 b5 b4 b3 b2 b1 b0
S SLAVE Addr /W A DATA A DATA A P
A6 A5 … …. A1 A0 R /W

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

From Matser to Slave A: Acknowlege bit (SDA Low)


From Slave to Master /A: Not Acknowlege bit (SDA High)
DAMERGI Emir – INSAT 2021 83

I2C: Write to slave


Byte 1 Byte 2 Byte n
b7 b6 b5 b4 b3 b2 b1 b0
S SLAVE Addr /W A DATA A DATA A P

Data Formats and meaning depends on the communication context:

• A user designed protocol (SOC to SOC).

• An order to the slave to perform an action.

• A Register Address + Data to be written

(I2C Peripheral Interfaces)

DAMERGI Emir – INSAT 2021 84

42
I2C: Read from slave (I2C Peripheral Interfaces)
Before reading, the Matser must start by sending (write) the register address

SLAVE Register SLAVE R


S /W A A P S A DATA A DATA /A P
Addr Addr Addr d

Start after Stop: not supported by all Devices


Another master can occupy the bus.

SLAVE Register SLAVE R


S /W A A S A DATA A DATA /A P
Addr Addr Addr d

Restart Condition
Without Stop

/A: Not Acknowlege bit (SDA High)


From Matser to Slave
the master orders the slave to
From Slave to Master stop sending
DAMERGI Emir – INSAT 2021 85

I2C: Slave Addresses


MMA 7660 Lis3SDH 24LC64
Accelerometer Accelerometer 64 Kbits EEPROM

1 Fixed 7 bits
Address:
2 configurable 7
1001 100 bits Addresses: 8 configurable 7
bits Addresses:
0011 1xx

0011 101 if sel=1 1 0 1 0 A2 A1 A0

0011 110 if sel=0

DAMERGI Emir – INSAT 2021 86

43
I2C: Arduino Programming - Configuration/Initilaization

An I2C object is an instance of TwoWire Class.


Flash RAM Processing
device To Declare an I2C object, the SDA and SCL must be
I2C1_SDA

GPIO
passed as parameters.c

# 𝑖𝑛𝑐𝑙𝑢𝑑𝑒 𝒘𝒊𝒓𝒆. 𝒉
I2C 1
I2C1_SCL
I2C 0
GPIO I2C0_SDA

I2C0_SCL

The Initialization of the I2C object is put in the setup


section (begin method)

Rq: For many Boards, the I2C objects can be


used without ddeclaration:
Wire1 for I2C1
Wire2 for I2C2,
Etc…

DAMERGI Emir – INSAT 2021 87

I2C: Arduino Programming - Write to slave

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);

To catch I2C communication if (MySensors_I2C.endTransmission(true) != 0) {


Errors, endTransmission return Serial.println ("problem writing to I2C device");
exit(0);
value can be tested. (!=0)
}
DAMERGI Emir – INSAT 2021 88

44
I2C: Arduino Programming - Read From slave

Restart Condition Without Stop

SLAVE Register SLAVE R


S /W A A S A DATA /A P
Addr Addr Addr d

MySensors_I2C.beginTransmission (Slave_Addr);

MySensors_I2C.write(Reg_Addr);

MySensors_I2C. endTransmission(false);

MySensors_I2C.requestFrom(Slave_Addr , 0x01); //Read 1 byte

uint8_t Rcv_byte = MySensors_I2C.read();

MySensors_I2C. endTransmission(true);

DAMERGI Emir – INSAT 2021 89

I2C/SPI Devices
Additinally to sensors, many other devices have I2C and SPI Interfaces:

OLED SSD EEPROM


Dispaly Memory LCD Dispaly Motor Control
(characters)

Bluetooth BLE
GPS Module
Module 4G/LTE
LORA Module Module

DAMERGI Emir – INSAT 2021 90

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

Home automation Automotive

Agriculture: smart Farms


Telemetry

The IoT Market

 As of 2013, 9 billion IoT units / $ 2 trillion.

 More than 40 billion IoT devices by 2020

 Revenue growth to $7 trillion in 2020

47
IoT Connectivity Technologies

WPAN: Wireless Personal


Area Network

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

10m 100m 1Km 10Km

The IoT Architecture (LPWA )

IoT End device Wireless


Connectivity IoT Gateway
(Thing)
Lora/Sigfox Lora/Sigfox
IP
connectivity
sensors

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

Fe: Further enhanced

eFe: even Further enhanced

eMTC MTC & NB


(LTE Cat M1) eFeMTC Evolution
MTC:
Machine‐Type
Communications
• Ultra Low Latency Communication
NB-IoT 5G-NR: (sub-millisecond)
FeNB-IoT
(LTE Cat M2) New Radio • CoMP (Coordinated Multi-Point)

NB: NarrowBand

IoT Connectivity Technologies

2G/3G/4G Cellular Network


10MBps
WIFI

1 MBps
Bluetooth BLE MTC

Zigbee NB
100 KBps

Lora
Zwave
10 KBps
LPWA: Low
Power Wide
Area Network

1 KBps

Sigfox

10m 100m 1Km 10Km

49
The IoT Architecture – LPWA with 5G

Public Base Station


5G IoT End
(5G)
device (Thing)
5G Wireless
connectivity
sensors
IP
connectivity
actuators Internet

sensors

IP connectivity
actuators

IoT Platform

Data Storage

Message
broker
End user
Application
Data Analysis

Smart Devices
Connectivity

Binary Stacks for wireless protocols

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)

Set WiFi mode:


1 is station mode (Module is client),
AT+CWMODE = … 2 is AP mode (Module acts like a WiFi router)
3 is AP+station mode (make the Module do both)
Connect to your WiFi. Provide your SSID name and password inside the double
AT+CWJAP = “<your-ssid>”,”<your-pw>” qoutes.

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

Wireless Connectivity with SoCs – Classic approach


Example: BLE Protocol Stack

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

Only User App is compiled


High Level Layers
(Host) are provided as
Binary Stacks

Low Level Layers (Controller) Flash RAM Processing


are Hardware implemented device Analog

GPIO
as Peripheral Interface RF

BLE Controller

GPIO
Binary Stacks are also called « Software Devices »
« Dispositif logiciel » 10
3
DAMERGI Emir – INSAT 2021

Wireless Connectivity with SoCs – New approach

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

P.I: Peripheral Interface 10


5
DAMERGI Emir – INSAT 2021

Wireless Connectivity with SoCs – STM32WB55

Cortex-M4 Processor executing the User code

User application

802,15.4 and Bluetooth


LowLevel Layers as HW 802.15.4 and Bluetooth high
Level Layers as Binary
Peripheral Interfaces Stacks in Flash memory:

• 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

Wifi Connectivity : Programming ESP8266/ESP32 SoCs with Arduino

Programming ESP SoCs with Arduino : ESP8266/32 as Wifi Client

#include <ESP8266WiFi.h> for esp8266

Connect to a Wifi Network (AP)

status returns WL_CONNECTED after connection


success

Returns The IP Address affected to the Smart device

10
8
DAMERGI Emir – INSAT 2021

54
Programming ESP SoCs with Arduino : ESP8266/32 as Wifi Access point

#include <ESP8266WiFi.h> for esp8266

SoC configured as Acess Point.


SSID parameters set.

Get The SoC (AP) IP Address

10
9
DAMERGI Emir – INSAT 2021

Programming ESP SoCs with Arduino : ESP8266/32 as Wifi Client and Access point

#include <ESP8266WiFi.h> for esp8266

Activate Dual Wifi Mode (AP + STATION)

Set AP SSID and PWD


Connect to a Wifi Network (AP)

status returns WL_CONNECTED after connection


success

Returns The IP Address assigned to the Smart device

11
0
DAMERGI Emir – INSAT 2021

55
Programming ESP SoCs with Arduino : ESP8266/32 as Web Server

The ESPAsyncWebServer Library provides an

easy way to build an asynchronous web server.

• Handle more than one connection at the

same time.

• Routes Handling and simple template

processing.

• Dynamic Web pages content update

through placeholder concept

11
1
DAMERGI Emir – INSAT 2021

Programming ESP SoCs with Arduino : ESP8266/32 as Web Server - Routing

• Routes are created and added to the server before it starts listening for requests.

• In the AsyncWebServer Library, Routes are handled by the ON method .

• A route refers to , an path (URI) , an HTTP method and handler combination sent by an HTTP

Client to the server.

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

The actions executed can be:


• Input/output operations (Example: Writing to a Pin (HIGH or LOW),
Reading a Pin State or a Sensor Value.
• Sending HTTP Responses to the Client using the request  send ()
method.

The send () method can ba called with different parameters :

1)
Example: request->send (200, "text/plain", "Hello World!");

request->send (404);

2)

Example: request->send (SPIFFS , "/index.html", "text/html");

11
3
DAMERGI Emir – INSAT 2021

Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – HTTP response

3)

Example: request->send( SPIFFS, "/index.html", "text/html " , false, htmlprocessor);

• 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

sending the html page to client.

• Placeholders are strings in html code placed between two %.

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

Application : The ESP based Smart Device must:

- Act as Acees Point with a Web Server running on it.

- Support a File System (SPIFFS)

- 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

and the GPIO state (GREEN).

11
1) https://randomnerdtutorials.com/esp32-web-server-spiffs-spi-flash-file-system/ 5
DAMERGI Emir – INSAT 2021

Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – Application

1) The html page contains a variable element (the string to be displayed after GPIO state). It must be a
placeholder

Index.html

2) The ressources to be used in this Application are:


- Index.html : URI = ‘/’
- Style.css : URI = ‘/style.css’ (not necessary)
- GREEN Button Press : URI = ‘/on’
11
- BLUE Button Press : URI = ‘/off’
6
DAMERGI Emir – INSAT 2021

58
Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – Application

Rq: For ESP8266:


#include ‘’FS.h’’
11
# include ‘’ESP8266WiFi.h’’ 7
DAMERGI Emir – INSAT 2021

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

Replacing the placeholder STATE by « GREEN » or « BLUE »

11
9
DAMERGI Emir – INSAT 2021

Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – Limits

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

Solution 1: Remote Access with


Dynamic DNS (expl: NO-IP) 1
Dynamic DNS
Server Web Client

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

Programming ESP SoCs with Arduino : ESP8266/32 as Web Server – Limits

Solution 2: Remote Access


with DataBase Server
Web Client
DataBase

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

MQTT : the IoT Messaging protocol

MQTT Protocol = MQ* Telemetry Transport 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)

Rq: It is possible to use a local MQTT Broker 12


New Implementations on Wireless Sensor protocols are in progress 5
DAMERGI Emir – INSAT 2021

MQTT : Topics

The MQTT relies on Topics.


• Each message sent by a Client (Publish) must contain a topic that the broker can use to forward the
message to interested clients.
• When a Client (Subscriber) request data, it must specify the topic.
• The Broker filters the messages according to the topic, to determine who is subscribed to each
message, and sending the message to these subscribed clients.

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.

Example of topics : MyHouse/Room1/Temp


MyHouse/Room1/Light
MyHouse/Room2/Humidity
MyHouse/Garagedoor

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

DUP (Dupplicate Message Flag): Retain:


0: First Message delivered 1: Order the Broker to retain the last
1: Client or Server re-livers a message received PUBLISH Message and deliver it as
Message Type: a first message to new subscribers
1: CONNECT
2: CONNACK
3: PUBLISH
4: PUBACK
5: PUBREC
6: PUBREL
7: PUBCOMP
8: SUBSCRIBE
9: SUBACK
10: UNSUBSCRIBE
11: UNSUBACK
12: PINGREQ
13: PINGRESP
14: DISCONNECT
QoS Level:

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

UNSUBSCRIBE 0 X X 2: Exactly-once (Delivery is guaranteed without Duplicates).


Example: Alarm System Triggered after receiving 2 Alerts.
1*) messages transmitted From Client (Publisher) To Broker
2*) messages transmitted From Broker to Client (Subscriber) 12
7
DAMERGI Emir – INSAT 2021

MQTT : Retained Messages

Client Client Client


A Broker B C

When a MQTT Client publishes


a Message into a given Topic
with retain=1
When a MQTT Clients
subscribes to the Topic,  The Broker will store the
it will receive instantly value until a new value is sent
the topic retained value to the Topic (by the same or
another Client)

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

(Best Effort, Fire and Forget)

Messages are guaranteed


to be delivered. Duplicate
Messages are possible.

Delivery is guaranteed without


Duplicates

12
9
DAMERGI Emir – INSAT 2021

MQTT : Session / Connection

• A session identifies an Attachment of a


client to a server.
• No communication is possible outside a
Session.
Last Will MQTT Client Infos Session –related
• A session is created with a CONNECT if authentication information
(or persistence) is
message and terminated with needed
DISCONNECT Message

13
0
DAMERGI Emir – INSAT 2021

65
MQTT : Keep Alive Timer & Session Lifetime

Keep alive Interval = 1.5 Keep Alive Timer (in seconds)

Each Time a Publish message is


received by the Broker:
• The Timer is re-armed (reset
and launched).

If the Client has no new data to


publish. It sends a PINGRESP
Message to rearm the timer.

If the Broker don’t receive a


Message from the Client during
the Keep Alive interval :
 The Client is disconnected

13
1
DAMERGI Emir – INSAT 2021

MQTT : Subscription

• A Subsrciption identifies an Logical Attachment of a client to a Topic.

• A Subsrciption can only be done when a Session is already available.

• A Subsrciption is created with a SUBSCRIBE message.

• A Subsrciption is Terminated with UNSUBSCRIBE Message.

• A Subsrciption can continue to exist even outside a session lifetime

(However without receiving messages). “See next slide”


13
2
DAMERGI Emir – INSAT 2021

66
MQTT: Clean session Bit & Session Persistance.

In case of connection loss, the session is terminated.

Clean Session Bit =1 :


• the server discards any previous information about the client (clean new session)
• The Client must connect and Subscribe again to receive published messages (all messages published
during session interrupt are deleted)
 Non persistent Session  Transient Subscription (ends with session)

Clean Session Bit = 0:


• the server keeps previous information about the disconnecting client.
• The Server stores QoS1 and QoS2 messages for this client.
• When the client reconnects (without subscribing again), it receives the stored messages from the server.
 Persistent Session  Durable Subscription (does not end with session)

13
3
DAMERGI Emir – INSAT 2021

MQTT: Last Will.


In case of an unexpected client (publisher) disconnection. The Subscribers are not notified and are not
aware of this disconnection.
 When establishing a connection to the Server, Clients can specify a Will Message (for a given Topic).
 This message will only be sent to all subscribers (a given topic) when the Client disconnects
unexpectaedly

13
4
DAMERGI Emir – INSAT 2021

67
MQTT: ESP32 Programming (with arduino)

Declarations

Library containing MQTT related objects and methods

AP Parameters. The AP must be connected to internet

MQTT Account parameters

The Device will be a Wifi client

Create an MQTT Client.


It is attached to the Wifi Client.

13
5
DAMERGI Emir – INSAT 2021

MQTT: ESP32 Programming (with arduino)


Setup

Connect to AP

Set MQTT Server Parameters +


Callback Function (executed when data is received
from Sever)

Connect to MQTT Account.(mqttServer


and mqttPort already specified)

Display Error code if connection fails

13
6
DAMERGI Emir – INSAT 2021

68
MQTT: ESP32 Programming (with arduino)
Loop

Loop: allows the client to process incoming


messages and maintain its connection to
the server

Callback Function The callback function receives the


parameters:
- A pointer to topic (char array)
- A pointer to data payload (char array)
- Length of the payload

It is possible to display the payload (message)

Process the payload:


For example :
If ‘0’  Led OFF
If ‘1’  Led ON 13
7
DAMERGI Emir – INSAT 2021

MQTT: ESP32 Programming (with arduino) – PubSubClient Methods


PubSubClient uses MQTT 3.1.1 by default.

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

MQTT: ESP32 Programming (with arduino) – PubSubClient Methods

It can only publish QoS 0


messages.

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

BLE: Bluetooth Low Energy


“ BlueTooth Smart”

71
BLE: vs Classic Bluetooth

Classic Bluetooth can handle high data volumes (File


Transfers, Audio Streaming, etc) but quickly consumes battery
life. It was not designed (2000) for devices with hard autonomy
constraints, but for devices which can be daily recharged ( PCs,
Smart Devices). Harald BlueTooth
Connection Times can last up to hours.

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

BLE: vs Classic Bluetooth

14
4
DAMERGI Emir – INSAT 2021

72
BLE: vs Classic Bluetooth

Classic Bluetooth Bluetooth Low Energy


range(theoretical max.) Up to 100m in outdoor. Up to 100m in outdoor.
(800m for BLE 5.0)

Over the air data rate Up to 3 Mbit/s 1 ( 2 Mbit/s ,BLE 5.0)

Application throughput Up to 2.1 Mbit.s Up to 0.3 Mbit/sec (1.3 BLE 5.0)

Latency(from a non- Typically 100 ms 6 ms


connected state)

Voice, video, steaming yes No *

Power comsumption 1 W as the reference 0.01 to 0.5 W

Peak Current < 30 mA <15 mA

*) 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

Layer Profile Profile


i+1 A B
In Bluetooth and BLE, the profile
concept is used extensively
Layer Protocol X
i

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.

Profiles can be considered of as purpose-specific constraints put on the use of the


underlying multi-purpose protocols, where such constraints help to ensure interoperability
between devices using the same profile.

14
6
DAMERGI Emir – INSAT 2021

73
BLE: Protocol Stack

SM :It offers services to other


protocol stack layers in order to
securely connect and exchange
data between BLE devices GATT/ATT can be considered as
(device pairing, key the Data Plane (data Formats,
distribution). organization, ..)

GAP/LL can be considered


as the Control Plane
L2CAP :This layer offers data
(Advertising, connection
encapsulation services to
handling, …)
upper layers. This allows
logical end to end data
communication.

Physical Layer : Specifies The modulation/demodulations


operations, the frequency band and hopping techniques, , the data
rates (1 Mbps for Bluetooth v4.2 & 2 Mbps (Bluetooth v5.0)
14
7
DAMERGI Emir – INSAT 2021

BLE: Physical Layer


 RF Specifications
 Operates in 2.4GHz ISM band
 40 channels with 2MHz spacing
 3 fixed advertising channels for Advertising/Broadcasting, which avoid 802.11 interference
 37 adaptively frequency hopped dynamic data channels

Wifi Channel 1 Wifi Channel 6 Wifi Channel 11

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

BLE: GAP Roles

- 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)

From Host A GAP Layer From Host B GAP Layer


”Central” Role desired ”Peripheral” Role desired
+params + params

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

BLE: Connection Parameters


• Slave latency: The number of connection events that a slave can choose to skip without risking a
disconnection. (<500)
 The Slave (Generally a Smart Device) can then save power.

Connection
Interval

• Connection supervision timeout: The maximum time between two received valid data packets
before a connection is considered lost. (max 30 sec)

(1+Slave Latency)* Connection Interval *2 < Supervision Timeout < 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

GATT profile describes how elementary data entities


(called Attributes) are organized and structured
together to obtain meaningful information structures
with high level semantics (called Services,
Characteristics). GATT is built on the top of ATT

ATT (Attribute protocol) is a low-level layer that


defines how elementary data (Attribute) is represented
Attribute Protocol is in a BLE server database and the methods by which
that data can be read or written.

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

BLE: ATT Attribute Structure

A 16 bits unique The Attribute Value holds Determine whether an

Identifier for each of the data that the server attribute can be read or

the attributes on the wants to expose. written to, whether it

server. Its length is variable and has can be notified or


a format that changes based indicated .
on the attribute type.

15
6
DAMERGI Emir – INSAT 2021

78
BLE: GATT Services

GATT profile organizes data stored on the Server and


accessed by the GATT Client as a set of services
- A Service contains, at least one, or
more characteristics. (They are
- A characteristic owns none or more descriptors.
(they are optional)

Rq: Each Element of the Data (Service, characteristic, Descriptor) is an


Attribute

Handle UUID Value Permission

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

BLE: Application GATT profiles


Profile Profile
“Heart Rate” “Blood Pressure” On the GATT profile, there are pre-defined profiles.
They are even more specialized than GATT and define
GATT Services UUID, values, and characteristics

15
8
DAMERGI Emir – INSAT 2021

79
BLE: GATT/ATT Roles
Request / Command

Responses (data, notification)

• It exposes the data it controls or • It interfaces with the server with


contains. the purpose of reading the server’s
• It is the device that accepts exposed data or controlling its
incoming commands from peer behavior.
devices and turns out other types • It sends out commands and
of packets such as responses, requests and listens for notifications
notifications and indications. and indications from a server.
• It is generally the smart device • It is generally a Smart phone (or PC)

15
9
DAMERGI Emir – INSAT 2021

BLE: GATT/ATT & GAP /LL Roles

Even if GATT/ATT and GAP/LL roles are independent.


the default GAP/LL configuration (in programming IDEs such as Arduino ) when configuring a Smart
devices as GATT server is : GAP Peripheral / LL Slave

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

Set Characteristics Initial Values (can be modified)


Start Service

16
1
DAMERGI Emir – INSAT 2021

BLE: ESP32 BLE Server Programming - 2

Setup continued .. Advertising the service

16
2
DAMERGI Emir – INSAT 2021

81
Smart Devices
Connectivity

Link Budget & Power Consumption

Wireless Link Budget (Bilan de Liaison)

𝑃 : Receive Signal Power [dBm]

𝑃 : Transmit Power [mW, dBm] 𝑃 , =𝑃 , − 𝐹𝑆𝐿

𝑃 , = 10 ∗ 𝑙𝑜𝑔 𝑃 , /1𝑚𝑊 ) 𝑃 = 1𝑚𝑊. 10


,
,

𝐹𝑆𝐿 = 40𝑑𝐵 + 20 log 𝐷

FSL: Free Space Loss


D: Distance in meter
(Rq: For 2.4 GHz: Wifi, BlueTooth)

Receiver Sensitivity [dBm]: is the weakest


RF signal level that a radio needs receive in
order to demodulate and decode a packet.

It is recommended to have :

𝑃 , ≥ 𝐒𝐎𝐌 + 𝑅𝑒𝑐𝑒𝑖𝑣𝑒𝑟 𝑆𝑒𝑛𝑠𝑖𝑡𝑖𝑣𝑖𝑡𝑦 [𝑑𝐵𝑚]

𝐒𝐎𝐌: 𝐒𝐲𝐬𝐭𝐞𝐦 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐧𝐠 𝐦𝐚𝐫𝐠𝐢𝐧

16
4
DAMERGI Emir – INSAT 2021

82
Wireless Link Budget : example

𝑃 : Transmit Power [mW, dBm] 𝑃 : Receive Signal Power [dBm]

𝑃 , = 10 ∗ 𝑙𝑜𝑔 𝑃 , /1𝑚𝑊 ) 𝑃 , =𝑃 , − 𝐹𝑆𝐿

𝐹𝑆𝐿 = 40𝑑𝐵 + 20 log 𝐷

FSL: Free Space Loss


D: Distance in meter
(Rq: For 2.4 GHz: )
,
𝑃 , = 1𝑚𝑊. 10
𝑃 , ≥ 𝐒𝐎𝐌 + 𝑅𝑒𝑐𝑒𝑖𝑣𝑒𝑟 𝑆𝑒𝑛𝑠𝑖𝑡𝑖𝑣𝑖𝑡𝑦 [𝑑𝐵𝑚]

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

A Battery is characterized by its capacity A Smart Device is characterized by its


(mA H) average current consumption per hour (mA)

Smart Device Autonomy [H] =


Battery Capacity / Smart Device Average Current Consumption

16
6
DAMERGI Emir – INSAT 2021

83
Output Power/Power Consumption (ESP32 and UBLOX)

16
7
DAMERGI Emir – INSAT 2021

ESP32 BLE Server Power Consumption


When a Smart Device is configured as a GATT Server (LL Slave), it wakes up only during
connection events and is idle the rest of the time (no power consumption)

X: Time (5 msec/unit) Y: Current (25mA/unit)

Event Activity Time

16
8
DAMERGI Emir – INSAT 2021

84
16
DAMERGI Emir – INSAT 2021
9

85

You might also like