How to run 8266-01 in AP and Server Mode at the same Time?
Posted: Thu Sep 01, 2016 6:51 pm
OK, I think I'm using the right terms. Below is some code I've modified from an Instructables that I can use to control two LEDs via a webpage (this is server mode, right?). However, it requires that the SSID and PW be hard coded and flashed.
I would like to add the AP function, running at the same time, so I can access the ESP and set parameters for the SSID and PW since I have several APs in my environment and will need to be able to change these "on the fly."
If possible, I would also like to change the labels of LED1 and LED2 to the actual appliance they will control using variables entered in the AP mode function. Can anyone point me in the right direction, or help me fill in the code?
I'm thinking the AP side would be a webpage with two buttons... 1 "click to enter SSID and PW" which leads to a page with two input boxes whose strings would be used in the server code setup; and 2 "click to enter Control Labels" which leads to a page with two input boxes to enter labels to replace the LED1 and LED2 labels on the server.
Both pages (SSID/PW config and Labels config) would have a submit button which would enter the variables (parameters) into the server code and restart the ESP so the changes would take effect.
I believe it can be done, I just can't find sample code since I'm not really sure what it's called. It doesn't seem like it should be too hard. Thanks for your help.
(BTW, I left the instructable link commented in the code for those who want to view the original posting.)
I would like to add the AP function, running at the same time, so I can access the ESP and set parameters for the SSID and PW since I have several APs in my environment and will need to be able to change these "on the fly."
If possible, I would also like to change the labels of LED1 and LED2 to the actual appliance they will control using variables entered in the AP mode function. Can anyone point me in the right direction, or help me fill in the code?
I'm thinking the AP side would be a webpage with two buttons... 1 "click to enter SSID and PW" which leads to a page with two input boxes whose strings would be used in the server code setup; and 2 "click to enter Control Labels" which leads to a page with two input boxes to enter labels to replace the LED1 and LED2 labels on the server.
Both pages (SSID/PW config and Labels config) would have a submit button which would enter the variables (parameters) into the server code and restart the ESP so the changes would take effect.
I believe it can be done, I just can't find sample code since I'm not really sure what it's called. It doesn't seem like it should be too hard. Thanks for your help.
(BTW, I left the instructable link commented in the code for those who want to view the original posting.)
Code: Select all
/* See: http://www.instructables.com/id/ESP8266-WiFi-Module-for-Dummies/ for the details on how to set the board up and upload the code to the ESP8266
Also see: http://www.instructables.com/id/The-Simple-Guide-to-Flashing-Your-ESP8266-Firmware/
*/
// Include the ESP8266 Library. This library is automatically provided by the ESP8266 Board Manager and does not need to be installed manually.
#include <ESP8266WiFi.h>
String codeVersion = "Version 2.0 Lights";
// WiFi Router Login - change these to your router settings
const char* SSID = "MyWifi SSID";
const char* password = "MyPassWord";
// Setup GPIO2
int pinGPIO2 = 2; //To control LED 1
int pinGPIO0 = 0; //To control LED 2
int led1Status = 0; //LED 1 0=off,1=on,2=dimmed
int led2Status = 0; //LED 2 0=off,1=on,2=dimmed
// Create the ESP Web Server on port 80
WiFiServer WebServer(80);
// Web Client
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.println(codeVersion);
// Setup the GPIO2 LED Pin - uses PWM to dim the LED.
pinMode(pinGPIO2, OUTPUT);
digitalWrite(pinGPIO2, LOW);
led1Status = 0;
// Setup the GPIO0 LED Pin - uses PWM to dim the LED.
pinMode(pinGPIO0, OUTPUT);
digitalWrite(pinGPIO0, LOW);
led2Status = 0;
// Connect to WiFi network
Serial.println();
WiFi.disconnect();
WiFi.mode(WIFI_STA);
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi");
// Start the Web Server
WebServer.begin();
Serial.println("Web Server started");
// Print the IP address
Serial.print("You can connect to the ESP8266 at this URL: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a user has connected
client = WebServer.available();
if (!client) {//restart loop
return;
}
// Wait until the user sends some data
Serial.println("New User");
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r\n');
Serial.println(request);
client.flush();
// Process the request:
if (request.indexOf("/LED1=ON") != -1) {
analogWrite(pinGPIO2, 1023);
led1Status = 1;
}
if (request.indexOf("/LED1=OFF") != -1) {
analogWrite(pinGPIO2, 0);
led1Status = 0;
}
if (request.indexOf("/LED1=DIM") != -1) {
analogWrite(pinGPIO2, 512);
led1Status = 2;
}
if (request.indexOf("/LED2=ON") != -1) {
analogWrite(pinGPIO0, 1023);
led2Status = 1;
}
if (request.indexOf("/LED2=OFF") != -1) {
analogWrite(pinGPIO0, 0);
led2Status = 0;
}
if (request.indexOf("/LED2=DIM") != -1) {
analogWrite(pinGPIO0, 512);
led2Status = 2;
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html; charset=UTF-8");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<title>Front Lights</title>");
client.println("</head>");
client.println("<body>");
client.println("<a href=\"/\">Refresh Status</a>");
client.println("</br></br>");
//check the LED status
if (led1Status == 0) {
client.print("LED1 is Off</br>");
client.println("Turn the LED1 <a href=\"/LED1=ON\">ON</a></br>");
client.println("Set LED1 to <a href=\"/LED1=DIM\">DIM</a></br>");
} else if (led1Status == 1) {
client.print("LED1 is On</br>");
client.println("Turn the LED1 <a href=\"/LED1=OFF\">OFF</a></br>");
client.println("Set LED1 to <a href=\"/LED1=DIM\">DIM</a></br>");
} else if (led1Status == 2) {
client.print("LED1 is Dimmed</br>");
client.println("Turn the LED1 <a href=\"/LED1=OFF\">OFF</a></br>");
client.println("Turn the LED1 <a href=\"/LED1=ON\">ON</a></br>");
}
if (led2Status == 0) {
client.print("LED2 is Off</br>");
client.println("Turn the LED2 <a href=\"/LED2=ON\">ON</a></br>");
client.println("Set LED2 to <a href=\"/LED2=DIM\">DIM</a></br>");
} else if (led2Status == 1) {
client.print("LED2 is On</br>");
client.println("Turn the LED2 <a href=\"/LED2=OFF\">OFF</a></br>");
client.println("Set LED2 to <a href=\"/LED2=DIM\">DIM</a></br>");
} else if (led2Status == 2) {
client.print("LED2 is Dimmed</br>");
client.println("Turn the LED2 <a href=\"/LED2=OFF\">OFF</a></br>");
client.println("Turn the LED2 <a href=\"/LED2=ON\">ON</a></br>");
}
client.println("</br>");
client.println("</br>");
client.println("</body>");
client.println("</html>");
delay(1);
Serial.println("User disconnected");
Serial.println("");
}