Greetings
I have 2 codes, one for access by a hotspot and one for the Arduino to send data to an external server on the network, at the moment I am interested in combining the 2 codes to allow access to change ssid or password in the future
I am experiencing a conflict between the libraries, it seems to me that the problem lies between line 10:
WiFiServer server(301);
For line 26:
ESP8266WebServer server(80);
(The error message is: conflicting declaration 'ESP8266WebServer server')
I greatly appreciate any attempt at help.
I went down to the base of the code, including the source from which I found it, so I hope it will be readable by the honorable public.
Addendum for future readers:
The code is not useful in copy paste (it doesn't work for some reason....)
The sting of the question focuses on the variable error
"server".
If I succeed, I hope to share with the public later.
The combined code is:
//https://www.instructables.com/Control-ESP8266-Over-the-Internet-from-Anywhere/
#include <ESP8266WiFi.h>
const char* ssid = "WIFI NAME";
const char* password = "WIFI PASSWORD";
const char* host = "IP OF THE ESP8266"; //it will tell you the IP once it starts up
//just write it here afterwards and upload
int ledPin = D3;
WiFiServer server(301); //just pick any port number you like
//-----------------------------Settings for hotspot-----------------------------------------
//https://lastminuteengineers.com/creating-esp8266-web-server-arduino-ide/
#include <ESP8266WebServer.h>
/* Put your SSID & Password */
const char* ssid_Point_mode = "NodeMCU"; // Enter SSID here
const char* password_Point_mode = "12345678"; //Enter Password here
/* Put IP Address details */
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
ESP8266WebServer server(80);
uint8_t LED1pin = D7;
bool LED1status = LOW;
uint8_t LED2pin = D6;
bool LED2status = LOW;
void setup() {
Serial.begin(115200);
pinMode(LED1pin, OUTPUT);
pinMode(LED2pin, OUTPUT);
WiFi.softAP(ssid_Point_mode, password_Point_mode);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
server.on("/", handle_OnConnect);
server.on("/led1on", handle_led1on);
server.on("/led1off", handle_led1off);
server.on("/led2on", handle_led2on);
server.on("/led2off", handle_led2off);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
//}
//---------------------------------So far definitions for hot spot-----------------------------
//void setup() {
Serial.begin(115200);
delay(10);
Serial.println(WiFi.localIP());
// prepare GPIO2
pinMode(ledPin, OUTPUT);
digitalWrite(D3, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
client.flush();
// Match the request
if (req.indexOf("") != -10) { //checks if you're on the main page
if (req.indexOf("/OFF") != -1) { //checks if you clicked OFF
digitalWrite(ledPin, LOW);
Serial.println("You clicked OFF");
}
if (req.indexOf("/ON") != -1) { //checks if you clicked ON
digitalWrite(ledPin, HIGH);
Serial.println("You clicked ON");
}
}
else {
Serial.println("invalid request");
client.stop();
return;
}
// Prepare the response
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "<!DOCTYPE HTML>\r\n<html>\r\n";
s += "<br><input type=\"button\" name=\"bl\" value=\"Turn LED ON \" onclick=\"location.href='/ON'\">";
s += "<br><br><br>";
s += "<br><input type=\"button\" name=\"bl\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
s += "</html>\n";
client.flush();
// Send the response to the client
client.print(s);
delay(1);
}
//------------------------------The page creator for Hotspot----------------------------------------------
void handle_OnConnect() {
LED1status = LOW;
LED2status = LOW;
Serial.println("GPIO7 Status: OFF | GPIO6 Status: OFF");
server.send(200, "text/html", SendHTML(LED1status,LED2status));
}
void handle_led1on() {
LED1status = HIGH;
Serial.println("GPIO7 Status: ON");
server.send(200, "text/html", SendHTML(true,LED2status));
}
void handle_led1off() {
LED1status = LOW;
Serial.println("GPIO7 Status: OFF");
server.send(200, "text/html", SendHTML(false,LED2status));
}
void handle_led2on() {
LED2status = HIGH;
Serial.println("GPIO6 Status: ON");
server.send(200, "text/html", SendHTML(LED1status,true));
}
void handle_led2off() {
LED2status = LOW;
Serial.println("GPIO6 Status: OFF");
server.send(200, "text/html", SendHTML(LED1status,false));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String SendHTML(uint8_t led1stat,uint8_t led2stat){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>LED Control</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr +=".button-on {background-color: #1abc9c;}\n";
ptr +=".button-on:active {background-color: #16a085;}\n";
ptr +=".button-off {background-color: #34495e;}\n";
ptr +=".button-off:active {background-color: #2c3e50;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>ESP8266 Web Server</h1>\n";
ptr +="<h3>Using Access Point(AP) Mode</h3>\n";
if(led1stat)
{ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";}
else
{ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";}
if(led2stat)
{ptr +="<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";}
else
{ptr +="<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";}
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
//----------------------------------------------------------------------------------------------------
I would like to express my deep appreciation to all the good people who contribute to this respectable forum for the beauty and combination of hands worldwide!
it is so beautiful!!!
Thank you very much everyone