LoRa SX1278/ESP32 relay control

Hi Everyone,

I have been playing around with this project for a few weekends now and it is driving me crazy. I have already made this project with a simple 433 Tx/Rx setup using RCSwitch but I am trying to get it working on the ESP 32 with LoRa.

The concept is just to switch a pump on when the water level is low in a tank and off when its high.

Can someone give me a nudge in the right direction of what I am missing please ?

I just can not figure out how to store the information received and then use it to switch the relay.

Below is what I have so far.. be gentle.

Cheers

Crumpy

//Tank water level Rxr 433Mhz LoRa ESP32

/*-----( Declare Constants )-----*/

#include <SPI.h>
#include <LoRa.h>

int state;

// WIFI_LoRa_32 ports

// GPIO5  -- SX1278's SCK
// GPIO19 -- SX1278's MISO
// GPIO27 -- SX1278's MOSI
// GPIO18 -- SX1278's CS
// GPIO14 -- SX1278's RESET
// GPIO26 -- SX1278's IRQ(Interrupt Request)

#define SS      18
#define RST     14
#define DI0     26
#define BAND    433E6

/*-----( Declare Variables )-----*/


/*----( SETUP: RUNS ONCE )----*/
void setup() {
  Serial.begin(115200);
  
  pinMode(16, OUTPUT);    // sets the pin as output for relay
  
  while (!Serial);        //if just the the basic function, must connect to a computer
  delay(1000); 
  
  SPI.begin(5,19,27,18);
  LoRa.setPins(SS,RST,DI0);
  Serial.println("LoRa Receiver");
  
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initial OK!");
} 


/*----( LOOP: RUNS CONSTANTLY )----*/
void loop() {

  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
  // received a packet
    Serial.print("Received packet :");
  // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());     
  } 
  while (LoRa.available()) {
  Serial.println(); 
  state = ((char)LoRa.read());
  
    }
 
   if (state == 1)   //If this code received then turn on pump
      {
      Serial.println("Pump ON");
      digitalWrite(16,HIGH);    //Pump relay ON
      delay(500);
      }

      if (state == 0)  //If this code received then turn off pump
      {
        Serial.println("Tank Full - Pump OFF");
        digitalWrite(16,LOW);   //Pump relay OFF
      }
  }  
  
}

I just can not figure out how to store the information received

But, you did figure out how to read it, and write it to the serial port. Quit just throwing the data away.

Serial Input Basics specifically refers to serial data, but the concepts work for ANY stream of data.

crumpy10:
Can someone give me a nudge in the right direction of what I am missing please ?

I just can not figure out how to store the information received and then use it to switch the relay.

Below is what I have so far.. be gentle.

Go through the code and add comments as to what you intend each line of code to do.

These comments and a general explanation of what the code is supposed to do, will help the forum readers to understand what it is you are trying to do.

I also suspect that if you read through the code adding the comments you will see what the problem is.