I am using an RDM6300 RFID reader https://www.itead.cc/wiki/RDM6300 that provides serial output at 9600 bps when a card is present. The cheap on the reader batches characters a bit, it seems, and RFID being RFID, sometimes you get spurious characters. At full speed, the reader spits out a card number (as 0x2, 12 ascii characters, 0x3) about every 100 msec. We depend on the repeated output to know that the card is still present, but since we do actually do need to do other things in the program, I've started turning the interrupts off after getting the 0x3 stop byte.
In order to narrow down what might be causing the issues, I wrote the simplest sketch I could think of.
In production, we validate the checksum etc., and we don't use a delay() normally.
Here is the whole sketch:
#include <Streaming.h>
#include <SoftwareSerial.h>
SoftwareSerial RFID(D2, -1, false, 256);
char nextByte = 0x0;
void setup()
{
ESP.wdtDisable();
ESP.wdtEnable(WDTO_8S);
RFID.begin(9600);
Serial.begin(115200);
}
void loop()
{
if (RFID.available() > 0)
{
nextByte = RFID.read();
Serial.write(nextByte);
if (nextByte == 0x3)
{
Serial << " " << millis() << " - " << endl;
RFID.enableRx(false);
delay(1000);
RFID.enableRx(true);
Serial << millis() << " + ";
}
}
ESP.wdtFeed();
}
I left it running overnight (with a card sitting on the reader), and in the morning, this was waiting:
14569722 + 00003A7D8EC9 14570053 -
14571053 + 00003A7D8EC9 14571082 -
14572083 + *? 14572087 -
14573087 + 00003A7D8EC9 14573118 -
14574118 +
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset
You can see the (kinda-)non-printable characters in this screenshot at https://www.screencast.com/t/Ksy3Ovjh90k5
I am certain that the wdt reset is not firing in my code. I think it must be in the interrupt handler. This happens with more than one NodeMCU. It does not happen if the reader is disconnected (ie there is never anything .available() ).
I have thought about/wondered:
- Can one wrap code in some equivalent of a try-catch block to "trap" the wdt reset?
- Have I got the right version of SoftSerial? AFAIK it's the version installed with the board library, but I don't see version info in the .h file.
- Is AltSoftSerial https://github.com/PaulStoffregen/AltSoftSerialor something a better idea? (I think it was designed for Teensy, but I've seen examples with it in ESP sketches.)
- How does one "share" the RX line on the board with the USB interface so that I could still upload the code but then use hardware? (But I don't know how to do that, and that means giving up any kind of debugging or serial output over usb... which means setting up some kind of OTA debugging...
I would prefer to just fix this
HELP!
Thank you.
- bph