The use of the ESP8266 in the world of IoT

User avatar
By aledrus
#73718
mrespman wrote:So I took my best stab at merging the sketches. The wifi manager does what it is supposed to... boots in AP mode, if necessary, let's me enter credentials, then restarts in server mode and successfully connects to my network.

I can ping the ip and get a response, but when I try to go to the http site, I get a page cannot be found error. I've missed something, but I don't know what. The server does not seem to work as a web_station client(?)

Here is the sketch I loaded. Any thoughts are appreciated. Thanks.
Code: Select all#include <FS.h>                   //this needs to be first, or it all crashes and burns...

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

//These lines are from the LED sketch and many are commented out here.
//I left them here for reference.

String codeVersion = "Version 2.0  Lights";

// // WiFi Router Login - change these to your router settings
//const char* SSID = "MY-SSID";
//const char* password = "MY-PASSWORD";

// Setup GPIO2 and GPIO0
int pinGPIO2 = 2; //To control LED1
int pinGPIO0 = 0; //To control LED2
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() {
//These first lines are from LED server sketch.
  // put your setup code here, to run once:
  Serial.begin(115200);
  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;

//These lines are from wifi manager sketch.
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  //exit after config instead of connecting
  wifiManager.setBreakAfterConfig(true);

  //reset settings - for testing
  wifiManager.resetSettings();


  //tries to connect to last known settings
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP" with password "Reilly.70"
  //and goes into a blocking loop awaiting configuration
  if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
    Serial.println("failed to connect, we should reset as see if it connects");
    delay(3000);
    ESP.reset();
    delay(5000);
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");


  Serial.println("local ip");
  Serial.println(WiFi.localIP());

//The rest of the void setup is from LED server sketch.
//I have commented it all out for now.
//I think this is the section where I have made mistakes and need help.

//  // 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() {
//The entire void loop is copied from LED server without any changes.
 
  // put your main code here, to run repeatedly:  // 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("");

}


THANKS for helping.


EDITED: SEE BELOW

Did you ever get this to work?

I followed the advice of the reply after you by doing a
Code: Select allWiFiServer server(8282);


But this did nothing. LIke you, i can ping the ESP (meaning the ESP has connected to the router).
But I cannot access its http services.


*** EDITED ***

I have noticed where the problem is.

In void loop(), I originally had the code like this:

Code: Select all  if (waitingforclient==0) {
    Serial.println("Local IP: "+locIP);
    waitingforclient=1;
    Serial.print("Waiting for web client..");
  }
  webClient = webServer.available();
  if (!webClient) {
    delay(1000);
    return;
  }
 


I thought the ESP was running, and was really waiting for the client (because that is what it said in Serial. I decided to investigate and changed it to this:

Code: Select all  if (waitingforclient==0) {
    Serial.println("Local IP: "+locIP);
    waitingforclient=1;
  }
  webClient = webServer.available();
  if (!webClient) {
    Serial.print("Waiting for web client..");
    delay(1000);
    return;
  }
 


I was hoping I'd see endless "Waiting for web client.." messages, but there wasn't. It crashed with stack errors and so on. So when I thought the device was waiting for client, it wasn't. (It still responded to pings though).

I looked back in your code in void setup() and I noticed you commented out the webserver:

Code: Select all//  // Start the Web Server
//  WebServer.begin();
//  Serial.println("Web Server started");


Putting this code back in and everything is fine =)