-
Notifications
You must be signed in to change notification settings - Fork 289
/
ESP01_MyBlockly_JSON.ino
442 lines (399 loc) · 13.3 KB
/
ESP01_MyBlockly_JSON.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
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
/*
ESP01 MyBlockly
Author : ChungYi Fu (Kaohsiung, Taiwan) 2019-1-28 21:30
https://www.facebook.com/francefu
Command Format :
http://APIP/?cmd=P1;P2;P3;P4;P5;P6;P7;P8;P9
http://STAIP/?cmd=P1;P2;P3;P4;P5;P6;P7;P8;P9
Default APIP: 192.168.4.1
http://192.168.4.1/?ip
http://192.168.4.1/?mac
http://192.168.4.1/?restart
http://192.168.4.1/?resetwifi=ssid;password
http://192.168.4.1/?inputpullup=pin
http://192.168.4.1/?pinmode=pin;value
http://192.168.4.1/?digitalwrite=pin;value
http://192.168.4.1/?analogwrite=pin;value
http://192.168.4.1/?digitalread=pin
http://192.168.4.1/?analogread=pin
http://192.168.4.1/?tcp=domain;port;request;wait
--> wait = 0 or 1 (waiting for response)
--> request = /xxxx/xxxx
http://192.168.4.1/?ifttt=event;key;value1;value2;value3
http://192.168.4.1/?thingspeakupdate=key;field1;field2;field3;field4;field5;field6;field7;field8
http://192.168.4.1/?thingspeakread=request
--> request = /channels/xxxxx/fields/1.json?results=1
http://192.168.4.1/?linenotify=token;request
--> request = message=xxxxx
--> request = message=xxxxx&stickerPackageId=xxxxx&stickerId=xxxxx
http://192.168.4.1/?i2cLcd=address;gpioSDA;gpioSCL;text1;text2
--> address(Decimal) : 0x27=39, 0x3F=63
--> SDA->gpio0, SCL->gpio2
STAIP:
Query:http://192.168.4.1/?ip
Link:http://192.168.4.1/?resetwifi=ssid;password
If you don't need to get response from ESP8266 and want to execute commands quickly,
you can append a parameter value "stop" at the end of command.
For example:
http://192.168.4.1/?digitalwrite=gpio;value;stop
http://192.168.4.1/?restart=stop
Control Page (http)
Source
https://github.com/fustyles/webduino/blob/master/ESP8266_MyFirmata.html
Page
https://fustyles.github.io/webduino/ESP8266_MyFirmata.html
LCD Library
https://github.com/agnunez/ESP8266-I2C-LCD1602
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
HTTPClient http;
// Enter your WiFi ssid and password
const char* ssid = "xxxxx"; //your network SSID
const char* password = "xxxxx"; //your network password
const char* apssid = "MyFirmata ESP01";
const char* appassword = "12345678"; //AP password require at least 8 characters.
WiFiServer server(80);
String Feedback="", Command="",cmd="",P1="",P2="",P3="",P4="",P5="",P6="",P7="",P8="",P9="";
byte ReceiveState=0,cmdState=1,strState=1,questionstate=0,equalstate=0,semicolonstate=0;
void ExecuteCommand()
{
Serial.println("");
//Serial.println("Command: "+Command);
Serial.println("cmd= "+cmd+" ,P1= "+P1+" ,P2= "+P2+" ,P3= "+P3+" ,P4= "+P4+" ,P5= "+P5+" ,P6= "+P6+" ,P7= "+P7+" ,P8= "+P8+" ,P9= "+P9);
Serial.println("");
if (cmd=="your cmd") {
// You can do anything
// Feedback="{\"data\":\"Sensor data"\"}";
}
else if (cmd=="ip") {
Feedback="{\"data\":\""+WiFi.softAPIP().toString()+"\"},{\"data\":\""+WiFi.localIP().toString()+"\"}";
}
else if (cmd=="mac") {
Feedback="{\"data\":\""+WiFi.macAddress()+"\"}";
}
else if (cmd=="restart") {
ESP.restart();
}
else if (cmd=="resetwifi") {
WiFi.begin(P1.c_str(), P2.c_str());
Serial.print("Connecting to ");
Serial.println(P1);
long int StartTime=millis();
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
if ((StartTime+5000) < millis()) break;
}
Serial.println("");
Serial.println("STAIP: "+WiFi.localIP().toString());
Feedback="{\"data\":\""+WiFi.softAPIP().toString()+"\"},{\"data\":\""+WiFi.localIP().toString()+"\"}";
/*
if (WiFi.localIP().toString()!="0.0.0.0") {
cmd="linenotify";
P1 = "yourToken";
P2 = "message="+WiFi.localIP().toString();
ExecuteCommand();
}
*/
}
else if (cmd=="inputpullup") {
pinMode(P1.toInt(), INPUT_PULLUP);
}
else if (cmd=="pinmode") {
if (P2.toInt()==1)
pinMode(P1.toInt(), OUTPUT);
else
pinMode(P1.toInt(), INPUT);
}
else if (cmd=="digitalwrite") {
pinMode(P1.toInt(), OUTPUT);
digitalWrite(P1.toInt(), P2.toInt());
}
else if (cmd=="digitalread") {
Feedback="{\"data\":\""+String(digitalRead(P1.toInt()))+"\"}";
}
else if (cmd=="analogwrite") {
pinMode(P1.toInt(), OUTPUT);
analogWrite(P1.toInt(),P2.toInt());
}
else if (cmd=="analogread") {
Feedback="{\"data\":\""+String(analogRead(P1.toInt()))+"\"}";
}
else if (cmd=="tcp") {
String domain=P1;
int port=P2.toInt();
String request=P3;
int wait=P4.toInt(); // wait = 0 or 1
Feedback="{\"data\":\""+tcp(domain,request,port,wait)+"\"}";
}
else if (cmd=="ifttt") {
String domain="maker.ifttt.com";
String request = "/trigger/" + P1 + "/with/key/" + P2;
request += "?value1="+P3+"&value2="+P4+"&value3="+P5;
Feedback="{\"data\":\""+tcp(domain,request,80,0)+"\"}";
}
else if (cmd=="thingspeakupdate") {
String domain="api.thingspeak.com";
String request = "/update?api_key=" + P1;
request += "&field1="+P2+"&field2="+P3+"&field3="+P4+"&field4="+P5+"&field5="+P6+"&field6="+P7+"&field7="+P8+"&field8="+P9;
Feedback="{\"data\":\""+tcp(domain,request,80,0)+"\"}";
}
else if (cmd=="thingspeakread") {
String domain="api.thingspeak.com";
String request = P1;
Feedback=tcp(domain,request,80,1);
int s=Feedback.indexOf("feeds");
Feedback=Feedback.substring(s+8);
int e=Feedback.indexOf("]");
Feedback=Feedback.substring(0,e);
Feedback.replace("},{",";");
Feedback.replace("\":\"",",");
Feedback.replace("\":",",");
Feedback.replace("\",\"",",");
Feedback.replace("\"","");
Feedback.replace("{","");
Feedback.replace("}","");
Feedback.replace("[","");
Feedback.replace("]","");
Feedback.replace(",\"",",");
Feedback.replace("\":",",");
Feedback="{\"data\":\""+Feedback+"\"}";
}
else if (cmd=="linenotify") { //message=xxx&stickerPackageId=xxx&stickerId=xxx
String token = P1;
String request = P2;
Feedback=LineNotify(token,request,1);
if (Feedback.indexOf("status")!=-1) {
int s=Feedback.indexOf("status");
Feedback=Feedback.substring(s);
int e=Feedback.indexOf("</body>");
Feedback=Feedback.substring(0,e);
Feedback.replace("\n","");
Feedback.replace(" ","");
}
Feedback="{\"data\":\""+Feedback+"\"}";
}
else if (cmd=="i2cLcd") {
LiquidCrystal_I2C lcd(P1.toInt(),16,2);
lcd.begin(0, 2);
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(P4);
lcd.setCursor(0,1);
lcd.print(P5);
}
else {
Feedback="{\"data\":\"Command is not defined\"}";
}
if (Feedback=="") Feedback="{\"data\":\""+Command+"\"}";
}
void setup()
{
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_AP_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+10000) < 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());
if (WiFi.status() == WL_CONNECTED)
{
WiFi.softAP((WiFi.localIP().toString()+"_"+(String)apssid).c_str(), appassword);
/*
cmd="linenotify";
P1 = "yourToken";
P2 = "message="+WiFi.localIP().toString();
ExecuteCommand();
*/
}
else
WiFi.softAP(apssid, appassword);
//WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0));
Serial.println("");
Serial.println("APIP address: ");
Serial.println(WiFi.softAPIP());
server.begin();
}
void loop()
{
Command="";cmd="";P1="";P2="";P3="";P4="";P5="";P6="";P7="";P8="";P9="";
ReceiveState=0,cmdState=1,strState=1,questionstate=0,equalstate=0,semicolonstate=0;
WiFiClient client = server.available();
if (client)
{
String currentLine = "";
while (client.connected())
{
if (client.available())
{
char c = client.read();
getCommand(c);
if (c == '\n')
{
if (currentLine.length() == 0)
{
client.println("HTTP/1.1 200 OK");
client.println("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
client.println("Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS");
client.println("Content-Type: application/json;charset=utf-8");
client.println("Access-Control-Allow-Origin: *");
client.println("Connection: close");
client.println();
if (Feedback=="")
client.println("[{\"data\":\"Hello World\"}]");
else
client.println("["+Feedback+"]");
client.println();
Feedback="";
break;
} else {
currentLine = "";
}
}
else if (c != '\r')
{
currentLine += c;
}
if ((currentLine.indexOf("/?")!=-1)&&(currentLine.indexOf(" HTTP")!=-1))
{
if (Command.indexOf("stop")!=-1) {
client.println();
client.println();
client.stop();
}
currentLine="";
Feedback="";
ExecuteCommand();
}
}
}
delay(1);
client.stop();
}
/*
if (SensorValue>LimitValue)
{
cmd="yourcmd";
P1="yourP1";
P2="yourP2";
P3="yourP3";
...
P9="yourP9";
ExecuteCommand();
delay(10000);
}
*/
}
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))) P1=P1+String(c);
if ((cmdState==0)&&(strState==2)&&(c!=';')) P2=P2+String(c);
if ((cmdState==0)&&(strState==3)&&(c!=';')) P3=P3+String(c);
if ((cmdState==0)&&(strState==4)&&(c!=';')) P4=P4+String(c);
if ((cmdState==0)&&(strState==5)&&(c!=';')) P5=P5+String(c);
if ((cmdState==0)&&(strState==6)&&(c!=';')) P6=P6+String(c);
if ((cmdState==0)&&(strState==7)&&(c!=';')) P7=P7+String(c);
if ((cmdState==0)&&(strState==8)&&(c!=';')) P8=P8+String(c);
if ((cmdState==0)&&(strState>=9)&&((c!=';')||(semicolonstate==1))) P9=P9+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 (state==true) Feedback += String(c);
if (c == '\n')
{
if (getResponse.length()==0) state=true;
getResponse = "";
}
else if (c != '\r')
getResponse += 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";
}
String LineNotify(String token, String message, byte wait)
{
message.replace("%","%25");
message.replace(" ","%20");
message.replace("&","%20");
message.replace("#","%20");
//message.replace("\'","%27");
message.replace("\"","%22");
message.replace("\n","%0D%0A");
message.replace("%3Cbr%3E","%0D%0A");
message.replace("%3Cbr/%3E","%0D%0A");
message.replace("%3Cbr%20/%3E","%0D%0A");
message.replace("%3CBR%3E","%0D%0A");
message.replace("%3CBR/%3E","%0D%0A");
message.replace("%3CBR%20/%3E","%0D%0A");
message.replace("%20stickerPackageId","&stickerPackageId");
message.replace("%20stickerId","&stickerId");
http.begin("http://linenotify.com/notify.php?token="+token+"&"+message);
int httpCode = http.GET();
if(httpCode > 0) {
return http.getString();
} else
return "Connection Error!";
}