-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi-serial.ino
268 lines (240 loc) · 5.37 KB
/
wifi-serial.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (c) 2017, Luc Yriarte
* License: BSD <http://www.opensource.org/licenses/bsd-license.php>
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
/* **** **** **** **** **** ****
* Constants
* **** **** **** **** **** ****/
#define BPS_HOST 9600
#define COMMS_BUFFER_SIZE 24576
#define SERIAL_PEER_DELAY_MS 15000
#define SERIAL_DUMP_DELAY_MS 5000
#define serverPORT 80
#define WIFI_CLIENT_DELAY_MS 500
#define WIFI_CONNECT_DELAY_MS 3000
#define WIFI_CONNECT_RETRY 5
#define LED 2
enum {
METHOD,
URI,
IGNORE
};
/* **** **** **** **** **** ****
* Global variables
* **** **** **** **** **** ****/
/*
* WiFi
*/
WiFiServer wifiServer(serverPORT);
WiFiClient wifiClient;
typedef struct {
char * SSID;
char * passwd;
IPAddress address;
IPAddress gateway;
IPAddress netmask;
} wifiNetInfo;
wifiNetInfo networks[] = {
{
"network1",
"password",
IPAddress(192,168,0,2),
IPAddress(192,168,0,1),
IPAddress(255,255,255,0)
},
{
"network2",
"password",
IPAddress(0,0,0,0),
IPAddress(0,0,0,0),
IPAddress(0,0,0,0)
}
};
int wifiStatus = WL_IDLE_STATUS;
char hostnameSSID[] = "ESP_XXXXXX";
char wifiMacStr[] = "00:00:00:00:00:00";
byte wifiMacBuf[6];
/*
* serial comms buffer
*/
char commsBuffer[COMMS_BUFFER_SIZE];
/* **** **** **** **** **** ****
* Functions
* **** **** **** **** **** ****/
void setup() {
#ifdef LED
pinMode(LED, OUTPUT);
#endif
Serial.begin(BPS_HOST);
wifiMacInit();
Serial.print("WiFi.macAddress: ");
Serial.println(wifiMacStr);
}
/*
* WiFi
*/
void wifiMacInit() {
WiFi.macAddress(wifiMacBuf);
int i,j,k;
j=0; k=4;
for (i=0; i<6; i++) {
wifiMacStr[j] = '0' + (wifiMacBuf[i] >> 4);
if (wifiMacStr[j] > '9')
wifiMacStr[j] += 7;
if (i>2)
hostnameSSID[k++] = wifiMacStr[j];
++j;
wifiMacStr[j] = '0' + (wifiMacBuf[i] & 0x0f);
if (wifiMacStr[j] > '9')
wifiMacStr[j] += 7;
if (i>2)
hostnameSSID[k++] = wifiMacStr[j];
j+=2;
}
}
bool wifiConnect(int retry) {
int i,n;
wifiStatus = WiFi.status();
if (wifiStatus == WL_CONNECTED)
return true;
#ifdef LED
digitalWrite(LED, LOW);
#endif
n = sizeof(networks) / sizeof(wifiNetInfo);
for (i=0; i<n; i++) {
if (wifiNetConnect(&networks[i], retry))
return true;
}
WiFi.disconnect();
wifiStatus = WiFi.status();
return false;
}
bool wifiNetConnect(wifiNetInfo *net, int retry) {
Serial.print("Connecting to: ");
Serial.println(net->SSID);
WiFi.config(net->address, net->gateway, net->netmask);
wifiStatus = WiFi.begin(net->SSID, net->passwd);
Serial.print("trying..");
while (wifiStatus != WL_CONNECTED && retry > 0) {
retry--;
Serial.print(".");
#ifdef LED
digitalWrite(LED, HIGH);
#endif
delay(WIFI_CONNECT_DELAY_MS);
#ifdef LED
digitalWrite(LED, LOW);
#endif
wifiStatus = WiFi.status();
}
Serial.println();
if (wifiStatus == WL_CONNECTED) {
#ifdef LED
digitalWrite(LED, HIGH);
#endif
Serial.print("WiFi client IP Address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin(hostnameSSID)) {
Serial.print("Registered mDNS hostname: ");
Serial.println(hostnameSSID);
}
}
return wifiStatus == WL_CONNECTED;
}
/*
* serial peer input
*/
int serialInput() {
unsigned long timeLoopStart,timeLoop;
int nread = 0;
timeLoopStart = timeLoop = millis();
// Serial timeout triggers on full commsBuffer, just wait for first command
while (nread == 0 && timeLoop - timeLoopStart < SERIAL_PEER_DELAY_MS) {
// drop older contents on buffer overflow
while ((nread = Serial.readBytes(commsBuffer, COMMS_BUFFER_SIZE)) == COMMS_BUFFER_SIZE);
timeLoop = millis();
}
commsBuffer[nread] = 0;
return nread;
}
void serialDump() {
int nread = 0;
while (nread = Serial.readBytes(commsBuffer, COMMS_BUFFER_SIZE-1)) {
commsBuffer[nread] = 0;
wifiClient.print(commsBuffer);
// wait for more content
delay(SERIAL_DUMP_DELAY_MS);
}
}
void serialCmd(char *cmd) {
// consume older contents
while (Serial.available()) {
Serial.read();
}
// send command
Serial.print(cmd);
}
void loop() {
int i, j, nread, readstate;
while (!wifiConnect(WIFI_CONNECT_RETRY))
delay(1000);
wifiServer.begin();
delay(200);
wifiClient = wifiServer.available();
if (wifiClient && wifiClient.connected()) {
delay(100);
i = j = 0;
readstate = METHOD;
commsBuffer[0] = '\0';
while (wifiClient.available()) {
char c = wifiClient.read();
switch (readstate) {
case METHOD:
if (c == '/')
readstate = URI;
else if (c == '\n')
readstate = IGNORE;
break;
case URI:
if (c == '\n' || c == ' ') {
commsBuffer[j++] = '\0';
readstate = IGNORE;
break;
}
i=1;
commsBuffer[j++] = c;
break;
default:
break;
}
}
// If a command was passed on URI send it
if (i)
serialCmd(commsBuffer);
wifiClient.println("HTTP/1.1 200 OK");
wifiClient.println("Content-Type: text/plain");
wifiClient.println("Access-Control-Allow-Origin: *");
wifiClient.println("Connection: close");
wifiClient.println();
// If a command was passed on URI print the result
if (i) {
while (true) {
nread = serialInput();
wifiClient.println(commsBuffer);
// continue printing the results until the next prompt
if (nread > 2 && commsBuffer[nread-1] == ' ' && commsBuffer[nread-2] == ':') {
break;
}
}
}
// otherwise dump serial buffer contents
else {
serialDump();
}
wifiClient.println();
delay(WIFI_CLIENT_DELAY_MS);
wifiClient.stop();
}
}