Mega 2560 with esp8266 communication with WiFi module failed

Hello, I need help with a connection to this module. I tried several examples, libraries etc... I followed this scheme.....

but I used mega2560 and the TX RX ports I used were 18 and 19.... I added to Arduino a 12v 2A power supply to ensure enough power to this module.

#include <WiFiEspAT.h>

// Emulate Serial1 on pins 6/7 if not present
#if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1)
#include <SoftwareSerial.h>
SoftwareSerial Serial1(19, 18); // RX, TX
#define AT_BAUD_RATE 9600
#else
#define AT_BAUD_RATE 115200
#endif

const char *server = "arduino.cc";

WiFiClient client;

void setup()
{
    Serial.begin(115200);
    while (!Serial)
        ;

    Serial1.begin(AT_BAUD_RATE);
    WiFi.init(Serial1);

    if (WiFi.status() == WL_NO_MODULE)
    {
        Serial.println();
        Serial.println("Communication with WiFi module failed!");
        // don't continue
        while (true)
            ;
    }

    // waiting for connection to Wifi network set with the SetupWiFiConnection sketch
    Serial.println("Waiting for connection to WiFi");
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(1000);
        Serial.print('.');
    }
    Serial.println();
    Serial.println("Connected to WiFi network.");

    Serial.println("Starting connection to server...");
    if (client.connect(server, 80))
    {
        Serial.println("connected to server");

        client.println("GET /asciilogo.txt HTTP/1.1");
        client.print("Host: ");
        client.println(server);
        client.println("Connection: close");
        client.println();
        client.flush();
    }
}

void loop()
{

    // if there are incoming bytes available
    // from the server, read them and print them
    while (client.available())
    {
        char c = client.read();
        Serial.write(c);
    }

    // if the server's disconnected, stop the client
    if (!client.connected())
    {
        Serial.println();
        Serial.println("disconnecting from server.");
        client.stop();

        // do nothing forevermore
        while (true)
            ;
    }
}

This is my last try... I can upload the code without error, but it always fails to connect to this module. I'm stuck and I need any help or suggestions.


Here are some settings I use in vs code.

You can add a 1,000,000 amp supply and it won't help, if the on board regulator that provides 3.3V to the ESP can't supply enough current.

You have no level shifter on the TX line, which is liable to damage the ESP RX pin.

Also, your diagram is whacky. You show an UNO not a Mega. A Mega has extra serial ports, and I sure hope you are actually using them...

Please specify your exact hardware.

so you have a Mega which has Serial1 (HAVE_HWSERIAL1). the SoftwareSerial line is skipped for Mega. no reason to set the pin numbers there.

did you wire Rx to TX? pin 18 to RX of esp-01?

has your Mega ATmega16u2 as USB chip or Ch340? The boards with Ch340 can't provide enough current on 3.3 V pin for esp8266.

As indicated in previous replies, why does you wiring diagram show the ESP connected to pins 0 and 1 while using Serial1 on the Mega?

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

The voltage regulators have to drop this to 5volt for the Mega and 3.3volt for the ESP8266.
That makes a 12volt supply a poor choice (too high), because most of it has to be burned off as heat.
Better to power the Mega with a 5volt cellphone charger, connected to the USB socket.

You must have a very good reason to try to join two processors.
Can't you use a WeMos D1 mini, or NodeMCU, or ESP32 board (no Mega).

If you must use this kludge, then add at least a 470uF/6.3volt buffer capacitor on the 3.3volt pin, to bridge the ~400mA transmit peaks of the ESP-01 (assuming). And a level shifter (1:2 voltage divider) on the TX line of the Mega, to prevent 5volt logic being rammed op the backside of a 3.3volt processor.
Leo..

1 Like

Probably I haven't chosen a good modul, because it's just my second project on Arduino ... I just searched for a WiFi module and bought this one. I need only to send some measured data to my server.
@anon57585045 Yes I'm using these serial ports 18 & 19 and I don't have any additional hardware.
@Juraj My USB chip is ATmega16u2. Yesterday, I tried to measure the voltage with the adapter and it has a stable 3.3V and without it drops below 3V.
@sterretje I tried several code examples and after it didn't work I tried to define these ports. Sorry for this scheme. I just found a picture similar to my connections.

Thanks for the image. That does clear up a few doubts. But it is not possible to verify the connections from that. Please provide a complete wiring diagram of the actual circuit in the image.

There is no fuss about making a diagram. Just include everything, label everything, use pen and paper and take a picture of the sheet.

Can you see how you've forced people to speculate?

Also it seems that you went back and edited your original post to look like some questions were already answered there. Please don't do that!

I didn't edit the original post, I saw the edit mark, but the text is still the same. This may have happened because the post was moved from the Installation and Troubleshooting section.

Did you try loading the serial loopback example sketch to communicate directly from the PC to the ESP using AT commands?

What adapter? What "it"?

Sorry, I meant the 3.3V pin and the adapter is only connected to Adruino as in the picture...

@fireflysk did not edit the post, it was just the move that resulted in the orange pencil.

@aarg , you should be able to see the edits of a post (including moves) if you click the pencil.

Thanks, I will look next time.

Yes, I tried AT commands. I'm not sure what you mean by "serial loop example." I tried some tutorials and examples, but nothing worked.

What did you do?

It's called "SerialPassthrough", look in the IDE examples.

I just tried something like that, but I didn't get a response...

Right. So now you know where to look. Have you verified the baud rates? An incorrect baud rate will stop the show. How did you decide that the ESP is set to 9600?

A screen shot of an actual AT command being sent, would have been more useful. Your send line is blank. What did you send? Can we see the results?

Also, you goofed up the program. You have merely repeated the transmission in one direction only.

Yes, I sent the command "AT" . The ESP should have a baud rate of 115200.

This is the example I found.....

Could you please tell me how it can be fixed or what code needs to be added.