Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

SoftwareSerial: wdt reset

Open supersjimmie opened this issue 10 years ago • 142 comments

I use both SoftwareSerial and WiFiClient. During normal operations, it sometimes works for many hours but other times it gives a wdt reset. This looks like it has a relation between SoftwareSerial and WiFiClient.

If I use mySerial.enableRX(false) before starting the function with the WiFiClient, it seems to keep working, otherwise it looks like to crash on different places.

ets Jan 8 2013,rst cause:4, boot mode:(3,6) wdt reset This was during client.print() operations, the first client.print with the data succeeded, then just after that, only when sending an empty line with client.print("\r\n") it gave the wdt reset.

But earlier, it was just during receiving data on the SoftwareSerial: `ets Jan 8 2013,rst cause:4, boot mode:(1,6)``

And a couple of other times also during normal receiving on the serial: ets Jan 8 2013,rst cause:4, boot mode:(3,7)

So many different boot modes, they all seem only to occur if I don't use the mySerial.enableRX(false).

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

supersjimmie avatar Jan 14 '16 14:01 supersjimmie

Ah, too bad, also when I use enableRX(true) ets Jan 8 2013,rst cause:4, boot mode:(1,6) wdt reset

I think I can better leave the github version for now, 2.0.0 is more stable on this.

supersjimmie avatar Jan 14 '16 20:01 supersjimmie

Please supply some code and information about baud rate and traffic volume

plerup avatar Jan 16 '16 16:01 plerup

After more investigation, the title can be changed to "SoftwareSerial wdt reset".

#include <SoftwareSerial.h>
SoftwareSerial mySerial(D2, -1, true, 128);

char telegram[64];

int readTelegram();

void setup()
{
  Serial.begin(115200);  // 115200  256000
  mySerial.begin(115200);
}

void loop() {
  int t = readTelegram();
  yield();
}

int readTelegram() {
  if (mySerial.available() > 0) {
    int len = 0;
    char c;
    unsigned long timeout = 2;
    unsigned long start = millis();
    while ((len < 63) && (millis() - start < timeout)) {
      c = mySerial.read();
      if  ((c == '\n') || (c == '\r')) break;
      if ((c >= 32) && (c <= 126)) {
        telegram[len++] = c;
      }
      yield();
    }
    telegram[len] = 0;
    return len;
  }
  else return 0;
}

Pin D2 is connected to my "Smart Meter", which generates a 27 lines, all terminated with \r\n. Each line is max 47 char long (shortest is 5 char, longest 47 char). It is at 115200 baud, with inverted signal. The lines are sent every 10 sec.

Sometimes there is some garbage, and sometimes no \r\n is received, so I have a timeout (2mSec) and a filter (c = 32-127).

Earlier I tried ReadBytsUntil, it also crashed. So I now tried to write it with just read() and my own filter and timeout function.

Also tried with a shorter timeout, using micros() and a value of 500.

After running for a couple of minutes, mostly less then an hour, it crashes with a WDT reset.

supersjimmie avatar Jan 18 '16 08:01 supersjimmie

This is a rather large burst of interrupts. Try using something like the code below which intends to read all the lines into a buffer and then treat the lines in the buffer afterwards

  #include <SoftwareSerial.h>

  #define MAX_SIZE 27*47
  SoftwareSerial mySerial(D2, -1, true, MAX_SIZE);

  char buffer[MAX_SIZE];

  void setup()
  {
    Serial.begin(115200);  // 115200  256000
    mySerial.begin(115200);
    Serial.println("Started");
  }

  void loop() {
     int pos = 0;
     while (mySerial.available() && pos < MAX_SIZE) {
        buffer[pos++] = mySerial.read();
     }
     if (pos) {
        // Data received, treat the buffer contents
        Serial.println(pos);
     }
  }

plerup avatar Jan 18 '16 22:01 plerup

BTW, which version are you using?

plerup avatar Jan 18 '16 22:01 plerup

I think when I read all lines into one large buffer, it will still going to crash after some time. It looks like series of lines has 3 different time gaps: Very short gap between each line, a bit short gap just before the last line (which contains a checksum, but that does not matter) and then the long 10 sec gap. I cannot exactly predict when data comes, so there will always be interrupts on unpredicted moments.

BTW: The WDT resets occur with Arduino IDE 1.6.7 and esp8266 latest GIT. Last reset with exactly the code above: rst cause: 4, boot mode:(1,7)

When I use DE 1.6.5 with esp8266 2.0.0 Stable, it runs fine for as long as I let it run. So, why are the interrupts a problem with the latest, while it is not with the older?

(I think, not for sure, I started with softwareserial 2.1.1 or 2.2)

supersjimmie avatar Jan 19 '16 07:01 supersjimmie

I have changed the code, to get the whole telegram before continuing:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(D2, -1, true, 128);

#define MAX_LINES 28
#define MAX_LENGTH 64
char buffer[MAX_LINES * MAX_LENGTH];

int readTelegram();

void setup()
{
  Serial.begin(115200);
  mySerial.begin(115200);
}

void loop() {
  int t = readTelegram();
}

int readTelegram() {
  if (mySerial.available() > 0) {
    memset(buffer, 0, sizeof(buffer));
    int len = 0;
    char c;
    unsigned long timeout = 700;
    unsigned long start = millis();
    while ((len < MAX_LINES * MAX_LENGTH) && (millis() - start < timeout)) {
      if (mySerial.available()) {
        c = mySerial.read();
        yield();
        buffer[len++] = c;
        Serial.write(c);
      }
      yield();
    }
    return len;
  }
  return 0;
}

No luck after a while:

 ets Jan  8 2013,rst cause:4, boot mode:(1,7)
wdt reset

Second attempt, boot mode:(1,6)

supersjimmie avatar Jan 19 '16 17:01 supersjimmie

Having the resets with: Arduino IDE 1.6.5 and 1.6.7. ESP-Arduino libraries 2.0.0 Stable and Latest GIT SoftwareSerial 2.2.

Works with SoftwareSerial 2.1 and all IDE, and all ESP libs. (but with 2.1 I have too much corrupted received data)

supersjimmie avatar Jan 20 '16 05:01 supersjimmie

@plerup I also tried your code example from 2 days ago. It was running for over an hour without a reset. (perhaps I should find time to let it run longer) But... As soon as I made one simple change:

    buffer[pos++] = mySerial.read();

To:

    buffer[pos] = mySerial.read();
    Serial.print(buffer[pos]);
    pos++;

WDT reset within a couple of minutes.

EDIT: Noo, too bad. The original code from you just crashed too with a wdt reset.

 #include <SoftwareSerial.h>

  #define MAX_SIZE 29*50
  SoftwareSerial mySerial(D2, -1, true, MAX_SIZE);

  char buffer[MAX_SIZE];

  void setup()
  {
    Serial.begin(115200);  // 115200  256000
    mySerial.begin(115200);
    Serial.println("Started");
  }

  void loop() {
     int pos = 0;
     while (mySerial.available() && pos < MAX_SIZE) {
        buffer[pos++] = mySerial.read();
     }
     if (pos) {
        // Data received, treat the buffer contents
        Serial.println(pos);
     }
  }

supersjimmie avatar Jan 21 '16 07:01 supersjimmie

So today I tried it with a hardware intverter and set the "inverted" flag to "false" in softwareserial. But I still get the WDT resets.

supersjimmie avatar Jan 22 '16 09:01 supersjimmie

Hi, What SoftwareSerial did you used ? I do not found this constructor:

SoftwareSerial mySerial(D2, -1, true, MAX_SIZE);

Into the SoftwareSerial that I use. The SoftwareSerial I have has this constructor:

SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic /* = false */) : 
  _rx_delay_centering(0),
  _rx_delay_intrabit(0),
  _rx_delay_stopbit(0),
  _tx_delay(0),
  _buffer_overflow(false),
  _inverse_logic(inverse_logic)
{
  setTX(transmitPin);
  setRX(receivePin);
}

I have had problems using SoftwareSerial library. Apparently the buffer it uses is not enough to communicate with the ESP8266 .

What is the fourth argument used in this builder - MAX_SIZE ?

odilonafonso avatar Jan 22 '16 12:01 odilonafonso

I use the latest mentioned here: https://github.com/esp8266/Arduino/blob/master/doc/libraries.md#softwareserial

SoftwareSerial(int receivePin, int transmitPin, bool inverse_logic = false, unsigned int buffSize = 64);

supersjimmie avatar Jan 22 '16 12:01 supersjimmie

Thanks. One help more: I found SoftwareSerial.h and SoftwareSerial.cpp on: "/opt/arduino-1.6.7/hardware/arduino/avr/libraries/SoftwareSerial" (I use Linux) I have to delete it? and put the new there? or I can put the new library on my local libraries directory - /home/odilon/Arduino/libraries. What is a file search order include the Arduino IDE is?

odilonafonso avatar Jan 22 '16 20:01 odilonafonso

@odilonafonso The SoftwareSerial you are referring to is the AVR version. If you are using the staging release of esp8266/arduino you will automatically get the correct version

plerup avatar Jan 26 '16 08:01 plerup

@supersjimmie The only reason I can think of for your problem is if there are a lot of data coming in a burst or if there are some noise on the GPIO pin. In those cases there will be constant calls to the interrupt routine and yielding will not occur before the watchdog resets the chip. You could try disabling the watchdog but then your WiFi will probably be flaky.

plerup avatar Jan 26 '16 08:01 plerup

I don't think it is noise, but you may be right that it has something to do with the amount of data. If it was noise, the data would be corrupted but it is very clear data.

As far as I understand now, the crashes occur if I do just any small other thing while there is data coming in. Even just printing the received character makes it all instable. For now it looks stable when I only capture data and store it in a buffer, without anything else like checking (is it a valid character) or printing it.

int readTelegram() {
  int len = 0;
  char c;
  unsigned long maxwait = 10000;
  unsigned long maxreceive = 650;
  memset(buffer, 0, sizeof(buffer));

  // Allow receiving data
  Serial.println("waiting...");
  mySerial.enableRx(true);

  // Wait max 10 sec for data
  unsigned long startwait = millis();
  while ((mySerial.available() == 0) && (millis() - startwait < maxwait)) {
    yield();
    ESP.wdtFeed();
  }

  // If data available, get entire telegram
  if (mySerial.available() > 0) {
    unsigned long startreceive = millis();
    while ((len < MAX_LINES * MAX_LENGTH) && (millis() - startreceive < maxreceive)) {
      if (mySerial.available() > 0) {
        buffer[len++] = mySerial.read();
      }
    }
  }

  // Stop receiving data
  mySerial.enableRx(false);

  // If any data has been received, show it
  return len;
}

I also noticed that I had to use enableRX(false) to avoid the interrupts when my code it doing something else (like sending data the the logging server) while new data is coming in on the line.

supersjimmie avatar Jan 27 '16 10:01 supersjimmie

@supersjimmie if you are using SoftwareSerial (and your reference to D2 rather than GPIO X) does that mean you are using an ESP with an Arduino rather than the ESP as an Arduino? The reason I ask is that I use Arduino with ESP / USB. Obviously USB and ESP are completely different but almost identical sketches crash regularly with the ESP whereas they are fine with the USB. Maybe I have a similar problem to you.

pieman64 avatar Jan 27 '16 11:01 pieman64

No I only use an ESP8266. D2 is because on a ESP8266 NodeMCU dev board the pins are labeled ad D0..D8, which is then translated to normal GPIO numbers. (D2 is GPIO4)

Here to be precise: https://github.com/esp8266/Arduino/blob/master/variants/nodemcu/pins_arduino.h#L37-L59

supersjimmie avatar Jan 27 '16 13:01 supersjimmie

So neither Arduino with ESP or ESP Standalone, more a hybrid the NodeMCU?

pieman64 avatar Jan 27 '16 13:01 pieman64

NodeMCU is just an ESP12 on a board with some extra features. https://www.google.nl/search?q=nodemcu So a standalone ESP.

supersjimmie avatar Jan 27 '16 13:01 supersjimmie