ESP8266LORA PROJECTSSTM32

ESP8266 Lora Gateway for STM32 and DHT11 Sensor, IoT Gateway

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

Altium Designer:

775 Motor

Altium Designer is the world’s most trusted PCB design system. Altium Designer enables engineers to effortlessly connect with every facet of the electronics design process. Over 35 years of innovation and development focused on a truly unified design environment makes it the most widely used PCB design solution. With Altium Designer you can create PCB designs with an intuitive and powerful interface that connects you to every aspect of the electronics design process. Route it your way through any angle, tune for delay, Push, Slide, and Walkaround faster than ever. Interact and collaborate with mechanical designers like never before in a photo-realistic, 3D design environment. If you want to get started with the Altium designer, you can click on the 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

Arduino Uno

SSD1306 128×64 Oled i2c display Module

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

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!


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 this article. 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:

/*
 * STM32 with DHT11 Temperature and Humidity sensor, LoRa SX1278, and Oled Display
 * 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 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:

/* 
 * ESP8266 LoRa Gateway for Temperature and Humidity Monitoring
 * Receiver side"Master Node".
 * 
 * 
  Module SX1278 // Nodemcu ESP8266    
    GND         ->   GND
    Vcc         ->   3.3V
    MISO        ->   D6
    MOSI        ->   D7    
    SLCK        ->   D5
    Nss         ->   D8
    Dio0        ->   D4
    RST         ->   D0      
 */

#include <SPI.h>
#include <LoRa.h> 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
  
char auth[] = "nPmAvH8sz3mwxzTbP2GUmtGcykbQnKAs";
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "AndroidAP7DF8";  // WiFi router name
char pass[] = "Eclinic"; // password

String inString = "";    // string to hold input
String MyMessage = "";

#define Nss 15 // D8 pin CS
#define rst 16 // D0 
#define dio0 2 // D4

float Hum_Value = 0.0; 
float Temp_Value = 0.0; 

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
   Serial.println("LoRa Receiver");
  LoRa.setPins(Nss, rst, dio0);
  
  if (!LoRa.begin(433E6)) { // or 915E6
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {

  Blynk.run();
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) { 
    // read packet   
    
    while (LoRa.available())
    {
      int inChar = LoRa.read();
      inString += (char)inChar;
      MyMessage = inString;     
    }

String q = getValue(inString, ',', 0); // Humidity
String r = getValue(inString, ',', 1); // Temperature

Hum_Value = q.toFloat();
Blynk.virtualWrite(V2,q); // Humidity
Blynk.virtualWrite(V3,r); // Temperature

inString = "";     
LoRa.packetRssi();    
  }
      
  Serial.println(MyMessage);  
  
}


String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;
 
    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}


Code Explanation:

#include <LoRa.h> 

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

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

char auth[] = "nPmAvH8sz3mwxzTbP2GUmtGcykbQnKAs";

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

char ssid[] = "AndroidAP7DF8";  // WiFi router name
char pass[] = "Eclinic"; // password

These are the WiFi credentials.

String inString = "";    // string to hold input
String MyMessage = "";

#define Nss 15 // D8 pin CS
#define rst 16 // D0 
#define dio0 2 // D4
Next, I defined some variables and pins. 

float Hum_Value = 0.0; 
float Temp_Value = 0.0; 

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

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
   Serial.println("LoRa Receiver");
  LoRa.setPins(Nss, rst, dio0);
  
  if (!LoRa.begin(433E6)) { // or 915E6
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

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

Blynk.virtualWrite(V2,q); // Humidity

Blynk.virtualWrite(V3,r); // Temperature

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

String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;
 
    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

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:

 

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

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