Lora SX1278 and STM32 based Temperature and Humidity Monitoring using Arduino IDE
Table of Contents
Lora SX1278 and STM32, Description:
In my previous tutorial, I interfaced the DHT11 Temperature and Humidity Sensor with the STM32F103C microcontroller board and displayed the temperature and humidity values on the I2C supported SSD1306 Oled display Module. Today, I am going to modify this project by adding the Lora SX1278 long range wireless transceiver module.
So, on the transmitter side I am using the Lora SX1278 with the STM32 and on the receiver side I am using the Lora SX1278 with the Arduino Nano. You might be thinking why STM32 on the Transmitter side and Arduino Nano on the receiver side? The answer is pretty straightforward, if you have been using Arduino boards then you should know the Arduino boards hangs a lot in the long run and for me it’s quite difficult to go and manually reset the Arduino board, so I decided to use the STM32F103C which is very fast as compared to the Arduino boards, stm32 may also get stuck but not as frequently as the Arduino boards. The reason I am using Arduino on the Receiver side is that, I have only one STM32 controller board, and moreover the receiver side will be in my room, and I will use it with my laptop to monitor the temperature and humidity values on the Serial monitor. So, most of the time the receiver side will be OFF. But the transmitter will remain ON. As the transmitter will remain ON for months so there is a high chance of the stm32 may get hanged, so this issue can be solved by using the watchdog timer, so if the stm32 gets hanged for the predefined time, the stm32 will automatically reset. I will make another tutorial about this, for now let’s forget about the watchdog timer.
So, before I am going to explain the Lora SX1278 interfacing with the STM32 and its programming; first let’s practically see how this system works. So, let’s go ahead and start with the Transmitter side.
You can clearly see the Temperature and Humidity values on the Oled display module. As, I am near to the transmitter side, so there is no need to use the receiver, I can directly read the Temperature and Humidity values. Now, let’s say I am away from the transmitter then I can connect my receiver circuit with the laptop and then using the Serial monitor I can read the temperature and humidity values.
You can see the temperature and Humidity values are comma separated, I used comma as the delimiter which will help me in splitting the values.
In the next example I will convert this into a Lora Gateway that is we will be sending the temperature and humidity values to the IoT platform and this way we will be able to monitor the temperature and humidity values from anywhere in the world. Without any further delay let’s get started!!!
Amazon Links:
DHT11 Temperature and Humidity Sensor
SSD1306 128×64 Oled i2c display Module
Other Tools and Components:
Super Starter kit for Beginners
PCB small portable drill machines
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Transmitter side circuit diagram:
This is the transmitter side circuit diagram which explains how the Lora SX1278, DHT11, and Oled display modules are interfaced with the STM32 microcontroller board. A 10K ohm resistor is connected between the VDD and DATA pin of the DHT11 Temperature and Humidity Module.
The SSD1306 Oled display module Power supply pins are connected with the 3.3V and GND pins of the STM32 microcontroller board. The SCL and SDA pins of the Oled display module are connected with the B6 and B7 pins of the STM32. B6 is the SCL and B7 is the SDA.
The VCC and GND pins of the Lora module are connected with the 3.3V and GND pins of the STM32 controller board. MISO pin is connected with PA6, MOSI pin is connected with PA7, SLCK pin is connected with PA5, and the NSS pin is connected with the PA4 pin of the STM32. So, that’s all about the Transmitter side circuit diagram. Now, let’s take a look at the receiver side circuit diagram.
Receiver side circuit diagram:
The VCC of the LoRa module is connected with the 3.3V of the Arduino. The MISO Pin of the LoRa module is connected with the Arduino’s pin 12. The MOSI pin is connected with the Arduino’s pin 11. The SCLK pin of the SX1278 LoRa module is connected with the Arduino’s pin 13. The NSS pin is connected with the Arduino’s pin 10 and the ground pin of the LoRa module is connected with the Arduino’s GND.
In the circuit diagram you can also see the SSD1306 I2C supported Oled display Module. I added this if incase you want to print the values on the Oled display module. The VCC and GND pins of the SSD1306 Oled display module are connected with the Arduino’s 3.3V and GND pins. The SDA and SCL pins of the Oled display module are connected with the Arduino’s I2C pins A4 and A5.
On the top you can see is the 5V regulated power supply based on the 7805 voltage regulator. You will need this power supply if you want to power up your Arduino using an external 12V power supply or a solar panel.
STM32 with Arduino IDE:
In order to program the STM32 microcontroller board using the Arduino IDE, first you will need to flash the bootloader, and you will also need to install the STM32 board, and some drivers. I have a very detailed tutorial on this and I highly recommend you should read this article. Now, let’s take a look at the programming. Now, let’s take a look at the programming.
Arduino Libraries:
STM32 Libraries:
Arduino Lora STM32 Library:
STM32 and Arduino Programming:
As this project is based on the wireless communication so, we have two programs, one program is written for the Transmitter side and the other program is written for the Receiver side. So, let’s first start with the Transmitter side programming.
STM32, SX1278 Lora, and DHT11 Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
/* * STM32 with DHT11 Temperature and Humidity sensor * DHT11 connected to PA2 * * Oled connection * B6 to SCL of Oled * B7 to SDA of Oled * * * Lora CONNECTIONS * SX1278 PINS STM32F103C * GND GND * VCC 3.3V * SCK PA5 * MISO PA6 * MOSI PA7 * NSS PA4 */ #include <Wire.h> //Library for using I2C #include <Adafruit_SSD1306_STM32.h> #include <DHT.h> //Library for using DHT sensor #include <LoRa_STM32.h> #define SS PA4 #define RST PB0 #define DI0 PA8 #define TX_P 17 #define BAND 433E6 #define ENCRYPT 0x78 #define DHTPIN PA2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); //initilize object dht for class DHT with DHT pin with STM32 and DHT type as DHT11 float h; float t; // for Oled display ssd1306 #define OLED_RESET -1 Adafruit_SSD1306 display(OLED_RESET); void setup() { Serial.begin(9600); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the pinMode(DHTPIN, OUTPUT); dht.begin(); //Begins to receive Temperature and humidity values. LoRa.setTxPower(TX_P); LoRa.setSyncWord(ENCRYPT); LoRa.setPins(SS, RST, DI0); if (!LoRa.begin(BAND)) { Serial.println("Starting LoRa failed!"); while (1); } //Clear the buffer. display.clearDisplay(); } void loop() { h = dht.readHumidity(); //Gets Humidity value t = dht.readTemperature(); //Gets Temperature value DisplayData(); String MyMessage = ""; MyMessage = MyMessage + String(h) + "," + String(t); // send packet LoRa.beginPacket(); LoRa.print(MyMessage); LoRa.endPacket(); delay(100); } void DisplayData() { display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0,0); display.println("Temp:"); display.setTextSize(1); display.setCursor(0,20); // column , row display.println(t); display.setTextSize(2); display.setCursor(0,35); display.println("Hum:"); display.setTextSize(1); display.setCursor(0,55); // column , row display.println(h); display.display(); delay(100); } |
As you can see, this is the same exact program from my previous tutorial, this time I added code for the Lora SX1278 module. I started off by adding the library and defined some pins and variables.
Inside the loop() function,
1 2 3 4 5 6 7 |
String MyMessage = ""; MyMessage = MyMessage + String(h) + "," + String(t); // send packet LoRa.beginPacket(); LoRa.print(MyMessage); LoRa.endPacket(); delay(100); |
I only added this code which sends the humidity and temperature values. If you find it confusing then you can watch my complete series of videos on the Arduino and Lora SX1278. The programming style of the Arduino and STM32 is almost the same. Now, let’s take a look at the receiver side programming.
SX1278 Lora Arduino Programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
/* * Temperature and Humidity Monitoring * Receiver side * * Module SX1278 // Arduino UNO/NANO GND -> GND Vcc -> 3.3V MISO -> D12 MOSI -> D11 SLCK -> D13 Nss -> D10 Di00 -> D2 RST -> D9 */ #include <SPI.h> #include <LoRa.h> String inString = ""; // string to hold input String MyMessage = ""; void setup() { Serial.begin(9600); while (!Serial); Serial.println("LoRa Receiver"); if (!LoRa.begin(433E6)) { // or 915E6 Serial.println("Starting LoRa failed!"); while (1); } } void loop() { // try to parse packet int packetSize = LoRa.parsePacket(); if (packetSize) { // read packet while (LoRa.available()) { int inChar = LoRa.read(); inString += (char)inChar; MyMessage = inString; } inString = ""; LoRa.packetRssi(); } Serial.println(MyMessage); } |
On the receiver side, I didn’t even change a single instruction. This is the same exact program which I explained in my Arduino and Lora based getting started tutorial. So, that’s all for now.
Watch Video Tutorial:
Your needs come first! Purplewaveindia has no agendas, and no loyalties to anyone other than you, our client. Purplewaveindia was established in 2018. And in a short span of 4-years, Purplewaveindia has become one of the prominent players in the Indian Audio Video Manufacturing and Integration Industry.
Hello! Could you make changes to the receiver sketch to connect the SSD1306 Oled and display the data received on it? Your project is amazing, don’t abandon this project because you can add functions for turning on a relay (fan or heater) and feedback that controls the operation of these relays. My knowledge is only enough to repeat your project, and not change it. Good luck to you!
Hi,
Thank you for this post, its very good example to start lora communication with different platform,
I tried and its working for me,
My question, from Tx side, can I use stm8l ic to send data Arduino based Rx.
For reference of stm8l with lora, I want to use “https://github.com/Exboom/LoRa_STM8/tree/master”
Please suggest.