Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. SoftwareSerial dropped packages

SoftwareSerial dropped packages

Scheduled Pinned Locked Moved Development
4 Posts 3 Posters 839 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TimDe
    wrote on last edited by
    #1

    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 ;)

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kimot
      wrote on last edited by
      #2

      Try other software serial libraries.
      AltSoftSerial
      NeoSWSerial

      1 Reply Last reply
      1
      • T Offline
        T Offline
        TimDe
        wrote on last edited by
        #3

        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!

        YveauxY 1 Reply Last reply
        0
        • T TimDe

          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!

          YveauxY Offline
          YveauxY Offline
          Yveaux
          Mod
          wrote on last edited by
          #4

          @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...

          http://yveaux.blogspot.nl

          1 Reply Last reply
          1
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          16

          Online

          11.7k

          Users

          11.2k

          Topics

          113.1k

          Posts


          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • MySensors
          • OpenHardware.io
          • Categories
          • Recent
          • Tags
          • Popular