mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-17 08:31:31 +10:00
reworking examples
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wifiManager;
|
||||
//reset saved settings
|
||||
//wifiManager.resetSettings();
|
||||
|
||||
//set custom ip for portal
|
||||
//wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
|
||||
|
||||
//fetches ssid and pass from eeprom and tries to connect
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
wifiManager.autoConnect("AutoConnectAP");
|
||||
//or use this for auto generated name ESP + ChipID
|
||||
//wifiManager.autoConnect();
|
||||
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
}
|
||||
+30
-9
@@ -11,6 +11,11 @@ char mqtt_server[40];
|
||||
char mqtt_port[6] = "8080";
|
||||
char api_token[32] = "YOUR_API_TOKEN";
|
||||
|
||||
//default custom static IP
|
||||
char static_ip[16] = "10.0.1.56";
|
||||
char static_gw[16] = "10.0.1.1";
|
||||
char static_sn[16] = "255.255.255.0";
|
||||
|
||||
//flag for saving data
|
||||
bool shouldSaveConfig = false;
|
||||
|
||||
@@ -50,6 +55,16 @@ void setupSpiffs(){
|
||||
strcpy(mqtt_port, json["mqtt_port"]);
|
||||
strcpy(api_token, json["api_token"]);
|
||||
|
||||
if(json["ip"]) {
|
||||
Serial.println("setting custom ip from config");
|
||||
strcpy(static_ip, json["ip"]);
|
||||
strcpy(static_gw, json["gateway"]);
|
||||
strcpy(static_sn, json["subnet"]);
|
||||
Serial.println(static_ip);
|
||||
} else {
|
||||
Serial.println("no custom ip in config");
|
||||
}
|
||||
|
||||
} else {
|
||||
Serial.println("failed to load json config");
|
||||
}
|
||||
@@ -88,19 +103,19 @@ void setup() {
|
||||
wm.addParameter(&custom_mqtt_port);
|
||||
wm.addParameter(&custom_api_token);
|
||||
|
||||
// set static ip
|
||||
IPAddress _ip,_gw,_sn;
|
||||
_ip.fromString(static_ip);
|
||||
_gw.fromString(static_gw);
|
||||
_sn.fromString(static_sn);
|
||||
wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);
|
||||
|
||||
//reset settings - wipe credentials for testing
|
||||
//wm.resetSettings();
|
||||
|
||||
//set minimu quality of signal so it ignores AP's under that quality, default is 8%
|
||||
//wm.setMinimumSignalQuality();
|
||||
|
||||
//sets timeout until configuration portal gets turned off
|
||||
//wm.setConfigPortalTimeout(120);
|
||||
|
||||
//automatically connect using saved credentials if they exist
|
||||
//If connection fails it starts an access point with the specified name
|
||||
//here "AutoConnectAP" if empty will auto generate basedcon chipid, if password is blank it will be anonymous
|
||||
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if (!wm.autoConnect("AutoConnectAP", "password")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
@@ -127,20 +142,26 @@ void setup() {
|
||||
json["mqtt_port"] = mqtt_port;
|
||||
json["api_token"] = api_token;
|
||||
|
||||
json["ip"] = WiFi.localIP().toString();
|
||||
json["gateway"] = WiFi.gatewayIP().toString();
|
||||
json["subnet"] = WiFi.subnetMask().toString();
|
||||
|
||||
File configFile = SPIFFS.open("/config.json", "w");
|
||||
if (!configFile) {
|
||||
Serial.println("failed to open config file for writing");
|
||||
}
|
||||
|
||||
json.printTo(Serial);
|
||||
json.prettyPrintTo(Serial);
|
||||
json.printTo(configFile);
|
||||
configFile.close();
|
||||
//end save
|
||||
shouldSaveConfig = false;
|
||||
}
|
||||
|
||||
Serial.println("local ip");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
Serial.println(WiFi.gatewayIP());
|
||||
Serial.println(WiFi.subnetMask());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
+44
-58
@@ -7,10 +7,10 @@
|
||||
#endif
|
||||
|
||||
//define your default values here, if there are different values in config.json, they are overwritten.
|
||||
//length should be max size + 1
|
||||
char mqtt_server[40];
|
||||
char mqtt_port[6] = "8080";
|
||||
char blynk_token[33] = "YOUR_BLYNK_TOKEN";
|
||||
char mqtt_port[6] = "8080";
|
||||
char api_token[32] = "YOUR_API_TOKEN";
|
||||
|
||||
//default custom static IP
|
||||
char static_ip[16] = "10.0.1.56";
|
||||
char static_gw[16] = "10.0.1.1";
|
||||
@@ -25,13 +25,9 @@ void saveConfigCallback () {
|
||||
shouldSaveConfig = true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
void setupSpiffs(){
|
||||
//clean FS, for testing
|
||||
//SPIFFS.format();
|
||||
// SPIFFS.format();
|
||||
|
||||
//read configuration from FS json
|
||||
Serial.println("mounting FS...");
|
||||
@@ -57,24 +53,18 @@ void setup() {
|
||||
|
||||
strcpy(mqtt_server, json["mqtt_server"]);
|
||||
strcpy(mqtt_port, json["mqtt_port"]);
|
||||
strcpy(blynk_token, json["blynk_token"]);
|
||||
strcpy(api_token, json["api_token"]);
|
||||
|
||||
if(json["ip"]) {
|
||||
Serial.println("setting custom ip from config");
|
||||
//static_ip = json["ip"];
|
||||
strcpy(static_ip, json["ip"]);
|
||||
strcpy(static_gw, json["gateway"]);
|
||||
strcpy(static_sn, json["subnet"]);
|
||||
//strcat(static_ip, json["ip"]);
|
||||
//static_gw = json["gateway"];
|
||||
//static_sn = json["subnet"];
|
||||
Serial.println(static_ip);
|
||||
/* Serial.println("converting ip");
|
||||
IPAddress ip = ipFromCharArray(static_ip);
|
||||
Serial.println(ip);*/
|
||||
} else {
|
||||
Serial.println("no custom ip in config");
|
||||
}
|
||||
|
||||
} else {
|
||||
Serial.println("failed to load json config");
|
||||
}
|
||||
@@ -84,58 +74,53 @@ void setup() {
|
||||
Serial.println("failed to mount FS");
|
||||
}
|
||||
//end read
|
||||
Serial.println(static_ip);
|
||||
Serial.println(blynk_token);
|
||||
Serial.println(mqtt_server);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
setupSpiffs();
|
||||
|
||||
// WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wm;
|
||||
|
||||
//set config save notify callback
|
||||
wm.setSaveConfigCallback(saveConfigCallback);
|
||||
|
||||
// setup custom parameters
|
||||
//
|
||||
// The extra parameters to be configured (can be either global or just in the setup)
|
||||
// After connecting, parameter.getValue() will get you the configured value
|
||||
// id/name placeholder/prompt default length
|
||||
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
|
||||
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5);
|
||||
WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 34);
|
||||
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
|
||||
WiFiManagerParameter custom_api_token("api", "api token", "", 32);
|
||||
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wifiManager;
|
||||
//add all your parameters here
|
||||
wm.addParameter(&custom_mqtt_server);
|
||||
wm.addParameter(&custom_mqtt_port);
|
||||
wm.addParameter(&custom_api_token);
|
||||
|
||||
//set config save notify callback
|
||||
wifiManager.setSaveConfigCallback(saveConfigCallback);
|
||||
|
||||
//set static ip
|
||||
// set static ip
|
||||
IPAddress _ip,_gw,_sn;
|
||||
_ip.fromString(static_ip);
|
||||
_gw.fromString(static_gw);
|
||||
_sn.fromString(static_sn);
|
||||
wm.setSTAStaticIPConfig(_ip, _gw, _sn);
|
||||
|
||||
wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);
|
||||
|
||||
//add all your parameters here
|
||||
wifiManager.addParameter(&custom_mqtt_server);
|
||||
wifiManager.addParameter(&custom_mqtt_port);
|
||||
wifiManager.addParameter(&custom_blynk_token);
|
||||
//reset settings - wipe credentials for testing
|
||||
//wm.resetSettings();
|
||||
|
||||
//reset settings - for testing
|
||||
//wifiManager.resetSettings();
|
||||
|
||||
//set minimu quality of signal so it ignores AP's under that quality
|
||||
//defaults to 8%
|
||||
wifiManager.setMinimumSignalQuality();
|
||||
|
||||
//sets timeout until configuration portal gets turned off
|
||||
//useful to make it all retry or go to sleep
|
||||
//in seconds
|
||||
//wifiManager.setConfigPortalTimeout(120);
|
||||
|
||||
//fetches ssid and pass and tries to connect
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//automatically connect using saved credentials if they exist
|
||||
//If connection fails it starts an access point with the specified name
|
||||
//here "AutoConnectAP" if empty will auto generate basedcon chipid, if password is blank it will be anonymous
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
|
||||
if (!wm.autoConnect("AutoConnectAP", "password")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
delay(3000);
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
// if we still have not connected restart and try all over again
|
||||
ESP.restart();
|
||||
delay(5000);
|
||||
}
|
||||
@@ -146,7 +131,7 @@ void setup() {
|
||||
//read updated parameters
|
||||
strcpy(mqtt_server, custom_mqtt_server.getValue());
|
||||
strcpy(mqtt_port, custom_mqtt_port.getValue());
|
||||
strcpy(blynk_token, custom_blynk_token.getValue());
|
||||
strcpy(api_token, custom_api_token.getValue());
|
||||
|
||||
//save the custom parameters to FS
|
||||
if (shouldSaveConfig) {
|
||||
@@ -154,12 +139,12 @@ void setup() {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonObject& json = jsonBuffer.createObject();
|
||||
json["mqtt_server"] = mqtt_server;
|
||||
json["mqtt_port"] = mqtt_port;
|
||||
json["blynk_token"] = blynk_token;
|
||||
json["mqtt_port"] = mqtt_port;
|
||||
json["api_token"] = api_token;
|
||||
|
||||
json["ip"] = WiFi.localIP().toString();
|
||||
json["gateway"] = WiFi.gatewayIP().toString();
|
||||
json["subnet"] = WiFi.subnetMask().toString();
|
||||
json["ip"] = WiFi.localIP().toString();
|
||||
json["gateway"] = WiFi.gatewayIP().toString();
|
||||
json["subnet"] = WiFi.subnetMask().toString();
|
||||
|
||||
File configFile = SPIFFS.open("/config.json", "w");
|
||||
if (!configFile) {
|
||||
@@ -170,6 +155,7 @@ void setup() {
|
||||
json.printTo(configFile);
|
||||
configFile.close();
|
||||
//end save
|
||||
shouldSaveConfig = false;
|
||||
}
|
||||
|
||||
Serial.println("local ip");
|
||||
+10
-8
@@ -1,14 +1,16 @@
|
||||
// LED will blink when in config mode
|
||||
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
|
||||
//for LED status
|
||||
#include <Ticker.h>
|
||||
Ticker ticker;
|
||||
int LED = BUILTIN_LED;
|
||||
|
||||
void tick()
|
||||
{
|
||||
//toggle state
|
||||
int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin
|
||||
digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state
|
||||
digitalWrite(LED, !digitalRead(LED)); // set pin to the opposite state
|
||||
}
|
||||
|
||||
//gets called when WiFiManager enters configuration mode
|
||||
@@ -26,24 +28,24 @@ void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
//set led pin as output
|
||||
pinMode(BUILTIN_LED, OUTPUT);
|
||||
pinMode(LED, OUTPUT);
|
||||
// start ticker with 0.5 because we start in AP mode and try to connect
|
||||
ticker.attach(0.6, tick);
|
||||
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wifiManager;
|
||||
WiFiManager wm;
|
||||
//reset settings - for testing
|
||||
//wifiManager.resetSettings();
|
||||
wm.resetSettings();
|
||||
|
||||
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
|
||||
wifiManager.setAPCallback(configModeCallback);
|
||||
wm.setAPCallback(configModeCallback);
|
||||
|
||||
//fetches ssid and pass and tries to connect
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if (!wifiManager.autoConnect()) {
|
||||
if (!wm.autoConnect()) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
ESP.restart();
|
||||
@@ -54,7 +56,7 @@ void setup() {
|
||||
Serial.println("connected...yeey :)");
|
||||
ticker.detach();
|
||||
//keep LED on
|
||||
digitalWrite(BUILTIN_LED, LOW);
|
||||
digitalWrite(LED, LOW);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -1,18 +0,0 @@
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; http://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:nodemcuv2]
|
||||
platform=espressif8266
|
||||
board=nodemcuv2
|
||||
framework=arduino
|
||||
|
||||
[platformio]
|
||||
src_dir=AutoConnectWithTimeout
|
||||
lib_dir=/Users/shawn/projects/microcontrollers/dev/libraries
|
||||
@@ -0,0 +1,127 @@
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
|
||||
// select which pin will trigger the configuration portal when set to LOW
|
||||
// ESP-01 users please note: the only pins available (0 and 2), are shared
|
||||
// with the bootloader, so always set them HIGH at power-up
|
||||
#define TRIGGER_PIN 0
|
||||
const char* modes[] = { "NULL", "STA", "AP", "STA+AP" };
|
||||
|
||||
WiFiManager wifiManager;
|
||||
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
Serial.setDebugOutput(true);
|
||||
delay(3000);
|
||||
Serial.println("\n Starting");
|
||||
|
||||
// WiFi.mode(WIFI_STA);
|
||||
// WiFi.printDiag(Serial);
|
||||
// Serial.println(modes[WiFi.getMode()]);
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
// WiFiManager wifiManager;
|
||||
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
//reset settings - for testing
|
||||
//wifiManager.resetSettings();
|
||||
|
||||
//sets timeout until configuration portal gets turned off
|
||||
//useful to make it all retry or go to sleep
|
||||
//in seconds
|
||||
wifiManager.setConfigPortalTimeout(60);
|
||||
wifiManager.setConnectTimeout(20);
|
||||
// wifiManager.setShowStaticFields(true);
|
||||
|
||||
// uint8_t menu[] = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE};
|
||||
// wifiManager.setMenu(menu);
|
||||
|
||||
// std::vector<WiFiManager::menu_page_t> menu = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE,wifiManager.MENU_SEP,wifiManager.MENU_ERASE,wifiManager.MENU_EXIT};
|
||||
// wifiManager.setMenu(menu);
|
||||
|
||||
// std::vector<WiFiManager::menu_page_t> menu = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE,wifiManager.MENU_SEP,wifiManager.MENU_ERASE,wifiManager.MENU_EXIT};
|
||||
// wifiManager.setMenu(menu);
|
||||
|
||||
|
||||
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", "", 40);
|
||||
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", "", 6);
|
||||
WiFiManagerParameter custom_token("api_token", "api token", "", 0);
|
||||
WiFiManagerParameter custom_tokenb("invalid token", "invalid token", "", 0);
|
||||
|
||||
//add all your parameters here
|
||||
wifiManager.addParameter(&custom_mqtt_server);
|
||||
wifiManager.addParameter(&custom_mqtt_port);
|
||||
wifiManager.addParameter(&custom_token);
|
||||
wifiManager.addParameter(&custom_tokenb);
|
||||
|
||||
// MENU_WIFI = 0,
|
||||
// MENU_WIFINOSCAN = 1,
|
||||
// MENU_INFO = 2,
|
||||
// MENU_PARAM = 3,
|
||||
// MENU_CLOSE = 4,
|
||||
// MENU_RESTART = 5,
|
||||
// MENU_EXIT = 6,
|
||||
// MENU_ERASE = 7,
|
||||
// MENU_SEP = 8
|
||||
|
||||
// const char* menu[] = {"wifi","wifinoscan","info","param","close","sep","erase","restart","exit"};
|
||||
// wifiManager.setMenu(menu,9);
|
||||
|
||||
std::vector<const char *> menu = {"wifi","wifinoscan","info","param","close","sep","erase","restart","exit"};
|
||||
wifiManager.setMenu(menu);
|
||||
|
||||
//set static ip
|
||||
// wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
|
||||
// wifiManager.setShowStaticFields(false);
|
||||
// wifiManager.setShowDnsFields(false);
|
||||
|
||||
wifiManager.setConfigPortalTimeout(60);
|
||||
wifiManager.startConfigPortal("AutoConnectAP", "password");
|
||||
|
||||
//fetches ssid and pass and tries to connect
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if(!wifiManager.autoConnect("AutoConnectAP")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
}
|
||||
|
||||
pinMode(TRIGGER_PIN, INPUT);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
// is configuration portal requested?
|
||||
if ( digitalRead(TRIGGER_PIN) == LOW ) {
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
//reset settings - for testing
|
||||
//wifiManager.resetSettings();
|
||||
|
||||
//sets timeout until configuration portal gets turned off
|
||||
//useful to make it all retry or go to sleep
|
||||
//in seconds
|
||||
wifiManager.setConfigPortalTimeout(120);
|
||||
|
||||
//it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
|
||||
//WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1
|
||||
//WiFi.mode(WIFI_STA);
|
||||
|
||||
// disable captive portal redirection
|
||||
// wifiManager.setCaptivePortalEnable(false);
|
||||
|
||||
if (!wifiManager.startConfigPortal("OnDemandAP")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
delay(3000);
|
||||
} else {
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
}
|
||||
}
|
||||
|
||||
// put your main code here, to run repeatedly:
|
||||
}
|
||||
@@ -1,124 +1,46 @@
|
||||
/**
|
||||
* OnDemandConfigPortal.ino
|
||||
* example of running the configPortal AP manually, independantly from the captiveportal
|
||||
* trigger pin will start a configPortal AP for 120 seconds then turn it off.
|
||||
*
|
||||
*/
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
|
||||
// select which pin will trigger the configuration portal when set to LOW
|
||||
// ESP-01 users please note: the only pins available (0 and 2), are shared
|
||||
// with the bootloader, so always set them HIGH at power-up
|
||||
#define TRIGGER_PIN 0
|
||||
const char* modes[] = { "NULL", "STA", "AP", "STA+AP" };
|
||||
|
||||
WiFiManager wifiManager;
|
||||
|
||||
int timeout = 120; // seconds to run for
|
||||
bool webportalrunning = false;
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
Serial.setDebugOutput(true);
|
||||
delay(3000);
|
||||
Serial.println("\n Starting");
|
||||
|
||||
// Serial.println(modes[WiFi.getMode()]);
|
||||
// WiFi.printDiag(Serial);
|
||||
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wifiManager;
|
||||
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
//reset settings - for testing
|
||||
//wifiManager.resetSettings();
|
||||
|
||||
//sets timeout until configuration portal gets turned off
|
||||
//useful to make it all retry or go to sleep
|
||||
//in seconds
|
||||
wifiManager.setConfigPortalTimeout(20);
|
||||
wifiManager.setConnectTimeout(10);
|
||||
// wifiManager.setShowStaticFields(true);
|
||||
|
||||
// uint8_t menu[] = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE};
|
||||
// wifiManager.setMenu(menu);
|
||||
|
||||
// std::vector<WiFiManager::menu_page_t> menu = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE,wifiManager.MENU_SEP,wifiManager.MENU_ERASE,wifiManager.MENU_EXIT};
|
||||
// wifiManager.setMenu(menu);
|
||||
|
||||
// std::vector<WiFiManager::menu_page_t> menu = {wifiManager.MENU_WIFI,wifiManager.MENU_INFO,wifiManager.MENU_PARAM,wifiManager.MENU_CLOSE,wifiManager.MENU_SEP,wifiManager.MENU_ERASE,wifiManager.MENU_EXIT};
|
||||
// wifiManager.setMenu(menu);
|
||||
|
||||
|
||||
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", "", 40);
|
||||
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", "", 6);
|
||||
WiFiManagerParameter custom_token("api_token", "api token", "", 0);
|
||||
WiFiManagerParameter custom_tokenb("invalid token", "invalid token", "", 0);
|
||||
|
||||
//add all your parameters here
|
||||
wifiManager.addParameter(&custom_mqtt_server);
|
||||
wifiManager.addParameter(&custom_mqtt_port);
|
||||
wifiManager.addParameter(&custom_token);
|
||||
wifiManager.addParameter(&custom_tokenb);
|
||||
|
||||
// MENU_WIFI = 0,
|
||||
// MENU_WIFINOSCAN = 1,
|
||||
// MENU_INFO = 2,
|
||||
// MENU_PARAM = 3,
|
||||
// MENU_CLOSE = 4,
|
||||
// MENU_RESTART = 5,
|
||||
// MENU_EXIT = 6,
|
||||
// MENU_ERASE = 7,
|
||||
// MENU_SEP = 8
|
||||
|
||||
// const char* menu[] = {"wifi","wifinoscan","info","param","close","sep","erase","restart","exit"};
|
||||
// wifiManager.setMenu(menu,9);
|
||||
|
||||
std::vector<const char *> menu = {"wifi","wifinoscan","info","param","close","sep","erase","restart","exit"};
|
||||
wifiManager.setMenu(menu);
|
||||
|
||||
//set static ip
|
||||
wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
|
||||
// wifiManager.setShowStaticFields(false);
|
||||
// wifiManager.setShowDnsFields(false);
|
||||
|
||||
//fetches ssid and pass and tries to connect
|
||||
//if it does not connect it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
if(!wifiManager.autoConnect("AutoConnectAP")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
}
|
||||
|
||||
pinMode(TRIGGER_PIN, INPUT);
|
||||
pinMode(TRIGGER_PIN, INPUT_PULLUP);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
// is configuration portal requested?
|
||||
if ( digitalRead(TRIGGER_PIN) == LOW ) {
|
||||
//WiFiManager
|
||||
//Local intialization. Once its business is done, there is no need to keep it around
|
||||
if ( digitalRead(TRIGGER_PIN) == LOW) {
|
||||
WiFiManager wifiManager;
|
||||
|
||||
//reset settings - for testing
|
||||
//wifiManager.resetSettings();
|
||||
|
||||
// set configportal timeout
|
||||
wm.setConfigPortaltimeout(timeout);
|
||||
|
||||
//sets timeout until configuration portal gets turned off
|
||||
//useful to make it all retry or go to sleep
|
||||
//in seconds
|
||||
wifiManager.setConfigPortalTimeout(120);
|
||||
|
||||
//it starts an access point with the specified name
|
||||
//here "AutoConnectAP"
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
|
||||
//WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1
|
||||
//WiFi.mode(WIFI_STA);
|
||||
|
||||
// disable captive portal redirection
|
||||
// wifiManager.setCaptivePortalEnable(false);
|
||||
|
||||
if (!wifiManager.startConfigPortal("OnDemandAP")) {
|
||||
if (!wm.startConfigPortal("OnDemandAP")) {
|
||||
Serial.println("failed to connect and hit timeout");
|
||||
delay(3000);
|
||||
} else {
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
}
|
||||
//reset and try again, or maybe put it to deep sleep
|
||||
ESP.reset();
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
|
||||
}
|
||||
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
@@ -7,14 +7,11 @@
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
|
||||
// select which pin will trigger the configuration portal when set to LOW
|
||||
// ESP-01 users please note: the only pins available (0 and 2), are shared
|
||||
// with the bootloader, so always set them HIGH at power-up
|
||||
#define TRIGGER_PIN 0
|
||||
|
||||
int timeout = 120; // seconds
|
||||
|
||||
WiFiManager wifiManager;
|
||||
WiFiManager wm;
|
||||
|
||||
int timeout = 120; // seconds to run for
|
||||
bool webportalrunning = false;
|
||||
int start = millis();
|
||||
|
||||
@@ -27,19 +24,18 @@ void setup() {
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
if(webportalrunning){
|
||||
wifiManager.process();
|
||||
wm.process();
|
||||
if(millis()-start > timeout*1000){
|
||||
Serial.println("webportaltimeout");
|
||||
webportalrunning = false;
|
||||
wifiManager.stopWebPortal(); // auto off
|
||||
wm.stopWebPortal(); // auto off
|
||||
}
|
||||
}
|
||||
|
||||
// is configuration portal requested?
|
||||
if ( digitalRead(TRIGGER_PIN) == LOW && !webportalrunning) {
|
||||
wifiManager.startWebPortal();
|
||||
wm.startWebPortal();
|
||||
// wm.startWebPortal();
|
||||
webportalrunning = true;
|
||||
start = millis();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
|
||||
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
|
||||
WiFiManager wm;
|
||||
|
||||
//reset settings - wipe credentials for testing
|
||||
//wm.resetSettings();
|
||||
|
||||
//set custom ip for portal
|
||||
//wm.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
|
||||
|
||||
//automatically connect using saved credentials if they exist
|
||||
//If connection fails it starts an access point with the specified name
|
||||
//here "AutoConnectAP" if empty will auto generate basedcon chipid, if password is blank it will be anonymous
|
||||
//and goes into a blocking loop awaiting configuration
|
||||
wm.autoConnect("AutoConnectAP");
|
||||
//or use this for auto generated name ESP + ChipID
|
||||
//wm.autoConnect();
|
||||
|
||||
//if you get here you have connected to the WiFi
|
||||
Serial.println("connected...yeey :)");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user