forked from fustyles/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP01_Leonardo_MyFirmata.ino
478 lines (431 loc) · 15.5 KB
/
ESP01_Leonardo_MyFirmata.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
Arduino Leonardo (keyboard,mouse) + ESP-01 (AT Command)
Author : ChungYi Fu (Kaohsiung, Taiwan) 2018-08-27 22:30
https://www.facebook.com/francefu
Page
https://github.com/fustyles/Arduino/blob/master/ESP8266_MyFirmata.html
Keyboard Modifiers (keyboardpress, keyboardwrite)
https://www.arduino.cc/en/Reference/KeyboardModifiers
Command Format :
http://APIP/?cmd=str1;str2;str3;str4;str5;str6;str7;str8;str9
http://STAIP/?cmd=str1;str2;str3;str4;str5;str6;str7;str8;str9
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/?at=AT+Command
http://192.168.4.1/?tcp=domain;port;request
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/?mousemove=xPos;yPos;wheel
http://192.168.4.1/?mouseclickleft
http://192.168.4.1/?mouseclickright
http://192.168.4.1/?mouseclickmiddle
http://192.168.4.1/?mousepressleft
http://192.168.4.1/?mousepressright
http://192.168.4.1/?mousepressmiddle
http://192.168.4.1/?mouserelease
http://192.168.4.1/?keyboardpress=keycode1;keycode2;keycode3;presstime
http://192.168.4.1/?keyboardprint=str1
http://192.168.4.1/?keyboardprintln=str1
http://192.168.4.1/?keyboardwrite=keycode
Default APIP: 192.168.4.1
STAIP:
Query: http://192.168.4.1/?ip
Link:http://192.168.4.1/?resetwifi=ssid;password
Control Page (http)
Source
https://github.com/fustyles/webduino/blob/master/ESP8266_MyFirmata.html
Page
https://fustyles.github.io/webduino/ESP8266_MyFirmata.html
Expanding Arduino Serial Port Buffer Size
https://internetofhomethings.com/homethings/?p=927
https://www.facebook.com/francefu/videos/10211231615856099/
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // ESP-01 TX->D10, RX->D11
#include <Keyboard.h>
#include <Mouse.h>
// Enter your WiFi ssid and password
String WIFI_SSID = ""; //your network SSID
String WIFI_PWD = ""; //your network password
String AP_SSID = "MyFirmata Leonardo";
String AP_PWD = "12345678"; //AP password require at least 8 characters.
String ReceiveData="", command="",cmd="",str1="",str2="",str3="",str4="",str5="",str6="",str7="",str8="",str9="";
String APIP="",APMAC="",STAIP="",STAMAC="",CID="";
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);
//If you want to execute command quickly, please don't execute "Feedback"!
if (cmd=="yourcmd")
{
//you can do anything
//if (debug == true) Feedback(CID,"<font color=\"red\">"+cmd+"="+str1+";"+str2+";"+str3+"</font>",0); --> HTML
//if (debug == true) Feedback(CID,cmd+"="+str1+";"+str2+";"+str3,1); --> XML
//if (debug == true) Feedback(CID,cmd+"="+str1+";"+str2+";"+str3,2); --> JSON
//if (debug == true) Feedback(CID,"<html>"+cmd+"="+str1+";"+str2+";"+str3+"</html>",3); --> Custom definition
}
else if (cmd=="ip")
{
Feedback(CID,"<html>APIP: "+APIP+"<br>STAIP: "+STAIP+"</html>",3);
}
else if (cmd=="mac")
{
Feedback(CID,"<html>APMAC: "+APMAC+"<br>STAMAC: "+STAMAC+"</html>",3);
}
else if (cmd=="resetwifi")
{
if (debug == true) Feedback(CID,"<html>"+str1+","+str2+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
delay(3000);
SendData("AT+CWQAP",2000);
SendData("AT+CWJAP_CUR=\""+str1+"\",\""+str2+"\"",5000);
}
else if (cmd=="restart")
{
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
delay(3000);
SendData("AT+RST",2000);
delay(2000);
setup();
initial();
}
else if (cmd=="at") // ?cmd=str1 -> ?at=AT+RST
{
if (debug == true) Feedback(CID,"<html>"+WaitReply(3000)+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
delay(1000);
mySerial.println(str1);
mySerial.flush();
}
else if (cmd=="tcp") // ?tcp=domain;port;request
{
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
delay(1000);
String Domain=str1;
/*
If request length is too long, it can't work!
Expanding Arduino Serial Port Buffer Size
https://internetofhomethings.com/homethings/?p=927
If you change buffer size to 256, request length must be less than or equal to 128.
*/
String request = "GET /"+str3+" HTTP/1.1\r\nHost: "+Domain+"\r\n\r\n";
SendData("AT+CIPSTART=4,\"TCP\",\""+Domain+"\","+str2, 4000);
SendData("AT+CIPSEND=4," + String(request.length()+2), 4000);
SendData(request, 2000);
delay(1);
SendData("AT+CIPCLOSE=4",2000);
}
else if (cmd=="inputpullup")
{
pinMode(str1.toInt(), INPUT_PULLUP);
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="pinmode")
{
pinMode(str1.toInt(), str2.toInt());
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="digitalwrite")
{
pinMode(str1.toInt(), OUTPUT);
digitalWrite(str1.toInt(),str2.toInt());
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="digitalread")
{
Feedback(CID,"<html>"+String(digitalRead(str1.toInt()))+"</html>",3);
}
else if (cmd=="analogwrite")
{
pinMode(str1.toInt(), OUTPUT);
analogWrite(str1.toInt(),str2.toInt());
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="analogread")
{
Feedback(CID,"<html>"+String(analogRead(str1.toInt()))+"</html>",3);
}
else if (cmd=="mousemove")
{
Mouse.move(str1.toInt(), str2.toInt(), str3.toInt());
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="mouseclickleft")
{
Mouse.click(MOUSE_LEFT);
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="mouseclickright")
{
Mouse.click(MOUSE_RIGHT);
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="mouseclickmiddle")
{
Mouse.click(MOUSE_MIDDLE);
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="mousepressleft")
{
Mouse.press(MOUSE_LEFT);
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="mousepressright")
{
Mouse.press(MOUSE_RIGHT);
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="mousepressmiddle")
{
Mouse.press(MOUSE_MIDDLE);
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="mouserelease")
{
Mouse.release();
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="keyboardpress")
{
if (str1!="") Keyboard.press(char(str1.toInt()));
if (str2!="") Keyboard.press(char(str2.toInt()));
if (str3!="") Keyboard.press(char(str3.toInt()));
delay(str4.toInt());
Keyboard.releaseAll();
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="keyboardprint")
{
Keyboard.print(str1);
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="keyboardprintln")
{
Keyboard.println(str1);
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else if (cmd=="keyboardwrite")
{
Keyboard.write(char(str1.toInt()));
if (debug == true) Feedback(CID,"<html>"+command+"</html>",3);
if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
}
else
{
Feedback(CID,"<html>Command is not defined</html>",3);
}
}
void setup()
{
Serial.begin(9600);
Mouse.begin();
Keyboard.begin();
//You must change uart baud rate of ESP-01 to 9600.
mySerial.begin(115200); //Default uart baud rate -> 19200,38400,57600,74880,115200
SendData("AT+UART_CUR=9600,8,1,0,0",2000); //Change uart baud rate of ESP-01 to 9600
mySerial.begin(9600); // 9600 ,you will get more stable data.
mySerial.setTimeout(10);
initial();
}
void initial()
{
SendData("AT+CWMODE_CUR=3",2000);
SendData("AT+CIPMUX=1",2000);
SendData("AT+CIPSERVER=1,80",2000); //port=80
SendData("AT+CIPSTO=5",2000); //timeout= 5 seconds
SendData("AT+CIPAP_CUR=\"192.168.4.1\"",2000); //APIP: 192.168.4.1
SendData("AT+CWSAP_CUR=\""+AP_SSID+"\",\""+AP_PWD+"\",3,4",2000);
//String STA_ip="192.168.0.100";
//String STA_gateway="192.168.0.1";
//String STA_netmask="255.255.255.0";
//SendData("AT+CIPSTA_CUR=\""+STA_ip+"\",\""+STA_gateway+"\",\""+STA_netmask+"\"",2000);
if (WIFI_SSID!="")
SendData("AT+CWJAP_CUR=\""+WIFI_SSID+"\",\""+WIFI_PWD+"\"",5000);
else
Serial.print("Please check your network SSID and password");
}
void loop()
{
getCommand();
if ((ReceiveData.indexOf("/?")!=-1)&&(ReceiveData.indexOf(" HTTP")!=-1))
{
CID=String(ReceiveData.charAt(ReceiveData.indexOf("+IPD,")+5));
//if (debug == false) SendData("AT+CIPCLOSE="+CID,0);
executecommand();
}
else if ((ReceiveData.indexOf("/?")!=-1)&&(ReceiveData.indexOf(" HTTP")==-1))
{
if(ReceiveData.indexOf("+IPD,")!=-1)
{
CID=String(ReceiveData.charAt(ReceiveData.indexOf("+IPD,")+5));
Feedback(CID,"<html>It can't work!Check the length of command.</html>",3);
}
}
else if ((ReceiveData.indexOf("/?")==-1)&&(ReceiveData.indexOf(" HTTP")!=-1))
{
CID=String(ReceiveData.charAt(ReceiveData.indexOf("+IPD,")+5));
Feedback(CID,"<html>Hello World</html>",3);
}
}
void SendData(String data,int TimeLimit)
{
mySerial.println(data);
mySerial.flush();
delay(10);
WaitReply(TimeLimit);
}
void Feedback(String CID,String Response,int datatype)
{
/*
If response length is too long, it can't work!
If you change buffer size to 256, response length must be less than or equal to 128.
*/
if (datatype==0)
{
Response="<!DOCTYPE HTML><html><head><meta charset=\"UTF-8\"></head><body>"+Response+"</body></html>";
}
else if (datatype==1)
{
Response="<?xml version=\"1.0\" encoding=\"UTF-8\"?><ESP8266><Data><Text>"+Response+"</Text></Data></ESP8266>";
}
else if (datatype==2)
{
Response="[{\"ESP8266\":\""+Response+"\"}]";
}
else
Response=Response;
SendData("AT+CIPSEND="+CID+","+(Response.length()+2),2000);
SendData(Response,2000);
delay(1);
SendData("AT+CIPCLOSE="+CID,2000);
}
String WaitReply(long int TimeLimit)
{
String ReceiveData="";
long int StartTime=millis();
while( (StartTime+TimeLimit) > millis())
{
if (mySerial.available())
{
delay(4);
while(mySerial.available())
{
ReceiveData=ReceiveData+String(char(mySerial.read()));
}
//Serial.println(ReceiveData);
if (ReceiveData.indexOf("OK")!=-1) return ReceiveData;
}
}
return ReceiveData;
}
void getCommand()
{
ReceiveData="";command="";cmd="";str1="";str2="";str3="";str4="";str5="";str6="";str7="";str8="";str9="";
byte ReceiveState=0,cmdState=1,strState=1,questionstate=0,equalstate=0,semicolonstate=0;
if (mySerial.available())
{
while (mySerial.available())
{
char c=mySerial.read();
ReceiveData=ReceiveData+String(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;
}
if (ReceiveData.indexOf(" HTTP")!=-1)
{
while (mySerial.available())
{
mySerial.read();
}
}
}
Serial.println(ReceiveData);
if (ReceiveData.indexOf("WIFI GOT IP")!=-1)
{
while(!mySerial.find('OK')){}
delay(10);
APIP="";APMAC="";STAIP="";STAMAC="";
int apipreadstate=0,staipreadstate=0,apmacreadstate=0,stamacreadstate=0,j=0;
mySerial.println("AT+CIFSR");
mySerial.flush();
delay(6);
while(mySerial.available())
{
char c=mySerial.read();
String t=String(c);
if (t=="\"") j++;
if (j==1)
apipreadstate=1;
else if (j==2)
apipreadstate=0;
if ((apipreadstate==1)&&(t!="\"")) APIP=APIP+t;
if (j==3)
apmacreadstate=1;
else if (j==4)
apmacreadstate=0;
if ((apmacreadstate==1)&&(t!="\"")) APMAC=APMAC+t;
if (j==5)
staipreadstate=1;
else if (j==6)
staipreadstate=0;
if ((staipreadstate==1)&&(t!="\"")) STAIP=STAIP+t;
if (j==7)
stamacreadstate=1;
else if (j==8)
stamacreadstate=0;
if ((stamacreadstate==1)&&(t!="\"")) STAMAC=STAMAC+t;
}
pinMode(13,1);
for (int i=0;i<20;i++)
{
digitalWrite(13,1);
delay(50);
digitalWrite(13,0);
delay(50);
}
Serial.println("APIP: "+APIP+"\nAPMAC: "+APMAC+"\nSTAIP: "+STAIP+"\nSTAMAC: "+STAMAC);
}
}
}