-
Notifications
You must be signed in to change notification settings - Fork 289
/
ESP01_SpeechRecognition.ino
314 lines (273 loc) · 9.96 KB
/
ESP01_SpeechRecognition.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
/*
ESP01 (Open in Chrome)
Author : ChungYi Fu (Kaohsiung, Taiwan) 2019-08-24 11:00
https://www.facebook.com/francefu
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
Default APIP: 192.168.4.1
STAIP:
Query:http://192.168.4.1/?ip
Link:http://192.168.4.1/?resetwifi=ssid;password
http://STAIP/?ip
http://STAIP/?mac
http://STAIP/?restart
http://STAIP/?resetwifi=ssid;password
http://STAIP/?speech=text;language;checkbox
*/
#include <ESP8266WiFi.h>
// Enter your WiFi ssid and password
const char* ssid = "xxxxx"; //your network SSID
const char* password = "xxxxx"; //your network password
const String page = "https://fustyles.github.io/webduino/ESP32_SpeechRecognition.html";
const char* apssid = "ESP01 Speech";
const char* appassword = "12345678"; //AP password require at least 8 characters.
WiFiServer server(80);
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;
void ExecuteCommand()
{
str1 = urldecode(str1);
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=="your cmd") {
// You can do anything
// Feedback="<font color=\"red\">Hello World</font>";
}
else if (cmd=="speech") {
if (str1.indexOf("on")!=-1||str1.indexOf("開")!=-1) { //Turn on the LED
ledcDetachPin(2);
pinMode(2, OUTPUT);
digitalWrite(2, 1);
Feedback="<font color=\"red\">Turn on the LED</font>";
}
else if (str1.indexOf("off")!=-1||str1.indexOf("關")!=-1) { //Turn off the LED
ledcDetachPin(2);
pinMode(2, OUTPUT);
digitalWrite(2, 0);
Feedback="<font color=\"red\">Turn off the LED</font>";
}
else
Feedback="<font color=\"red\">No defined => "+str1+"</font>";
}
else if (cmd=="ip") {
Feedback="AP IP: "+WiFi.softAPIP().toString();
Feedback+=", ";
Feedback+="STA IP: "+WiFi.localIP().toString();
}
else if (cmd=="mac") {
Feedback="STA MAC: "+WiFi.macAddress();
}
else if (cmd=="restart") {
setup();
Feedback=Command;
}
else if (cmd=="resetwifi") {
WiFi.begin(str1.c_str(), str2.c_str());
Serial.print("Connecting to ");
Serial.println(str1);
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="STAIP: "+WiFi.localIP().toString();
}
else {
Feedback="Command is not defined";
}
}
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;
}
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);
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();
if (WiFi.status() == WL_CONNECTED) {
pinMode(2, OUTPUT);
for (int i=0;i<5;i++) {
digitalWrite(2,HIGH);
delay(100);
digitalWrite(2,LOW);
delay(100);
}
}
else {
pinMode(2, OUTPUT);
for (int i=0;i<3;i++) {
digitalWrite(2,HIGH);
delay(500);
digitalWrite(2,LOW);
delay(500);
}
}
}
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;
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();
client.println("[{\"esp8266\":\""+Feedback+"\"}]");
client.println();
*/
/*
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: text/xml; charset=utf-8");
client.println("Access-Control-Allow-Origin: *");
client.println("Connection: close");
client.println();
client.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
client.println("<esp8266><feedback>"+Feedback+"</feedback></esp8266>");
client.println();
*/
if (str2=="") str2="en-US";
if (Feedback=="") {
Feedback="Redirect...";
Feedback+="<script>setTimeout(() => location.href = \""+page+"?"+WiFi.localIP().toString()+"&"+str2+"&"+str3+"\", 1000);</script>";
}
else {
Feedback+="<script>setTimeout(() => location.href = \""+page+"?"+WiFi.localIP().toString()+"&"+str2+"&"+str3+"\", 3000);</script>";
}
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: text/html; charset=utf-8");
client.println("Access-Control-Allow-Origin: *");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html><head>");
client.println("<meta charset=\"UTF-8\">");
client.println("<meta http-equiv=\"Access-Control-Allow-Origin\" content=\"*\">");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
client.println("</head><body>");
client.println(Feedback);
client.println("</body></html>");
client.println();
Feedback="";
break;
} else {
currentLine = "";
}
}
else if (c != '\r') {
currentLine += c;
}
if ((currentLine.indexOf("/?")!=-1)&&(currentLine.indexOf(" HTTP")!=-1)) {
currentLine="";
Feedback="";
ExecuteCommand();
}
}
}
delay(1);
client.stop();
}
}
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;
}
}
//https://github.com/zenmanenergy/ESP8266-Arduino-Examples/blob/master/helloWorld_urlencoded/urlencode.ino
String urldecode(String str)
{
String encodedString="";
char c;
char code0;
char code1;
for (int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == '+'){
encodedString+=' ';
}else if (c == '%') {
i++;
code0=str.charAt(i);
i++;
code1=str.charAt(i);
c = (h2int(code0) << 4) | h2int(code1);
encodedString+=c;
} else{
encodedString+=c;
}
yield();
}
return encodedString;
}
unsigned char h2int(char c)
{
if (c >= '0' && c <='9'){
return((unsigned char)c - '0');
}
if (c >= 'a' && c <='f'){
return((unsigned char)c - 'a' + 10);
}
if (c >= 'A' && c <='F'){
return((unsigned char)c - 'A' + 10);
}
return(0);
}