-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi.ino
160 lines (134 loc) · 4.27 KB
/
wifi.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
#include <ESP8266mDNS.h>
#include <DoubleResetDetector.h>
#include <WiFiManager.h>
// Number of seconds after reset during which a
// subseqent reset will be considered a double reset.
#define DRD_TIMEOUT 5
// RTC Memory Address for the DoubleResetDetector to use
#define DRD_ADDRESS 0
DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS);
WiFiManager wm;
unsigned int startTime = millis();
void restart(void) {
delay(RESTART_DELAY * 1000);
ESP.restart();
}
void printWifiStatus() {
String ssid = WiFi.SSID();
int8_t rssi = WiFi.RSSI();
IPAddress ip = WiFi.localIP();
// print the SSID of the network you're attached to:
DEBUG_INFO("SSID: %s, IP Address: %s, Signal (RSSI): %d dBm", ssid.c_str(), ip.toString().c_str(), rssi);
}
void startConfigPortal(void) {
// Start configuration portal non blocking, so we can use rolloffino anyway.
wm.setConfigPortalBlocking(false);
DEBUG_INFO("Starting configuration portal with SSID %s (timeout %d seconds).", WIFI_DEFAULT_AP_SSID, WIFI_PORTAL_TIMEOUT);
if (!wm.startConfigPortal(WIFI_DEFAULT_AP_SSID, WIFI_DEFAULT_AP_SECRET)) {
DEBUG_DEBUG("Portal is already running");
}
startTime = millis();
}
void setup_wifi() {
// diagnostic, allow time to get serial monitor displayed
delay(INIT_DELAY_SECS * 1000);
// it is a good practice to make sure your code sets wifi mode how you want it.
// explicitly set mode, esp defaults to STA+AP
WiFi.mode(WIFI_STA);
WiFi.forceSleepWake();
WiFi.setSleepMode(WIFI_NONE_SLEEP);
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
wm.setHostname("rolloffino");
MDNS.begin("rolloffino");
wm.setConfigPortalBlocking(false);
// set configportal timeout
// wm.setConfigPortalTimeout(WIFI_PORTAL_TIMEOUT);
bool res;
if (drd.detectDoubleReset()) {
DEBUG_INFO("Double Reset detected. Starting configuration portal on %s...", WIFI_DEFAULT_AP_SSID);
startConfigPortal();
} else {
DEBUG_DEBUG("Connecting to last configured AP");
// wm.setConfigPortalTimeout(WIFI_PORTAL_TIMEOUT);
wm.setWiFiAutoReconnect(true);
wm.setConnectTimeout(WIFI_CONNECTION_TIMEOUT);
// password protected ap
res = wm.autoConnect(WIFI_DEFAULT_AP_SSID, WIFI_DEFAULT_AP_SECRET);
if (!res) {
DEBUG_ERROR("Failed to connect to SSID %s... restarting in %ds", wm.getWiFiSSID().c_str(), RESTART_DELAY);
// restart();
startConfigPortal();
} else {
//if you get here you have connected to the WiFi
DEBUG_INFO("connected to %s yeey :)", wm.getWiFiSSID().c_str());
connectWifi();
}
}
DEBUG_INFO("Network online, ready for rolloffino driver connections.");
}
void connectWifi() {
DEBUG_VERBOSE("not connected");
if (indiConnected) {
DEBUG_INFO("Lost the WiFi connection");
}
indiConnected = false;
if (client) {
client.stop();
}
// Start listening
server.begin();
printWifiStatus();
}
WiFiClient get_wifi_client(WiFiClient client) {
if (!client) {
client = server.available();
}
if (client.connected()) {
DEBUG_VERBOSE("client.connected");
if (!indiConnected) {
indiConnected = true;
// indiData = false;
DEBUG_INFO("rolloffino driver connected");
}
} else {
DEBUG_VERBOSE("NOT client.connected");
if (indiConnected) {
indiConnected = false;
DEBUG_INFO("rolloffino driver disconnected");
}
}
DEBUG_VERBOSE("after client.connected checks");
return client;
}
void wifi_loop(Motor *m) {
MDNS.update();
MDNS.addService("rolloffino", "tcp", 8888);
// Process WiFiManager config portal
wm.process();
// check for timeout
if ((millis() - startTime) > (WIFI_PORTAL_TIMEOUT * 1000)) {
DEBUG_INFO("Portal timeout after %d seconds...", WIFI_PORTAL_TIMEOUT);
if (wm.getConfigPortalActive()) {
DEBUG_INFO("Config portal is active... restarting...");
restart();
} else {
DEBUG_INFO("Config portal is not active... continue...");
startTime = millis();
}
}
client = get_wifi_client(client);
// Wait for incoming data from the INDI driver
if (client) {
client.flush();
if (client.available() > 0) {
DEBUG_VERBOSE("available data...");
parseCommand(m);
}
} else {
DEBUG_VERBOSE("No data available. Sleeping...");
}
}
void drd_loop() {
drd.loop();
}
// #endif