Introduction: ESP32 Long Distance - LoRaWan

About: Do you like technology? Follow my channel on Youtube and my Blog. In them I put videos every week of microcontrollers, arduinos, networks, among other subjects.

In this article, we are going to discuss LoRaWAN, a network that travels with little energy. But for just how far? With the chip I use in the video, the ESP32, the control distance reaches 3.6 kilometers. So today, we talk about the characteristics of the chip and the LoRa network, which was practically made for IoT (Internet of Things).

In the assembly, in the example of Heltec, with the ESP32 plus the SX1276 chip, we have Sender sending the data packet to the Receiver. In the green head device, the Receiver, we have the signal that is at -9dB. This is because the antennas are practically glued and extremely close, but as you move them away from each other, you can see that the signal strength decreases, reaching -124dB when you stop to receive the information. In the same display, Rx9 bytes indicate the packet size and Hello is the string that the yellow cable device, Sender, is sending. In Sender, we also have a LED flashing at 500 milliseconds.

Step 1: ESP32 LoRaWan

Concerning my ESP32 cards, I bought them with the LoRa chips already built-in. These development kits already come with display, which I consider advantageous. So we dismantled the kit. Under the device, we can see the information on the SX1276 chip.

It is important to remember that the ESP32 has three chips: Flash memory, Espressif, which is the processor of Tensilica, and also there is a USB to Serial Ttl, of Silicon Labs. In the case of this development kit, we also have a fourth chip, which is the SX1276.

Step 2: How the LoRa Network Is Used

To understand the use of the network, we have the LoRa chips communicating with each other, measuring geographic position, temperature, pressure, humidity, and all the information that is analyzed through the IoT in the LoRa network. We then have a hub, the Gateway, which receives all these signals. This is going to enter the TCP / IP network LoRaWAN SSL and then follow to a server side, a more robust thing, IBM Watson, Amazon, in short, the data is processed at another level.

Step 3: Key Features of LoRa Technology and LoRaWAN Protocol

Here, I show some characteristics of the LoRa technology and the LoRaWAN protocol.

I emphasize that the idea of this network LoRa is not for you to send multimedia content, but to send packages of data of reading.

LoRaWAN Protocol

LoRaWAN is a protocol specification built upon the LoRa technology developed by the LoRa Alliance. It uses unlicensed radio spectrum in the Industrial, Scientific, and Medical (ISM) bands to enable low power and wide area communication between remote sensors and gateways connected to the network. This standards-based approach to building an LPWAN enables the rapid deployment of public or private IoT networks anywhere using hardware and software that is bi-directional, secure, interoperable, and mobile, which provides a precise location and works the way you would expect it to.

Step 4: CHIP Sx1276

Then, by disassembling the development kit, we unscrew the display and locate the LoRa chip, created by the French company Semtech. Here, I have a table taken from the Datasheet with information of this device on the 1276, 1277, 1278, and 1279 models. The difference is more in frequency.

Step 5: ESP32 LoRa X ESP32 WiFi

Comparing the traditional ESP32 with ESP32 LoRa, we again point out that the traditional has three chips, while LoRa has four. This fourth chip, in this case, is the sx1276.

It is connected via the SPI port. As for OLED Flat, it is connected to i2c.

Step 6: ESP32 LoRa Pinout

It is important to note the pins that are already occupied.

Step 7: Test

ESP32 LoRaWAN TTGO To perform the test, we took the chips from TTGO, put on an antenna and battery, and walked out. Right away, we found it running at 915 MHz. We tested the 433 MHz as well.

We set up a packet to send Sender to the Receiver. The second will receive this packet and measure the signal strength in dB, which will show on the display. In this way, it will be possible for you to perform a test. I also intend to do this using my drone, which travels up to 7 kilometers, to see how far I can get.

Step 8: ESP32 LoRaWAN - Heltec

This is Heltec's model, which is at 433 MHz and works very well. If you are looking to buy an ESP32, I recommend this brand because it has a Lib inside the Arduino IDE, which makes handling easier.

Step 9: Lib and Heltec Examples

Heltec examples are in GitHub.

https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series

Below is the path to locate the source code of your computer after installation.

C: \ Program Files (x86) \ Arduino \ hardware \ heltec \ esp32 \ libraries \ LoRa \ examples \ OLED_LoRa_Sender

Step 10: Manual PDF

I translated the manual from Portuguese and will deliver it to you in a PDF file.

Register to my email list:

Subscribe to the link: pdf.fernandok.com

You will receive a message confirming your application. Check your inbox, as well as your spam box, among others.

With the confirmed registration, I will send you a second email with the download link from the manual: "How to install Arduino IDE ESP32 LoRa - Heltec on Windows".

Step 11: Source Code

Today, we have two .INO programs, the Sender and the Receiver.

Sender and Receiver

First, I declare the SPI pins and set the radio frequency, that is, I make all the necessary statements.

#include <spi.h> //responsável pela comunicação serial
#include <lora.h> //responsável pela comunicação com o WIFI Lora #include <wire.h> //responsável pela comunicação i2c #include "SSD1306.h" //responsável pela comunicação com o display #include "images.h" //contém o logo para usarmos ao iniciar o display // Definição dos pinos #define SCK 5 // GPIO5 -- SX127x's SCK #define MISO 19 // GPIO19 -- SX127x's MISO #define MOSI 27 // GPIO27 -- SX127x's MOSI #define SS 18 // GPIO18 -- SX127x's CS #define RST 14 // GPIO14 -- SX127x's RESET #define DI00 26 // GPIO26 -- SX127x's IRQ (Interrupt Request) #define BAND 915E6 //Frequencia do radio - podemos utilizar ainda : 433E6, 868E6, 915E6 #define PABOOST true

