-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifimanager.ino
78 lines (62 loc) · 1.67 KB
/
wifimanager.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <ESP8266mDNS.h>
#include "WifiManager.hpp"
//==============================
// HARDWARE SETTINGS
//==============================
#define BAUD 115200
#define HTTP_PORT 80
#define STASSID "SSID"
#define STAPSK "CorrectHorseBatteryStaple"
//==============================
// NETWORK SETTINGS
//==============================
const char* ssid = STASSID;
const char* password = STAPSK;
//==============================
// GLOBALS
//==============================
WifiManager wifi_manager;
ESP8266WebServer httpServer(HTTP_PORT);
String host = "awesome-iot-device";
//==============================
// SETUP FUNCTIONS
//==============================
void setup() {
Serial.begin(BAUD);
wifi_manager.SetHost(host);
wifi_manager.Setup(httpServer);
delay(1000);
SetupWiFi();
SetupWebpages();
}
void SetupWebpages() {
//Add custom pages
//httpServer.on("/page1", HTTP_POST, handlePage1);
//httpServer.on("/page2", HTTP_GET, handlePage2);
httpServer.begin();
}
void SetupWiFi(void) {
Serial.println("Booting...");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int wifi_attempts = 0;
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
WiFi.begin(ssid, password);
Serial.println("WiFi failed, retrying.");
delay(100);
wifi_attempts += 1;
if (wifi_attempts > 10){
return;
}
}
Serial.printf("Open http://%s.local in your browser\n", host.c_str());
}
void loop() {
wifi_manager.CheckStatus();
wifi_manager.dnsServer.processNextRequest();
httpServer.handleClient();
}