IOT

STM32 NRF24L01 Sensor Node with ESP32 NRF24L01 Gateway

Using the NRF24L01 STM32 Board and the ESP32 Wifi Module, we will monitor sensor data wirelessly in this project. We’ll start by building a Wireless Sensor Node. The sensing unit, processing unit, transceiver unit, and power unit are the four essential components of a sensor node. Any Sensor can be used in the Sensing unit. The BME280 Barometric Pressure Sensor is what I’m utilizing. The STM32F103C Microcontroller serves as the processing unit, while the NRF24L01 2.4 GHz Wireless Transceiver Module serves as the transmitter. When it comes to the power source, the device may run on a 3.7V Lithium-Ion battery.

ESP32 Wifi Module and NRF24L01 Module will be used to create a WiFi Gateway. An IoT gateway handles everything from protocol translation to data encryption, processing, management, and filtering. A gateway sits between devices and sensors to connect with the cloud in an IoT environment. The gateway in this project receives sensor data from numerous sensor nodes and uploads it to the cloud via the wifi network. The Thingspeak Server is the cloud server we’re using here. ThingSpeak is an open-source Internet of Items application and API for storing and retrieving data from things over the Internet or via a Local Area Network utilizing the HTTP and MQTT protocols.

Hardware Required:

  • ESP-WROOM-32 ESP-32S Development Board
  • NRF24L01 Module
  • STM32F103C8T6 Bluepill Board
  • BME280 Barometric Pressure Sensor
  • Power Supply
  • Connecting Wires
  • Breadboard

NRF24L01 Module

NRF24L01

The nRF24L01 is a wireless transceiver module, which means it can send and receive data wirelessly. It operates at a frequency of 2.4GHz. This frequency is part of the ISM band and is authorized to use for engineering projects in most countries. When used properly, the modules may span a distance of 100 meters.

The module runs on 3.3V, although its SPI pins can handle up to 5V. Mesh networking can be formed with this module because each module has an address range of 125 and can communicate with up to six other modules. Because the NRF24L01 module communicates via SPI, it may be used with any microcontroller that has SPI pins, such as the STM32 or Arduino boards.

STM32F103C DISCOVERY BOARD

STM32F103C8T6 Discovery Board was designed as a development board that is similar to Arduino in terms of advanced capabilities and accessibility. The STM32 Discovery Board enables the development of high-reliability applications by utilizing an advanced performance microcontroller known as the Arm Cortex-M4 32-bit core. I think you’re familiar with ARM Architecture. It provides versatility and customization, allowing you to experiment with libraries, communication protocols, GPIO pins, and so on.

Sensor Node Using NRF24L01 & STM32F103C

Let’s have a look at the Sensor Node Circuit. With the NRF24L01, we may create a Sensor Node with any microcontroller. The STM32F103C Bluepill Board, for example, is the finest solution. Aside from the microcontroller, any sensor can be connected to the STM32 board. For demonstration purposes, I recommend the BME280 Barometric Pressure Sensor.

The connection between STM32F103C Board, NRF24L01 Wireless Transceiver Module & BME280 Sensor is given below.

  • NRF24L01 VCC ………………………………………… 3.3V of STM32F103C
  • NRF24L01 CSN ………………………………………… PA4 of STM32F103C
  • NRF24L01 MOSI ………………………………………… PA7 of STM32F103C
  • NRF24L01 GND ………………………………………… GND of STM32F103C
  • NRF24L01 CE ………………………………………… PB0 of STM32F103C
  • NRF24L01 SCK ………………………………………… PA5 of STM32F103C
  • NRF24L01 MISO ………………………………………… PA6 of STM32F103C

WiFi Gateway Using NRF24L01 & ESP32

The NRF24L01 Transceiver Module is used by the Gateway to receive data from a single or several Sensor Nodes. It uploads the data to the server after receiving it. The WiFi Network can be utilized to establish a connection with the Server. For designing the WiFi Gateway, the ESP32 and NRF24L01 are the ideal options.

The following diagram shows the connection between the ESP32 Board and the NRF24L01 Wireless Transceiver Module.

circuit ESP32 nRF24L01
  • NRF24L01 VCC ………………………………………… 3.3V of ESP32
  • NRF24L01 CSN ………………………………………… D5 of ESP32
  • NRF24L01 MOSI ………………………………………… D23 of ESP32
  • NRF24L01 GND ………………………………………… GND of ESP32
  • NRF24L01 CE ………………………………………… D4 of ESP32
  • NRF24L01 SCK ………………………………………… D18 of ESP32
  • NRF24L01 MISO ………………………………………… D19 of ESP32

Project PCB Gerber File & PCB Ordering Online

If you don’t want to put the circuit together on a breadboard and instead want a PCB for your project, here is the PCB for you. EasyEDA an online circuit schematics and PCB design program was used to create the PCB Board for the ESP32 NRF24L01 Gateway and STM32 NRF24L01 Node. Below is a picture of the Sender PCB.

g

The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from https://www.nextpcb.com/

  1. Download Gerber File for Node PCB: Sender PCB
  2. Download Gerber File for Gateway PCB: Receiver PCB

Now you can visit the NextPCB official website by clicking here: https://www.nextpcb.com/. So you will be directed to the NextPCB website.

You can now upload the Gerber File and place an order on the website. The PCBs are in excellent condition. That is why the majority of people entrust NextPCB with their PCB and PCBA needs.

Library Installation

1. BME280 Library

The BME280 library allows you to read and understand data from Bosch BME280 environmental sensors through I2C, SPI, or Sw SPI. It calculates the temperature, humidity, and pressure as well as the environment.

Download BME280 Library

2. NRF24L01 Library

