I have an issue with missed Serial data. I don´t know how to solve it. I am only sending/receiving very small packages which are under 10 Bytes each, so I believe it can´t be an overflow issue.
If Serial data is available I am running a while loop until there is no data for 1 second and then publish the data via MQTT.
I double checked with my logic analyzer and there are two messages about 5 seconds apart, but only the first one is recognized and send. The behaviour definitely happens repeatedly
Here is the critical code part:
void loop()
{
static unsigned long tReconnect = 0;
int iLast = 0;
char received[100] = {};
if (client.isConnected())
client.loop();
else if(millis() - tReconnect > 30000)
{
client.connect("Siro-Gateway");
if (client.isConnected())
{
client.publish("MQTT/SiroGateway/online","1");
client.subscribe("MQTT/SiroGateway/set/#");
}
tReconnect = millis();
}
if(iLast = Receive(received))
{
client.publish("MQTT/SiroGateway/received", received);
for(int i = 0; i <= iLast; i++) {
received[i] = 0;
}
iLast = 0;
}
}
int Receive(char received[])
{
int byteCount = 0;
int i = 0;
int iLast = 0;
unsigned long tLast = 0;
while(Serial1.available() || ((micros() - tLast) < 1000))
{
if(Serial1.available())
{
byteCount = Serial1.available();
for(i = iLast; i < byteCount + iLast; i++) {
received[i] = Serial1.read();
}
iLast = i;
tLast = micros();
}
if((micros() - tLast) > 1000)
return iLast;
}
return 0;
}