ESP8266 ProjectsLORA PROJECTSSTM32

How to Build an ESP8266 LoRa Gateway for STM32 and DHT11 Sensor

Last Updated on May 11, 2026 by Engr. Shahzada Fahad

ESP8266 Lora Gateway, Description:

 

ESP8266 Lora Gateway for STM32 and DHT11 Sensor, IoT Gateway- Before, I am going to explain how to make your own ESP8266 Lora Gateway; first, I am going to talk about my previous tutorial, because today’s tutorial is entirely based on my previous tutorial. So, in my previous tutorial I explained how to interface the Lora SX1278 with the STM32 and Arduino Nano.

ESP8266 Lora Gateway

On the Transmitter side I used the Lora SX1278 with the STM32 and on the receiver side I used the same Lora SX1278 with the Arduino. So, it really doesn’t matter which controller board you use on the receiver side. To explain this, today, I will replace the Arduino Nano with the Nodemcu ESP8266 WiFi module to convert it into ESP8266 Lora Gateway. Anyways, in this tutorial, I was simply sending the temperature and humidity values from the transmitter side (which is based on the STM32, Lora SX1278, DHT11, and I2C supported Oled display module) to the receiver side (which is based on the Arduino Nano and Lora SX1278 module).

ESP8266 Lora Gateway

To separate the Temperature and Humidity values I used comma as the delimiter, and today you will practically see how we split the two values on the receiver side and then how these values are sent to the IoT Platform.


Today, we are going to make an IoT gateway for this STM32 based Temperature and Humidity project. We will go with the same project, the transmitter side will remain exactly the same, we are not going to change anything on the Transmitter side. But on the Receiver side we will replace the Arduino Nano with the Nodemcu ESP8266 WiFi Module with the help of which we will send the temperature and humidity values to the Blynk IoT platform and this way we can monitor the temperature and humidity values from anywhere in the world. So, before I am going to explain the interfacing and programming, first, let’s watch the ESP8266 Lora Gateway in action.

ESP8266 Lora Gateway

You can clearly see the transmitter side (LoRa Node) is exactly the same. The DHT11 Sensor, Lora SX1278, and the same Oled display modules are still connected with the STM32 microcontroller board. You can directly read the temperature and humidity values on the Oled display module when you are near to the Transmitter side or the LoRa Node. But if the same LoRa Node is installed at a different location where you have no access to the internet or you have multiple LoRa Nodes then, first you will need to transmit the sensors data to the LoRa Gateway and then from there you can send the sensors values to the IoT Platform which in my case is the Blynk application. For now, you can only see the values on the Oled display module.

Let’s go ahead and turn ON the ESP8266 Lora Gateway, you will get the notification as soon as the Nodemcu ESP8266 is connected with the WiFi, then you can simply click the play button on the App and you are good to go.

ESP8266 Lora Gateway

The same values which you can see on the Oled display module are also displayed on the Gauges. So, how it’s doing this? The STM32 sends a message consisting of the comma separated Temperature and Humidity values to the ESP8266 Lora Gateway. The Gateway splits the message using comma as the delimiter and store the sensors values in variables and then finally the values are sent to the Blynk IoT Platform. Now with this setup I can monitor the Temperature and Humidity values from anywhere in the world. You can connect multiple Lora Nodes with the Lora Gateway which I have already explained in my LoRa SX1278 STM32 temperature monitoring tutorial. Anyways, my ESP8266 Lora Gateway is working perfectly; you can go ahead and replace the DHT11 sensor with other sensors as per your requirement. Now, you have got an idea of what you are going to learn after reading this article. Without any further delay, let’s get started!!!

Note: this old version of the Blynk app is no more functional. For the blynk mobile App setup and Blynk.cloud dashboard setup ready my article on the New Blynk V2.0. In this article I have explained how to migrate your projects from Blynk 1.0 to the new Blynk V2.0. You can also watch the video.



Amazon Links:

LoRa SX1278 Module

STM32F103C

Nodemcu ESP8266 WiFi Module:

DHT11 Temperature and Humidity Sensor

Arduino Nano USB C type (Recommended)

*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!


Lora SX1278 Interfacing with STM32:

ESP8266 Lora Gateway

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 circuit diagram of the ESP8266 Lora Gateway.


ESP8266 Lora Gateway Circuit Diagram:

ESP8266 Lora Gateway