The nRF24L01 RadioHead Library is not supported by the STM32 Board (#include RH NRF24.h>). As a result, we’ll need to use the RF24 Library. This library is based on the standard SPI library and is designed to be as compliant as possible with the chip’s intended operation. The library can be downloaded from the following Github link. In the Arduino IDE, add the library.

Download NRF24L01 Library

Setting Up Thingspeak Server

ThingSpeak is a cloud-based IoT analytics tool that lets you aggregate, visualize, and analyze live data streams. You can submit data from your devices to ThingSpeak, generate real-time visualizations of live data, and issue alarms.

We need to set up the Thingspeak dashboard to receive the data and display in graphical format. You can visit Thingspeak and create an account here.

Create a new channel with the parameters shown below, as seen in the image. After you’ve filled in all of these settings, click the Save channel button.

Go to the API Key and copy the Write API Key. The API key is required in the Gateway Code.

Source Code/Program for Sensor Node

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(PB0, PA4); // CE, CSN on Blue Pill
const uint64_t address = 0xF0F0F0F0E1LL;
int counter = 0;


float temperature;
float humidity;
float altitude;
float pressure;

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme;

struct MyData
{
int counter;
float temperature;
float humidity;
float altitude;
float pressure;
};
MyData data;

void setup()
{
Serial.begin(115200);
radio.begin(); //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening(); //This sets the module as transmitter

if (!bme.begin(0x76))
{
Serial.println(“Could not find a valid BME280 sensor, check wiring!”);
while (1);
}

}

void loop()
{
data.counter = counter;
data.temperature = bme.readTemperature();
data.pressure = bme.readPressure() / 100.0F;
data.altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
data.humidity = bme.readHumidity();

Serial.print(“Packet No. = “);
Serial.println(data.counter);

Serial.print(“Temperature = “);
Serial.print(data.temperature);
Serial.println(“*C”);

Serial.print(“Pressure = “);
Serial.print(data.pressure);
Serial.println(“hPa”);

Serial.print(“Approx. Altitude = “);
Serial.print(data.altitude);
Serial.println(“m”);

Serial.print(“Humidity = “);
Serial.print(data.humidity);
Serial.println(“%”);

Serial.println();

radio.write(&data, sizeof(MyData));

Serial.println(“Data Packet Sent”);
Serial.println(“”);

counter++;
delay(5000);
}

Source Code/Program for ESP32 Wifi Gateway

Make adjustments to the Wifi SSID and Password before posting the code. Substitute your own API Key for the Thingspeak API Key.

#include <WiFi.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

String apiKey = “C25ICK6FHOR7PST4”;

const char* ssid = “Alexahome”;
const char* password = “loranthus”;

const char* server = “api.thingspeak.com”;

RF24 radio(4, 5);
const uint64_t address = 0xF0F0F0F0E1LL;

struct MyData
{
int counter;
float temperature;
float humidity;
float altitude;
float pressure;
};
MyData data;

WiFiClient client;

void setup()
{
Serial.begin(115200);
radio.begin();


Serial.println(“Receiver Started….”);
Serial.print(“Connecting to “);
Serial.println(ssid);
Serial.println();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);

radio.openReadingPipe(0, address); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening(); //This sets the module as receiver
}

int recvData()
{
if ( radio.available() )
{
radio.read(&data, sizeof(MyData));
return 1;
}
return 0;
}


void loop()
{
if(recvData())
{

Serial.println(“Data Received:”);
Serial.print(“Packet No. = “);
Serial.println(data.counter);

Serial.print(“Temperature = “);
Serial.print(data.temperature);
Serial.println(“*C”);

Serial.print(“Pressure = “);
Serial.print(data.pressure);
Serial.println(“hPa”);

Serial.print(“Approx. Altitude = “);
Serial.print(data.altitude);
Serial.println(“m”);

Serial.print(“Humidity = “);
Serial.print(data.humidity);
Serial.println(“%”);

Serial.println();

if (client.connect(server, 80))
{
String postStr = apiKey;
postStr += “&field1=”;
postStr += String(data.temperature);
postStr += “&field2=”;
postStr += String(data.pressure);
postStr += “&field3=”;
postStr += String(data.altitude);
postStr += “&field4=”;
postStr += String(data.humidity);
postStr += “\r\n\r\n\r\n\r\n”;

client.print(“POST /update HTTP/1.1\n”);
client.print(“Host: api.thingspeak.com\n”);
client.print(“Connection: close\n”);
client.print(“X-THINGSPEAKAPIKEY: ” + apiKey + “\n”);
client.print(“Content-Type: application/x-www-form-urlencoded\n”);
client.print(“Content-Length: “);
client.print(postStr.length());
client.print(“\n\n”);
client.print(postStr);
delay(1000);
Serial.println(“Data Sent to Server”);
}
client.stop();
}
}

Result: ESP32+ NRF24L01 Wifi Gateway with STM32+ NRF24L01 Node

STM32 NRF24L01 ESP32 Gateway

You can upload the above codes to the corresponding nodes after building the Sensor Node and Gateway on a breadboard.

You can start using the Serial Monitor as soon as the code is submitted.

The Receiver/Gateway receives the BME280 Sensor Data from the Transmitter. On the Serial Monitor, the Gateway displays the received data, such as temperature, humidity, pressure, and altitude.

Similarly, you can monitor the data online on Thingspeak Server. For that, go to the private view of Thingspeak Server. The data will be logged on after the interval of 15 seconds. To make the device better, you can implement the Sleep Mode feature and change Data Sending Interval.

Conclusion: 

Hope this blog helps you to understand how to use STM32 NRF24L01 Sensor Node with ESP32 NRF24L01 Gateway. We, MATHA ELECTRONICS  will come back with more informative blogs.

Leave a Reply

Your email address will not be published.