forked from kentaylor/WiFiManager
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConfigOnStartup.ino
78 lines (67 loc) · 3.16 KB
/
ConfigOnStartup.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
This example will open a configuration portal for 60 seconds when first powered up.
ConfigOnSwitch is a a bettter example for most situations but this has the advantage
that no pins or buttons are required on the ESP8266 device at the cost of delaying
the user sketch for the period that the configuration portal is open.
Also in this example a password is required to connect to the configuration portal
network. This is inconvenient but means that only those who know the password or those
already connected to the target WiFi network can access the configuration portal and
the WiFi network credentials will be sent from the browser over an encrypted connection and
can not be read by observers.
*/
// define ESP8266 or ESP32
#define ESP8266;
//#define ESP32;
#include <WiFi32Manager.h> // https://github.com/edumeneses/WiFi32Manager
// already includes:
// Wifi.h (https://github.com/esp8266/Arduino) or ESP8266WiFi.h (https://github.com/esp8266/Arduino)
// AND
// WebServer.h or ESP8266WebServer.h
// AND
// DNSServer.h
// Onboard LED I/O pin on NodeMCU board
const int PIN_LED = 2; // D4 on NodeMCU and WeMos. Controls the onboard LED.
void setup() {
// put your setup code here, to run once:
// initialize the LED digital pin as an output.
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;
//sets timeout in seconds until configuration portal gets turned off.
//If not specified device will remain in configuration mode until
//switched off via webserver.
if (WiFi.SSID()!="") wifiManager.setConfigPortalTimeout(60); //If no access point name has been previously entered disable timeout.
//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() {
delay(10000);
// put your main code here, to run repeatedly:
}