SmartDevices Chap1-2
SmartDevices Chap1-2
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.
2
DAMERGI Emir – INSAT 2021
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
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
Smart Devices: IoT Edge and Endpoint Devices
Internet Access
The Edge
Gateway
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
Smart Devices
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….
• 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
B A A B
E
Chapter 2,3 and D (Arduino)
D
C
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
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
16
DAMERGI Emir – INSAT 2021
Smart Devices : Computing Systems – Socs/MCUs
17
DAMERGI Emir – INSAT 2021
Smart Devices : Computing Systems - Boards
NodeMCU (ESP8266)
NodeMCU (ESP32)
18
DAMERGI Emir – INSAT 2021
Smart Devices : Debug Access Port and In-System Programming (ISP)
IDE PC Target
Source Flash Mem
Code Processing (Code)
device
22
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
Smart Devices : Arduino IDE
25
DAMERGI Emir – INSAT 2021
Smart Devices : Arduino Sketchs Structure
26
DAMERGI Emir – INSAT 2021
Chap 2
Smart Devices : Context Awareness
Emir DAMERGI
• Digital I/O INSAT 2021/22
• Serial (UART) Communication
• Analog Data Acquisition (Analog Sensors)
• Digital Data Acquisition (Digital Sensors)
• Data Storage
Smart Devices
Digital Input /output
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
30
DAMERGI Emir – INSAT 2021
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
31
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
32
DAMERGI Emir – INSAT 2021
Digital I/O: power Interface
~ mA
5V
SoC pins can only support low current (~30 mA max) and Voltage (5V).
33
DAMERGI Emir – INSAT 2021
Smart Devices
UART Communications
USART:
Synchronous
Asynchronous
Bluetooth
Zigbee
TX
Wifi
RX
GPS
PC
Terminal
TX USB/Serial Converter
serial USB
(CH340, PL2110, ST-
Link)
RX
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…
38
DAMERGI Emir – INSAT 2021
UART Communications: Sending Data
39
DAMERGI Emir – INSAT 2021
UART Communications; Receiving Data
char rc;
rc = Serial.read();
receivedChars[nbr] = rc;
nbr++;
}
40
DAMERGI Emir – INSAT 2021
Smart Devices
Analog Sensors
Processing Analog quantities ?
Processor
Sampling
Analog Digital
Conversion
Quantization
DAMERGI Emir – INSAT 2021 42
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
time
Fourier Transform
frequency
Fmax
2 = 00..010
1 = 00..001
0 = 00..000 Vin
Vref- Vref- Vref +
Vref- Vref+
- ΔQ Emir – INSAT 2021
+ΔQ +2*ΔQ DAMERGI 45
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
47
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino Programming
A SoC with:
• D12 conneceted to a LED.
• D3 connected to a Button (Pressed =LOW)
• A1 connected to an TMP36 (Temperature Sensor)
• ADC with 10 bits max resolution, Vref++5V, Vref-=0V
48
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino Programming
Vref-= 0V
0,5
ΔQ = (Vref+ - Vref-) / 2N = 5000/1024
49
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino Programming
𝑁𝑖𝑛 ∗ 5000
( ) − 500
𝑇𝑒𝑚𝑝 𝑚𝑉 = 1024
9,33
50
DAMERGI Emir – INSAT 2021
Analog I/O: Arduino programming (PWM)
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
51
DAMERGI Emir – INSAT 2021
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
53
DAMERGI Emir – INSAT 2021
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
Registers
Digital Sensors contain some Registers used to store
ADC
• A/D Converter and Communication configuration
Analog
• Conversion Results (binary data)
Sensor
Digital Sensor
55
DAMERGI Emir – INSAT 2021
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).
56
DAMERGI Emir – INSAT 2021
Smart Devices
Digital Sensors : SPI
Serial Connectivity: SPI
SPI MASTER SPI SLAVE
SPC
SDI
SDO
/SS
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 60
Serial Connectivity: SPI
SDI / Ctrl + 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 61
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 63
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);
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.
Bit 0 of CTRL_REG2 = 1
to start a Temperature
conversion
Bit 0 of STATUS_REG = 1
Conversion Finished.
New Values available.
• 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)
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
Restart Condition
Without Stop
1 Fixed 7 bits
Address:
2 configurable 7
1001 100 bits Addresses: 8 configurable 7
bits Addresses:
0011 1xx
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);
MySensors_I2C.beginTransmission (Slave_Addr);
MySensors_I2C.write(Reg_Addr);
MySensors_I2C. endTransmission(false);
MySensors_I2C. endTransmission(true);
Bluetooth BLE
GPS Module
Module 4G/LTE
LORA Module Module
SDA = 21 LIS3DSH is a 3 axis accelerometer MEMS sensor with +/- 6G Full Scale.
SCL = 22
• The results for each axis are 16 bits integer and are stored in 2
registers (2’s complement format).
• WHO AM I Register (Generally exists in each I2C device) and helps
identifying the device. For the LIS3SDH the value is 0x3F
• 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)
81
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation : File System for ESP in Arduino
82
DAMERGI Emir – INSAT 2021
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
83
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.
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. 84
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation: JSON Format
{ ‘’ParamName1’’ : Value1 ,
{ "firstName": "INSAT",
"lastName": "INSAT",
"age": 25,
},
86
DAMERGI Emir – INSAT 2021
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 ]
87
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
}
}
88
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation : JSON Deserialization
89
DAMERGI Emir – INSAT 2021
Data Storage / Manipulation : JSON Serialization
90
DAMERGI Emir – INSAT 2021