-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESP8266Client.cpp
455 lines (416 loc) · 10.2 KB
/
ESP8266Client.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
extern "C" {
#include "string.h"
}
#include "Arduino.h"
#include "ESP8266Client.h"
#define AT "AT"
#define AT_DISCONNECT "AT+CIPCLOSE"
#define AT_RESET "AT+RST"
#define AT_WIFI_MODE "AT+CWMODE"
#define AT_JOIN_AP "AT+CWJAP"
#define AT_LIST_AP "AT+CWLAP"
#define AT_STATUS "AT+CIPSTATUS"
#define AT_TCPSTART "AT+CIPSTART"
#define AT_CIPSEND "AT+CIPSEND"
#define AT_GET_IP "AT+CIFSR"
#define MAX_SOCK_NUM 1
uint16_t ESP8266Client::_srcport = 1024;
//AT+CIPSTART="TCP","192.168.10.117",10000
//AT+CIPSTART="TCP","192.168.10.117",10000
char buffer[50];
SoftwareSerialLocal *serialPort=0;
//#define DEBUG_SERIAL
/*
* Using a modified SoftwareSerial library to read directly what is on the Serial Buffer
* Official SoftwareSerial has receive buffer private, modified to use it public
*/
ESP8266Client::ESP8266Client() : _sock(MAX_SOCK_NUM) {
}
ESP8266Client::ESP8266Client(uint8_t sock) : _sock(sock) {
}
int ESP8266Client::connect(const char* host, uint16_t port) {
// Look up the host first
// No DNS implementation as of now
#ifdef DEBUG_SERIAL
Serial.println("::connect(DNS)");
#endif
return 1;
}
void ESP8266Client::setSerial( SoftwareSerialLocal *port ){
serialPort = port;
}
int ESP8266Client::connect(IPAddress ip, uint16_t port) {
#ifdef DEBUG_SERIAL
Serial.println("::connect(uint8_t)");
#endif
//Close anny connection
serialPort->println("AT+CIPCLOSE");
delay(20);
uint8_t* rawAdress = rawIPAddress(ip);
sprintf( buffer, "AT+CIPSTART=\"TCP\",\"%d.%d.%d.%d\",%d", rawAdress[0], rawAdress[1], rawAdress[2], rawAdress[3], port);
#ifdef DEBUG_SERIAL
Serial.println(buffer);
#endif
//serialPort->println(buffer);
//serialPort->setTimeout(5000);
if( sendWaitRespond(buffer,"OK",5000) ){
#ifdef DEBUG_SERIAL
Serial.println("true");
#endif
return 1;
} else {
#ifdef DEBUG_SERIAL
Serial.println("false");
#endif
return 0;
}
}
size_t ESP8266Client::write(uint8_t b) {
#ifdef DEBUG_SERIAL
Serial.println("::write(uint8_t)");
#endif
while(!sendWaitRespond("AT","OK",10000) ){
}
#ifdef DEBUG_SERIAL
Serial.println("ready to write byte");
#endif
//serialPort->print("AT+CIPSEND=1");
//if (serialPort->find(">"))
if(sendWaitRespond("AT+CIPSEND=1",">",10000) )
{
#ifdef DEBUG_SERIAL
Serial.println("byte sent");
#endif
//OK
serialPort->write(b);
serialPort->println();
return sizeof(uint8_t);
}else
{
serialPort->println("AT+CIPCLOSE");
return 0;
}
}
size_t ESP8266Client::write(const uint8_t *buf, size_t size) {
#ifdef DEBUG_SERIAL
Serial.println("::write()");
#endif
while(!sendWaitRespond("AT","OK",10000) ){
}
#ifdef DEBUG_SERIAL
Serial.println("ready to write bytes");
#endif
//serialPort->write(buf,(size/sizeof(uint8_t)));
sprintf( buffer, "AT+CIPSEND=%d", size/sizeof(uint8_t) );
if( sendWaitRespond(buffer,">",5000) ) {
#ifdef DEBUG_SERIAL
Serial.println("writing");
#endif
//OK
serialPort->write(buf,(sizeof(uint8_t) * size));
serialPort->print("\n");
serialPort->flush();
return size;
} else {
serialPort->println("AT+CIPCLOSE");
return 0;
}
}
/*
* Received data starts with +IPD
* Returned data must be stripped from +IPD
* After that comes the amount of byte received
* On the next line then is the actual data
*/
//Skip chars
//Sample +IPD,0,4:df
char skip[] ={ '+','I','P','D' };
char inBuffer[255];
int buffPos=0;
int buffSize=0;
int ESP8266Client::read( ) {
if( buffPos < buffSize ){
//Serial.print("From Buffeer ");
char retValue = inBuffer[buffPos];
//Serial.print(retValue);
buffPos++;
return retValue;
}
//Serial.println("::read");
buffPos = 0;
buffSize = 0;
if( !serialPort->available() ){
return -1;
}
//memset(inBuffer, '\n', sizeof(inBuffer));
//Skip line feeds
//Serial.println("to peek");
/*while( serialPort->peek() == 10
|| serialPort->peek() == 13){
serialPort->read();
}*/
//Skip +IPDXX:
//Serial.println("skiping");
/*if( serialPort->peek() == skip[0] ){
if( serialPort->read() == skip[0] &&
serialPort->read() == skip[1] &&
serialPort->read() == skip[2] &&
serialPort->read() == skip[3] ){*/
if( serialPort->find("+IPD,") ){
buffSize=0;
#ifdef DEBUG_SERIAL
Serial.println("Found IPD");
#endif
char tmp;
while( serialPort->peek() != ':' ){
tmp = serialPort->read();
if( tmp <48 || tmp > 57){ continue; }
buffSize = buffSize * 10;
#ifdef DEBUG_SERIAL
Serial.print(tmp);
#endif
buffSize += (tmp - 48);
}
if( buffSize< 0){
#ifdef DEBUG_SERIAL
//This means that the parsing /reading of port is incrrectly done
Serial.println("Error on Message Size");
#endif
return -1;
}
#ifdef DEBUG_SERIAL
Serial.print("Message Size = ");
Serial.println(buffSize);
#endif
//Move till the :
serialPort->find(":");
buffPos=0;
while( buffPos < buffSize ){
inBuffer[buffPos] = char(serialPort->read());
#ifdef DEBUG_SERIAL
Serial.print(char(inBuffer[buffPos]));
#endif
buffPos++;
//wait for next character...
while( buffPos < buffSize && !serialPort->available() ){
#ifdef DEBUG_SERIAL
Serial.println("reading...");
#endif
delay(10);
}
}
buffPos = 0;
#ifdef DEBUG_SERIAL
if( buffSize > 0 ){
Serial.println("data in");
}
#endif
/*while( serialPort->peek() != 10 &&
serialPort->peek() != 13 ){
inBuffer[buffSize] = char(serialPort->read());
Serial.print(char(inBuffer[buffSize]));
buffSize++;
}
Serial.print("Buffer Size = ");
Serial.println(buffSize);*/
//inBuffer[buffSize] = 13;
//buffSize++;
/*char tmp;
while( serialPort->peek() != ':' ){
buffSize * 10;
tmp = serialPort->read();
Serial.print(tmp);
buffSize += (tmp - 48);
//buffSize += (char(serialPort->read()) - 48);
#ifdef DEBUG_SERIAL
Serial.println(".");
#endif
Serial.print(buffSize);
}
//Skip next char
Serial.print(char(serialPort->read()));
Serial.println("Message Size");
Serial.println(buffSize);
bool reading = true;
buffPos=0;
while(buffPos < buffSize ){
inBuffer[buffPos] = serialPort->read();
Serial.print(char(inBuffer[buffPos]));
buffPos++;
}*/
//inBuffer[buffPos] = 0;
buffPos = 0;
flush();
//forget about anything else
//flush();
//return read();
buffPos++;
return inBuffer[0];
}
#ifdef DEBUG_SERIAL
else{
Serial.println("no IPD");
}
Serial.println("ret -1");
#endif
return -1;
}
int ESP8266Client::available() {
if( buffSize > 0 ) return true;
if( read() < 0 ) return false;
#ifdef DEBUG_SERIAL
Serial.println( "avilable Read");
#endif
if( buffPos> 0 ){
#ifdef DEBUG_SERIAL
Serial.println( "buffPos >0");
#endif
buffPos--;
return true;
}
//return serialPort->available();
return false;
}
int ESP8266Client::read(uint8_t *buf, size_t size) {
#ifdef DEBUG_SERIAL
Serial.println("NOT IMPLEMENTED ::read(uint8)");
#endif
return serialPort->read();
}
int ESP8266Client::peek() {
#ifdef DEBUG_SERIAL
Serial.println("::peek()");
#endif
//serialPort->println("::peek");
/* uint8_t b;
// Unlike recv, peek doesn't check to see if there's any data available, so we must
if (!available())
return -1;
::peek(_sock, &b);
return b;*/
return serialPort->peek();
}
void ESP8266Client::flush() {
#ifdef DEBUG_SERIAL
Serial.println("::flush()");
#endif
serialPort->flush();
}
void ESP8266Client::stop() {
#ifdef DEBUG_SERIAL
Serial.println("::stop()");
#endif
//sendWaitRespond( "AT+CIPCLOSE", "OK", 1500);
}
uint8_t ESP8266Client::connected() {
#ifdef DEBUG_SERIAL
Serial.println("::connected()");
#endif
//If Unlink is there then this has been disconnected, no need to continue
//Yes if you send the word Unlink in your package it might get all messed up
//return sendWaitRespond("AT+CIPSTATUS", "STATUS:3", 500 );
read();
if( buffSize> 0 && buffPos > 0 ){
#ifdef DEBUG_SERIAL
Serial.println("Something found");
#endif
buffPos--;
}
//check AT first
//sendWaitRespond("AT","OK", 500 );
bool connected = sendWaitRespond("AT+CIPSTATUS", "TCP", 1500 );
/*if( strstr(serialPort->_receive_buffer, "Unlink")){
serialPort->flush();
connected = false;
Serial.println("Not Connected");
}*/
return connected;
//return sendWaitRespond("AT+CIPSTATUS", "TCP", 1500 );
}
uint8_t ESP8266Client::status() {
#ifdef DEBUG_SERIAL
Serial.println("::status()");
#endif
return sendWaitRespond("AT+CIPSTATUS", "STATUS:5", 2500 );
}
// the next function allows us to use the client returned by
// EthernetServer::available() as the condition in an if-statement.
ESP8266Client::operator bool() {
return 1;
}
bool ESP8266Client::operator==(const ESP8266Client& rhs) {
return _sock == rhs._sock && _sock != MAX_SOCK_NUM && rhs._sock != MAX_SOCK_NUM;
}
bool ESP8266Client::connectAP(char *ssid, char *password) {
char buffer[50];
if( !sendWaitRespond("AT", "OK", 300 ) ){
!sendWaitRespond("AT+RST", "wwww.", 500 );
if( !sendWaitRespond("AT", "OK", 300 ) ){
#ifdef DEBUG_SERIAL
Serial.println("AT FAIL");
#endif
return false;
}
}
#ifdef DEBUG_SERIAL
Serial.println("AT OK");
#endif
serialPort->println("AT+CWQAP");
delay(500);
serialPort->println("AT+CWMODE=1");
delay(500);
sprintf( buffer, "AT+CWJAP=\"%s\",\"%s\"", ssid, password);
return sendWaitRespond(buffer, "OK", 10000 );
/*serialPort->println( buffer );
delay(2000);
if(serialPort->find("OK")) {
return true;
} else {
return false;
}*/
}
bool tryATOK() {
serialPort->setTimeout(1500);
serialPort->println("AT");
if( serialPort->find("OK") ){
return true;
}
return false;
}
String recBuffer;
bool ESP8266Client::sendWaitRespond( char *message, char *response, int msTimeout) {
bool bussy = false;
serialPort->flush();
do{
bussy = false;
serialPort->println(message);
serialPort->setTimeout(msTimeout);
#ifdef DEBUG_SERIAL
Serial.println(message);
#endif
if( serialPort->find(response) ){
#ifdef DEBUG_SERIAL
Serial.println("ret true");
#endif
serialPort->flush();
return true;
}
if( strstr(serialPort->_receive_buffer, "busy")){
bussy =true;
#ifdef DEBUG_SERIAL
Serial.println("bussy");
Serial.println(serialPort->_receive_buffer);
#endif
serialPort->flush();
while(!tryATOK()){
//Serial.println("trying AT");
}
}
delay(50);
}while(bussy);
#ifdef DEBUG_SERIAL
Serial.println(serialPort->_receive_buffer);
//Serial.println("ret false");
#endif
return false;
}