Sender: OLED_LoRa_Sender.ino

Here, I have the display that is connected to the i2c pins and the strings.

//variável responsável por armazenar o valor do contador (enviaremos esse valor para o outro Lora)
unsigned int counter = 0; //parametros: address,SDA,SCL SSD1306 display(0x3c, 4, 15); //construtor do objeto que controlaremos o display String rssi = "RSSI --"; String packSize = "--"; String packet ;

Setup: OLED_LoRa_Sender.ino

Here, we configure the pins as output, as well as the actions of the display. We initiate LoRa communication and configure the pins that will be used by the library.

Highlight: Every time you connect the ESP32 to the LoRa network, it checks if the antenna is plugged into the Pigtail connector, because if you connect to the USB without it being connected to the antenna, you can burn the transmitter chip.

void setup()
{ //configura os pinos como saida pinMode(16,OUTPUT); //RST do oled pinMode(25,OUTPUT); digitalWrite(16, LOW); // reseta o OLED delay(50); digitalWrite(16, HIGH); // enquanto o OLED estiver ligado, GPIO16 deve estar HIGH display.init(); //inicializa o display display.flipScreenVertically(); display.setFont(ArialMT_Plain_10); //configura a fonte para um tamanho maior //imprime o logo na tela logo(); delay(1500); display.clear(); //apaga todo o conteúdo da tela do display SPI.begin(SCK,MISO,MOSI,SS); //inicia a comunicação serial com o Lora LoRa.setPins(SS,RST,DI00); //configura os pinos que serão utlizados pela biblioteca (deve ser chamado antes do LoRa.begin) //inicializa o Lora com a frequencia específica. if (!LoRa.begin(BAND,PABOOST)) { display.drawString(0, 0, "Starting LoRa failed!"); display.display(); //mostra o conteúdo na tela while (1); } //indica no display que inicilizou corretamente. display.drawString(0, 0, "LoRa Initial success!"); display.display(); //mostra o conteúdo na tela delay(1000); }

Loop: OLED_LoRa_Sender.ino

Here's how to assemble a package and turn on the LED.

void loop()
{ //apaga o conteúdo do display display.clear(); display.setTextAlignment(TEXT_ALIGN_LEFT); display.setFont(ArialMT_Plain_16); display.drawString(0, 0, "Sending packet: "); display.drawString(40, 26, String(counter)); display.display(); //mostra o conteúdo na tela //beginPacket : abre um pacote para adicionarmos os dados para envio LoRa.beginPacket(); //print: adiciona os dados no pacote LoRa.print("hello "); LoRa.print(counter); //endPacket : fecha o pacote e envia LoRa.endPacket(); //retorno= 1:sucesso | 0: falha //beginPacket : abre um pacote para adicionarmos os dados para envio LoRa.beginPacket(); //print: adiciona os dados no pacote LoRa.print("hello "); LoRa.print(counter); //endPacket : fecha o pacote e envia LoRa.endPacket(); //retorno= 1:sucesso | 0: falha //incrementa uma unidade no contador counter++; digitalWrite(25, HIGH); // liga o LED indicativo delay(500); // aguarda 500ms digitalWrite(25, LOW); // desliga o LED indicativo delay(500); // aguarda 500ms }

Print - Logo: OLED_LoRa_Sender.ino

This function only prints the logo on the display.

//essa função apenas imprime o logo na tela do display
void logo() { //apaga o conteúdo do display display.clear(); //imprime o logo presente na biblioteca "images.h" display.drawXbm(0,5,logo_width,logo_height,logo_bits); display.display(); }

Setup: OLED_LoRa.Reciver.ino

We start LoRa here with the specific frequency, we point the correct initialization in the display, and we enable LoRa to receive the data sent by Sender. We also check the receipt of the package.

//inicializa o Lora com a frequencia específica.
if (!LoRa.begin(BAND,PABOOST)) { display.drawString(0, 0, "Starting LoRa failed!"); display.display(); while (1); } //indica no display que inicilizou corretamente. display.drawString(0, 0, "LoRa Initial success!"); display.drawString(0, 10, "Wait for incomm data..."); display.display(); delay(1000); //LoRa.onReceive(cbk); LoRa.receive(); //habilita o Lora para receber dados } void loop() { //parsePacket: checa se um pacote foi recebido //retorno: tamanho do pacote em bytes. Se retornar 0 (ZERO) nenhum pacote foi recebido int packetSize = LoRa.parsePacket(); //caso tenha recebido pacote chama a função para configurar os dados que serão mostrados em tela if (packetSize) { cbk(packetSize); //função responsável por recuperar o conteúdo do pacote recebido delay(10); } }

Print on OLED: OLED_LoRa.Reciver.ino

Here, we configure the information that will appear on the display.

//função responsável por configurar os dados que serão exibidos em tela.
//RSSI : primeira linha //RX packSize : segunda linha //packet : terceira linha void loraData(){ display.clear(); display.setTextAlignment(TEXT_ALIGN_LEFT); display.setFont(ArialMT_Plain_16); display.drawString(0 , 18 , "Rx "+ packSize + " bytes"); display.drawStringMaxWidth(0 , 39 , 128, packet); display.drawString(0, 0, rssi); display.display(); }

Step 12: