Arduino Mega and ESP01 for web server question

Dear people,

I would like to built a simple web server to turn on or off an LED with Mega and ESP01.
I learnt that we need AT commands for ESP01, and I have found a website which discussed on downloading the flasher and firmware, as link below: https://create.arduino.cc/projecthub/pratikdesai/flash-firmware-on-esp8266-esp-01-module-e1f758

I used the wifiESP library on the Arduino IDE and found problems.


The code is shown below:

/*
 WiFiEsp example: WebServer

 A simple web server that shows the value of the analog input 
 pins via a web page using an ESP8266 module.
 This sketch will print the IP address of your ESP8266 module (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to display the web page.
 The web page will be automatically refreshed each 20 seconds.

 For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
*/

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(18, 19); // RX, TX
#endif

char ssid[] = "SINGTEL-9875";            // your network SSID (name)
char pass[] = "xxxx";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
int reqCount = 0;                // number of requests received

WiFiEspServer server(80);


void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
  printWifiStatus();
  
  // start the web server on port 80
  server.begin();
}


void loop()
{
  // listen for incoming clients
  WiFiEspClient client = server.available();
  if (client) {
    Serial.println("New client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          Serial.println("Sending response");
          
          // send a standard http response header
          // use \r\n instead of many println statements to speedup data send
          client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
            "Refresh: 20\r\n"        // refresh the page automatically every 20 sec
            "\r\n");
          client.print("<!DOCTYPE HTML>\r\n");
          client.print("<html>\r\n");
          client.print("<h1>Hello World!</h1>\r\n");
          client.print("Requests received: ");
          client.print(++reqCount);
          client.print("<br>\r\n");
          client.print("Analog input A0: ");
          client.print(analogRead(0));
          client.print("<br>\r\n");
          client.print("</html>\r\n");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(10);

    // close the connection:
    client.stop();
    Serial.println("Client disconnected");
  }
}


void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  
  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.println();
}

Kindly help me. Thank you.
Best regards.

That is 'a' method, not the one i would use.

The error message is telling you what is wrong.

You can use the Mega as an uploader, is it a board with dip-switches to set the upload mode ?
Anyway, let's ask @Juraj , he will know what to do.

Dear Deva,

There is a reset switch for the ESP8266 adaptor.
I agree that it is likely the firmware that is wrong.

Thanks, and lets wait for @Juraj please.

have a look at ESP0101S-RELAY-MODULE-TUTORIAL which operates a relay but can be replaced with a LED
no need for a Mega unless you require it for other tasks

Dear @horace ,

Thanks for your information. I would like to use Mega for bigger input/output projects. I want to try out on turning on and off LED first.

Best regards.

rather than the complication of Mega + ESP-01 (how do you intend to communicate between them?) use something like a getting-started-w-nodemcu-esp8266
WiFi plus plenty of IO and will even run an access point, e.g. ESP8266 example: Wi-Fi Access point, static IP, web-server and remote GPIO control

Thanks, but I would like to use 5V for inputs and outputs.

Ok, well regardless of that, I suggest that you do program the ESP-01 directly using the Arduino IDE (eg make your own firmware) and then let the 2 boards communicate over Serial (similarly to the way they do when the AT-commands firmware is on it) Although WiFiEsp.h does simplify things (once you have a supported version on the ESP) It does remain cumbersome, and on some other level, it is just wrong to control a secondary board, by giving it commands as an interpreter, rather than compiling the code to do the things you want and uploading that.

about the code you are trying now, i am not sure if the WiFiEsp.h library corrects the baud rate (i somehow seem to remember it does)
but nearly all ESPs come with a firmware that uses 115200bps
so

Serial1.begin(9600);

should be

Serial1.begin(115200);

and of course using a Mega, this shouldn't get compiled

#include "SoftwareSerial.h"
SoftwareSerial Serial1(18, 19); // RX, TX
#endif

but it does show that you do not fully comprehend what you are doing. (which makes sense of course, otherwise why would you come here for help ?)

What you can do for now though, is upload the SerialPassthrough example from the IDE (under communication) and change both baud rates to 115200, open the Serial monitor, set the baud-rate to 115200 as well, and set the line ending to 'Both LF&CR' and type

AT+GMR

just to see what version is actually on the ESP-01 at the moment.
If you want to keep going down the avenue you planned, we may as well see if you can manage to communicate with the ESP at all, and if you can mange to set the dip-switches correctly.

what are we talking about a Mega with a seperate ESP-01 or a Mega-WiFi_R3_ATmega2560_ESP8266 ?

Dear Deva,

Thanks for writing. I want to make a web application on free 000webhost.com actually. I was able to use ESP32 to do it, but my codes got deleted after 1 month of inactivity. Sad. Have you ever worked on web application? Would like to display LED state on website and turn on LED with laptop cursor.

I took my code from
https://randomnerdtutorials.com/control-esp32-esp8266-gpios-from-anywhere/
and
https://randomnerdtutorials.com/cloud-weather-station-esp32-esp8266/

Thank you and best regards.

Dear @horace ,

I am talking about a Mega and a separate ESP-01. I have also seen a Mega with embedded ESP8266 also, wonder if it can be easily coded?

Thanks and best regards.

what is the AT firmware version? if WiFiEsp reports "unsupported" there is a chance you have a newer version and you can use my WiFiEspAT library.
The AT firmware baud rate is 115200 so unless you changed it, use Serial1.begin(115200);. (with WiFiEspAT. WiFiEsp works reliable only with 9600 baud)

Dear @Juraj ,

The AT firmware version is v0.9.2.2 AT Firmware.bin.

I have seen the WifiEspAT code. There isn't any ssid and password code, how is it possible to connect to the Wifi?

Thanks and best regards.

In that case, let's see your wiring.

Which both are tutorials where you control the esp 'through' an external website. I only use the ESP locally, letting it connect to a home Wifi network, and let the ESP serve a webpage from which i can send commands to whatever it is connected to.

0.9.2.2 is ancient. you have to upgrade it to use the libraries.

https://github.com/jandrassy/WiFiEspAT#getting-started

Dear Deva,

TX of ESP01 to RX1 (D18) Mega
RX of ESP01 to TX1 (D19) Mega
GND of ESP01 to GND Mega

Thanks and best regards.

I am doubtful about how useful the Mega-WiFi_R3_ATmega2560_ESP8266 will be

  1. having to switch the DIL switches ON/OFF between loading and running code is awkward - also not sure how long the DIL switch will last
  2. had intermittent problems loading code into processors and with the Mega Serial3 connection to the ESP8266 serial - when I used contact cleaner on the DIL switches this appeared to clear up the immediate problems but I am not sure how long it will last

I did not attempt to use the ESP8266 AT commands but loaded code into the both the ESP8266 and Mega - application worked OK after dealing with problems reported above

So do you plug the ESP-01 uploader into the USB for power ?
Does the uploader hold the ESP in 'flash' mode ?
Is the RX pin of the uploader connected directly to the ESP's RX ? if so, you should use a voltage divider to reduce the 5v logic levels that the mega is using to 3.3v for the ESP. or you will fry the board.
Good news is, you have an uploader, so you can start uploading to the ESP without much issue.

3.3 V pin of Mega with an FTDI chip usually can't power esp-01, becuase it can provide only 50 mA

Dear @Juraj ,

I power the ESP01 with battery bank.

Thanks and best regards.