The project shows how to create a simple application using the RIOT operating system to measure temperature and relative humidity. We use a DHT22 digital sensor in combination with an STM32 Nucleo-64 F401RE development board.
Wiring the hardware componentsThe DHT22 digital sensor has three pins:
- connect the
GND
pin with theGND
pin of the STM32 Nucleo board, - connect the
VCC
pin with the5V
pin of the STM32 Nucleo board, - connect the
DAT
pin with theD2
pin of the STM32 Nucleo board.
To create a new application you need to create a directory containing two files: (1) the Makefile and (2) the main.c file.
At minimum the Makefile should define the following basic aspects of the application:
- The name of the application (
APPLICATION
) - The location of the RIOT base directory (
RIOTBASE
) - Additionally, it is required to include the
Makefile.include
from theRIOTBASE
# A simple name of the application
APPLICATION = temperature_humidity
# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../../RIOT
include $(RIOTBASE)/Makefile.include
Within the Makefile we also define that the application will use the RIOT device driver for the DHT22 digital sensor:
USEMODULE += dht
In addition we will also use the FMT module to easily convert the values received from the sensor as 16-bit fixed point numbers into a decimal string.
USEMODULE += fmt
The above are enough to setup a new application. More details can be found on the RIOT documentation.
The next step is to create the main.c
file where the main code of the application is defined.
First we include the headers of the FMT and DHT modules and also the definition of the dht_params
structure that we will use to configure the device driver. We also include stdio.h
for printing debug messages.
#include <stdio.h>
#include "dht.h"
#include "dht_params.h"
#include "fmt.h"
The DHT22 sensor is connected to the Arduino connector D2. For the STM32 F401RE MCU this translates to PA10 pin. So the device driver is configured and initialized as follows:
dht_params_t my_params;
my_params.pin = GPIO_PIN(PORT_A, 10);
my_params.type = DHT22;
my_params.in_mode = DHT_PARAM_PULL;
dht_t dev;
if (dht_init(&dev, &my_params) == DHT_OK) {
printf("DHT sensor connected\n");
}
else {
printf("Failed to connect to DHT sensor\n");
return 1;
}
The next step is to poll the digital sensor for temperature and relative humidity values.
int16_t temp, hum;
if (dht_read(&dev, &temp, &hum) != DHT_OK) {
printf("Error reading values\n");
}
The last step is to convert the temperature and relative humidity values retrieved from the sensors as 16-bit fixed point numbers into a decimal string:
char temp_s[10];
size_t n = fmt_s16_dfp(temp_s, temp, -1);
temp_s[n] = '\0';
char hum_s[10];
n = fmt_s16_dfp(hum_s, hum, -1);
hum_s[n] = '\0';
Now we will simply print these values to the debug output:
printf("DHT values - temp: %s°C - relative humidity: %s%%\n",
temp_s, hum_s);
Compiling the code and flushing the deviceTo compile the application, program the STM32 board and connect through the USB to receive debug information, we execute the following command:
make BOARD=nucleo-f401re flash term
Here is what we should expected as output:
main(): This is RIOT! (Version: 2021.04-devel-200-g67e5a)
DHT sensor connected
DHT values - temp: 22.3°C - relative humidity: 60.5%
More details on the code and insights on how the different modules of RIOT are used in this application can be found on the GITHUB repository.
Comments