forked from tzapu/WiFiManager
/
ConfigOnSwitch.ino
96 lines (85 loc) · 4.02 KB
/
ConfigOnSwitch.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
This example will open a configuration portal when no WiFi configuration has been previously
entered or when a button is pushed. It is the easiest scenario for configuration but
requires a pin and a button on the ESP8266 device. The Flash button is convenient
for this on NodeMCU devices.
*/
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //https://github.com/kentaylor/WiFiManager
// select wich pin will trigger the configuraton 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
// Onboard LED I/O pin on NodeMCU board
const int PIN_LED = 2; // D4 on NodeMCU and WeMos. Controls the onboard LED.
/*Trigger for inititating config mode is Pin D3 and also flash button on NodeMCU
* Flash button is convenient to use but if it is pressed it will stuff up the serial port device driver
* until the computer is rebooted on windows machines.
*/
const int TRIGGER_PIN = 0; // D3 on NodeMCU and WeMos.
/*
* Alternative trigger pin. Needs to be connected to a button to use this pin. It must be a momentary connection
* not connected permanently to ground. Either trigger pin will work.
*/
const int TRIGGER_PIN2 = 13; // D7 on NodeMCU and WeMos.
// Indicates whether ESP has WiFi credentials saved from previous session
bool initialConfig = false;
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");
WiFi.printDiag(Serial); //Remove this line if you do not want to see WiFi password printed
if (WiFi.SSID()==""){
Serial.println("We haven't got any access point credentials, so get them now");
initialConfig = true;
}
else{
digitalWrite(PIN_LED, HIGH); // Turn led off as we are not in configuration mode.
WiFi.mode(WIFI_STA); // Force to station mode because if device was switched off while in access point mode it will start up next time in access point mode.
unsigned long startedAt = millis();
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);
}
pinMode(TRIGGER_PIN, INPUT_PULLUP);
pinMode(TRIGGER_PIN2, INPUT_PULLUP);
if (WiFi.status()!=WL_CONNECTED){
Serial.println("failed to connect, finishing setup anyway");
} else{
Serial.print("local ip: ");
Serial.println(WiFi.localIP());
}
}
void loop() {
// is configuration portal requested?
if ((digitalRead(TRIGGER_PIN) == LOW) || (digitalRead(TRIGGER_PIN2) == LOW) || (initialConfig)) {
Serial.println("Configuration portal requested.");
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 or device is restarted.
//wifiManager.setConfigPortalTimeout(600);
//it starts an access point
//and goes into a blocking loop awaiting configuration
if (!wifiManager.startConfigPortal()) {
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.
ESP.reset(); // This is a bit crude. For some unknown reason webserver can only be started once per boot up
// so resetting the device allows to go back into config mode again when it reboots.
delay(5000);
}
// put your main code here, to run repeatedly:
}