IoT-First Assignment
Develop an IoT device using RIOT-OS and connect it to AWS using MQTT-SN
The goal of this assignment is to create a cloud-based IoT system that collects information from temperature and light sensors. The IoT device will be developed using RIOT-OS with STM32 Nucleo-64 F401RE while the cloud-based services will be based on the AWS cloud-based service ecosystem.
System Components
- IoT Device
- MQTT-SN Broker
- MQTT-SN/MQTT transparent bridge
- Cloud-based IoT Backend
- Web-based Dashboard
IoT Device
STM32 F401RE board with light and temperature sensors and LED’s (actuators) are used to develop an IoT device prototype using RIOT-OS.
Hardware components we will use:
· STM32 Nucleo-64 F401RE
· NTC Thermistor + 10 KOhm resistor
· Photocell + 1 KOhm resistor
· Red and RGB LED’s
· Breadboard and wires
Hardware Setup:
Thermistor
Locate the Thermistor and the 10KOhm resistor in series on the breadboard. Use a jumper wire to connect the top connector of the Thermistor with the 5V pin of the STM32 Nucleo board and the bottom connector of the Thermistor — the one which is connected with the 10KOhm resistor — with the A0 pin of the board. Finally connect the bottom connector of the 10KOhm resistor with the GND pin of the board.
Photocell
Locate the photocell and the 1KOhm resistor in series on the breadboard. Use a jumper wire to connect the top connector of the photocell with the 5V pin of the STM32 Nucleo board and the bottom connector of the photocell — the one which is connected with the 1KOhm resistor — with the A1 pin of the board. Finally connect the bottom connector of the 1KOhm resistor with the GND pin of the board.
Red LED
Connect top connector of the RED LED with the D8 pin and the bottom connector with the GND pin of the STM32 Nucleo board. This is used to notify user when the temperature is high.
RGB LED
Connect first connector (Red) of the RGB LED with the D7 pin of the STM32 Nucleo board and the third connector (Green) with the D9 and the forth one (Blue) with D10 pin of the board. Finally connect the second pin (GND) with the GND pin of the board. This is used to notify user for three different light density status.
RIOT — OS :
RIOT is a real-time multi-threading operating system, which supports a range of devices, which are typically found in the IoT.
My RIOT firmware is based on the original example of emcute_mqttsn available in GitHub.Each RIOT application has two files namely main.c and Makefile.
main.c:
Above all the code of the emcute_mqttsn, which is mandatory, it contains the ADC lines initialization and reading the sensors values for both temperature and light sensors. The values of two ADC lines, 0 and 1 will be published every 5 second to the MQTT-SN broker.
High temperature status and light status in term of being high, Normal, and Low will be published to the MQTT-SN broker.
Makefile:
RIOT provides the ethos_uhcpd tool to provide network connectivity to an MCU via the USB connection. It is used in this assignment, as STM32 Nucleo F401RE does not support wireless network interface. The related modules are added in the Makefile.
The ethos_uhcpd tool will use the script setup_network.sh located in the $(RIOTTOOLS)/ethos directory using superuser privileges. This script sets up a tap device, configures a prefix and starts an uhcpd server serving that prefix towards the tap device. The execution of the script is specified within the Makefile.ethos.conf file. In the Makefile we need to include this additional Makefile as follows:
include $(CURDIR)/Makefile.ethos.conf
More information are presented here.
MQTT-SN Broker
We need to set up an MQTT-SN broker where the RIOT-OS application will publish the sensor values over UDP/IPv6. One such broker is RSMB.
How to Setup:
1. Get the RSMB here: https://github.com/eclipse/mosquitto.rsmb
git clone https://github.com/eclipse/mosquitto.rsmb.git
2. Go into the source folder and build the RSMB
cd mosquitto.rsmb/rsmb/src
make
3. Create a config file. In this case, we run the RSMB as MQTT and MQTT-SN capable broker, using port 1885 for MQTT-SN and 1886 for MQTT and enabling IPv6, so save the following to config.conf
:
# add some debug outputtrace_output protocol# listen for MQTT-SN traffic on UDP port 1885listener 1885 INADDR_ANY mqtts ipv6 true# listen to MQTT connections on tcp port 1886listener 1886 INADDR_ANY ipv6 true#MQTT-S outgoing local bridgeconnection local_bridge_to_mosquitto address 127.0.0.1:1883 topic both_directions both topic awsiot_to_localgateway in topic localgateway_to_awsiot out
The last part of the config file is related to the bridge connection specification containing the connection name, address , topics and their data directions , from MQTT-SN to MQTT.
4. Start the broker:
./broker_mqtts config.conf
Networking:
RIOT /MQTT-SN — “tapbr0”
1. Setup tap
and tapbr
devices using RIOT's tapsetup
script:
sudo ./RIOTDIR/dist/tools/tapsetup/tapsetup
2. Assign a ipv6 address to the tapbr0
interface.
sudo ip -6 addr add 2001:db8:0:f101::1/64 dev tapbr0
Nucleo F401RE — “tap0”
I have used “Network setup (NUCLEO side)” procedure has been explained here.
1. Assign of IPv6 address to the tap0
interface.
netif_add(“4”,“2001:0db8:0:f101::5”)
Using the “netif_add” function in the main.c . The device will get the specific IP address through tap0 after flashing the application to the board.
MQTT-SN/MQTT transparent bridge
We need a config file for running the mosquito as a bridge between MQTT-SN in our local environment and the MQTT in AWS IoT Core. For this we have to follow the instructions here .
Hints:
· You should have a private key file to make a SSH connection through a SSH client like “PuTTY”to the Amazon EC2 instance. Generally, you should generate a key pairs and assign it to your EC2 instance while creating it. For example for connecting to your EC2 instance via PuTTY , you should have save the key pairs in “.ppk” format. In addition , you can connect with “Public IPv4 address” you will see in the Amazon EC2 dashboard to connect to it.The username is “ubuntu” if you use ubuntu version.
· You have to create a thing and the three certificates for these things, which they are going to use in the bridge config file. They are listed here:
rootCAPEM encoded client certificatePEM encoded client private key
The thing we create is assigned to a certificate; a policy also is attached to certificate with the following properties:
Policy document:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "iot:*", "Resource": "*" } ]}
· Get value of your AWS IoT Core ATS endpoint by running the following command. It should be replaced in the bridge config file.
aws iot describe-endpoint — endpoint-type iot:Data-ATS
Now we have all the information is needed to run the Mosquito broker.
Run the Mosquito Broker :
Go to the ./etc/mosquitto/conf.d
mosquitto -c bridge.conf
Run the Application
Tap setup
sudo ./RIOTDIR/dist/tools/tapsetup/tapsetup
Assign IP to tapbr0
sudo ip -6 addr add 2001:db8:0:f101::1/64 dev tapbr0
Start Broker
./broker_mqtts config.conf
Make RIOT Application
make flash term //in the application path
Conncet To MQTT-SN Broker
con 2001:db8:0:f101::1 1885 //Give server address and port
Publish / Subscribe to MQTT-SN
sub both_directions
pub both_directions
Run Mosquito Broker/Bridge
mosquitto -c bridge.conf
Messages containing temperature and light density also their status will be published from RIOT to MQTT-SN.
Message will receive in AWS IoT Core :
Amazon Kinesis Firehose — Analytics Tool
Amazon Kinesis Firehose is the easiest way to capture, transform, and load streaming data continuously into AWS from thousands of data sources, such as IoT devices. It is a fully managed service that automatically scales to match the throughput of your data and requires no ongoing administration.
Amazon Kinesis Analytics allows you to process streaming data coming from IoT devices in real time with standard SQL, without having to learn new programming languages or processing frameworks, providing actionable insights promptly.
You have to do the following steps to be able to monitor the IoT device values in the Amazon Kinesis Analytics.
1. Create a Firehose delivery stream and a S3 bucket to assign to it.
2. Create a Rule for it.
3. Create an application.
4. Write a simple SQL query
5. Save and Run the query
-- Create an output stream with four columns, which is used to send IoT data to the destinationCREATE OR REPLACE STREAM "DESTINATION_SQL_BASIC_STREAM" (datetime VARCHAR(32), id INTEGER, temperature INTEGER, temp_status VARCHAR(32) , lightdens INTEGER,light_status VARCHAR(32));-- Create a pump that continuously selects from the source stream and inserts it into the output data streamCREATE OR REPLACE PUMP "STREAM_PUMP_1" AS INSERT INTO "DESTINATION_SQL_BASIC_STREAM"-- Filter specific columns from the source streamSELECT STREAM "datetime", "id", "temperature", "temp_status" , "lightdens" , "light_status" FROM "SOURCE_SQL_STREAM_001";
For better understanding of doing the 5 steps, check here.
The result is presented here :
This is a project for the Internet of Things course of the MSc Engineering in Computer science at the Sapienza University of Rome.
· GitHub repository: link
· LinkedIn: link