replace a W5100 Ethernet Shield with an ESP8266 WiFi UART Serial Module

Is it possible to replace a W5100 Ethernet Shield with an ESP8266 WiFi UART Serial Module ESP-01 ?

Greetings for the New Year

I have a home based Arduino Mega 2560 and stuggling locally to get the Ethernet Shields, but the ESP8266 modules are easily available.

My current sketch uses both www client and server, as it transmits log data ( temperature readings, etc ) to my web server in the form of a GET url to a php page ( which stores the data online ), and also acts as a server which allows me to connect remotely from a browser and toggle pins, etc.

The examples that I see online for the ESP8266 all seem to be related to using an Arduino to program the ESP8266 module using an Arduino as a programmer, but not really seeing anything that points to replacing the functionality of the Ethernet Shield with the ESP module.

May be I am googling for the wront terms. Pointers please ?

Would the setup connection procedure use AT commands from the Arduino to connect the ESP to my WiFi router ?
Are the WiFi settings / password specified in the AT commands ?

Current code that I am using includes :

#include <Ethernet.h>
#include <utility/w5100.h>
char serverName[] = "http://www.xxxx.co.za";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 101);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255,255,255,0);
IPAddress myserver(41,203,18,70);
EthernetServer server(82);
EthernetClient client;
EthernetClient WWWclient;

if (client.connect(myserver, 80)) {
	client.print("GET http://www.xxxx.co.za/serveit.php?data=");
	client.print(Location);
	client.print("--");
	client.print(LogTxt);
	client.println(" HTTP/1.1");
	client.println("Host: www.xxxx.co.za");
	client.println();
	delay(100);
	client.stop();
}

For this application, I recommend using this library:

The API is similar to the Ethernet library so you shouldn't have too much trouble modifying code written for the Ethernet library. The library is written to be used with ESP8266 modules running the AT firmware but the AT commands are all hidden away in the library so you don't need to worry about them at all.

The examples that come with the library are configured to use Serial1 on the Mega 2560. This means you should connect the RX pin of the ESP8266 to pin 18 of your Mega and the TX pin of the ESP8266 to pin 19 of your Mega.

pert:
For this application, I recommend using this library:
GitHub - bportaluri/WiFiEsp: Arduino WiFi library for ESP8266 modules
The API is similar to the Ethernet library so you shouldn't have too much trouble modifying code written for the Ethernet library. The library is written to be used with ESP8266 modules running the AT firmware but the AT commands are all hidden away in the library so you don't need to worry about them at all.

The examples that come with the library are configured to use Serial1 on the Mega 2560. This means you should connect the RX pin of the ESP8266 to pin 18 of your Mega and the TX pin of the ESP8266 to pin 19 of your Mega.

Thank You pert.

Do you know if it can be run as Client and Server at the same time ? So as a Client it can upload log data to a remote php script, and as a Server it can accept remote connections and respond to commands sent to it ?

I'd guess yes, but I have only used the library for one fairly basic project I helped a friend with. Other than that, I've mostly just run the WebServer example sketches as a "Hello World" sanity check while I was helping people on the forum try to use the library.

You could use WiFiLink firmware and library

pert:
The examples that come with the library are configured to use Serial1 on the Mega 2560. This means you should connect the RX pin of the ESP8266 to pin 18 of your Mega and the TX pin of the ESP8266 to pin 19 of your Mega.

Pert : the code in the samples shows :

SoftwareSerial Serial1(6, 7); // RX, TX

Is this to be changed to 18, 19 ?

DaveO:
Pert : the code in the samples shows :

SoftwareSerial Serial1(6, 7); // RX, TX

Is this to be changed to 18, 19 ?

the example has

#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

for Mega HAVE_HWSERIAL1 is defined so SoftwareSerial Serial1 is not used (it is #if-NOT-definded HAVE_HWSERIAL1)

hardware Serial1 is used with Mega

Juraj:
for Mega HAVE_HWSERIAL1 is defined so SoftwareSerial Serial1 is not used (it is #if-NOT-definded HAVE_HWSERIAL1)

hardware Serial1 is used with Mega

OK. So that code is ignored ( and I assume can be deleted from the sketch ) when only a Mega is used because Serial1 already exists on the Mega pins 18 & 19 ?

DaveO:
OK. So that code is ignored ( and I assume can be deleted from the sketch ) when only a Mega is used because Serial1 already exists on the Mega pins 18 & 19 ?

yes