The Super Long Distance Wifi of The Esp32 Lora With Arduino Ide
The Super Long Distance Wifi of The Esp32 Lora With Arduino Ide
The Super Long Distance Wifi of The Esp32 Lora With Arduino Ide
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE
by Fernando Koyanagi
This is very useful for debugging and testing your with the read data.
program, due to the display that already accompanies
it. Today, we will deal again with the ESP32 LoRa, In addition to collecting the data, the BME280 shows
this time in a project using the BME280 sensor. You me the time it took to do all the reading and send it to
already know that I’m a big fan of this chip for the the other ESP. I do all this using the LoRa radio,
possibility of its use in the field. We will then send which goes far, as proven in a test where we reached
requests using LoRa, perform the temperature, 6.5 km.
pressure, and humidity readings using the BME280
sensor, as well as send the response to the request
https://youtu.be/ohg2HACr9n4
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 1
Step 1: Communication
3 - Slave sends back a packet with data read from the BME280 sensor
4 - The Master receives it, verifies the package, and displays the data
Step 3: BME280
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 2
Step 4: Assembly
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 3
Step 5: Library BME280
Search for adafruit sensor based libraries and install the Adafruit Unified Sensor
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 4
Step 7: Modify LoRa 32 WiFi I2C Pinout
It’s important to mention that the first time I compiled accept these pins. The libraries that are accepted are
the BME280 in ESP32, it did not work out. So, this tip # 4 and # 15. This change is therefore very important.
is imperative: you have to modify a .h from within the
arduino compiler. This is an issue that is not quickly Therefore:
discovered, thus I want to explain here what you
should do. Open the file:
In C: \Users\ C:\Users\
<YOUR_USER>\Documents\Arduino\hardware\helte <YOUR_USER>\Documents\Arduino\hardware\helte
c\esp32\variants\wifi_lora_32\pins_arduino.h, more c\esp32\variants\wifi_lora_32\pins_arduino.h
specifically in this last folder: pins_arduino.h, Heltec
placed SAD and SCL on pins 21 and 22, Change the SDA pins to 4, and the SCL pin to 15.
respectively. However, the Adafruit lib does not
This source code is the same as I used in the video: ESP32 LoRa with Arduino IDE Send and Receive TX RX. I
just modified it for the BME280.
Step 9: LoRaSendReceiveBME280.ino
At the beginning, we will include the libraries and define the destinations of the GPIOs, in addition to the frequency
of the radio. Also, we will create constants to inform the Slave about the data work, as well as the return of these
to the Master. We also structure the sensor data and point the variable to control the display.
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 5
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <SSD1306.h>
LoRaSendReceiveBME280.ino - setupDisplay
void setupDisplay(){
//O estado do GPIO16 é utilizado para controlar o display OLED
pinMode(16, OUTPUT);
//Reseta as configurações do display OLED
digitalWrite(16, LOW);
//Para o OLED permanecer ligado, o GPIO16 deve permanecer HIGH
//Deve estar em HIGH antes de chamar o display.init() e fazer as demais configurações,
//não inverta a ordem
digitalWrite(16, HIGH);
//Configurações do display
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_16);
display.setTextAlignment(TEXT_ALIGN_LEFT);
}
LoRaSendReceiveBME280.ino - setupLoRa
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 6
//Configurações iniciais do LoRa
void setupLoRa(){
//Inicializa a comunicação
SPI.begin(SCK, MISO, MOSI, SS);
LoRa.setPins(SS, RST, DI00);
//Inicializa o LoRa
if (!LoRa.begin(BAND, true)){
//Se não conseguiu inicializar, mostra uma mensagem no display
display.clear();
display.drawString(0, 0, "Erro ao inicializar o LoRa!");
display.display();
while (1);
}
//Ativa o crc
LoRa.enableCrc();
//Ativa o recebimento de pacotes
LoRa.receive();
}
Master.ino – setup
In this part, we do the configurations that involve the Master, the configurations on the compilation, the interval
between shipments, as well as the time of the last shipment. We also call up the initial display and LoRa settings.
void setup(){
Serial.begin(115200);
//Chama a configuração inicial do display
setupDisplay();
//Chama a configuração inicial do LoRa
setupLoRa();
display.clear();
display.drawString(0, 0, "Master");
display.display();
}
Master.ino - loop
In this Loop, we define the sending of the package to inform the Slave that we’d like to receive data and also
check if there are packages to be received.
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 7
void loop(){
//Se passou o tempo definido em INTERVAL desde o último envio
if (millis() - lastSendTime > INTERVAL){
//Marcamos o tempo que ocorreu o último envio
lastSendTime = millis();
//Envia o pacote para informar ao Slave que queremos receber os dados
send();
}
Master.ino - send
We initialize the package and send what is contained in "GETDATA", to finalize and send the package later.
void send(){
//Inicializa o pacote
LoRa.beginPacket();
//Envia o que está contido em "GETDATA"
LoRa.print(GETDATA);
//Finaliza e envia o pacote
LoRa.endPacket();
}
Master.ino - receive
In this step, we check if the packet has the minimum length of characters that we expect and store a string. We
also analyze if the header is what we expect, and we begin to read the data and show it on the display.
void receive(){
//Tentamos ler o pacote
int packetSize = LoRa.parsePacket();
Master.ino - showData
Finally, we show the time the Master took to create the package, send it, receive the Slave, read it, create a new
package and send it to the Master that receives and reads it. This is printed on the display.
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 8
void showData(Data data){
//Tempo que demorou para o Master criar o pacote, enviar o pacote,
//o Slave receber, fazer a leitura, criar um novo pacote, enviá-lo
//e o Master receber e ler
String waiting = String(millis() - lastSendTime);
//Mostra no display os dados e o tempo que a operação demorou
display.clear();
display.drawString(0, 0, String(data.temperature) + " C");
display.drawString(0, 16, String(data.pressure) + " Pa");
display.drawString(0, 32, String(data.humidity) + "%");
display.drawString(0, 48, waiting + " ms");
display.display();
}
#endif
Starting the Slave code, we compiled only if the Master is not defined in the main file. We have included the
libraries and pointed out the person responsible for reading the temperature, pressure, and humidity.
#include <adafruit_sensor.h>
#include <adafruit_bme280.h>
Slave.ino - Setup
void setup(){
Serial.begin(115200);
//Chama a configuração inicial do display
setupDisplay();
//Chama a configuração inicial do LoRa
setupLoRa();
display.clear();
display.drawString(0, 0, "Slave esperando...");
display.display();
}
Slave.ino - Loop
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 9
As in the Master, in this part of the Slave Loop, we check if it has the expected number of characters and store the
data in a String. We also simulated the reading of the data. We create the package, finalize it, and send it. Finally,
we show the information on the display.
void loop(){
//Tenta ler o pacote
int packetSize = LoRa.parsePacket();
if(received.equals(GETDATA))
{
//Faz a leitura dos dados
Data data = readData();
Serial.println("Criando pacote para envio");
//Cria o pacote para envio
LoRa.beginPacket();
LoRa.print(SETDATA);
LoRa.write((uint8_t*)&data, sizeof(data));
//Finaliza e envia o pacote
LoRa.endPacket();
showSentData(data);
}
}
}
Slave.ino - readData
In this part, we deal with the function where the data is read.
Slave.ino - showSentData
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 10
void showSentData(Data data)
{
//Mostra no display
display.clear();
display.drawString(0, 0, "Enviou:");
display.drawString(0, 16, String(data.temperature) + " C");
display.drawString(0, 32, String(data.pressure) + " Pa");
display.drawString(0, 48, String(data.humidity) + "%");
display.display();
}
#endif
INO
The Super Long Distance WiFi of the ESP32 LoRa With Arduino IDE: Page 11