This is the circuit diagram of the ESP8266 Lora Gateway. The MISO pin of the Lora SX1278 Module is connected with the D6 pin of the Nodemcu ESP8266 WiFi Module. The MOSI pin of the Lora module is connected with the D7 Pin. The SLCK pin is connected with the Nodemcu ESP8266 pin D5, and the NSS pin of the Lora SX1278 Transceiver module is connected with the pin D8 of the Nodemcu ESP8266 module. While the VCC and GND pins of the Lora module are connected with the 3.3V and GND pins of the Nodemcu module.

On the top is the 5v regulated power supply based on the LM7805 voltage regulator. You can use this regulated power supply to power up your Nodemcu module using a 12V adaptor or a solar panel. Don’t forget to add these 470uF decoupling capacitors at the input and output of the 7805 regulator. Now you have multiple options, you can use an adaptor, or you can use a power bank, or you can also use a cell phone charger. So, that’s all about the circuit diagrams.


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 my STM32 bootloader and Arduino IDE setup tutorial. You will also need to install the Nodemcu ESP8266 board in the Arduino IDE. Now, let’s take a look at the programming.

Lora library for ESP8266:

Download: LoRa.h

STM32 Libraries:

Arduino Lora STM32 Library:

DHT Library for STM32:

ESP8266 Lora Gateway Programming:

As this project is based on the wireless communication so, we have two programs, one program is written for the Transmitter side (Lora Node) and the other program is written for the Receiver side (ESP8266 LoRa Gateway) which is the Master Node. So, let’s first start with the Transmitter side programming.



STM32 DHT11, Transmitter Side Programming:

As I said earlier, I am using the same transmitter connections and the same programming and as you can clearly see, I have not even changed a single instruction. For detailed explanation read my previous article. Now, let’s take a look at the ESP8266 Lora Gateway programming.


ESP8266 Lora Gateway, Receiver Side programming:


Code Explanation:

This is the same LoRa library which I have been using with the Arduino. This LoRa library also works with the Nodemcu ESP8266.

Next, you will also need to add these two libraries if you are using ESP8266 controller board and the Blynk application.

This is the Authentication token which you get while making the Blynk application.

These are the WiFi credentials.

I also defined two variables of the type float for storing the humidity and temperature values.

The code which you can see inside the setup() function is exactly the same code as explained in my previous Lora based projects. This time I only added Blynk.begin(auth, ssid, pass); this line of code.

Inside the loop() function we run the Blynk and rest of the code is from my multiple sensors monitoring project the only modification is the addition of these two instructions

Which are used to send the humidity and temperature values to the Blynk application using the virtual pins V2 and V3.

The getValue() function is a user-defined function and I have been using it for years in different projects for splitting the sensors values and string messages. So, that’s all about the programming.



Blynk App for the ESP8266 Lora Gateway:

For the Blynk application designing watch my video tutorial given below. Or you can do it yourself, all you need is to add two gauges and assign the virtual pins V2 and V3.

Watch Video Tutorial:

ESP8266 Lora Gateway for STM32 and DHT11 Sensor, IoT Gateway, STM32 Lora Gateway, Lora SX1278

Troubleshooting — Problems You Might Face Building This Gateway Project

Problem 1: The STM32 transmitter is running but the ESP8266 gateway receives nothing at all

You power on both sides, check the Serial Monitor on the ESP8266, and it just sits there showing nothing received. The DHT11 readings on the STM32 side look fine on the OLED, but nothing is crossing over the LoRa link.

The first thing to check is whether both LoRa modules are configured with exactly the same frequency, bandwidth, spreading factor, and coding rate. The SX1278 library requires all these parameters to match perfectly on both ends — if even one value is different between the transmitter and receiver code, they will never communicate. Go through both sketches line by line and confirm every LoRa.setFrequency(), LoRa.setSpreadingFactor(), LoRa.setSignalBandwidth(), and LoRa.setCodingRate4() call is identical.

The second thing to verify is the SPI wiring on both sides. The SX1278 uses SPI communication and a wrong pin connection — especially mixing up MISO and MOSI — will prevent the library from even initializing the module. On the STM32, MISO is PA6, MOSI is PA7, SCLK is PA5, and NSS is PA4. On the ESP8266 side, double check that the LoRa module CS pin goes to the correct GPIO. Open the Serial Monitor and look for the LoRa initialization message — if it says “LoRa init failed” the wiring or library configuration is wrong before you even think about transmission.

Problem 2: LoRa communication is working — messages are being received — but the parsed temperature and humidity values are wrong or show as zero

The STM32 sends temperature and humidity as a single comma-separated string — something like “28.5,65.2”. The ESP8266 receives this string and splits it using the comma as a delimiter to extract the two values. If the parsing is going wrong, the most common cause is extra characters in the received string.

The LoRa library sometimes appends a newline character or carriage return at the end of a received packet. If your parsing code does not trim these before converting to a float, the humidity value will fail to parse correctly and show as zero. Use the trim() function on the received string before splitting it, and also trim each substring after splitting.

Another cause is the STM32 sending the values in a slightly different format than expected — for example “28.50,65.20” instead of “28.5,65.2”. The comma split will still work but floating point conversion can behave differently depending on the number of decimal places. Print the raw received string to the ESP8266 Serial Monitor before any parsing so you can see exactly what bytes are arriving and where the issue is.

Problem 3: Blynk app shows the device as offline even though the ESP8266 is connected to WiFi

This is a very common Blynk issue that trips up a lot of people. The ESP8266 successfully connects to your home WiFi — you can see the IP address printed on the Serial Monitor — but the Blynk app stubbornly shows the device as offline with a grey indicator.

The first thing to check is whether you are using the old Blynk legacy platform or the new Blynk 2.0. These two systems are completely separate and incompatible. If you signed up for Blynk recently, you are on Blynk 2.0 and you need the new Blynk library version 1.0 or higher. The old library will not work with the new platform at all. In the Arduino IDE Library Manager, search for Blynk and make sure you have the latest version installed, not version 0.6.x.

The second thing to check is your Auth token. In Blynk 2.0 the token comes from the web dashboard at blynk.cloud, not from the mobile app. In the old legacy system it came from the mobile app. Using a token from the wrong source is one of the most common mistakes. Copy a fresh token from the correct location for your platform and paste it into your code.

Problem 4: DHT11 sensor on the STM32 is returning 255 for both temperature and humidity

If you see 255 or -999 or some other obviously wrong fixed number from the DHT11, the sensor is not being read at all. This is not a LoRa or gateway problem — it is specifically a DHT11 and STM32 compatibility issue.

The standard Arduino DHT library does not always work correctly with STM32 boards because the STM32 timing is different from Arduino. The fix is to use the DHT library that has been specifically tested with STM32 — the one that Engr. Shahzada Fahad provides in his dedicated STM32 and DHT11 tutorial on this site. Download that exact library version and replace any other DHT library you have installed.

Also make sure the 10K pull-up resistor is connected between the VDD and DATA pins of the DHT11. Without this resistor the data signal is floating and the reading will always fail. This is not optional — the DHT11 requires this pull-up to communicate correctly.

Problem 5: The system works indoors but the LoRa range drops dramatically when moved to a real installation location

You tested everything on your desk and it worked beautifully across the room. But when you install the STM32 node in its intended location — perhaps a farm field, a storage room, or an outdoor enclosure — the signal drops out completely even at a short distance.

The most significant factor affecting LoRa range is antenna quality and placement. The wire antenna that comes with cheap SX1278 modules is often poorly cut and not the correct length for 433MHz. For 433MHz, the correct quarter-wave antenna length is exactly 164mm. Measure and trim your antenna wire to this length. Also make sure both the transmitter and receiver antennas are oriented vertically and in the same direction — horizontal at one end and vertical at the other cuts range significantly.

Walls, metal objects, and water all absorb 433MHz signals. A single concrete wall can reduce your effective range by 70%. Position the gateway ESP8266 as high as possible and in a location with line-of-sight or near line-of-sight to the transmitter node for maximum range.

Problem 6: The ESP8266 gateway receives data correctly but crashes or resets after running for a few hours

An ESP8266 that crashes after running well for several hours is almost always running out of heap memory. This is a classic memory leak problem that is common in projects that use Strings heavily.

Every time the ESP8266 receives a LoRa packet and creates a new String object to hold it, a small amount of RAM gets allocated. If this memory is not properly freed — which happens with String objects due to heap fragmentation on the ESP8266 — the available RAM shrinks slowly until the system crashes.

The fix is to replace all String objects in your gateway code with char arrays. Instead of String received = LoRa.readString(), use char received[32] and read character by character. This uses fixed stack memory instead of heap memory and eliminates the fragmentation problem entirely. Also add Blynk.run() and ArduinoOTA.handle() in the main loop without any blocking delays to keep the watchdog timer happy and prevent resets from watchdog timeouts.

Frequently Asked Questions

