tzapu / WiFiManager

ESP8266 WiFi Connection manager with web captive portal
http://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
MIT License
6.45k stars 1.94k forks source link

WebServer: Only own custom routes after setup #1598

Closed SkHCrusher closed 1 year ago

SkHCrusher commented 1 year ago

Basic Infos

Challenge: After the Wifi setup, make only two own routes available without the config portal via web server

Hardware

WiFimanager 2.0.15

Esp8266/Esp32:

Hardware: ESP-32 (Wemos Lolin32 lite)

Description

I would like to use the WifiManager to configure the wifi for my alarm device via interface. After that I want to make only two routes available:

But I can't get my own web server to run, because the manager already uses it. I can start the webserver of the Wifi-Manager with startWebPortal(), but then the configuration menu is accessible again.

So how do I get only my two routes accessible after the successful Wifi setup.

Thanks for your help

Settings in IDE

Module: WEMOS LOLIN32 Lite

Sketch

#include <WiFiManager.h>

// Generell
bool alarmActive = false;
unsigned long currentTime = 0; 

// LED
const int ledPin = 5;
const int ledSwitchTime = 200;
bool currentLedState = false; 
unsigned long lastLedTime = 0;

// Wifi
WiFiManager wm;

void setup() {
  Serial.begin(115200);

  // LED
  pinMode(ledPin, OUTPUT);

  // Custom Routes
  wm.setWebServerCallback(bindServerCallback);

  // Init Wifi
  bool res;
  res = wm.autoConnect("TestAP", "password");
  if(!res) {
    ESP.restart();
  }

  // Start new Webserver
  Serial.println("Custom Webserver");
  wm.startWebPortal();
}

void loop() {
  currentTime = millis();
  wm.process();

  if (alarmActive) {
    // LED
    if ((lastLedTime + ledSwitchTime) < currentTime) {
      currentLedState = !currentLedState;
      lastLedTime = currentTime;
    }

    if (currentLedState) {
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(ledPin, LOW);
    }
  } else {
    // LED Off
    digitalWrite(ledPin, LOW);
  }
}

void bindServerCallback(){
  // Custom Routes
  wm.server->on("/alarm-start", activateAlarm);
  wm.server->on("/alarm-stop", deactivateAlarm);
}

void activateAlarm() {
  alarmActive = true;
  currentLedState = true;
  lastLedTime = currentTime;

  wm.server->send(200, "text/plain", "Alarm accepted");
}

void deactivateAlarm() {
  alarmActive = false;

  wm.server->send(200, "text/plain", "Alarm deactivated");
}
tablatronix commented 1 year ago

you can use the WM server its shared, for some reason you cannot stop and reopen a port in esp, some unknown bug.

You could override all other routes if you want them to not be available.. you can also probably remove everything form the menu, and leave the routes.

but you should be able to bind to the existing ones and override them, I think i tested this in the SUPER example

https://github.com/tzapu/WiFiManager/blob/master/examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino#L76

SkHCrusher commented 1 year ago

I don't find this in your example. But found another solution:

WebServer server(8080);
....
server.on("/alarm-start", activateAlarm);
server.on("/alarm-stop", deactivateAlarm);
server.begin();

I open the webserver on another port and this works for me :-) Thank you.