This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ConfigOnSwitch.ino
241 lines (187 loc) · 8.13 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/****************************************************************************************************************************
ConfigOnSwitch.ino
WiFi/Credentials Manager for SAM DUE, SAMD, nRF52, STM32F/L/H/G/WB/MP1, etc. boards running `ESP8266/ESP32-AT-command` shields
ESP_AT_WiFiManager is a library for the Teensy, SAM DUE, SAMD, nRF52, STM32F/L/H/G/WB/MP1, etc. boards running `ESP8266/ESP32-AT-command` shields
(https://github.com/esp8266/Arduino) to enable easy configuration and reconfiguration of WiFi, etc. credentials using a Captive Portal
Based on and modified from Tzapu https://github.com/tzapu/WiFiManager
and from Ken Taylor https://github.com/kentaylor
Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WiFiManager
Licensed under MIT license
*****************************************************************************************************************************/
/****************************************************************************************************************************
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 device.
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.
*****************************************************************************************************************************/
// Credits of [Miguel Alexandre Wisintainer](https://github.com/tcpipchip) for this simple yet effective method
// For some STM32, there is only definition of Serial in variant.h, and is used for Serial/USB Debugging
// For example, in Nucleo-144 F767ZI original variant.h
//
// #define SERIAL_PORT_MONITOR Serial
// #define SERIAL_PORT_HARDWARE Serial
//
// To use ESP8266/ESP32-AT, we need another Serial, such as Serial1
// To do this, first, in corresponding variant.h, modify as follows:
// #define SERIAL_PORT_HARDWARE Serial1
//
// then assign pins D0 = RX/D1 = TX to be Hardware Serial1 by putting in sketch as follows:
//
// #define EspSerial SERIAL_PORT_HARDWARE //Serial1
// HardwareSerial Serial1(D0, D1);
//
// This must be included in defines.h for each board you'd like to use ESPSerial as Serial1
//
// The pin usage must be modified according to your boards.
#include "defines.h"
/* 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 = 16; //22; // Change the PIN to whatever you'd like
/*
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 = 23; // Change the PIN to whatever you'd like
// Indicates whether CP is forced
bool forcedConfig = false;
void heartBeatPrint()
{
static int num = 1;
if (WiFi.status() == WL_CONNECTED)
Serial.print("H"); // H means connected to WiFi
else
Serial.print("F"); // F means not connected to WiFi
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(" ");
}
}
void check_status()
{
static unsigned long checkstatus_timeout = 0;
#define HEARTBEAT_INTERVAL 10000L
// Print hearbeat every HEARTBEAT_INTERVAL (10) seconds.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
heartBeatPrint();
checkstatus_timeout = millis() + HEARTBEAT_INTERVAL;
}
}
void enterConfigPortal()
{
//Local intialization. Once its business is done, there is no need to keep it around
ESP_AT_WiFiManager ESP_AT_wiFiManager;
ESP_AT_wiFiManager.setDebugOutput(true);
ESP_AT_wiFiManager.setMinimumSignalQuality(-1);
// 0 for random channel
//ESP_AT_wiFiManager.setAPChannel(0);
ESP_AT_wiFiManager.setAPChannel(1);
// Default AP IP is 192.168.4.1. Uncomment to use different AP IP
ESP_AT_wiFiManager.setAPStaticIPConfig(staticAP_IP);
// Set static STA IP
//ESP_AT_wiFiManager.setSTAStaticIPConfig(IPAddress(192, 168, 2, 114));
//Check if there is stored WiFi router/password credentials.
//If not found, device will remain in configuration mode until switched off via webserver.
Serial.println("Opening Config Portal.");
Router_SSID = ESP_AT_wiFiManager.WiFi_SSID();
Router_Pass = ESP_AT_wiFiManager.WiFi_Pass();
if ( !forcedConfig && (Router_SSID != "") && ESP_AT_wiFiManager.isWiFiConfigValid() )
{
if (ESP_AT_wiFiManager.connectWifi(Router_SSID, Router_Pass) == WL_CONNECTED)
{
Serial.println(F("Got stored Credentials. Try to connect first"));
return;
}
ESP_AT_wiFiManager.setConfigPortalTimeout(60); //If no access point name has been previously entered disable timeout.
Serial.println(F("Got stored Credentials but can't connect. Timeout 60s"));
}
else
Serial.println(F("Forced CP, No stored or not valid Credentials. No timeout"));
forcedConfig = false;
// SSID to uppercase
ssid.toUpperCase();
//Starts an AP and goes into a blocking loop awaiting configuration
Serial.println("Start Config Portal, SSID = " + ssid + ", Pass = " + password);
digitalWrite(LOCAL_PIN_LED, LED_ON); // Turn led on as we enter Config Portal
if (!ESP_AT_wiFiManager.startConfigPortal((const char *) ssid.c_str(), password))
Serial.println(F("Not connected to WiFi but continuing anyway."));
else
Serial.println(F("WiFi connected...yeey"));
digitalWrite(LOCAL_PIN_LED, LED_OFF); // Turn led off as we exit Config Portal
ESP_AT_wiFiManager.resetBoard();
}
void setup()
{
// put your setup code here, to run once:
// initialize the LED digital pin as an output.
pinMode(TRIGGER_PIN, INPUT_PULLUP);
pinMode(TRIGGER_PIN2, INPUT_PULLUP);
pinMode(LOCAL_PIN_LED, OUTPUT);
// turn the LED on by making the voltage LOW to tell us we are in configuration mode.
digitalWrite(LOCAL_PIN_LED, LED_ON);
Serial.begin(115200);
while (!Serial && millis() < 5000);
unsigned long startedAt = millis();
#if USE_ESP32_AT
Serial.println("\nStart ConfigOnSwitch with ESP32-AT WiFi module on " + String(BOARD_NAME));
#else
Serial.println("\nStart ConfigOnSwitch with ESP8266-AT WiFi module on " + String(BOARD_NAME));
#endif
Serial.println(ESP_AT_WIFIMANAGER_VERSION);
// initialize serial for ESP module
EspSerial.begin(115200);
// initialize ESP module
WiFi.init(&EspSerial);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD)
{
Serial.println(F("WiFi shield not present"));
// don't continue
while (true);
}
enterConfigPortal();
// For some unknown reason webserver can only be started once per boot up
// so webserver can not be used again in the sketch.
#define WIFI_CONNECT_TIMEOUT 30000L
#define WHILE_LOOP_DELAY 200L
#define WHILE_LOOP_STEPS (WIFI_CONNECT_TIMEOUT / ( 3 * WHILE_LOOP_DELAY ))
startedAt = millis();
while ( (WiFi.status() != WL_CONNECTED) && (millis() - startedAt < WIFI_CONNECT_TIMEOUT ) )
{
int i = 0;
while ((!WiFi.status() || WiFi.status() >= WL_DISCONNECTED) && i++ < WHILE_LOOP_STEPS)
{
delay(WHILE_LOOP_DELAY);
}
}
Serial.print(F("After waiting "));
Serial.print((millis() - startedAt) / 1000);
Serial.print(F(" secs in setup(), connect result is "));
if (WiFi.status() == WL_CONNECTED)
{
Serial.print(F("connected. Local IP: "));
Serial.println(WiFi.localIP());
}
}
void loop()
{
// is configuration portal requested?
if ((digitalRead(TRIGGER_PIN) == LOW) || (digitalRead(TRIGGER_PIN2) == LOW))
{
Serial.println("\nConfig Portal requested.");
forcedConfig = true;
enterConfigPortal();
}
// put your main code here, to run repeatedly
check_status();
}