Why use LoRa for this project instead of just connecting the DHT11 directly to the ESP8266?

This is a great question and the answer comes down to distance and environment. If your sensor is right next to your WiFi router, connecting the DHT11 directly to the ESP8266 is simpler and perfectly fine. But LoRa becomes essential when the sensor needs to be placed far from any WiFi coverage — in a farm field hundreds of meters away, in a basement with no WiFi signal, in a factory building on the other side of a thick concrete wall, or in a remote outdoor location. LoRa can cover 1 to 15 kilometers depending on environment and antenna setup, which WiFi simply cannot match. The gateway approach also scales well — you can have multiple STM32 sensor nodes spread across a large area all reporting back to a single ESP8266 gateway, which then sends everything to the cloud over WiFi.

Can I replace the STM32 transmitter with an Arduino Nano or ESP32?

Yes, completely. Engr. Shahzada Fahad actually explained the Arduino and LoRa SX1278 combination in earlier tutorials on this site. The LoRa library and DHT11 library work on Arduino Nano and ESP32 as well. The receiver side ESP8266 gateway code stays exactly the same regardless of what board is on the transmitter side — all it cares about is receiving the comma-separated string over LoRa, which looks the same whether it was sent by STM32, Arduino, or ESP32. The STM32 was chosen as the transmitter in this project specifically because it is more stable for long-running unattended operation compared to Arduino boards which can occasionally hang after extended use.

Can I add more sensors to the STM32 node and send more data through the gateway?

Absolutely. The comma-separated string format is easy to extend. If you want to add a soil moisture sensor, you just add a third value to the string — something like “28.5,65.2,342” where the third number is the soil moisture reading. On the ESP8266 gateway side, split the string into three parts instead of two and create a third Blynk virtual pin to display it. You can add as many sensors as you want this way, limited only by the LoRa packet size which is 255 bytes maximum — more than enough for dozens of sensor readings in text format.

How do I add multiple STM32 sensor nodes to the same gateway?

Each LoRa node needs a unique identifier in its transmitted string so the gateway knows which node a packet came from. The simplest approach is to add a node ID at the beginning of the string — for example node 1 sends “1,28.5,65.2” and node 2 sends “2,31.2,58.7”. On the gateway side, parse the first value to get the node ID, then route the temperature and humidity values to the corresponding Blynk virtual pins for that node. Blynk supports up to 10 virtual pins on the free plan and more on paid plans, so you can display data from multiple nodes simultaneously on the same dashboard.

Can I replace Blynk with a different IoT platform for this project?

Yes and there are several good alternatives. ThingSpeak is completely free and excellent for logging sensor data and creating graphs over time. The ESP8266 sends an HTTP request to ThingSpeak instead of using the Blynk library. MQTT with a broker like HiveMQ or Mosquitto gives you more flexibility and is popular in professional IoT systems. Home Assistant running on a Raspberry Pi is a great option if you want everything on your local network without any cloud dependency. The LoRa and STM32 transmitter side of this project does not change at all for any of these alternatives — only the ESP8266 gateway code changes to use a different platform’s API.

What is the maximum distance this LoRa setup can cover?

In an open field with no obstructions and the correct antenna, the SX1278 at 433MHz can reach 5 to 10 kilometers. In a typical suburban environment with houses and trees, expect 500 meters to 2 kilometers reliably. Inside buildings with concrete walls, range drops to 50 to 200 meters depending on wall thickness and number of walls between nodes. The spreading factor setting in the LoRa configuration directly trades off range against data rate — a higher spreading factor gives longer range but slower transmission. For a temperature and humidity sensor that only sends data every few seconds, using a higher spreading factor like SF10 or SF11 is a good choice since data rate does not matter much and range improves significantly.

If you are building this gateway project and run into any specific problem not covered above, drop a comment below describing exactly what your Serial Monitor shows and I will personally help you debug it.

 


Discover more from Electronic Clinic

Subscribe to get the latest posts sent to your email.

Engr. Shahzada Fahad

Engr. Shahzada Fahad is an Electrical Engineer with over 15 years of hands-on experience in electronics design, programming, and PCB development. He specializes in microcontrollers (Arduino, ESP32, STM32, Raspberry Pi), robotics, and IoT systems. He is the founder and lead author at Electronic Clinic, dedicated to sharing practical knowledge.

Related Articles

4 Comments

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Discover more from Electronic Clinic

Subscribe now to keep reading and get access to the full archive.

Continue reading

Electronic Clinic
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.