forked from collin80/A0RET
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwifi.cpp
194 lines (181 loc) · 7.25 KB
/
wifi.cpp
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
#include "config.h"
#include "wifi.h"
#include "gvret_comm.h"
#include "SerialConsole.h"
static IPAddress broadcastAddr(255,255,255,255);
WiFiManager::WiFiManager()
{
lastBroadcast = 0;
}
void WiFiManager::setup()
{
if (settings.wifiMode == 1) //connect to an AP
{
WiFi.mode(WIFI_STA);
WiFi.begin((const char *)settings.SSID, (const char *)settings.WPA2Key);
WiFiEventId_t eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info)
{
Serial.print("WiFi lost connection. Reason: ");
Serial.println(info.disconnected.reason);
SysSettings.isWifiConnected = false;
if (info.disconnected.reason == 202)
{
Serial.println("Connection failed, rebooting to fix it.");
esp_sleep_enable_timer_wakeup(10);
esp_deep_sleep_start();
delay(100);
}
}, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
}
if (settings.wifiMode == 2) //BE an AP
{
WiFi.mode(WIFI_AP);
WiFi.softAP((const char *)settings.SSID, (const char *)settings.WPA2Key);
}
}
void WiFiManager::loop()
{
boolean needServerInit = false;
int i;
if (settings.wifiMode > 0)
{
if (!SysSettings.isWifiConnected)
{
if (WiFi.isConnected())
{
Serial.print("Wifi now connected to SSID ");
Serial.println((const char *)settings.SSID);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
needServerInit = true;
}
if (settings.wifiMode == 2)
{
Serial.print("Wifi setup as SSID ");
Serial.println((const char *)settings.SSID);
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
needServerInit = true;
}
if (needServerInit)
{
SysSettings.isWifiConnected = true;
wifiServer.begin(23); //setup as a telnet server
wifiServer.setNoDelay(true);
ArduinoOTA.setPort(3232);
ArduinoOTA.setHostname("A0RET");
// No authentication by default
//ArduinoOTA.setPassword("admin");
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
}
}
else
{
if (WiFi.isConnected() || settings.wifiMode == 2)
{
if (wifiServer.hasClient())
{
for(i = 0; i < MAX_CLIENTS; i++)
{
if (!SysSettings.clientNodes[i] || !SysSettings.clientNodes[i].connected())
{
if (SysSettings.clientNodes[i]) SysSettings.clientNodes[i].stop();
SysSettings.clientNodes[i] = wifiServer.available();
if (!SysSettings.clientNodes[i]) Serial.println("Couldn't accept client connection!");
else
{
Serial.print("New client: ");
Serial.print(i); Serial.print(' ');
Serial.println(SysSettings.clientNodes[i].remoteIP());
}
}
}
if (i >= MAX_CLIENTS) {
//no free/disconnected spot so reject
wifiServer.available().stop();
}
}
//check clients for data
for(i = 0; i < MAX_CLIENTS; i++){
if (SysSettings.clientNodes[i] && SysSettings.clientNodes[i].connected())
{
if(SysSettings.clientNodes[i].available())
{
//get data from the telnet client and push it to input processing
while(SysSettings.clientNodes[i].available())
{
uint8_t inByt;
inByt = SysSettings.clientNodes[i].read();
SysSettings.isWifiActive = true;
//Serial.write(inByt); //echo to serial - just for debugging. Don't leave this on!
wifiGVRET.processIncomingByte(inByt);
}
}
}
else {
if (SysSettings.clientNodes[i]) {
SysSettings.clientNodes[i].stop();
}
}
}
}
else
{
if (settings.wifiMode == 1)
{
Serial.println("WiFi disconnected. Bummer!");
SysSettings.isWifiConnected = false;
SysSettings.isWifiActive = false;
}
}
}
}
if (SysSettings.isWifiConnected && ((micros() - lastBroadcast) > 1000000ul) ) //every second send out a broadcast ping
{
uint8_t buff[4] = {0x1C,0xEF,0xAC,0xED};
lastBroadcast = micros();
wifiUDPServer.beginPacket(broadcastAddr, 17222);
wifiUDPServer.write(buff, 4);
wifiUDPServer.endPacket();
}
ArduinoOTA.handle();
}
void WiFiManager::sendBufferedData()
{
for(int i = 0; i < MAX_CLIENTS; i++)
{
size_t wifiLength = wifiGVRET.numAvailableBytes();
uint8_t* buff = wifiGVRET.getBufferedBytes();
if (SysSettings.clientNodes[i] && SysSettings.clientNodes[i].connected())
{
SysSettings.clientNodes[i].write(buff, wifiLength);
}
}
wifiGVRET.clearBufferedBytes();
}