Serial communication = receiving wrong number

Hello!

I am trying to send a variable int (0-255) from an Node MCU V1 ESP 8266 to a Teensy 3.2.

My wiring is :
ESP8266 TX --> Teensy 9 (RX) (Serial 2 on Teensy)
ESP8266 RX --> Teensy 10 (TX) (Serial 2 on Teensy)

I have used the code in Serial input basics but I am getting very strange results.

Basically I am receiving the 3 same numbers: 52 then 48 then 10 and it starts again.
This is not at all what i am sending. I have tried sending a fixed value (400) and i still get the same result!

Help me please !!!

code ESP 8266 (sender)

 
int val = 0;

void setup() {
  
  Serial.begin(9600);
 
}

void loop() {

     Serial.print(440);
       Serial.print('\n');
      
   
 }
    

code Teensy 3.2 (receiver)

#include <Audio.h>
#include <SD.h>
#include <SerialFlash.h>

// set this to the hardware serial port you wish to use
#define HWSERIAL Serial2

bool S1 = false;
int val = 0; //my value

void setup() {
 
  Serial.begin(9600);
  Serial.print("teensy ready");
  HWSERIAL.begin(9600);
  

}

void loop() {
  
  receiveS1();
  showS1 ();


}



void receiveS1() {
  if (HWSERIAL.available() > 0) {
    val = HWSERIAL.read();

    S1 = true;
  }
}
  void showS1 () {
    if (S1 == true) {
     Serial.println("receving:");
      Serial.println(val, DEC);
      S1 = false;
    }
  }

You are printing the decimal value of what is received. IS this not what you are seeing?

val = HWSERIAL.parseInt();

I am receving : (with ```
Serial.println("receving:");


receiving:
48
receiving:
52
receiving:
10

in a loop, 
I should receive 
440
440
440

will try this and report back tomorrow,
thanks :slight_smile:

the print() function turns your number into the ASCII representation so 440 turns into "440" which is a string of 3 chars with whose value is
'4' == 0x34 (hex) == 52 (decimal)
'0' == 0x30 (hex) == 48 (decimal)

and
'\n' == 0x0A (hex) == 10 (decimal)

So it looks like you are getting what you should be.

that makes a lot of sense! thanks!

What should i use to get 440?

Serial.println(val); // OR 
Serial.parseInt(); 

You are receiving a SINGLE byte whose decimal value cannot exceed 256.

bytewise reading is like that:

void receiveS1() {
  if (HWSERIAL.available() >= 1) {
    byte Char = HWSERIAL.read();
    if (Char > 47 && Char < 58)   val = val * 10 + (Char - 48);
    else S1 = true;
  }
}

Ok, I have it working now, thank you everyone.
Just a bit confused how that all works.

I have (to send)

void setup() {
  
  Serial.begin(9600);
 
}

void loop() {
     Serial.print(255);
     Serial.print('\n'); 
     delay(100); 
     Serial.print(0);
     delay(100);  
     Serial.print('\n'); 
 }

and to receive :

void receiveS1() {
  if (HWSERIAL.available() >= 1) {
    val = HWSERIAL.parseInt();
    Char = HWSERIAL.read();
    if (Char > 47 && Char < 58)   val = val * 10 + (Char - 48);
    else S1 = true;
  }
}

  void showS1 () {
    if (S1 == true) {
     Serial.println("receving:");
      Serial.println(Char);
       Serial.println("val:");
      Serial.println(val);
      S1 = false;
    }
  }

In the serial monitor i get
receiving:
10
val:
255
receiving:
10
val:
0

So parseInt() returns my value but the byte reading

void receiveS1() {
  if (HWSERIAL.available() >= 1) {
    byte Char = HWSERIAL.read();
    if (Char > 47 && Char < 58)   val = val * 10 + (Char - 48);
    else S1 = true;
  }
}

returns 10 (which if i understand is the linebreak? '\n' == 0x0A (hex) == 10 (decimal)

this is nonsence. use one of the options

void receiveS1() {
  if (HWSERIAL.available() >= 1) {
    val = HWSERIAL.parseInt(); //this will get complete integer number until not digit character comes
     S1 = true;
  }
}

or

void receiveS1() {
  if (HWSERIAL.available() >= 1) {
    Char = HWSERIAL.read();
    if (Char > 47 && Char < 58)   val = val * 10 + (Char - 48);
    else S1 = true;
  }
}

Yes, I realised this now.

Thank you