About ESP8266-01

Hey :slight_smile:

I'm still learning Arduino on Arduino Mega2560 and i have a problem about ESP8266-01. I found a topic on internet for creating dynamic web server with jQuery and AJAX for ESP8266 NodeMCU. They use these libraries; ESP8266WiFi.h , WiFiClient.h, ESP8266WebServer.h. I think libraries for only works with NodeMCU because when i try compile, i got an error like this "Error compiling for board Arduino/Genuino Mega or Mega 2560." :confused: :frowning:

Is there any way to use this libraries with Arduino Mega2560 & ESP8266-01 ? :roll_eyes: :drooling_face:

Here the code. I want use this code with Arduino Mega2560 & ESP8266-01.

Thanks for your answers already.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <FS.h>   //Include File System Headers
 
const char* htmlfile = "/index.html";
 
//WiFi Connection configuration
const char *ssid = "circuits4you.com";
const char *password = "password";
 
#define LED 2
 
ESP8266WebServer server(80);
 
 
void handlePWM(){
  String PWM = server.arg("pwm");
  int p = 1024 - (PWM.toInt())*10;
  Serial.println(p);
  analogWrite(LED,p);
  server.send(200, "text/plane","");
}
 
void handleRoot(){
  server.sendHeader("Location", "/index.html",true);   //Redirect to our html web page
  server.send(302, "text/plane","");
}
 
void handleWebRequests(){
  if(loadFromSpiffs(server.uri())) return;
  String message = "File Not Detected\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " NAME:"+server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  Serial.println(message);
}
 
void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
 
  pinMode(LED,OUTPUT);
  //Initialize File System
  SPIFFS.begin();
  Serial.println("File System Initialized");
 
  
  //Connect to wifi Network
  WiFi.begin(ssid, password);     //Connect to your WiFi router
  Serial.println("");
 
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
 
  //Initialize Webserver
  server.on("/",handleRoot);
  server.on("/setLED",handlePWM); //Reads ADC function is called from out index.html
  server.onNotFound(handleWebRequests); //Set setver all paths are not found so we can handle as per URI
  server.begin();  
}
 
void loop() {
 server.handleClient();
}
 
bool loadFromSpiffs(String path){
  String dataType = "text/plain";
  if(path.endsWith("/")) path += "index.htm";
 
  if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
  else if(path.endsWith(".html")) dataType = "text/html";
  else if(path.endsWith(".htm")) dataType = "text/html";
  else if(path.endsWith(".css")) dataType = "text/css";
  else if(path.endsWith(".js")) dataType = "application/javascript";
  else if(path.endsWith(".png")) dataType = "image/png";
  else if(path.endsWith(".gif")) dataType = "image/gif";
  else if(path.endsWith(".jpg")) dataType = "image/jpeg";
  else if(path.endsWith(".ico")) dataType = "image/x-icon";
  else if(path.endsWith(".xml")) dataType = "text/xml";
  else if(path.endsWith(".pdf")) dataType = "application/pdf";
  else if(path.endsWith(".zip")) dataType = "application/zip";
  File dataFile = SPIFFS.open(path.c_str(), "r");
  if (server.hasArg("download")) dataType = "application/octet-stream";
  if (server.streamFile(dataFile, dataType) != dataFile.size()) {
  }
 
  dataFile.close();
  return true;
}

Is there any way to use this libraries with Arduino Mega2560 ?

No

If you has an ESP8266 then why not use it ?

You can use the IDE to program the ESP8266 using those ESP8266 libraries. See the [Beginner's Guide to ESP8266.](https://tttapa.github.io/ESP8266/Chap01 - ESP8266.html)

Do you actually have an ESP8266 ?

UKHeliBob:
Do you actually have an ESP8266 ?

I have ESP8266 - 01

Is ESP 8266 - 01 same thing with ESP8266 NodeMCU ?

siriusjq:
I have ESP8266 - 01

Is ESP 8266 - 01 same thing with ESP8266 NodeMCU ?

It is not quite the same, but the libraries work (the differences are in board layout and memory size) If you want to compile for an ESP-01 you should download & install the ESP cores for the IDE and select generic ESP 8266 as your board.