SoftwareSerial dropped packages



  • Hi there,

    I am working on a project where I am using one Arduino Uno with a nRF24L01+ module as a Receiver of sensor data. The sketch I am using is:

    #include <SoftwareSerial.h>
    
    #include <SPI.h>
    #include <RF24.h>
    
    SoftwareSerial mySerial(7, 8); // RX, TX
    
    RF24 radio(9,10);
    byte addresses[][6] = {"1MZ","2MZ"};
    
    void setup() {
      mySerial.begin(4800);
      
      radio.begin();
      radio.setPayloadSize(4);
      radio.setDataRate(RF24_2MBPS);
      radio.setPALevel(RF24_PA_MAX);
      radio.openReadingPipe(1,addresses[1]);
      radio.startListening();
    }
    
    void loop() {
      unsigned long AnkommendeNachricht;
          if( radio.available()){
            while (radio.available()) {
              radio.read( &AnkommendeNachricht, sizeof(AnkommendeNachricht));
          } 
          mySerial.println(AnkommendeNachricht);
       }  
    }
    

    When I watch on a Serial monitor, every second I receive a 10 number long message (It works how it should). After receiving I want to send the messages to a Wemos D1mini via SoftwareSerial. After receiving the data with the Wemos I want to split the data in 4 parts and then publish them one after another as a MQTT-message. The code I am using is this:

    #include <ESP8266WiFi.h>
    #include <PubSubClient.h>
    
    #include <SoftwareSerial.h>
    #include <SPI.h>
    
    SoftwareSerial mySerial(D3, D4); // RX, TX
    
    const char* ssid = "...";
    const char* password = "...";
    //const char* mqtt_server = "...";
    const char* mqtt_server = "iot.eclipse.org";
    
    WiFiClient espClient;
    PubSubClient client(espClient);
    char msg[50];
    char Number[1];
    char Idle[3];
    char Work[3];
    char Height[3];
    char Buffer[9];
    char Topic;
    
    void reconnect() {
      while (!client.connect("ESP8266Client")) {
          delay(50);
      }
    }
    
    void setup() {
      mySerial.begin(9600);
    
      WiFi.begin(ssid, password);
      client.setServer(mqtt_server, 1883);
    }
    
    void loop() {
      client.loop();
      if (!client.connected()) {
        reconnect();
      }
      int pos=0;
      if (mySerial.available()) {
        while (mySerial.available()) {
            Buffer[pos++]=mySerial.read();
            delay(3);
        }
    
        }
        for (int i=0; i <3; i++){
          Idle[i]=Buffer[i+1];
        }
        for (int i=0; i <3; i++){
          Work[i]=Buffer[i+4];
        }
        for (int i=0; i <3; i++){
          Height[i]=Buffer[i+7];
        }
    
        snprintf (msg, 50, Idle);
        client.publish("Idle", msg);
        snprintf (msg, 50, Work);
        client.publish("Work", msg);
        snprintf (msg, 50, Height);
        client.publish("Height", msg);
      }
    }
    

    When I now watch the received messages on the MQTT-Broker I find that some messages are not being received. So it seams that some messages are lost due to the SoftwareSerial (If I change the delay, the packet error rate changes too...). Are there any advises regarding this topic? I searched a lot and didn´t find a solution.

    PS: I do not want to use the MQTT-bridge sketch by mysensor 😉



  • Try other software serial libraries.
    AltSoftSerial
    NeoSWSerial



  • I got the solution but I don´t know why the old sketch doen´t work and the new one does. I changed the buffer[] char to a string and changed the delay(3) to delay(1).
    Thanks for the advise anyway! Appreciate it!


  • Mod

    @timde your buffer of 9 characters is definitely too small to store a 10 character message. The code will work, but will overwrite the character(s) located after your buffer.
    Anything can sit there, and it can change over compile iterations, so behavior will be flaky.
    Switching to string, which re-allocates memory dynamically as characters get added to it, will fix this issue.
    I guess you added the delay because you are missing characters, right?
    If so, this is very tricky as it needs to sync to the rate at which new characters come in...


 

384
Online

7.6k
Users

8.5k
Topics

91.2k
Posts