Section of Code Repeating and Repeating

Hi all,

I have a problem where once the ESP32 has received a BT message from a phone it will send the message via LoRa to another ESP32 multiple times when I only want it to send the LoRa message once!

I think the problem might be in this piece of code:

  if (rxValue.length() > 0 && newData == false) {
    receivedChars[rxValue.length() + 1] = {};
    strcpy(receivedChars, rxValue.c_str());
    newData = true;
  }
    }
};

That piece of code relates to this piece of code:

void sendNewData() {
  if (newData == true) {
    LoRa.beginPacket(); //begin the LoRa packet
    LoRa.print(receivedChars); //include the variiable "counter"
    LoRa.endPacket(); //end and send the LoRa packet

    newData = false;

    Serial.println ("Message Sent:");
    Serial.println (receivedChars);

    display.clearDisplay(); //clear OLED screen
    display.setCursor(0, 0); //start at top left of screen
    display.println("MESSAGE SENT"); //display text "LORA SENDER"
    display.display();
    delay (5000);
  }
}
}

What am I doing wrong?

Thanks,

Zeb

…All of my code:

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
bool BT = false;

std::string rxValue; // Could also make this a global var to access it in loop()

#define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" //used for receiving data with "WRITE"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" //used to send data with  "NOTIFY"

//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>

//Libraries for OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

/*DEFINE THE PINS USED BY LORA TRANSCEIVER MODULE*/
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26

/*SET THE CHANNEL AND FREQUENCY OF LORA TRANSCEIVER*/
#define BAND 915E6 //915MHz, E6 channel

/*DEFINE THE PINS USED BY THE OLED SCREEN*/
#define OLED_SDA 4
#define OLED_SCL 15
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST); //WHAT DOES THIS DO?

char loraSyncWord[255];

char myID[255];

const byte numChars = 255;

char receivedChars[numChars];   // an array to store the received data from the Serial Monitor

char loraDataChar[numChars]; //An Array to copy the loraData String contents to

char lastBTData[numChars];

String loraData; //Received LoRa data goes in this String

boolean newData = false;

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      rxValue = pCharacteristic->getValue();
    }
};

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

  BLEDevice::init("Zeb's BT ESP32"); // Give it a name

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID_TX,
                      BLECharacteristic::PROPERTY_NOTIFY
                    );

  pCharacteristic->addDescriptor(new BLE2902());

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID_RX,
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service
  pService->start();

  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");

  /*RESET OLED DISPLAY VIA SOFTWARE*/
  pinMode(OLED_RST, OUTPUT);
  digitalWrite(OLED_RST, LOW);
  delay(20);
  digitalWrite(OLED_RST, HIGH);

  /*INIALIZE THE OLED DISPLAY*/
  Wire.begin(OLED_SDA, OLED_SCL);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32 OLED
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  /*SETUP THE OLED SCREEN AND DISPLAY SOME TEXT*/
  display.clearDisplay(); //clear the OLED display
  display.setTextColor(WHITE); //Set the text colour to white
  display.setTextSize(1); //set the text size
  display.setCursor(0, 0); //start at top left of OLED screen
  display.print("LORA TRANSCEIVER "); //display title "LORA TRANSCEIVER"
  display.display(); //WHAT DOES THIS DO?

  /*DEFINE SOME PINS FOR OLED AND LORA MODULE*/
  SPI.begin(SCK, MISO, MOSI, SS);  //SPI LoRa pins
  LoRa.setPins(SS, RST, DIO0);   //setup LoRa transceiver module
  LoRa.setSyncWord(0x45); //Set the "group" ID

  /*START-UP LORA MODULE?*/
  if (!LoRa.begin(BAND)) { //if LoRa does to begin 915E6?
    Serial.println("Starting LoRa failed!");
    while (1);
  }

  /*SET THE LORA MODULES SPREADING FACTOR, SYNC WORD*/
  LoRa.setSpreadingFactor(12); //LoRa spreading factor can range from 7 to 12 (Minimize bandwidth and maximize spreading factor to boost link budget. Maximize coding rate to boost reliability)

  Serial.println("Please enter your ID");

  while (Serial.available() < 1) {
  }

  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      strcpy(myID, receivedChars); //(char *)(strstr(receivedChars);
      Serial.print ("ID received, your ID is: ");
      Serial.println (myID);
    }
  }

  /*TELL USER THAT LORA SETUP WAS SUCCESSFUL VIA OLED SCREEN*/
  display.setCursor(0, 10); //OLED screen second line far left
  display.print("LoRa Initializing OK!"); //display on OLED screen
  display.display(); //WHAT DOES THIS DO?
  delay(2000);
}

void loop() {
  display.clearDisplay(); //clear OLED screen
  display.setCursor(0, 0); //start at top left of screen
  display.println("LORA TRANSCEIVER"); //display text "LORA TRANSCEIVER"
  display.display();

  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    //read packet
    while (LoRa.available()) {
      loraData = LoRa.readString();
    }

    //print RSSI of packet
    int rssi = LoRa.packetRssi();

    loraData.toCharArray(loraDataChar, numChars);

    if (strchr(loraDataChar, '@') == NULL) {
      Serial.println ("Message Received:");
      Serial.println (loraDataChar);

      if (deviceConnected) {
        pCharacteristic->setValue(loraDataChar); // Sending a test message
        pCharacteristic->notify(); // Send the value to the app!
      }

      /*DISPLAY ON OLED SCREEN THAT PACKET HAS BEEN RECEIVED*/
      display.clearDisplay();
      display.setCursor(0, 0);
      display.print("MESSAGE RECEIVED");
      display.setCursor(0, 55);
      display.print("RSSI:");
      display.print(rssi);
      display.display();
      delay (5000);
    }

    /*DISPLAY RECEIVED LORA PACKET TO YOU IF YOUR ID IS INCLUDED*/
    if (strchr(loraDataChar, '@') != NULL) {
      if (strstr(loraDataChar, myID)) { //if data received includes my ID
        strcpy(loraDataChar, (char *)(strstr(loraDataChar, myID) + strlen(myID) + 1));
        Serial.println ("Message Received:");
        Serial.print ("*");
        Serial.println (loraDataChar);

        if (deviceConnected) {
          pCharacteristic->setValue(loraDataChar); // Sending a test message
          pCharacteristic->notify(); // Send the value to the app!
        }

        /*DISPLAY ON OLED SCREEN THAT PACKET HAS BEEN RECEIVED*/
        display.clearDisplay();
        display.setCursor(0, 0);
        display.print("MESSAGE RECEIVED");
        display.setCursor(0, 55);
        display.print("RSSI:");
        display.print(rssi);
        display.display();
        delay (5000);
      }
    }
  }

  recvWithEndMarker();
  sendNewData();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
  if (rxValue.length() > 0 && newData == false) {
    receivedChars[rxValue.length() + 1] = {};
    strcpy(receivedChars, rxValue.c_str());
    newData = true;
  }
}

void sendNewData() {
  if (newData == true) {
    LoRa.beginPacket(); //begin the LoRa packet
    LoRa.print(receivedChars); //include the variiable "counter"
    LoRa.endPacket(); //end and send the LoRa packet

    newData = false;

    Serial.println ("Message Sent:");
    Serial.println (receivedChars);

    display.clearDisplay(); //clear OLED screen
    display.setCursor(0, 0); //start at top left of screen
    display.println("MESSAGE SENT"); //display text "LORA SENDER"
    display.display();
    delay (5000);
  }
}