I am using Atmega 2560 with embedded ESP8266 board. I have a webserver protocol within the ESP8266 module which will send data packet to Atmega 2560 using Serial port. (works fine)
However, my challenge is sending data packets from Atmega 2560 to ESP8266 so that it can update the webpage with sensor values. Kindly share me your feedback if you were able to perform this activity.
mock code at Atmega 2560
byte temp[] ={5,15,25};
byte temp2[] ={};
HardwareSerial *ESPComm = (HardwareSerial *)&Serial3;
void setup() {
//transfer packet to ESP
Serial.begin(115200);
// recieve packets from ESP
ESPComm->begin(115200);
}
void loop() {
ESPComm->write( '0' );
//transfer packets from ESP to MCU
if ( Serial3.available() ) { Serial.write( Serial3.read() ); }
// listen for user input and send it to the ESP8266
if ( Serial.available() ) { Serial3.write( Serial.read() ); }
}
// To verify the event from ESP
void serialEvent3() {
int i =0;
while (Serial3.available()) {
temp2[i] = Serial3.read();
Serial.write(temp2[i]);
i++;
}
}
//void serialEvent() {
//while(Serial.available()) {
// Serial3.write( Serial.read());
// }
//}
The objective is to pass temp[] packets from Atmega board to ESP and from ESP to Atmega.
P.S -> i also have the boards which you have in your pic. If you believe that module works fine with the code shared i am fine with that too. Kindly let me know on what would be the pin out connections.
if i read it in the serial port of ESP it would create garbled messages and would not serve the purpose of differentiating incoming and outgoing messages.
void loop() {
// Monitor serial communication
while(Serial.available()) {
message = Serial.readString();
messageReady = true;
}
// Only process message if there's one
if(messageReady) {
// The only messages we'll parse will be formatted in JSON
DynamicJsonDocument doc(1024); // ArduinoJson version 6+
// Attempt to deserialize the message
DeserializationError error = deserializeJson(doc,message);
if(error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
messageReady = false;
return;
}
if(doc["type"] == "request") {
doc["type"] = "response";
// Get data from analog sensors
doc["distance"] = 20;
doc["gas"] = 10;
serializeJson(doc,Serial);
}
messageReady = false;
}
}
// To verify the event from ESP
void serialEvent3() {
int i =0;
while (Serial3.available()) {
temp2[i] = Serial3.read();
Serial.write(temp2[i]);
i++;
}
}
DPin-14 (TX3) of MEGA maintains 0 to 5V logic.
D2 (RX) pin of Wemos maintains 0 to 3.3V logic.
so, you need logic sifter and that is what I have provided you using passive components.
//-----------------------------------------------
D1 (TX) pin of Wemos maintains 0 to 3.3V logic.
DPin-15 (RX3) of MEGA will accept 3V to 5V as HIGH. So, you don't need logic sifter here.