I can print a serial array but not use it in an IF statement

Hi Everyone,

I have been trying to learn a bit more in depth Arduino by making a LoRa Tx/Rx to activate a water pump that fills a tank then switches off. Yes I have used various parts of different sketches and some of my own working out but I am really stuck on this one last problem. The Tx’r sends a message , the Rx’r gets the message, with start and end markers, parses the data and stores it in ‘receivedChars’. The sketch then prints the message received perfectly but it does nothing else! From line 104 on the Rx’r sketch does nothing.
If I have put the serial data received in an array (receivedChars) and it prints it to the serial monitor, why wont my IF statements work?
I am a pretty green programmer so go easy on me. I know there is a fair amount of copy and paste but thats how I learn.

I have been round and round in circles and its driving me mad now. I have a feeling the start and end markers may be throwing something out but I do not know why?

Any pointers would be much appreciated.

Cheers

Crumpy

Tx’r sketch-:

//Tank water level indicator with 433Mhz ESP32 & LoRa Tx'r

/*-----( Declare Constants )-----*/
#include <WiFi.h>
#include <SPI.h>
#include <LoRa.h>
#include <Arduino.h>
const byte ReedPinLow = 16;   //byte is okay
const byte ReedPinHigh = 17;  //make all pin definitions a variable

// 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  //433Mhz selected

/*-----( Declare Variables )-----*/
bool pumpOn = false;

 
void setup(){ /*----( SETUP: RUNS ONCE )----*/
  
  WiFi.mode(WIFI_OFF);
  btStop();
  
  
  Serial.begin(115200);

  //Set pins as inputs and outputs
  pinMode(ReedPinHigh, INPUT);
  pinMode(ReedPinLow, INPUT);

  while (!Serial); //If just the the basic function, must connect to a computer

LoRa.setSpreadingFactor(12);
LoRa.setSignalBandwidth(125E3);
LoRa.setTxPower(17, PA_OUTPUT_PA_BOOST_PIN);
LoRa.setCodingRate4(5);
LoRa.setSyncWord(0xF3);
LoRa.setPreambleLength(6);
//LoRa.enableCrc();
  
  SPI.begin(5,19,27,18);
  LoRa.setPins(SS,RST,DI0);
  Serial.println("LoRa Sender");

  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initial OK!");
}
 
void loop() {/*----( LOOP: RUNS CONSTANTLY )----*/

  
  //Water low and pump on
  if (digitalRead(ReedPinLow) && !pumpOn){
    Serial.println("Bottom sensor triggered, Pump Start");
    
      LoRa.beginPacket();               //Pump start Tx
      LoRa.print("<empty>");
      LoRa.endPacket();   
        
    pumpOn = true; //remember we started the pump
    delay(1000);
  }
  //Water high and pump off
  
  else if (digitalRead(ReedPinHigh) && pumpOn){
    Serial.println("Top sensor triggered, Tank Full Pump Stop");
    
      LoRa.beginPacket();     //Pump stop code Tx           
      LoRa.print("<full>");
      LoRa.endPacket();
      
    pumpOn = false; //remember we turned off the pump
    delay(1000);
  }
}

Rx’r Sketch-:

//Tank water level Rxr 433Mhz LoRa ESP32

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

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

// 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 )-----*/

const byte numChars = 32;
char receivedChars[numChars]; //an array to store received data

boolean newData = false;
int relayPin = 16;
int relayState;

/*----( SETUP: RUNS ONCE )----*/

void setup() {
  
  Serial.begin(115200);
  
  pinMode(relayPin, OUTPUT);    // sets the pin as output for relay
  relayPin = relayState;
  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() {

    int packetSize = LoRa.parsePacket();      // try to parse packet
    if (packetSize) {
       Serial.print("Received packet :");      // received a packet
       recvWithStartEndMarkers();
       showNewData();
    }
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (LoRa.available() > 0 && newData == false) {
        rc = LoRa.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}
   
void showNewData() {
    if (newData == true) {
        //Serial.print("This just in ... ")
        Serial.println(receivedChars);
        newData = false;
    }
 
    if (receivedChars == "empty")   //If this code received then turn on pump
      {
      Serial.println("Pump ON");
      digitalWrite(16,HIGH);    //Pump relay ON
      relayState = HIGH;
      delay(100);
      }

      if (receivedChars == "full")  //If this code received then turn off pump
      {
        Serial.println("Tank Full - Pump OFF");
        digitalWrite(16,LOW);   //Pump relay OFF
        relayState = LOW;
        delay(100);
      }
}
if (receivedChars == "empty")

You cannot compare strings like that. Use strcmp() instead

Hi Bob,

Thanks for the hint but I really am a learner here, sorry I dont understand.

Would you mind explaining a little please?

Cheers

Crumpy

An example :

char aMessage[] = {"hello"};

void setup()
{
  Serial.begin(115200);
  if (aMessage == "hello")  //the wrong way
  {
    Serial.println("matched using ==");
  }
  if (strcmp(aMessage, "hello") == 0) //the right way
  {
    Serial.println("matched using strcmp()");
  }
}

void loop()
{
}

Hey Bob,

Thanks very much, that worked a treat but I have not got a clue how the program flow works. Why is it comparing a zero?

Here is my working Rx’r code for future people if they want it. Its working on a LoRa Heltec ESP32 clone. Later I intend it to be connected to a float tube with hall sensors in it to detect the water level.

//Tank water level Rxr 433Mhz LoRa ESP32

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

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

// 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 )-----*/

const byte numChars = 32;
char receivedChars[numChars]; //an array to store received data

boolean newData = false;
int relayPin = 16;
int relayState;

/*----( SETUP: RUNS ONCE )----*/

void setup() {
  
  Serial.begin(115200);
  
  pinMode(relayPin, OUTPUT);    // sets the pin as output for relay
  relayPin = relayState;
  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() {

    int packetSize = LoRa.parsePacket();      // try to parse packet
    if (packetSize) {
       Serial.print("Received packet :");      // received a packet
       recvWithStartEndMarkers();
       showNewData();
    }
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (LoRa.available() > 0 && newData == false) {
        rc = LoRa.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}
   
void showNewData() {
    if (newData == true) {
        //Serial.print("This just in ... ")
        Serial.println(receivedChars);
        newData = false;
    }
 
    if (strcmp(receivedChars, "empty") == 0)  //If this code received then turn on pump
      {
      Serial.println("Pump ON");
      digitalWrite(16,HIGH);    //Pump relay ON
      relayState = HIGH;
      delay(100);
      }

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

Why is it comparing a zero?

Because strcmp() returns 0 on a match.