Introduction: ESP32 LORA - Alarm With IR Barrier

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.

Considering that it offers a solution to many current problems, today I am continuing the discussion concerning the ESP32 LORA, a marvel for this Internet of Things world. In this article, I deal again with safety when discussing an infrared (IR) barrier alarm. Our project is formed by two communicating ESP32 LORAs.

One will be the ISSUER and will be connected to the infrared barrier sensor. The other, the RECEIVER, will be connected to a relay module. Each second, the ISSUER sends a code to the RECEIVER with the current state of the barrier. All of this is performed to avoid problems that often occur with the connection of barriers while using wires. Therefore, our intention is to replace all wiring by LORA radios.

Step 1: LORA Esp32 OLED

Step 2: Relay Module

Step 3: Active Infrared Sensor (VAT)

Characteristics:

• Double Beam

• Sight adjustment

• This infrared sensor emits infrared light through one point, and receives that signal at another point.

• It’s like there’s an invisible line, and when it is crossed, the alarm is triggered.

• Due to this feature, the active sensor is also called a barrier sensor or perimeter sensor.

• Regarding the operating distance, it may vary according to the characteristics and manufacturer of the sensor.

• It is important to note that this distance must be informed by the manufacturer of the Active Infrared Sensor (IVA).

• In the case of rain or mist, the operating distance drops considerably (approximately in half).

Operation:

• The Active Infrared Sensor (IVA) is a transmitter and receiver pair of infrared light signals invisible to humans.

• An invisible line forms where the transmitter emits infrared light signals, and the receiver receives these signals.

• The interruption of this light signal triggers an alarm control panel or triggers any other necessary controls.

• In order to avoid false alarms caused by the accidental interruption of the light beam, there are two light beam sensors, which must be interrupted simultaneously to generate an alarm.

Step 4: Assembly

Emissor: Sender

Receptor: Receiver

Step 5: Libraries

Add the following "Adafruit SSD1306" library.

Simply access "Sketch >> Include Libraries >> Manage Libraries ..."

Step 6: Software

We will create two programs: one for the ESP32 LORA that will work as an EMITTER, and another program for the LORA ESP32 that will work as a RECEIVER.

Step 7: SENDER Program

The SENDER program will be configured to check the status of the barrier. Every second, it will send the current status of the barrier to the RECEIVER. Also, the display will always update.

SENDER Program

Let's include libraries and make LORA chip definitions.

#include <SPI.h>
#include <LoRa.h> #include <Wire.h> #include "SSD1306.h"

//pino rele #define PIN_RELAY 23

// Pin definetion of WIFI LoRa 32 // HelTec AutoMation 2017 support@heltec.cn #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 PABOOST true

long int frequency = 433000000;

int statusRelay = LOW;

SSD1306 display(0x3c, 4, 15);

Setup

In this step, we will define the LORA pins and make the display settings.

void setup() {
pinMode(16,OUTPUT); pinMode(2,OUTPUT);

Serial.begin(115200);

pinMode(PIN_RELAY, INPUT); digitalWrite(16, LOW); // set GPIO16 low to reset OLED delay(50); digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high

display.init(); display.flipScreenVertically(); display.setFont(ArialMT_Plain_10); delay(1500); display.clear(); SPI.begin(SCK,MISO,MOSI,SS); LoRa.setPins(SS,RST,DI00); if (!LoRa.begin(frequency,PABOOST)) { display.drawString(0, 0, "Starting LoRa failed!"); display.display(); while (1); } LoRa.enableCrc(); display.drawString(0, 0, "LoRa Initial success!"); display.display(); delay(1000); }

Loop

In the Loop, we will configure the relay and set the operation of the LEDs.

