H2: IoT Hardware for a 12-Factor Application

Make no mistake sensors will drive the next-generation user experiences. We’ve just got started with wearables.

- Karthee Madasamy, MD, Qualcomm Ventures

_The previous blog is at: “_H1: Deciphering the 12 Factor Applications style

Introduction

The Internet of Things (IoT) is a term that refers to the network of devices that connect to each other via the internet. It involves connecting commonplace items or things to the internet via sensors and IP addresses. Anything from your shoes to your automobile to your coffee pot may be considered an everyday object. Connecting these gadgets to the internet allows data and user analytics to be extracted. The data and analytics gathered may be utilised for a variety of purposes, including surveys, automation, alerts, and just improving user experiences.

This project is primarily a hardware device with a hint of IoT. We have two systems: a frontend system, which will be our hardware device, and a backend system which will be our server. Our IoT setup will constitute an in-house JKUAT SES development board which is based on the ESP32 WROOM chips(i.e Any ESP32 chip can therefore be used as an alternative) and a DHT11 module as our temperature and humidity sensor. With this, we can send temperature and humidity data to our backend services for monitoring purposes.

For connectivity, the ESP32 chip has an in-built WIFI module that allows us to go into production by deploying the device to homes given that most homes have WIFI installed.

Project Stack and Components

JKUAT SES development board

The schematic and the board layout of JKUAT SES development can be found here. In case you are using any other ESP32-based board, the following two schematics can be helpful.

ESP32 pinout

DHT11

To detect water vapour, the electrical resistance between two electrodes is measured by the DHT11. A moisture-holding substrate with electrodes placed to the surface serves as the humidity sensor component. When water vapour is absorbed by the substrate, ions are released, increasing the conductivity between the electrodes. The relative humidity affects the change in resistance between the electrodes. The resistance between the electrodes reduces as the relative humidity rises, whereas the resistance rises as the relative humidity falls.

The DHT11 has a built-in thermistor that monitors temperature. The calibration coefficients are stored and controlled by an IC on the device.

MQTT

The Internet of Things (IoT) has gone from zero to pervasive hype in a very short period. Message Queuing Telemetry Transport, or MQTT, is, I believe, one of the most critical elements affecting the condition of IoT today.

Message Queuing Telemetry Transport (MQTT) is a lightweight messaging protocol designed for usage in situations where clients require a minimal code footprint and are linked to unreliable networks or networks with restricted capacity. It’s mostly utilised for M2M (machine-to-machine) communication and Internet of Things connectivity.

MQTT was developed in 1999 by Dr Andy Stanford-Clark and Arlen Nipper. The communication method’s original aim was to allow monitoring equipment in the oil and gas sector to communicate data to remote servers.

MQTT uses a PUSH/SUBSCRIBE architecture to run on top of TCP/IP. There are two sorts of systems in MQTT architecture: clients and brokers. The server with which the clients communicate is known as a broker. Client messages are received by the broker, who then forwards them to other clients. Clients connect to the broker rather than communicating directly with one another.

When a client (known as a “publisher”) wants to spread information, it will publish to a specific topic, and the broker will then send that information to any customers (known as “subscribers”) that have subscribed to that subject.

Any client can be both a publisher and a subscriber. The clients are usually unaware of each other and are only aware of the broker who acts as an intermediary. The “pub/sub model” is a popular term for this structure.

MQTT also reduces transmissions by using a well-defined, compact message structure. In comparison to HTTP, each message has a fixed header of only 2 bytes.

OTA

Instead of needing the user to connect the ESP32 to a computer via USB to execute the update, OTA programming allows the user to update/upload new software to the ESP32 via Wi-Fi.

When there is no physical access to the ESP module, the OTA capability comes in handy. It helps to cut down on the time spent upgrading each ESP module during maintenance.

One of the most useful features of OTA is that it allows a single central location to deliver an update to many ESPs on the same network.

The only drawback is that you must include an additional OTA code with each drawing you publish to use OTA in the next update.

Ways To Implement OTA In ESP32
There are two ways to implement OTA functionality in ESP32.

1. Basic OTA — Over-the-air updates are sent through Arduino IDE.
2. Web Updater OTA — Over-the-air updates are sent through a web browser.

On an OTA architecture, there are two key components:
1. The remote device is in charge of checking for updates, downloading the new version, and installing it on its system.
2. The cloud server is in charge of creating, delivering, and managing updates to linked devices.

Security is key in IoT and we would need a way to verify that the binary file being downloaded is the correct file and not malware. This can be done by both systems, IoT devices and the Server. In this case, we use the server and ensure the communication is encrypted in between. This will reduce the IoT device work while sending the work to the server.

Circuit

The DHT11 has the following connecting wires:
- GND is a common ground for both the dht11 and microcontroller.
- 5 V is a positive voltage that powers the dht11.
- Control transmit data

Wiring the DHT11 to the SES development board is easy:

1. Connect the VCC pin(Red wire) to the 5V on the SES development board.
2. Connect the GND pin(Black wire) to the ground. 
3. Connect the Data pin(Purple wire) to pin 15(i.e You can use any GPIO pins as the Data Pin)

Code

Credentials

Set the Wifi, and MQTT credentials to publish messages to the MQTT server.

Hardware integration code

Libraries

Here we import the necessary libraries we will need for our project. Some of these libraries cannot be found in the default setup. If that is the case, you can install them in your setup by going to Sketch > Include Library > Manage Libraries and search for the library name as follows:

Declaration, definition and initialization

Declare, and define variables and initialize classes.

setup function

Loop Function

Handle server requests

Get download link

Download binary firmware

Setup WIFI function

Reconnect function

Subscription

How you handle the reception of data all depends on your level of expertise. One could set up a decoy MQTT server in their local station device 
and tap into the data from the open port for their own application or just simply:

Configure HiveMQ browser client to visualize the data in the browser

Procedure:
1. Go to this [link](http://www.hivemq.com/demos/websocket-client/) and click on connect button.
2. Add the subscription topics one for each topic the ESP32 uses.

As soon as the ESP32 client start publishing data to the one topic in our code you should be able to see the changes in the page under the messages.

Important

The code provided above uses the Serial library for debugging purposes. The whole setup will only operate with the Serial port open.
For deployment, comment out all the Serial instances in the code above.

The whole code can be found here

As an IoT product there’s nothing like the excitement of getting the first version of the product out — it gives one that adrenaline pump.

- Bipin RR, VP Digital Services, Tata Elxsi

If you liked this article, click the👏 multiple times below so other people will see it here on Medium.

Let’s be friends on Twitter. Happy Coding 😉

Acknowledgements

1. Kelvin Gitu
2. Washington Kamadi
3. Calvin Mwadime

References

1. https://blackd0t.gitbook.io/africastalking-eris-devkit-playground/arduino/dht11
2. https://lastminuteengineers.com/esp32-ota-web-updater-arduino-ide/
3. https://microcontrollerslab.com/esp32-ota-over-the-air-updates-asyncelegantota-library-arduino/
4. https://www.survivingwithandroid.com/esp32-mqtt-client-publish-and-subscribe/
5. https://www.allacronyms.com/MQTT/Message_Queuing_Telemetry_Transport
6. https://horasvilla.com/iot-and-iiot-what-is-the-difference-between-them/

NEXT BLOG SERIES: H3: First half of the 12-factor app