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;
}
}
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.
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;
}
}