forked from fustyles/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP01_UartWifi_station.ino
268 lines (235 loc) · 7.15 KB
/
ESP01_UartWifi_station.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
/*
ESP-01 + Arduino Uno (without using AT Command)
Author : ChungYi Fu (Kaohsiung, Taiwan) 2018-10-13 15:00
https://www.facebook.com/francefu
Uart Command Format :
?cmd=str1;str2;str3;str4;str5;str6;str7;str8;str9
?restart
?resetwifi=ssid;password
?tcp=domain;port;request;wait
?ifttt=event;key;value1;value2;value3
?thingspeakupdate=key;field1;field2;field3;field4;field5;field6;field7;field8
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 2); // ESP01 TX(gpio2)->D10, RX(gpio0)->D11
#include <ESP8266WiFi.h>
// Enter your WiFi ssid and password
const char* ssid = "xxxxx"; //your network SSID
const char* password = "xxxxx"; //your network password
String Feedback="", Command="",cmd="",str1="",str2="",str3="",str4="",str5="",str6="",str7="",str8="",str9="";
byte ReceiveState=0,cmdState=1,strState=1,questionstate=0,equalstate=0,semicolonstate=0;
boolean debug = false;
void ExecuteCommand()
{
Serial.println("");
//Serial.println("Command: "+Command);
Serial.println("cmd= "+cmd+" ,str1= "+str1+" ,str2= "+str2+" ,str3= "+str3+" ,str4= "+str4+" ,str5= "+str5+" ,str6= "+str6+" ,str7= "+str7+" ,str8= "+str8+" ,str9= "+str9);
Serial.println("");
if (cmd=="restart")
{
setup();
}
else if (cmd=="resetwifi")
{
WiFi.begin(str1.c_str(), str2.c_str());
long int StartTime=millis();
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
if ((StartTime+5000) < millis()) break;
}
Feedback="STAIP: "+WiFi.localIP().toString();
}
else if (cmd=="tcp")
{
String domain=str1;
int port=str2.toInt();
String request=str3;
int wait=str4.toInt(); // wait = 0 or 1
Feedback=tcp(domain,request,port,wait);
}
else if (cmd=="ifttt")
{
String domain="maker.ifttt.com";
String request = "/trigger/" + str1 + "/with/key/" + str2;
request += "?value1="+str3+"&value2="+str4+"&value3="+str5;
Feedback=tcp(domain,request,80,0);
}
else if (cmd=="thingspeakupdate")
{
String domain="api.thingspeak.com";
String request = "/update?api_key=" + str1;
request += "&field1="+str2+"&field2="+str3+"&field3="+str4+"&field4="+str5+"&field5="+str6+"&field6="+str7+"&field7="+str8+"&field8="+str9;
Feedback=tcp(domain,request,80,0);
}
else if (cmd=="thingspeakread")
{
String domain="api.thingspeak.com";
String request = str1;
Feedback=tcp(domain,request,80,1);
}
else
{
Feedback="Command is not defined";
}
if (debug==true)
{
Serial.println(Feedback);
mySerial.println(Feedback); // Send Feedback to Arduino Uno
mySerial.flush();
delay(10);
}
}
void setup()
{
mySerial.begin(9600);
mySerial.setTimeout(10);
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_STA);
//WiFi.config(IPAddress(192, 168, 201, 100), IPAddress(192, 168, 201, 2), IPAddress(255, 255, 255, 0));
WiFi.begin(ssid, password);
delay(1000);
Serial.println("");
Serial.print("Connecting to ");
Serial.println(ssid);
long int StartTime=millis();
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
if ((StartTime+5000) < millis()) break;
}
if (WiFi.status() == WL_CONNECTED)
{
pinMode(2, OUTPUT);
for (int i=0;i<5;i++)
{
digitalWrite(2,LOW);
delay(100);
digitalWrite(2,HIGH);
delay(100);
}
}
Serial.println("");
Serial.println("STAIP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
Command="";cmd="";str1="";str2="";str3="";str4="";str5="";str6="";str7="";str8="";str9="";
ReceiveState=0,cmdState=1,strState=1,questionstate=0,equalstate=0,semicolonstate=0;
if (Serial.available())
{
while (Serial.available())
{
char c=Serial.read();
Serial.print(String(c));
getCommand(c);
delay(1);
}
if (cmd!="") ExecuteCommand();
}
if (mySerial.available())
{
while (mySerial.available())
{
char c=mySerial.read();
getCommand(c);
delay(1);
}
if (cmd!="") ExecuteCommand();
}
}
void getCommand(char c)
{
if (c=='?') ReceiveState=1;
if ((c==' ')||(c=='\r')||(c=='\n')) ReceiveState=0;
if (ReceiveState==1)
{
Command=Command+String(c);
if (c=='=') cmdState=0;
if (c==';') strState++;
if ((cmdState==1)&&((c!='?')||(questionstate==1))) cmd=cmd+String(c);
if ((cmdState==0)&&(strState==1)&&((c!='=')||(equalstate==1))) str1=str1+String(c);
if ((cmdState==0)&&(strState==2)&&(c!=';')) str2=str2+String(c);
if ((cmdState==0)&&(strState==3)&&(c!=';')) str3=str3+String(c);
if ((cmdState==0)&&(strState==4)&&(c!=';')) str4=str4+String(c);
if ((cmdState==0)&&(strState==5)&&(c!=';')) str5=str5+String(c);
if ((cmdState==0)&&(strState==6)&&(c!=';')) str6=str6+String(c);
if ((cmdState==0)&&(strState==7)&&(c!=';')) str7=str7+String(c);
if ((cmdState==0)&&(strState==8)&&(c!=';')) str8=str8+String(c);
if ((cmdState==0)&&(strState>=9)&&((c!=';')||(semicolonstate==1))) str9=str9+String(c);
if (c=='?') questionstate=1;
if (c=='=') equalstate=1;
if ((strState>=9)&&(c==';')) semicolonstate=1;
}
}
String tcp(String domain,String request,int port,byte wait)
{
WiFiClient client_tcp;
if (client_tcp.connect(domain, port))
{
Serial.println("GET " + request);
client_tcp.println("GET " + request + " HTTP/1.1");
client_tcp.println("Host: " + domain);
client_tcp.println("Connection: close");
client_tcp.println();
String getResponse="",Feedback="";
boolean state = false;
int waitTime = 3000; // timeout 3 seconds
long startTime = millis();
while ((startTime + waitTime) > millis())
{
while (client_tcp.available())
{
char c = client_tcp.read();
if (c == '\n')
{
if (getResponse.length()==0) state=true;
getResponse = "";
}
else if (c != '\r')
getResponse += String(c);
if (state==true) Feedback += String(c);
if (wait==1)
startTime = millis();
}
if (wait==0)
if ((state==true)&&(Feedback.length()!= 0)) break;
}
client_tcp.stop();
return Feedback;
}
else
return "Connection failed";
}
/*
Arduino Uno
Uart Command Format:
?cmd=str1;str2;str3;str4;str5;str6;str7;str8;str9
?restart
?resetwifi=ssid;password
?tcp=domain;port;request;wait
?ifttt=event;key;value1;value2;value3
?thingspeakupdate=key;field1;field2;field3;field4;field5;field6;field7;field8
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // ESP01 TX(gpio2)->D10, RX(gpio0)->D11
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
while (mySerial.available())
{
char c=mySerial.read();
Serial.write(c);
}
while (Serial.available())
{
char c=Serial.read();
mySerial.print(c);
}
}
*/