Arduino Mega to ESP8266 Serial communication problem

Hello, There !

In one of the my project, I am trying to establish serial communication between Arduino Mega-2560 (Master) and ESP8266(Slave). In that case i am able to get shared data(0xD414) by Arduino to read over ESP8266 , but unfortunately i am unable to find from where getting the "0" value initially at the shared value. Can anyone suggest me how to remove "0" from the original value. That is (0D4, 014) instead of D4 and 14 after receiving data

Here it is the snapshot of result: Picture

Master Code

uint16_t adcvalue = 0xD414;
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.write(highByte(adcvalue)); //Write the serial data
  Serial.write(lowByte(adcvalue)); //Write the serial data
  delay(100);
}

Slave Code:

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

void loop() {
  adcvalue = Serial.read(); 
  Serial.print(highByte(adcvalue),HEX);
  Serial.println(lowByte(adcvalue),HEX);
  delay(100);
}

Here it is the snapshot of result

There is a sticky on how to post pictures.

void loop() {
  Serial.write(highByte(adcvalue)); //Write the serial data
  Serial.write(lowByte(adcvalue)); //Write the serial data
  delay(100);
}

Serial.write writes the individual bytes.

void loop() {
  adcvalue = Serial.read();
  Serial.print(highByte(adcvalue),HEX);
  Serial.println(lowByte(adcvalue),HEX);
  delay(100);
}

Serial.read() only reads 1 byte, The ‘highByte()’ is never actually in the variable.
You are also not making sure there is anything to be read, but that is a different matter.
Let’s assume there is for now. then

void loop() {
  adcvalue = Serial.read();
  adcvalue = (adcvalue << 8) | Serial.read();  // shift the first byte 8 bits left and add (or 'or') the next byte 
  Serial.print(highByte(adcvalue),HEX);
  Serial.println(lowByte(adcvalue),HEX);
  delay(100);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.