256dpi / arduino-mqtt

MQTT library for Arduino
MIT License
1.01k stars 232 forks source link

Anyone having softwareserial working with mqtt? #240

Closed Teddyz closed 1 year ago

Teddyz commented 3 years ago

I am running ESP8266 with all latest releases. My program is long and complicated. And I added https://github.com/plerup/espsoftwareserial to be able to control my 3D printer in one of my nodes.

It works fairly stable until I connect to mqtt, then received data is corrupt. Below is an example when I run 38400 bps. It is usable at 4800 bps (~one error per 100 bytes). I also have a Telnet server on my ESP8266. And that can be connected without problems, so I think I have isolated my problem to mqtt. I have > than 10 kB free ram.

#include <MQTTClient.h>
#include "ESPTelnet.h"
#include <SoftwareSerial.h> // https://github.com/plerup/espsoftwareserial

#define PIN_SWSER_RX 14  // D5 -> Marlin D16 
#define PIN_SWSER_TX 12  // D6 -> Marlin D17
void init_mainfunction() {
  swSer.begin(38400, SWSERIAL_8N1, PIN_SWSER_RX, PIN_SWSER_TX, false, 250); // invert, bufCapacity=64, isrBufCapacity=0
  pinMode( PIN_SWSER_RX, INPUT); // To override INPUT_PULLUP
}
void swSer_check() {
  static char buf[ 128 ];
  static uint16_t pos = 0;
  static uint32_t when = millis();

  while ( swSer.available() ) {
    char x = swSer.read();
    yield();
    if ( pos > 0 && ( x == '\r' || x == '\n' || x == 0x00 || pos >= ( sizeof( buf ) - 2 ) || ( millis() - when ) > 1500 ) ) { // complete message or full buffer or timeout
      log( 1, p( "< %s", buf ) ); // My function to add date/time and send received text to Serial/MQTT/Telnet
      pos = 0; // reset buffer
    }
    else if ( x != 0x00 && x != '\r' && x != '\n' ) {
      buf[ pos++ ] = x;
      buf[ pos ] = 0x00;
      when = millis();
    }
  }
}

Example:
2021-03-05 11:09:24.831 1 < cho:busy: pausAd fo⸮ usep
2021-03-05 11:09:28.826 1 < e⸮ho:b paued ⸮or u
2021-03-05 11:09:28.832 1 < c⸮o:busy: pausedfor ⸮ser
2021-03-05 11:09:30.830 1 < ecxo:buy: 
2021-03-05 11:09:32.827 1 < aused for user⸮
2021-03-05 11:09:34.827 1 < cho:busy: pa⸮2u
2021-03-05 11:09:34.833 1 < cho:busy: pau⸮ed for
2021-03-05 11:09:34.834 1 < user
2021-03-05 11:09:36.833 1 < ⸮cho:⸮ pa⸮⸮for
2021-03-05 11:09:36.835 1 < user
2021-03-05 11:09:40.829 1 < echK⸮⸮⸮ ⸮ause` fo⸮ use⸮
2021-03-05 11:09:40.835 1 < cK⸮⸮⸮ pusedfor user
2021-03-05 11:09:42.835 1 < Y⸮⸮⸮⸮⸮: p]2⸮⸮use⸮
2021-03-05 11:09:44.836 1 < ec@o:buy: X]fop usEr
2021-03-05 11:09:46.833 1 < eho:bpsy:
2021-03-05 11:09:46.836 1 < paused for us}r
2021-03-05 11:09:50.831 1 < ech:bus: p]2⸮⸮ use⸮
2021-03-05 11:09:52.831 1 < cK⸮⸮⸮ pausedaser
2021-03-05 11:09:52.838 1 < ⸮K⸮⸮⸮ paued f⸮r usgr
2021-03-05 11:09:54.838 1 < ec⸮o:buCy: ⸮ause` fo⸮ usep
2021-03-05 11:09:55.642 1 Forced MQTT disconnect after 665 seconds was SUCCESSFUL
2021-03-05 11:09:56.838 1 < echo:busy: paused for user
2021-03-05 11:09:58.838 1 < echo:busy: paused for user
2021-03-05 11:10:00.839 1 < echo:busy: paused for user
2021-03-05 11:10:02.840 1 < echo:busy: paused for user
2021-03-05 11:10:04.840 1 < echo:busy: paused for user
2021-03-05 11:10:06.840 1 < echo:busy: paused for user
2021-03-05 11:10:08.841 1 < echo:busy: paused for user

When connected to mqtt:
2021-03-05 11:17:29.920 -1 ESP.getMaxFreeBlockSize 17024 -> 17456
2021-03-05 11:17:29.928 -1 ESP.getFreeHeap 20864 -> 20192

I wonder if anyone has mqtt+softwareserial working together, and if anyone have thoughts on what can be wrong?

256dpi commented 1 year ago

Closing a stale, please reopen if issues persists.