Hi folks, I'm trying to get a SoftwareSerial port working on a NodeMCU, receiving from an Arduino Nano. I've connected the pins between the two boards with a level-shifter, but haven't been unable to get a simple test running with either SoftwareSerial or with EspSoftwareSerial. ( A bit more about my experience with EspSoftwareSerial is here: How to use espsoftwareserial with other platforms · Issue #197 · plerup/espsoftwareserial · GitHub). I'm hoping someone can suggest a way forward.
Here's working code with HW serial (connecting to Rx/Tx pins on NodeMCU):
void setup()
{
Serial.begin(9600);
delay(1000);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED off
}
void loop() {
if (Serial.available()) {
int result = Serial.read();
// blink the light so I know that something's happening
digitalWrite(LED_BUILTIN, LOW);// turn the LED on
delay(100); // wait for 0.1 second.
digitalWrite(LED_BUILTIN, HIGH); // turn the LED off
}
}
"Working" means the onboard LED blinks each time a byte is available (one byte / 5 sec). And here's the code with SoftwareSerial (or EspSoftwareSerial) that is not working (connecting to pins D5/D6)
#include <SoftwareSerial.h>
SoftwareSerial swSerial(5, 6); // 5 is RX, 6 is TX for Receiver
void setup()
{
swSerial.begin(9600);
delay(1000);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED off
}
void loop() {
if (swSerial.available()) {
int result = swSerial.read();
// blink the light so I know that something's happening
digitalWrite(LED_BUILTIN, LOW);// turn the LED on
delay(100); // wait for 0.1 second.
digitalWrite(LED_BUILTIN, HIGH); // turn the LED off
}
}
For completeness, here's the code running on the Nano (pins D5/D6 connected to NodeMCU via level shifter):
#include <SoftwareSerial.h>
SoftwareSerial swSerial(5, 6); // 5 is RX, 6 is TX
void setup() {
Serial.begin(9600);
swSerial.begin(9600);
delay(1000);
}
uint8_t counter = 0;
unsigned long cachedTime = millis();
void loop() {
if ((millis() - cachedTime) > 5000) { // one byte every 5 seconds
Serial.println(counter);
Serial.flush();
swSerial.write(counter++);
swSerial.flush();
cachedTime = millis();
}
}