1

I connect my NodeMCU to Arduino Uno. The goal is to receive the whole string for example "12", every 1 sec.

This is my code on Arduino Uno side ( the sender)

int mock = 0;

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

void loop() {
    //Write mock to NodeMCU
    s.print((int)mock,DEC);
    s.println();

    mock++;
    delay(1000);
}

On NodeMCU side ( the receiver)

 char data; //Initialized variable to store recieved data
 String line;
 void setup() {
   Serial.begin(115200);
   s.begin(115200);
   while (!Serial) {
    ; 
  }
}

void loop() {

    if( s.available() >= 2) {
        for (int i = 0; i< 2; i++ ) {
            data = s.read();
            line += data;
        }
        delay(1000);
    }


    Serial.print(line);

}

But the result on the serial panel is not what I expected Serial terminal

How can I make it so that I can receive something like

12
(wait one sec)
13
(wait one sec)
....

Node MCU just prints the whole thing, I can't delay it.

  • I don't see you printing what you have read. Also line is not defined in your code. Please provide a full minimal example – chrisl Nov 5 at 12:59
  • @chrisl Sorry i forgot to include the variable's declaration. I plan to use "line" to store all data that received and then print it to the console. – Thang Nguyen Nov 6 at 3:53
0

in your nodemcu use

void loop() {
    if(s.readBytesUntil('\n',data ,2)>0)
      Serial.println(data);
    delay(1000);
}

Serial.readBytesUntil(character, buffer, length)

Serial.readBytesUntil() reads characters from the serial buffer into an array. The function terminates (checks being done in this order) if the determined length has been read, if it times out , or if the terminator character is detected (in which case the function returns the characters up to the last character before the supplied terminator). The terminator itself is not returned in the buffer.

Serial.readBytesUntil() returns the number of characters read into the buffer. 0 means that the length parameter <= 0, a time out occurred before any other input, or a termination character was found before any other input.

  • 1
    readBytesUntil waits a second for the data. it is better to wait for the first character with Serial.available(). and you don't handle \r – Juraj Nov 6 at 13:04
  • yeah he is going to print it in console \r\r means twice return to beginning of line no diffrence in result if he wants a better approach he needs to define two bytes or more header and a checksum then define a constant size packet so he can reach a better solution with even faster speed beside I would change sender s.print((int)mock,DEC); s.println(); to s.print((int)mock,DEC); s.print('\n') – david Nov 6 at 14:39
1

If you send "12" you send 4 characters/bytes: '1', '2' and a new line which is two characters: ('\r', '\n').

On the receiver side you check for two characters (not the new line), so you should take it into account.

Actually, since you will eventually reach "100" you will receive 5 characters ('1', '0', '0', '\r', \'n').

So it is better to change the receiver side to store the bytes in line UNTIL a new line is received (than you can support values of any length).

Also remove the delay on the receiver side, or at least decrease it a lot, because it doesn't matter to check (too) often. if there is no byte available, it will check again. Now it can happen at time 1001 ms a byte would be received, but at time 1000 ms it is checked and it will not be checked again until time 2000 ms.

  • 2
    println() is \r\n – Juraj Nov 5 at 10:54
  • @Juraj Thanks (I should have known). – Michel Keijzers Nov 5 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.