void loop()
{ //recupera o estado do relé statusRelay = digitalRead(PIN_RELAY); //se o relé ativou if( statusRelay == HIGH ) { refreshDisplay("Estado Normal"); } else{ refreshDisplay("Ativar Alarme"); } display.drawString(0,45,"Freq: "+String(frequency)); display.display();

sendPacket(statusRelay);

//acende LED interno para indicar que enviou o pacote e da um delay digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level) delay(700); // wait for a second digitalWrite(2, LOW); // turn the LED off by making the voltage LOW delay(500); // wait for a second }

sendPacket and refreshDisplay

Now, let's take a look at the parameters of the barrier status, the sending of data packets, and the updating of display information.

//envia o pacote na rede Lora
//parâmetro: status da barreira (HIGH | LOW) void sendPacket(int status) { // envia um pacote LoRa.beginPacket(); LoRa.print("="); //delimitador LoRa.print(status); //1(ativo) ou 0(inativo) LoRa.print("#"); //delimitador LoRa.endPacket(); }

//atualiza as informações no display void refreshDisplay(String str) { display.clear(); display.setTextAlignment(TEXT_ALIGN_LEFT); display.setFont(ArialMT_Plain_16); display.drawString(0, 0, str); display.display(); }

Step 8: RECEPTOR Program

The RECEIVER program will listen to the LORA network. Observe the following, for the cases when it receives a package with the following format:

= S #

Where: S can be 1 (enabled) or 0 (disabled)

= and # are delimiters

According to S, we will activate or deactivate the relay connected to it.

RECEPTOR Program

This time in the Receiver, we will include the libraries and make definitions of the LORA chip.

#include <SPI.h>
#include <LoRa.h> #include <Wire.h> #include "SSD1306.h"

// Pin definetion of WIFI LoRa 32 // HelTec AutoMation 2017 support@heltec.cn #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 PABOOST true

#define PIN_RELAY 23

SSD1306 display(0x3c, 4, 15); String rssi = "RSSI --"; String packSize = "--"; String packet ;

long int frequency = 433000000;

int statusRelay = LOW;

Setup

Unlike the Sender, in the Receiver, the PIN_RELAY will be OUTPUT, because here this relay should be activated. In this part of the code, we still work with the display and the LORA chip.

void setup() {
Serial.begin(115200);

pinMode(PIN_RELAY, OUTPUT); pinMode(16,OUTPUT); digitalWrite(16, LOW); // set GPIO16 low to reset OLED delay(50); digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high、 display.init(); display.flipScreenVertically(); display.setFont(ArialMT_Plain_10);

delay(1500); display.clear(); SPI.begin(SCK,MISO,MOSI,SS); LoRa.setPins(SS,RST,DI00); if (!LoRa.begin(frequency,PABOOST)) { display.drawString(0, 0, "Starting LoRa failed!"); display.display(); while (1); } LoRa.enableCrc(); display.drawString(0, 0, "LoRa Initial success!"); display.drawString(0, 10, "Wait for incomm data..."); display.display(); delay(1000); LoRa.receive(); }

Loop and parserPacket

In this part, we configure the printing on the display and deal with the data package.

void loop() {
int packetSize = LoRa.parsePacket(); //imprime na tela a frequencia atual que está configurada display.drawString(0,45,"Freq: "+String(frequency)); display.display(); if (packetSize) { parserPacket(packetSize); } }

//faz o parser do pacote recebido para tratar os dados void parserPacket(int packetSize) { packet =""; packSize = String(packetSize,DEC); for (int i = 0; i < packetSize; i++) { packet += (char) LoRa.read(); } rssi = "RSSI " + String(LoRa.packetRssi(), DEC) ; parserData(); loraData(); }

parserData and loraData

Finally, here is a function that activates the relay, as well as the last settings that involve the display.

void parserData()
{ // Serial.println(packet); //o primeiro byte do pacote tem o indice da frequencia que o emissor está usando, // colocamos esse valor em CMD para fazeras verificações String c1 = String(packet.charAt(0)); if(c1 == "="){ int c2 = String(packet.charAt(1)).toInt(); String c3 = String(packet.charAt(2)); if(c3 == "#"){ digitalWrite(PIN_RELAY,c2); } } }

void loraData(){ display.clear(); display.setTextAlignment(TEXT_ALIGN_LEFT); display.setFont(ArialMT_Plain_16); display.drawString(0, 5, rssi); display.drawString(0 , 20 , packet); display.display(); }