-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESP8266Modem.cpp
More file actions
210 lines (180 loc) · 4.52 KB
/
ESP8266Modem.cpp
File metadata and controls
210 lines (180 loc) · 4.52 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
#include <stdint.h>
#include <WString.h>
#include "ESP8266Modem.h"
ESP8266Modem::ESP8266Modem(Stream *ptxSerial, Stream *prxSerial, Stream *pdebugSerial)
{
this->_txSerial = ptxSerial;
this->_rxSerial = prxSerial;
this->_debugSerial = pdebugSerial;
}
bool ESP8266Modem::isPresent(uint16_t timeout, uint8_t retries)
{
String response;
do{
tx_print(F("AT\r\n"));
uint32_t currentTime = millis();
while(millis() < (currentTime + timeout))
{
while(_rxSerial->available()) response += (char) _rxSerial->read();
if(response.indexOf(F("OK")) != -1)
{
debug_print(response);
return true;
}
}
debug_print(response);
response = "";
}while(retries-- > 1);
return false;
}
String ESP8266Modem::connectWifi(const char * ssid, const char * password, uint16_t timeout)
{
String cmd = "AT+CWJAP=\"" + String(ssid) + "\",\"" + String(password) + "\"\r\n";
debug_print(sendModemCommand("AT+CWMODE=3\r\n", 2500));
return sendModemCommand(cmd.c_str(), timeout);
}
String ESP8266Modem::disconnectWifi()
{
return sendModemCommand(F("AT+CWQAP\r\n"), 500);
}
String ESP8266Modem::showNetworkStatus()
{
return sendModemCommand(F("AT+CIFSR\r\n"), 1000);
}
String ESP8266Modem::swReset()
{
String rsp = sendModemCommand(F("AT+RST\r\n"), 1000);
delay(500);
return rsp;
}
String ESP8266Modem::connectToServer(String type, String server, uint16_t port)
{
String cmd = "AT+CIPSTART=\"" + type + "\",\"" + server + "\"," + String(port) + "\r\n";
return sendModemCommand(cmd, 10000);
}
String ESP8266Modem::httpGet(String data, uint16_t timeout_ms)
{
debug_print("Send request");
String response = sendModemCommand("AT+CIPSEND=" + String(data.length()) + "\r\n", 3000);
if(response.indexOf("ERROR") != -1) return response;
bool success = false;
uint32_t currentTime = millis();
while(millis() < (currentTime + 1000))
{
while(_rxSerial->available()) response += (char) _rxSerial->read();
/* Quebra o loop se o OK for encontrado */
if(response.indexOf('>') != -1)
{
debug_print("Sending authorized.");
success = true;
break;
}
}
if(success) tx_print(data);
else
{
debug_print("Authorization timed out");
return response;
}
success = false;
currentTime = millis();
debug_print("Waiting for +IPD");
uint16_t rcvdBytes = 0;
uint16_t IPDSize = 0;
while(millis() < (currentTime + timeout_ms))
{
while(_rxSerial->available()) response += (char) _rxSerial->read();
if(response.indexOf("+IPD,") != -1 && response.indexOf(":") != -1)
{
debug_print("Got IPD. Reading...");
String sizeString = response.substring(response.indexOf("+IPD,") + 5, response.indexOf(":"));
IPDSize = sizeString.toInt();
debug_print("Total response size: str(" + sizeString + ") | int(" + String(IPDSize) + ")");
while(rcvdBytes < IPDSize)
{
if(_rxSerial->available() > 0)
{
response += String((char) _rxSerial->read());
rcvdBytes++;
}
if(millis() > (currentTime + timeout_ms))
{
debug_print("Receiving timed out...");
break;
}
}
success = true;
break;
}
}
if(!success)
{
debug_print("Error, timed out without response");
response += "\nTimed out.";
}
else
{
debug_print("Received " + String(rcvdBytes) + " from " + String(IPDSize));
}
debug_print("Done.\n");
return response;
}
template <typename Output>
void ESP8266Modem::debug_print(Output text)
{
if(_debugSerial != NULL && _isDebugEnabled)
{
_debugSerial->print(F(" * ESPModemDEBUG: "));
_debugSerial->println(text);
}
}
template <typename Output>
void ESP8266Modem::tx_print(Output text)
{
if(_txSerial != NULL)
{
_txSerial->print(text);
}
}
String ESP8266Modem::sendModemCommand(String cmd, uint16_t timeout)
{
String response = "";
debug_print("Sending: \n" + String(cmd));
tx_print(cmd.c_str());
bool success = false;
uint32_t currentTime = millis();
while(millis() < (currentTime + timeout))
{
while(_rxSerial->available()) response += (char) _rxSerial->read();
/* Quebra o loop se o OK for encontrado */
if(response.indexOf(F("OK")) != -1)
{
success = true;
break;
}
}
if(success)debug_print("Success! RSP: \n_\n" + response + "\n_");
else debug_print("Failed :\n_\n " + response + "\n_");
return response;
}
void ESP8266Modem::setHwResetPin(uint8_t pin)
{
_rstPin = pin;
digitalWrite(_rstPin, HIGH);
pinMode(_rstPin, OUTPUT);
}
void ESP8266Modem::hwReset()
{
digitalWrite(_rstPin, LOW);
delay(10);
digitalWrite(_rstPin, HIGH);
delay(500);
}
void ESP8266Modem::enableDebug()
{
_isDebugEnabled = true;
}
void ESP8266Modem::disableDebug()
{
_isDebugEnabled = false;
}