1

I want the number from an RFID tag read by a reader connected to an Arduino Mega to end up as a string on a NodeMCU connected to the Mega. I am trying to confirm that the string is on the NodeMCU using the serial monitor. With the code below, the serial monitor for the NodeMCU prints "receivedChars:" only and not the number from the tag. What am I missing?

Code for Arduino Mega


#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 53
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.


String sent = "n";
String UID_string = ""; 

void setup() {

    SPI.begin();            // Init SPI bus
    mfrc522.PCD_Init(); // Init MFRC522 card
    Serial.begin(115200); // Initialize serial communications with the PC
    Serial.println("Running Version Jan5a");
    Serial.println("Mega:Serial Monitor initialized at 115200 baud...");
    Serial3.begin(9600); // Initialize serial communication with ESP
    Serial.println("Mega:Serial Port 3 for NodeMCU initialized at 9600 baud...");
    Serial.println("Mega: Scan PICC to see UID and type...");
}

////////


void SendUIDtoESP() {

if (sent == "n") {
  Serial.print("sent= ");
  Serial.print(sent);
  //if (UID_string.length() > 0)  {
  UID_string = "Hello World"; //test line
  Serial3.print("<");
  Serial3.print(String(UID_string));
  Serial3.print(">");
  Serial.println("Mega:Sending UID string to ESP");
  sent = "y";
  Serial.print("after sending, sent= ");
  Serial.print(sent);
//  } else {
 //   Serial.println("Mega: Error: UID-string not set yet!");
  //}

}
} // end Send UID to ESP

void loop {
SendUIDtoESP() ;
}

Code for NodeMCU




int numChars = 0;
String receivedChars = "";
boolean newData = false;
String tag = "";
unsigned long lastTime = 0;
unsigned long timerDelay = 5000;


SoftwareSerial s(D1,D2);// assign pins to send data to and receive data from NodeMCU to Arduino Mega rx, tx

void setup() {
Serial.begin(9600); 
    s.begin(9600);
    Serial.println("NodeMCU: Waiting for serial connection with Mega to be available over s");
  while (! s.available() >0){
    Serial.print("."); // waiting
    return;
  }
  Serial.println("NodeMCU: Looks like Mega is available."); 
} // end void setup

 void recvWithStartEndMarkers() { //listen to Mega
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (s.available() > 0 && newData == false) {
        rc = s.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                Serial.println(rc);
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;     
            }
        }
        else if (rc == startMarker) {
          Serial.println("startMarker received");
            recvInProgress = true;
        }     
    }
} // end recvWithStartEndMarkers()

void loop() {
recvWithStartEndMarkers();
}
10
  • why the Mega? NodeMcu has SPI too
    – Juraj
    Apr 11, 2021 at 16:29
  • what is this?: String UID_string = (String)UID_unsigned;
    – Juraj
    Apr 11, 2021 at 16:33
  • I think I took the block containing that line from here: github.com/miguelbalboa/rfid/issues/63#issuecomment-66874814 Apr 11, 2021 at 16:54
  • I'm using the NodeMCU for Wifi for the Mega (not reflected here to cut down on code length); I'm using the Mega because I plan on adding an unknown number of components down the road and think I need the ports. Apr 11, 2021 at 16:56
  • determine if the serial comm actually works as expected ... start with simple hello world sketch
    – jsotola
    Apr 11, 2021 at 16:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.