-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathESP8266.h
More file actions
218 lines (178 loc) · 4.7 KB
/
Copy pathESP8266.h
File metadata and controls
218 lines (178 loc) · 4.7 KB
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
#ifndef ESP8266_H
#define ESP8266_H
#include "mbed.h"
/**
* An interface to the ESP8266
*/
class ESP8266 {
public:
/**
* Create an interface to the ESP8266
*
* @param tx UART TX line pin
* @param rx UART RX line pin
*/
ESP8266(PinName tx, PinName rx) : _serial(tx, rx)
{
_serial.baud(115200);
_connected = false;
}
bool connect(const char* ssid, const char* pass, uint8_t mode = 0)
{
// Set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
strcpy(cmdbuff, "AT+CWMODE=1\r\n");
sendCMD();
getReply(500, 20);
// DHCP Enabled in Station Mode
strcpy(cmdbuff, "AT+CWDHCP=1,1\r\n");
sendCMD();
getReply(500, 20);
sprintf(cmdbuff,"AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pass);
sendCMD();
getReply(10000, 200);
if (strstr(replybuff, "OK") == NULL) return false;
sprintf(cmdbuff,"AT+CIPMUX=%d\r\n", mode);
sendCMD();
getReply(500, 20);
_connected = true;
return true;
}
char* httpGet(const char* host, const char* url, int port = 80)
{
connectToServer(host, port);
sprintf(webbuff,
"GET %s HTTP/1.1\r\nHost: %s\r\nAccept: */*\r\nConnection: close\r\n\r\n",
url, host);
sendWebBuff();
return replybuff;
}
// Send data to the ThingSpeak
bool sendThingSpeak(char* data, const char* apikey)
{
connectToServer("184.106.153.149", 80);
sprintf(webbuff, "GET /update?key=%s&%s\r\n", apikey, data);
sprintf(cmdbuff,"AT+CIPSEND=%d\r\n", strlen(webbuff)); // send buffer character length.
sendCMD();
getReply(200, 50);
SendWEB(); // send data
memset(webbuff, '\0', sizeof(webbuff));
volatile int weberror = 1;
t2.reset();
t2.start();
while(weberror == 1 && t2.read() < 5)
{
getReply(500, 24);
if (strstr(replybuff, "OK") != NULL)
{
weberror=0; // wait for valid SEND OK
}
}
t2.stop();
if(weberror == 1)
{ // error
return false;
}
else
{ // success
return true;
}
}
bool isConnected(void)
{
return _connected;
}
private:
Serial _serial;
volatile bool _connected;
Timer t1;
Timer t2;
char cmdbuff[64];
char webbuff[1024];
char replybuff[1024];
// Connect to host via TCP
void connectToServer(const char* host, int port)
{
sprintf(cmdbuff,"AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", host, port);
sendCMD();
getReply(2000, 50);
}
bool sendWebBuff(void)
{
sprintf(cmdbuff,"AT+CIPSEND=%d\r\n", strlen(webbuff)); // send buffer character length.
sendCMD();
getReply(200, 50);
SendWEB(); // send data
memset(webbuff, '\0', sizeof(webbuff));
return sendCheck();
}
// Wait for ESP "SEND OK" reply
bool sendCheck()
{
volatile int weberror = 1;
t2.reset();
t2.start();
while(weberror == 1 && t2.read() < 5)
{
getReply(500, 24);
if (strstr(replybuff, "SEND OK") != NULL)
{
weberror=0; // wait for valid SEND OK
}
}
t2.stop();
if(weberror == 1)
{ // error
strcpy(cmdbuff, "AT+CIPMUX=0\r\n");
sendCMD();
getReply(500, 10);
sprintf(cmdbuff, "AT+CIPCLOSE\r\n");
sendCMD();
getReply(500, 600);
return false;
}
else
{ // close current connection
sprintf(cmdbuff, "AT+CIPCLOSE\r\n");
sendCMD();
getReply(500, 600);
return true;
}
}
// ESP Command data send
void sendCMD()
{
_serial.printf("%s", cmdbuff);
}
// Large WEB buffer data send
int SendWEB()
{
volatile int i = 0;
if(_serial.writeable())
{
while(webbuff[i] != '\0')
{
_serial.putc(webbuff[i]);
i++;
}
}
return i;
}
// Get Command and ESP status replies
void getReply(int timeout, int getcount)
{
volatile int replycount = 0;
memset(replybuff, '\0', sizeof(replybuff));
t1.reset();
t1.start();
while(t1.read_ms() < timeout && replycount < getcount)
{
if(_serial.readable())
{
replybuff[replycount] = _serial.getc();
replycount++;
}
}
t1.stop();
}
};
#endif