ESP32 As LoRa Gateway With Arduino

by Lan_Makerfabs in Circuits > Arduino

10804 Views, 34 Favorites, 0 Comments

ESP32 As LoRa Gateway With Arduino

test5.jpg

In the past, I had done a project that monitored the soil moisture with LoRa and used the ESP32-LoRa board to be a LoRa gateway by programming with Micropython. It has a little unfriendly for Arduino users.

You can check the previous project via the video.

Now I wanted to program the ESP32 with Arduino, and assemble it with the LoRa expansion board as the LoRa gateway to receive the soil moisture reading transmitted by the soil moisture sensor.

Previous Project

Lora IoT Irrigation System - Outdoor Simulation Test

Soil Moisture

Soil moisture is one of the important parameters of the soil. It is not only an indispensable survival factor for vegetation life but also forms soil solution together with soluble salts, which becomes the medium supplying nutrients to plants. So that monitoring the soil moisture becomes more and more necessary for farms, gardens.

Soil Moisture Sensor

test3.jpg
test7.jpg

The LoRa soil moisture sensor is based on Atmel's Atmega328P, it collects local air temperature/ humidity with the sensor AHT10, and detects the soil moisture with capacitor-humility measurement solution with 555. The sensor transmits the reading to the LoRa gateway with LoRa communication. And it is very suitable to use to remind if the plants need watering in a garden.

The LoRa module onboard connects to Atmega328P with SPI and GPIO, the connection detail as the below:

Atmega328P ------> LoRa module
D12 ----> MISO 
D11 ------> MISO 
D13 ------> SCK 
D10 ------> NSS 
D4 ------> RESET 
D2 ------> DIO0 
D6 ----> DIO1 

Program Soil Moisture Sensor

1. For connecting to the gateway successfully, it was necessary to use the same library(RadioLib.h) to drive the LoRa module.

2. When the LoRa transmitted the data, add the sign to the transmitted information for ESP32 to recognize it.

String message = "#"+(String)packetnum+" Humidity:"+(String)humidity+"% Temperature:"+(String)temperature+"C"+" ADC:"+(String)sensorValue+" SOIL2";
  Serial.println(message);
  packetnum++;
  Serial.println("Transmit: Sending to rf95_server");
  	 int state = radio.transmit(message);

3. You can get the code from GitHub.

LoRa Gateway

test4.jpg
test6.jpg

This Lora Gateway is a combination of MakePython ESP32 and MakePython LoRa. ESP32 is a very popular MCU for Arduino or MicroPython. MakePython ESP32 base on the ESP32 and display, it is designed to make it easy for developing, that you can program it just plug the USB cable.

MakePython LoRa is a two-channel LoRa expansion for MakePython ESP32. It provides a solution about LoRa to WIFI.

There are two LoRa modules on board, in this project, one module named U2 was used to receive the data. When two boards are assembled, the ESP32 connects to the LoRa module with SPI. LoRa module connected to ESP32 with SPI and GPIOs and the schematic is shown.

The detailed connection of the ESP32 and U2 LoRa module:

ESP32 -------> U2 LoRa module 
IO12 ------> MISO 
IO13 ------> MOSI 
IO14 ------> SCK 
IO32 ------> NSS 
IO33 ------> RESET 
IO36 ------> DIO0 
IO27 ------> DIO1 

Next let’s program the ESP32 to drive the U2 LoRa module to receive something.

Program ESP32

1. Config the interface of the LoRa module referring to the schematic.

#define DIO0 36
#define DIO1 27

#define LORA_RST 33
#define LORA_CS 32

#define SPI_MOSI 13
#define SPI_MISO 12
#define SPI_SCK 14

2. Install the LoRa library—RadioLib.h to support the LoRa module working.

3. Receive the data transmitted by the soil moisture sensor.

    String str;
    int state = radio.receive(str);

4. For recognizing the data of which sensor transmitted, every data would be included the special sign such as “SOIL1”.

if(str.indexOf("SOIL1")>-1)
        {   sensorADC = str.substring(str.indexOf("ADC:")+4,str.indexOf("ADC:")+7);
            
            ADCvalue[0] =  sensorADC;
            Serial.println("1: "+ADCvalue[0]);         
            }
            
        if(str.indexOf("SOIL2")>-1)
        {   sensorADC = str.substring(str.indexOf("ADC:")+4,str.indexOf("ADC:")+7);

            ADCvalue[1] =  sensorADC;
            Serial.println("2: "+ADCvalue[1]);
            
            }

5. Display the data on the SSD1306 which have featured in the ESP32 board.

      display.clearDisplay();
      display.setTextSize(2);              
      display.setTextColor(SSD1306_WHITE); 
      display.setCursor(0, 0);             
      display.print(F("SOIL1: "));
      display.println(ADCvalue[0]);
      display.print(F("SOIL2: "));
      display.println(ADCvalue[1]);
      display.display();

6. The code can be obtained from Github.

Test

test1.jpg
test2.jpg

I have two sensors and power them respectively by the battery. Put one sensor to the plant pot and the other to the garden. Power the ESP32 up and the LCD would show the sensor reading.

Besides, ESP32 can transmit the data received by the LoRa module to the website via WIFI, such as transmitting to the Thingspeak. It also can transmit the data or commands to the other LoRa or LoRa-Relay modules via LoRa.

I hope it will be helpful for you.