-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifi_functions.ino
312 lines (275 loc) · 8.1 KB
/
wifi_functions.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Use ESP8266 to repeatedly send GET command to server and parse
// response.
//
// Written by Joe Steinmeyer, Joel Voldman
// 2015, 2016
// Uses some helper functions inspired by ESP8266 library
// itead/ITEADLIB_Arduino_ESP8266
//Most functions in here are from L02A for 6.S08.
//Some have been removed (including loop/setup) or edited
#include <ESP8266.h>
#define wifiSerial Serial1
#define serialYes true
//WIFI global constants and variables
const int wifiControlPin = 2;
String wifis = ""; // holds list of wifisc
String MAC = ""; // MAC address
String get_response =""; // generic string to hold responses
bool wifi_good = false; // connected to AP
String SSID = "testnet"; // SSID and password
String password = "password";
void wifiEnable(bool yn){
digitalWrite(wifiControlPin,LOW);
if (yn){
delay(250);
digitalWrite(wifiControlPin,HIGH);
delay(1000);
}
}
void emptyRx() { // Empty ESP8266 buffer
while(wifiSerial.available() > 0) {
wifiSerial.read();
}
}
bool check() {
emptyRx();
wifiSerial.println("AT");
if (serialYes) Serial.println("checking..");
boolean ok = false;
if (wifiSerial.find("OK")) {
if (serialYes) Serial.println("ESP8266 present");
ok = true;
}
return ok;
}
void printWifiResponse(){
while (wifiSerial.available()>0){
char cByte = wifiSerial.read();
Serial.write(cByte);
}
}
void resetWifi() {
// set station mode
wifiSerial.println("AT+CWMODE=3");
delay(1000);//give some breathing room
wifiSerial.println("AT+RST"); //reset required to take effect
delay(2000);
if (wifiSerial.find("ready")){
if (serialYes) Serial.println("ESP8266 restarted and ready");
}
printWifiResponse();
}
bool connectWifi(String ssid, String password) {
emptyRx();
String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
wifiSerial.println(cmd);
unsigned long start = millis();
String response="";
while (millis() - start <9000){ //probably can rewrite this thing if needed.
if (wifiSerial.available()){
char c = wifiSerial.read();
if (serialYes) Serial.print(c);
response = String(response+c);
}
if (response.indexOf("OK") != -1){
break;
}
}
if(response.indexOf("OK") !=-1) {
wifiSerial.println("AT+CIFSR");
String resp2 = "";
start = millis();
while(millis()-start < 6000){
if(wifiSerial.available()){
char c = wifiSerial.read();
resp2 = String(resp2+c);
}
}
if (serialYes){
Serial.println("Device IP Info: ");
Serial.println(resp2);
Serial.println("Connected!");
return true;
}
}
else {
if (serialYes) Serial.println("Cannot connect to wifi");
return false;
}
}
String readString(String target1, String target2, String target3, uint32_t timeout)
{
String data;
char a;
unsigned long start = millis();
while (millis() - start < timeout) {
while(wifiSerial.available() > 0) {
a = wifiSerial.read();
if(a == '\0') continue;
data += a;
}
if (data.indexOf(target1) != -1) {
break;
} else if (data.indexOf(target2) != -1) {
break;
} else if (data.indexOf(target3) != -1) {
break;
}
}
return data;
}
// Set up TCP connection
bool startComm(String domain, int port) {
String data;
String start_comm = "AT+CIPSTART=\"TCP\",\"" + domain + "\"," + String(port);
wifiSerial.println(start_comm);
if (serialYes) Serial.println(start_comm);
data = readString("OK", "ERROR", "ALREADY CONNECT", 10000);
if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
return true;
}
return false;
}
bool setMux(int m) {
String data;
wifiSerial.print("AT+CIPMUX=");
wifiSerial.println(m);
data = readString("OK","ERROR","XX",4000);
if (data.indexOf("OK") != -1) {
return true;
}
return false;
}
// Read data from the wifi and test it for any of three target strings
bool readTest(String target1, String target2, String target3, uint32_t timeout) {
String data_tmp;
data_tmp = readString(target1, target2, target3, timeout);
if (data_tmp.indexOf(target1) != -1) {
return true;
} else if (data_tmp.indexOf(target2) != -1) {
return true;
} else if (data_tmp.indexOf(target3) != -1) {
return true;
} else {
return false;
}
}
bool sendComm(String buffer, int len) {
wifiSerial.print("AT+CIPSEND="); //send length command
wifiSerial.println(len);
if (readTest(">", "XX", "XX", 5000)) { //if we get '>', send rest
emptyRx();
for (uint32_t i = 0; i < len; i++) {
wifiSerial.write(buffer[i]);
}
return readTest("SEND OK", "XX", "XX", 10000);
}
return false;
}
// Read data from wifi and place into string
String receiveData(uint32_t timeout) {
String response;
unsigned long start = millis();
while (millis() - start <timeout){
if (wifiSerial.available()>0){
char c = wifiSerial.read();
Serial.print(c);
response=String(response+c);
}
}
return response;
}
String httpComm(String domain, int port, String path, String comm) {
String response;
emptyRx(); // empty buffer
if (setMux(0)) { // set mux
emptyRx();
if (startComm(domain, port)) { // set up tcp
emptyRx();
if (sendComm(comm, comm.length())) { //send command
response = receiveData(5000); //receive response
} else {
Serial.println("Send failed");
}
} else {
Serial.println("Unable to start connection");
}
} else {
Serial.println("MUX command failed");
}
wifiSerial.println("AT+CIPCLOSE"); //close tcp connection
return response;
}
String getMAC(uint32_t timeout) {
String response;
wifiSerial.println("AT+CIPSTAMAC?"); //send MAC query
unsigned long start = millis();
while (millis() - start <timeout){
if (wifiSerial.available()>0){
char c = wifiSerial.read();
Serial.print(c);
response=String(response+c);
}
}
int stringStart = response.indexOf(":") + 2;
return response.substring(stringStart,stringStart+17);
}
void initializeWifi() {
if (serialYes) Serial.begin(9600); //number meaningless on teensy (1MBit/s)
//wifi serial setup:
wifiSerial.begin(115200);
wifiSerial.setTimeout(4000);
pinMode(wifiControlPin,OUTPUT);
wifiEnable(true);
if (check()){ //if ESP8266 is present
Serial.println("wifi present");
resetWifi(); //reset
MAC = getMAC(1500);
emptyRx();
}
}
void listWifis(){
wifis="";
if (serialYes) Serial.println("Checking for Wifis!!!");
emptyRx();
wifiSerial.println("AT+CWLAP");
unsigned long start = millis();
while (millis() - start <5000){
if (wifiSerial.available()){
char c = wifiSerial.read();
wifis = String(wifis+c);
}
}
}
void requester(String PostData){ //sends POST Request to dev1 py file
{
if (wifi_good != true){ //if we're not connected to a network
if (check()){ //if ESP8266 present
listWifis();
if (serialYes) Serial.println(wifis);
bool good = connectWifi(SSID,password); //connect to network
if (good){
wifi_good = true; // connected to a network
}else{
wifi_good=false;
}
}
}
String returnData = "";
String steps = "";
if (wifi_good){ //if we're connected to a network
// web server parameters
String domain = "iesc-s2.mit.edu";
int port = 80;
String path = "student_code/canepa/dev1/sb1.py";
// send a GET command, will return weather info
String send_comm =
"POST "+ path + " HTTP/1.1\r\n" +
"Host: " + domain + "\r\n\r\n" +
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8" + "Content-Length:" +
(passcode.length()+12) + "\n" + PostData;
get_response = httpComm(domain, port, path, send_comm);
if (serialYes) Serial.println(get_response);
delay(200); // delay 0.2 secs
}
}}