-->
Page 1 of 1

Adding own website to wifiManager

PostPosted: Sat Mar 24, 2018 2:48 am
by MartynC
Hi,

I've started going in circles with this. I am using the Ken Taylor port of the wifiManager library and want to add my own website.

I have the wifiManager library working.
I have the website working when the credentials are hard coded.
Failed at combining the two.

What I would like to achieve:
- Use the wifiManager portal to enter the wifi credentials
- Connect to the local wifi
- serve a small website over the local network.
- Still have the wifiManager portal active but only only when connected directly to the ESP8266 on 192.168.4.1

In the below sketch. The ESP8266 connects to the local network but when I go to the new ip address I get the wifiManager portal. I don't get my website.

EDIT: It appears the captive portal is still active and the code in the loop() function is never run (I don't get the "Looping" message in the serial monitor).


Everything tells me this should be straight forward but I cannot get it to work.

Code: Select all
#include <ESP8266WiFi.h>
WiFiServer server(80);


#include <ESP8266WiFi.h>         

//needed for library
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>     

 
// Onboard LED I/O pin on NodeMCU board
const int PIN_LED = D4;

String request = "";


unsigned long time_now = 0;
unsigned long time_prev = 0;


void setup()
{

    pinMode(PIN_LED, OUTPUT);
    Serial.begin(115200);
    Serial.println("\n Starting");

    unsigned long startedAt = millis();
    WiFi.printDiag(Serial); //Remove this line if you do not want to see WiFi password printed
    Serial.println("Opening configuration portal");

    digitalWrite(PIN_LED, LOW); // turn the LED on by making the voltage LOW to tell us we are in configuration mode.
   
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;
   

    //it starts an access point
    //and goes into a blocking loop awaiting configuration
    if (!wifiManager.startConfigPortal("ESP8266","password"))
    {    //Delete these two parameters if you do not want a WiFi password on your configuration access point
         Serial.println("Not connected to WiFi but continuing anyway.");
    }
    else
    {
         //if you get here you have connected to the WiFi
         Serial.println("connected...yeey :)");
    }
   
   digitalWrite(PIN_LED, HIGH); // Turn led off as we are not in configuration mode.
        // For some unknown reason webserver can only be started once per boot up
        // so webserver can not be used again in the sketch.
     
    Serial.print("After waiting ");
    int connRes = WiFi.waitForConnectResult();
    float waited = (millis()- startedAt);
    Serial.print(waited/1000);
    Serial.print(" secs in setup() connection result is ");
    Serial.println(connRes);
   
    if (WiFi.status()!=WL_CONNECTED)
    {
        Serial.println("failed to connect, finishing setup anyway");
    }
    else
    {
        Serial.print("local ip: ");
        Serial.println(WiFi.localIP());
    }
   
}


void loop()
{
   
   
    // Check if a client has connected
    WiFiClient client = server.available();

    if (client)
    {
        Serial.println("Client is connected");
        // Read the first line of the request
        request = client.readStringUntil('\r');
        Serial.print(F("request = "));   Serial.println( request );

        client.flush();
       
        client.print( "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" );
        client.print( "<!DOCTYPE html><html><head><title>Title</title></head><body><div id='main'><h2>H2 title</h2></div></body></html>" );

        delay(5);
        client.stop();
        Serial.println("Client disonnected");
        time_prev = millis();
    }

    time_now = millis();
    if (time_now - time_prev > 5000)
    {
         time_prev = millis();
         Serial.println( "Looping" );
    }
   
 
}