Internet of Things, Publish-Subscribe and MQTT
In questa pagina 5
This note introduces the Internet of Things (IoT), the devices that form it and the operating systems that run on them, then the publish/subscribe communication model and the way MQTT uses it. Application scenarios and the communication technologies are in Internet of Things and Application ScenariosThe Internet of Things joins ubiquitous computing (objects that sense, decide and act) with Internet connectivity (objects that transmit, use cloud computing and network information); an IoT system has nodes, a network and a back-end, and is applied in smart city, home, mobility, health, industry, farming and grid scenarios.Internet of Things and Application Scenarios → and Data Transmission Technologies - Wired, Wireless, LAN and WANSurvey of the data transmission technologies used to connect measurement and IoT nodes, from local ones (RFID, NFC, Bluetooth, WiFi, ZigBee) to wide-area ones (LoRa, SigFox, GSM/GPRS, UMTS, LTE, LTE-M, NB-IoT, 5G), with frequencies, ranges and bit rates.Data Transmission Technologies - Wired, Wireless, LAN and WAN →; the request/response alternative for the Web is in ReST Web ServicesA web service lets programs call a service over a network with standard messages carried by HTTP; data are written in XML (nested tags, attributes, namespaces, XSD validation) or JSON (RFC 8259: objects, arrays, strings, numbers, true, false, null; more compact); SOAP is an XML message protocol (Envelope, optional Header, Body, Fault) sent with an HTTP POST and described by WSDL; REST is an architectural style in which the service exposes resources identified by URIs and manipulated with HTTP methods (GET reads, POST creates, PUT replaces, PATCH modifies, DELETE removes) with meaningful status codes (200, 201 with Location, 204, 400, 404, 409), stateless, cacheable, with a uniform interface.ReST Web Services →.
Embedded systems and microcontrollers
Definition (embedded system). An embedded system is the use of electronics and software inside a product that has a specific function, or set of functions, as opposed to a general-purpose computer. It is any device with a computing chip that is not a workstation, a desktop or a laptop.
Examples: phones, cameras, smart TVs, washing machines, microwave ovens, home security systems, automotive, sensors and actuators in automatic systems. They are often tightly coupled to the environment, which gives real-time constraints: speed of movement, measurement accuracy and duration decide the timing of the software operations.
A microcontroller (MCU) integrates processor, memory and peripherals:
- 8, 16 or 32-bit architectures;
- limited RAM (about KB) and EEPROM or flash (from KB to MB);
- usually no MMU (so no virtual memory); some (ARM Cortex-M3 and M4) have an MPU (memory protection unit);
- speed of about MHz;
- many I/O and communication interfaces, and timers;
- consumption of about mW.
A deeply embedded system has a processor whose behaviour is hard to observe both for the programmer and the user: it uses a microcontroller, is not reprogrammable once the logic is burned into ROM, and has little or no user interaction. These are single-purpose dedicated devices: 1) they sense something in the environment, 2) do basic processing, 3) do something with the result. They often have wireless capability and live in sensor networks over wide areas; the IoT depends heavily on them. They have extreme constraints of size, memory, time and power.
The Internet of Things
The IoT is the growing (short-range) interconnection of intelligent embedded devices, from household appliances to small sensors. They provide information from sensors, act on the environment and sometimes modify themselves, to manage a larger system (a factory, a city). They are mostly deeply embedded devices, with low power, low bandwidth and little data, communicating with each other; there are also security cameras, VoIP phones and others with high-bandwidth streaming.
Four generations of deployment: 1) IT (PCs, servers, routers, firewalls; wired); 2) OT (operational technology: machines with embedded IT built by non-IT companies, for instance medical machinery, kiosks; wired); 3) personal technology (smartphones, tablets, e-readers; wireless of various kinds); 4) sensor and actuator technology: single-purpose, wireless devices that are part of larger systems: this is the IoT proper.
Architecture levels: edge (network of devices with sensors and actuators, connected by a gateway to the upper communication network), fog (pre-processing near the sensors of the mass of data they produce), core (backbone connecting local networks to high-performance servers and databases), cloud (storage and processing of IoT data and applications that manage the devices).
Constrained devices
Most IoT devices have few resources (memory, computation, network), no MMU and low power: they are constrained devices. Many have 8 or 16-bit microcontrollers with little RAM and memory and low-speed wireless connectivity. Devices become cheaper, smaller and more efficient, but not necessarily more powerful. IETF classes:
| class | resources | example |
|---|---|---|
| 0 | less than 10 KB of RAM and 100 KB of flash | "mote" or smart dust sensors spread over a region, collecting data and passing it on to a central point (temperature sensors on a farm) |
| 1 | about 10 KB of RAM and 100 KB of flash | constrained in code and computation, they do not easily talk to Internet nodes but use dedicated protocols (CoAP) without a gateway; with routing and security functions |
| 2 | more resources | small gateways, which talk to computers but need lightweight, low-power protocols |
Components of an IoT device: microcontroller (the "intelligent" part), sensor (measures a physical, chemical or biological parameter and produces an analog or digital signal), actuator (receives a signal and acts on the environment), transceiver (usually wireless; with RFID: tags and readers).
Requirements of an operating system for the IoT
- Little memory: many orders of magnitude less than smartphones, tablets and large embedded devices.
- Heterogeneous hardware: 32-bit ARM dominates, but constrained devices are still 8 and 16 bit.
- Network connection: a stack with many low-power techniques and protocols (LoRaWAN, BLE).
- Energy efficiency: they often have to run for years on one charge; low-power modes (deep sleep), because many tasks are periodic.
- Real time: real-time data flows from sensors (surveillance), devices talking to each other (robots in a warehouse), immediate response to safety events (car brakes).
- Security: mechanisms and updates at application level (antivirus), network level (authentication) and device level (integrity validation).
Typical architecture: system and support libraries (shell, log), minimal drivers and file system, a low-power network stack (also IP), the kernel (scheduler, synchronisation, mutual exclusion, timers), and a HAL (hardware abstraction layer) with a consistent API for GPIO, UART, SPI, I2C.
Operating systems for constrained devices
There are two routes: adapt an existing OS (embedded Linux; Clinux for devices without MMU, with a single address space and no fork), or use a purpose-built OS. Embedded Linux is typically not suitable for microcontrollers and deeply embedded systems, nor for hard real-time constraints; small RTOSs (RIOT, FreeRTOS) suit constrained devices better.
| Contiki-NG | RIOT | Zephyr | FreeRTOS | |
|---|---|---|---|---|
| architecture | monolithic, event-driven | microkernel | monolithic | small kernel |
| scheduling | cooperative (protothreads) | preemptive fixed-priority, tickless | preemptive, priority-based | preemptive priority plus round robin, tick-based |
| MCU | 32 bit | 8, 16, 32 bit | 32 bit only | many |
An experimental comparison with all services and the network stack gives a RAM requirement of about 29.8 KB (Contiki-NG), 33.0 KB (RIOT) and 52.9 KB (Zephyr) and flash of 50.6 KB, 59.9 KB and 130.5 KB: Contiki-NG and RIOT fit a class 1 device, Zephyr requires class 2. RIOT in its minimal configuration (core, cpu and board on an Arm Cortex-M) needs 3.2 kB of ROM and 2.8 kB of RAM; its scheduler is tickless: when no other task runs, the idle thread (lowest priority) puts the system to sleep until an external event wakes it up, which saves the maximum energy. In RIOT a lower priority number means higher priority.
The publish/subscribe model
Three roles: the publisher produces data, the subscriber subscribes to receive it, the broker is the intermediary that records the subscriptions and forwards each published event to those interested.
publisher --- publish(topic, data) ---> BROKER --- notify(topic, data) ---> subscriber A
^ \-> subscriber B
subscriber A, B --- subscribe(topic) ---+The model works like a hardware interrupt: the subscriber does not ask repeatedly (polling), it registers once and receives asynchronous notifications when the event happens. Publisher and subscriber are said to be decoupled:
| decoupling | meaning |
|---|---|
| in space | the publisher does not know the identity and number of subscribers, and vice versa |
| in time | they need not be active together (if the broker keeps the messages) |
| in synchronisation | neither of the two stays blocked waiting for the other |
Advantages: high scalability, efficiently distributed traffic, independent producers and consumers. Disadvantages: one extra step (more latency), the broker is a point of dependence (if it fails, the system fails), reliability and persistence of events must be guaranteed.
Topics and MQTT
In MQTT the subscription is made on a topic, a hierarchical name whose levels are separated by a slash, and the broker forwards a message to every subscriber whose subscription matches the topic. In subscriptions + replaces one level and # replaces all the remaining levels.
Example. A sensor publishes the temperature on the topic weather/padua/temperature; an app subscribes to weather/padua/# and receives every measurement for Padua, while another subscribes to weather/+/temperature and receives the temperature of every city.
The certified sources describe MQTT only through this publish/subscribe model and its topic wildcards, and list it among the protocols for IoT video (together with RTSP) because it is efficient in energy for continuous data. The details of the MQTT protocol itself (packet types, quality-of-service levels, retained messages, keep-alive) are part of the program but are not covered by any certified source, so they are not written here.
Typical mistakes
- Saying that publish/subscribe is a variant of polling: the subscriber registers once and is notified.
- Forgetting that the broker is a single point of dependence.
- Inverting priorities in RIOT (a lower number means higher priority).
Versione ripasso
Embedded systems. Electronics and software inside a product with a specific function (not a general computer): phones, cameras, washing machines, automotive, sensors and actuators. Often coupled to the environment: real-time constraints.
Microcontroller. 8/16/32 bit; RAM in KB, EEPROM/flash from KB to MB; usually no MMU (some have an MPU, ARM Cortex-M3/M4); MHz; mW; many interfaces and timers. Deeply embedded: MCU, not reprogrammable (logic in ROM), little interaction; senses, processes, acts; basis of the IoT; extreme limits of size, memory, time, power.
IoT. Short-range interconnection of embedded devices. Generations: IT, OT, personal, sensors and actuators (the IoT proper). Levels: edge (sensors, actuators, gateway), fog (pre-processing), core (backbone), cloud (storage, applications). Constrained devices (IETF): class 0 (under 10 KB RAM and 100 KB flash, smart dust), class 1 (about 10 KB and 100 KB, CoAP without gateway), class 2 (gateways). Components: microcontroller, sensor, actuator, transceiver (RFID: tags and readers).
OS requirements. Little memory, heterogeneous hardware, low-power networking (LoRaWAN, BLE), energy (deep sleep), real time, security. HAL: GPIO, UART, SPI, I2C. Embedded Linux not for MCUs or hard real time; Clinux: no MMU, one address space, no fork.
RTOS comparison. Contiki-NG: event-driven, cooperative protothreads; RIOT: microkernel, fixed-priority preemptive, tickless (idle thread sleeps; lower number = higher priority; minimum 3.2 kB ROM and 2.8 kB RAM); Zephyr: monolithic, 32 bit only; FreeRTOS: priority plus round robin, tick-based. RAM and flash: Contiki-NG 29.8 and 50.6 KB; RIOT 33.0 and 59.9 KB; Zephyr 52.9 and 130.5 KB: first two class 1, Zephyr class 2.
Publish/subscribe. Publisher, subscriber, broker forwarding events: like a hardware interrupt, asynchronous notifications without polling. Decoupling in space, time, synchronisation. Pros: scalability, efficient traffic; cons: extra latency, dependence on the broker, event persistence.
MQTT topics. Hierarchical names with /; + replaces one level, # all remaining levels. weather/padua/# receives everything for Padua; weather/+/temperature the temperature of every city. Other MQTT details have no certified source and are not written.
Typical mistakes: publish/subscribe as polling; ignoring the broker dependence; inverted RIOT priorities.