Hello everyone,
I have 2 ESP 32 Lora boards from Heltec.
Now I'm trying to establish a point-to-point connection, there is a master and a slave. The master polls the slave with the "Get Data1" packet.
The slave responds with a series of data which it receives via the serial interface. The whole thing works for about 4-6h. The salve then no longer receives any data (Polling).
If I now press the reset button on the slave, the communication works again.
Ao the Serialmonitor i can see that the Master ist polling the Slave.
Thanks in advance
Enclosed the code of the Reciver
#include "heltec.h"
//#include "SoftwareSerial.h"
#include "Arduino.h"
//SoftwareSerial SWSerial2;
#define BAND 868E6 //you can set band here directly,e.g. 868E6,915E6
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
float temperature = 0.0;
float humidity = 0.0;
float wind = 0.0;
boolean newData = false;
String LoRaData;
String incoming;
String ID = "01";
bool Sendrequest;
void setup()
{
pinMode(25, OUTPUT);
Heltec.begin(false /DisplayEnable Enable/, true /Heltec.LoRa Enable/, true /Serial Enable/, true /PABOOST Enable/, BAND /long BAND/);
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8N1, 16, 17);
LoRa.setSyncWord(0xF4);
LoRa.setCodingRate4(10);
LoRa.setSpreadingFactor(12);
//LoRa.setTxPower(10,);
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial2.available() > 0 && newData == false)
{
rc = Serial2.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 parseData()
{ // split the data into its parts
char *strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
temperature = atof(strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
humidity = atof(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
wind = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData()
{
Serial.print("Temperatur:");
Serial.println(temperature);
Serial.print("Feuchte:");
Serial.println(humidity);
Serial.print("Wind:");
Serial.println(wind);
}
//void sendMessage(String outgoing)
void sendMessage()
{
LoRaData = ID + "T" + String(temperature) + "H" + String(humidity) + "W" + String(wind);
//Send LoRa packet to receiver
LoRa.beginPacket();
LoRa.print(LoRaData);
LoRa.endPacket();
Sendrequest = false;
Serial.println("Reply");
digitalWrite(25, LOW);
}
void onReceive(int packetSize)
{
if (packetSize == 0)
return; // if there's no packet, return
while (LoRa.available())
{
incoming = LoRa.readString();
}
if (incoming == "GetData1")
{
Sendrequest = true;
Serial.println("Request");
}
}
void loop()
{
if (Sendrequest == true)
{
digitalWrite(25, HIGH);
sendMessage();
}
Serial.print("alive");
// parse for a packet, and call onReceive with the result:
onReceive(LoRa.parsePacket());
//Serial.println(Serial2.readString());
recvWithStartEndMarkers();
if (newData == true)
{
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
//showParsedData();
newData = false;
}
// parse for a packet, and call onReceive with the result:
// onReceive(LoRa.parsePacket());
}