Projects
MQTT

MQTT

APIs are a synchronous form of communication where the requesting application (the client) must wait for the API to complete the requested action and return a response, before the client can continue its processing. While this works very well in regular applications, in scenarios where low-powered microcontrollers with limited data bandwidth have to send data over the Internet to a processing service, a synchronous API may be inefficient.

The alternative to APIs is an asynchronous form of communication such as MQTT which is used by IoT platforms. We will be using MQTT for all sensor data to be sent by the microcontrollers to the processing system.

IoT Platforms

IoT platforms serve multiple functions, the primary one being to receive the data from microcontrollers over the MQTT protocol. In addition, they may validate the data to ensure it is being published by authorized microcontrollers and in the right format, before sending them on for further processing.

The microcontroller boards will receive the data from the sensors attached to it and publish the raw data (without any pre-processing) to an MQTT Broker. The data from the MQTT Broker will be read by a subscribing microservice and written to a NoSQL DB and the Smart Contract as required.

Every microcontroller and sensor used in the solution will have an id programmed into it. The mapping of microcontrollers and sensors will be stored in the NoSQL DB. The microcontrollers used for an order will also be mapped in the NoSQL DB. This mapping will be used to validate the data received and to filter out invalid data before it is sent to the Smart Contract.

Microcontrollers will send data to the MQTT Broker at a predefined frequency.

AWS IoT Platform

There are multiple IoT platforms, each work in different ways. We will be using the AWS IoT platform as the MQTT broker and receive data from the microcontroller and to make it available to a Lambda function to process to save it to the database and Smart Contract. AWS provides a seamless integration between its MQTT Broker and Microservices, without requiring an API.

Things, IoT Rules, and Topics

Microcontrollers will publish data to the IoT platform using the MQTT protocol. To use AWS IoT Core, each microcontroller must be registered as a Thing.

The following three Things must be registered:

i. The Loading microcontroller.
ii. The On Vehicle microcontroller.
iii. The Unloading microcontroller.

AWS IoT Core works with the approach of rules that are used as triggers to execute a function when data is published from a microcontroller to a Topic. With this approach, there is no process that “subscribes” to the topic as in a traditional MQTT architecture.

In this implementation, we will be using AWS Lambda functions to process the data. There will be a single topic and a single rule configured to invoke a Lambda function which will read the published data and pre-process the data before storing it in the database and sending it to the smart contract.

The diagram below illustrates how MQTT and Lambda functions work together:

MQTT and Lambda Integration

The list of rules and the steps to set up the AWS IoT platform and create rules will be shared with subscribers.