forked from veproza/ESP8266-SD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp8266.ino
204 lines (193 loc) · 3.79 KB
/
esp8266.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
#include <SoftwareSerial.h>
SoftwareSerial dbgSerial(6, 7); // RX, TX
class ESP
{
public:
ESP(int rstPin);
void init(Stream *serial);
bool waitFor(String expected);
bool waitFor(String expected, int timeout);
bool startServer(int port);
bool sendData(char data[], unsigned int len);
bool closeConnection();
bool closeConnection(char connectionId);
void drain();
private:
void reset();
int _rstPin;
char currentConnectionId;
char ipdString[5];
uint8_t ipdPosition;
void checkIpd(char currentByte);
Stream *_serial;
};
ESP::ESP(int rstPin)
{
_rstPin = rstPin;
currentConnectionId = '0';
ipdString[0] = '+';
ipdString[1] = 'I';
ipdString[2] = 'P';
ipdString[3] = 'D';
ipdString[4] = ',';
}
void ESP::init(Stream *serial)
{
_serial = serial;
pinMode(_rstPin, OUTPUT);
reset();
}
void ESP::reset()
{
digitalWrite(_rstPin, LOW);
delay(100);
digitalWrite(_rstPin, HIGH);
waitFor("ready", 50000);
}
bool ESP::startServer(int port)
{
_serial->println("AT+CIPMUX=1");
if (false == waitFor("OK")) {
return false;
}
_serial->print("AT+CIPSERVER=1,");
_serial->println(port);
if (false == waitFor("OK")) {
return false;
}
drain();
return true;
}
bool ESP::sendData(char data[], unsigned int len)
{
_serial->print("AT+CIPSEND=");
_serial->print(currentConnectionId);
_serial->print(',');
_serial->println(len);
waitFor(">");
_serial->println(data);
waitFor("SEND OK");
}
bool ESP::closeConnection()
{
return closeConnection(currentConnectionId);
}
bool ESP::closeConnection(char connectionId)
{
dbgSerial.print("Cl: ");
dbgSerial.println(connectionId);
_serial->print("AT+CIPCLOSE=");
_serial->println(connectionId);
return true;
//return waitFor("Unlink");
}
void ESP::drain()
{
while(_serial->available()) {
_serial->read();
}
}
bool ESP::waitFor(String expected)
{
waitFor(expected, 100000);
}
bool ESP::waitFor(String expected, int timeout)
{
int t0 = millis();
int position = 0;
int length = expected.length();
char currentByte;
char expectedText[127];
expected.toCharArray(expectedText,length + 1);
do {
if(_serial->available()) {
currentByte = _serial->read();
checkIpd(currentByte);
if(currentByte == expectedText[position]) {
position++;
if(position == length) {
return true;
}
} else {
position = 0;
}
}
} while (millis() - t0 < timeout);
return false;
}
void ESP::checkIpd(char currentByte)
{
if(ipdPosition == 5) { // currentByte is now connection ID
if(currentByte != '0') {
closeConnection(currentByte);
}
ipdPosition = 0;
}
if(currentByte == ipdString[ipdPosition]) {
ipdPosition++;
} else {
ipdPosition = 0;
}
}
ESP esp(12); // DIO to RST
void setup()
{
dbgSerial.begin(115200);
pinMode(13, OUTPUT);
Serial.begin(115200);
dbgSerial.println("Init");
esp.init(&Serial);
if(esp.startServer(80)) {
dbgSerial.println("Ready.");
};
}
void loop()
{
while(Serial.available()) {
bool result = esp.waitFor("GET ", 200);
if(result) {
char address[64];
if(getRequestPath(address, 64))
{
dbgSerial.println(address);
handleAddress(address);
}
}
}
delay(500);
}
void handleAddress(char address[])
{
if(0 == strcmp(address, "/status")) {
dbgSerial.println("STATUS");
char data[] = "HTTP/1.0 200 OK\n\nprdel";
esp.sendData(data, sizeof(data));
esp.closeConnection();
} else {
dbgSerial.println("Unknown");
char data[] = "HTTP/1.0 404\n\n";
esp.sendData(data, sizeof(data));
esp.closeConnection();
}
}
bool getRequestPath(char data[], uint8_t len)
{
char current;
int position = 0;
unsigned long t0 = millis();
while(millis() - t0 < 200) {
while(Serial.available()) {
current = Serial.read();
if(current == ' ') {
data[position] = '\0';
return true;
}
if(position == len) {
return false;
}
data[position] = current;
position++;
}
}
return false;
}