/*
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();
}
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
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
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.
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.
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)
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.
I am doubtful about how useful the Mega-WiFi_R3_ATmega2560_ESP8266 will be
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
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.