I have connected a ESP8226 to a MEGA2560 module. Here are the connections
ESP8226 TX connects to RX2 pin of MEGA2560 (Pin 17)
ESP8226 RX connects to TX2 pin of MEGA2560 (pin 16)
ESP8226 GND to MEGA2560 GND
ESP8226 5V to MEGA2560 5V.
The ESP8226 is going to be an independent module from the MEGA2560. It will receive commands from
WiFi connection and sends it to MEGA2560 via RX2/TX2 lines.
The ESP8226 is already programmed and loaded with a simple Web Page. There is a button
on the Web page. When the button is pushed the LED located on the ESP8226 module turns
on and off telling me that there is a good connection.
At the same time, the ESP send out a message like "Cabinlights_on" using the
Serial.print("Cabinlights_on\n"); or
Serial.print("Cabinlights_off\n"); depending on the state of the button pressed.
I verify the message by opening the Serial Monitor on the Ardunio environment. The serial port sees the
messages and they match the status of the LED.
Now I connect the MEGA2560 with its own program to receive the data from the ESP8226 using serial port 2.
I open the Serial Monitor to see if I receive anything.
Every time I push the button on the HTML web page, I see the LED on the ESP8226 turns on or off.
So I know the ESP8226 side is OK.
But I do not see anything on the MEGA2560. Nothing shows on the Serial Monitor.
I have tried a few examples reading the serial port. But I can not make the MEGA2560 get anything.
Is there a special way that ESP8226 needs to send out data to MEGA2560.
This is a ESP8226 module made by Adafruit (HUZZAH). It says on the board that it operates
between 3.5 to 6VDC. I also checked the input voltage when it was connected to the USB
port of the computer. The voltage reading was +5VDC.
That's OK assuming your 5V is going into the 5V side of the voltage regulator on the Huzzah and not the 3.3V side.
But the 5V GPIO outputs from the Mega will damage the 3.3V GPIO inputs of the ESP. And, the 3.3V GPIO logic outputs of the ESP will have lower noise margin when read by the Mega's 5V GPIO inputs.
There is no connections between the GPIO lines of the ESP8226 to the MEGA2560. The LED
is on the ESP8226 module and not the MEGA2560. The LED is used to make sure that the
ESP8226 receives commands from the HTML web page.
The only connection between the ESP and the MEGA2560 is the power, GND, and TX/RX lines.
My problem is that I do not receive any data on the MEGA2560 module via the TX/RX lines.
I can buy a level shifter to try if you think that is the problem.
Regarding the Baud rate. Yes I made sure that they both match.
Attached are my programs on both sides. The SerialEvent is just one of the examples I found on Ardunio.
and the WiFi_CMS is my program loaded into the ESP8226. I did not include the Web Pages.
In the WiFi_CMS program Looking at the handleForm() function, you can see that under the
if (t_state=="LED")
{
I send out "CabinLights_on" or "Cabinlights_off" strings.
That is where the MEGA2560 should see the data coming in.