bittybytey
bittybytey

Reputation: 13

ESP NOW Receiver reports incorrect int compared to what I send

Using ESP8266's and the ESP NOW and public domain code template at Random Nerd Tutorials, I have a byte to decimal problem I needing help in solving.

Within the ESP Sender sketch, I store an 'int' sent from an Arduino's analogRead(A0). I can Serial.print(Analog_value) this int perfectly fine in the Receivers Serial monitor, e.g. it appears like "1035"

But when I assign this value to the "int member of the Senders struct , and then actually send the struct to a second ESP8266 which is instructed to prints the received 'int from the struct', It doesn't work. It only see ASCII value for \n.

To troubleshoot, I stripped the other struct's members to just have the int by itself and repeated the process, but now, printing out the received int, a separate ASCII code for each digit of the analog value is given! (better than before - as least I can see a representation of the data but it begs the question: why didn't this full set of ASCII values appear previously?).

For the analog value of 1035, I see "49" ASCII code for the first digit 1 (thousands), then ASCII "48" for the second digit 0 (hundreds), ASCII code '51' for 3(tens) then ASCII code "53" for 5 (units). Then ASCII codes for and /r & /n codes.
Receiver side I see...
Bytes received: 4
Int: 49
Bytes received: 4
Int: 48
Bytes received: 4
Int: 50
Bytes received: 4
Int: 51
Bytes received: 4
Int: 13
Bytes received: 4
Int: 10

However, when the int to send is just one number, e.g. '8' and generates serial.print data like this...
Bytes received: 4
Int: 56
Bytes received: 4
Int: 13
Bytes received: 4
Int: 10

The consistency of receiving 4 bytes whether '8' or '1035' is sent, must mean that what is being sent is the 2-byte 'space' to contain the value of my int - populated of course by the bits that represent '8' or '1035'. So why then is this 2 byte value later split up into powers of 10 for each decimal digit of the int, and has the ASCII value of that character digit printed when I do a serial.print command? Other serial.prints of int types show the normal decimal. How can I just print out the decimal value of the first two (of the 4) bytes sent and have a variable hold the a normal decimal value?

Upvotes: 0

Views: 44

Answers (1)

Clifford
Clifford

Reputation: 93554

Serial.print(Analog_value) takes the integer value Analog_value and converts it to a string, before sending the string to the serial port. That is what print implies.

You need to receive and display the string or convert the string back to an integer. You cannot simply assign a single received character or even the whole string to an integer and expect it to work.

The characters sequence 49 48 50 51 13 10 is a string : "1023\r\n".

The second is also a string "8\r\n".

If you truly want to send the actual bytes of Analog_value then:

Serial.write( &Analog_value, sizeof Analog_value ) ;

However you then have the issue that the endianness (byte order of multi-byte integers), may differ between sender and receiver. And the issue of knowing where each integer starts and ends when sending a sequence of integers. I would not recommend it without a clearly defined protocol and byte order (that need not being the native byte order of either processor).

It is unclear from your description why it has CR+LF appended, perhaps you used Serial.println(Analog_value) ("ln" is for "line" and the method adds CR+LF at the end). That is something either your Arduino code or receiving ESP code is doing. You chose to omit the code so it is not possible to tell. However it does tell you where one string ends and another one starts.

The debug output stating 4 characters received is clearly incorrect, and likely an error in your debug code which again you have failed to provide.

Upvotes: 1

Related Questions