diff --git a/Arduino_Combination_Lock.ino b/Arduino_Combination_Lock.ino new file mode 100644 index 000000000..eb5e163c3 --- /dev/null +++ b/Arduino_Combination_Lock.ino @@ -0,0 +1,199 @@ +#include +Servo myservo; //建立Servo物件控制門鎖 +int ServorPIN=3; //Servo輸出腳位 + +int btn0=4; //重設按鈕腳位 +int btn1=5; //密碼數字1按鈕腳位 +int btn2=6; //密碼數字2按鈕腳位 +int btn3=7; //密碼數字3按鈕腳位 +int ledR=9; //指示燈Red腳位 +int ledG=10; //指示燈Green腳位 +int ledB=11; //指示燈Blue腳位 +double t; //暫存連續輸入密碼間隔時間 +int stateInitial=0; //輸入密碼狀態 1-初始化設定密碼狀態,2-解鎖輸入密碼狀態 +int Timelimit=1000; //連續輸入密碼逾時時間(ms) +int n1=0; //第一個密碼輸入狀態 +int n2=0; //第二個密碼輸入狀態 +int n3=0; //第三個密碼輸入狀態 +String pwd1=""; //紀錄初始化密碼第一個數字 +String pwd2=""; //紀錄初始化密碼第二個數字 +String pwd3=""; //紀錄初始化密碼第三個數字 +String inp1=""; //紀錄輸入解鎖密碼第一個數字 +String inp2=""; //紀錄輸入解鎖密碼第二個數字 +String inp3=""; //紀錄輸入解鎖密碼第三個數字 + +void setup() { + Serial.begin(9600); //鮑率 + + myservo.attach(ServorPIN); //設定Servo輸出腳位 + myservo.write(0); //Servo旋轉至解鎖位置 + + pinMode(btn0, INPUT); + pinMode(btn1, INPUT); + pinMode(btn2, INPUT); + pinMode(btn3, INPUT); + + pinMode(ledR, OUTPUT); + pinMode(ledG, OUTPUT); + pinMode(ledB, OUTPUT); + + analogWrite(ledR,0); + analogWrite(ledG,0); + analogWrite(ledB,0); +} + +void loop() { + int s0 = digitalRead(btn0); //讀取重設按鈕 + //Serial.println(s0); + int s1 = digitalRead(btn1); //讀取密碼1按鈕 + //Serial.println(s1); + int s2 = digitalRead(btn2); //讀取密碼2按鈕 + //Serial.println(s2); + int s3 = digitalRead(btn3); //讀取密碼3按鈕 + //Serial.println(s3); + + if (s0==HIGH) //重設按鈕按下 + { + n1=0;n2=0;n3=0;t=0;pwd1="";pwd2="";pwd3=""; + + //亮藍燈 + analogWrite(ledR,0); + analogWrite(ledG,0); + analogWrite(ledB,255); + + stateInitial=1; //進入初始化密碼設定狀態 + myservo.write(0); //Servo旋轉至解鎖位置 + } + + if ((s1==HIGH||s2==HIGH||s3==HIGH)&&(stateInitial==1||stateInitial==2)) //若偵測到密碼按鈕按下 + { + //綠燈閃爍一次 + analogWrite(ledR,0); + analogWrite(ledG,255); + analogWrite(ledB,0); + delay(10); + analogWrite(ledR,0); + analogWrite(ledG,0); + analogWrite(ledB,0); + + if (n1==0) //尚未輸入任何密碼 + { + n1=1; //輸入第一個密碼狀態 + n2=0;n3=0;t=0; + if (stateInitial==1) //初始化設定密碼狀態 + { + if (s1==HIGH) pwd1="1"; + if (s2==HIGH) pwd1="2"; + if (s3==HIGH) pwd1="3"; + } + else if (stateInitial==2) //解鎖輸入密碼狀態 + { + if (s1==HIGH) inp1="1"; + if (s2==HIGH) inp1="2"; + if (s3==HIGH) inp1="3"; + } + } + else if (n1==1&&n2==0&&t>=100&&t<=Timelimit) //在Timelimit時間內偵測到輸入第二個密碼 + { + n2=1; //輸入第二個密碼狀態 + n3=0;t=0; + if (stateInitial==1) + { + if (s1==HIGH) pwd2="1"; + if (s2==HIGH) pwd2="2"; + if (s3==HIGH) pwd2="3"; + } + else if (stateInitial==2) + { + if (s1==HIGH) inp2="1"; + if (s2==HIGH) inp2="2"; + if (s3==HIGH) inp2="3"; + } + } + else if (n2==1&&n3==0&&t>=100&&t<=Timelimit) //在Timelimit時間內偵測到輸入第三個密碼 + { + n3=1; //輸入第三個密碼狀態 + t=0; + if (stateInitial==1) + { + if (s1==HIGH) pwd3="1"; + if (s2==HIGH) pwd3="2"; + if (s3==HIGH) pwd3="3"; + } + else if (stateInitial==2) + { + if (s1==HIGH) inp3="1"; + if (s2==HIGH) inp3="2"; + if (s3==HIGH) inp3="3"; + } + } + } + if (t>Timelimit&&n3==1) //超過時間範圍且已輸入第三個密碼 + { + if (stateInitial==1) + { + myservo.write(90); //Servo旋轉至上鎖位置 + + //亮紅燈 + analogWrite(ledR,255); + analogWrite(ledG,0); + analogWrite(ledB,0); + + stateInitial=2; //進入解鎖輸入密碼狀態 + } + else if (stateInitial==2) + { + if (pwd1==inp1&&pwd2==inp2&&pwd3==inp3) + { + //密碼正確,亮白燈 + analogWrite(ledR,255); + analogWrite(ledG,255); + analogWrite(ledB,255); + + myservo.write(0); //Servo旋轉至解鎖位置 + } + else + { + //密碼錯誤,亮紅燈 + analogWrite(ledR,255); + analogWrite(ledG,0); + analogWrite(ledB,0); + + myservo.write(90); //Servo旋轉至上鎖位置 + + inp1="";inp2="";inp3=""; + } + } + n1=0;n2=0;n3=0;t=0; + } + else if (t>Timelimit&&n3==0) //時間範圍內偵測密碼不足三位 + { + if (stateInitial==1) + { + //密碼不足三位,亮藍燈 + analogWrite(ledR,0); + analogWrite(ledG,0); + analogWrite(ledB,255); + + pwd1="";pwd2="";pwd3="";inp1="";inp2="";inp3=""; + } + else if (stateInitial==2) + { + //密碼錯誤,亮紅燈 + analogWrite(ledR,255); + analogWrite(ledG,0); + analogWrite(ledB,0); + + myservo.write(90); //Servo旋轉至上鎖位置 + + inp1="";inp2="";inp3=""; + } + n1=0;n2=0;n3=0;t=0; + } + + if (n1==1||n2==1) //偵測到第一次或第二次輸入密碼後開始計時 + { + delay(1); + t=t+1; + } +} diff --git a/Arduino_Electric_Fan.ino b/Arduino_Electric_Fan.ino new file mode 100644 index 000000000..36a40ec2d --- /dev/null +++ b/Arduino_Electric_Fan.ino @@ -0,0 +1,89 @@ +/* +Arduino Uno +3V DC Motor +Motor Driver Board +SG90 Mini Servo +*/ + +#include +Servo myservo; + +int servoPin=4; //伺服馬達腳位 +int motorPin1=5; //馬達驅動IC腳位 +int motorPin2=6; //馬達驅動IC腳位 +int speedButtonPin=7; //風速四段變速切換按鈕腳位 +int rotateButtonPin=8; //風向旋轉切換按鈕腳位 + +int angle=90; //風向初始角度 +int degree=5; //單位時間風向旋轉角度 +int pressCount=0; //風速切換按鈕點選次數 +int rotateState=-1; //風向旋轉狀態 1(旋轉), -1(停止) +int rotateInterval=200; //風向旋轉時間間隔 (ms) + +void setup() +{ + Serial.begin(9600); + + pinMode(speedButtonPin, INPUT_PULLUP); + pinMode(rotateButtonPin, INPUT_PULLUP); + pinMode(motorPin1, OUTPUT); + pinMode(motorPin2, OUTPUT); + + myservo.attach(servoPin); + myservo.write(angle); +} + +void loop() +{ + //讀取風速切換按鈕狀態 + if (digitalRead(speedButtonPin)==0) + { + pressCount=(pressCount+1)%4; //風速四段切換 + //Serial.println(pressCount); + + switch (pressCount) + { + case 0: + analogWrite(motorPin1,0); + analogWrite(motorPin2,0); + break; + case 1: + analogWrite(motorPin1,100); //0~255 + analogWrite(motorPin2,0); + break; + case 2: + analogWrite(motorPin1,120); //0~255 + analogWrite(motorPin2,0); + break; + case 3: + analogWrite(motorPin1,140); //0~255 + analogWrite(motorPin2,0); + break; + default: + analogWrite(motorPin1,0); + analogWrite(motorPin2,0); + break; + } + delay(500); + } + + //讀取風向旋轉切換按鈕狀態 + if (digitalRead(rotateButtonPin)==0) + { + rotateState*=(-1); //風向旋轉狀態切換 + delay(500); + } + + if (rotateState==1) //風向來回轉動 + { + angle+=degree; + if ((angle<5)||(angle>175)) + { + degree*=(-1); + angle+=degree*2; + } + myservo.write(angle); + + delay(rotateInterval); + } +} diff --git a/Arduino_Microphone_Sensor.ino b/Arduino_Microphone_Sensor.ino new file mode 100644 index 000000000..9e45c960e --- /dev/null +++ b/Arduino_Microphone_Sensor.ino @@ -0,0 +1,98 @@ +void ShowLED(int p); //自定函式 + +//麥克風感測器由左而右 A0,G,+,D0 +//調整麥克風感測器可變電阻值使類比訊號值約500左右 + +int m1=A0; //麥克風A0腳位 +int m2=12; //麥克風D0腳位 +double t; //紀錄連續擊掌間隔時間 +int timelimit=1000; //連續擊掌逾時間隔時間 +int n1=0; //第一次擊掌狀態 +int n2=0; //第二次擊掌狀態 +int n3=0; //第三次擊掌狀態 +int bin1=0; //紀錄第一次擊掌結果 +int bin2=0; //紀錄第二次擊掌結果 +int bin3=0; //紀錄第三次擊掌結果 +int lim1=520; //產生聲音下限值 +int lim2=700; //產生聲音大小聲區分值 +int LED[]={2,3,4,5,6,7,8,9}; //控制8個LED燈腳位 +int LEDs[]={10,11,12}; //擊掌指示燈腳位 + +void setup() { + for (int i=0;i=lim1) //偵測到聲音高於下限值,表示由擊掌產生 + { + if (n1==0) //尚未擊掌過 + { + n1=1; //第一次擊掌狀態 + n2=0;n3=0;t=0; + digitalWrite(LEDs[0],HIGH); + + if (s2>=lim2) + bin1=1; //音量>=基準值,第一位數字設為1 + else + bin1=0; //音量<基準值,第一位數字設為0 + delay(200); //延遲200ms,避免同一次擊掌產生連續的輸入 + } + //隔timelimit內偵測到第二次擊掌 + else if (n1==1&&n2==0&&t>=200&&t<=timelimit) + { + n2=1; //第二次擊掌狀態 + n3=0;t=0; + digitalWrite(LEDs[1],HIGH); + if (s2>=lim2) + bin2=1; + else + bin2=0; + delay(200); + } + //隔timelimit內偵測到第三次擊掌 + else if (n2==1&&n3==0&&t>=200&&t<=timelimit) + { + n3=1; //第三次擊掌狀態 + t=0; + digitalWrite(LEDs[2],HIGH); + if (s2>=lim2) + bin3=1; + else + bin3=0; + } + } + } + if (t>timelimit) //超過逾期時間 + { + if (n1==1&&n2==1&&n3==1) + ShowLED(bin1*4+bin2*2+bin3); //二進制數字轉成十進制數字顯示 + + n1=0;n2=0;n3=0;t=0;bin1=0;bin2=0;bin3=0; + + digitalWrite(LEDs[0],LOW); + digitalWrite(LEDs[1],LOW); + digitalWrite(LEDs[2],LOW); + delay(100); + } + if (n1==1||n2==1) //偵測到第一次或第二次擊掌後重新計時 + { + delay(1); + t=t+1; + } +} + +void ShowLED(int p) +{ + digitalWrite(LED[p],HIGH); +} diff --git a/Bluetooth_Leonardo_MyFirmata.ino.ino b/Bluetooth_Leonardo_MyFirmata.ino.ino deleted file mode 100644 index 0672dee6d..000000000 --- a/Bluetooth_Leonardo_MyFirmata.ino.ino +++ /dev/null @@ -1,215 +0,0 @@ - -/* -Arduino Leonardo(Uart) + Bluetooth - -Author : ChungYi Fu (Kaohsiung, Taiwan) 2018-4-20 09:00 - -Command Format : -?cmd=str1;str2;str3;str4;str5;str6;str7;str8;str9 - -?inputpullup=pin -?pinmode=pin;value -?digitalwrite=pin;value -?analogwrite=pin;value -?digitalread=pin -?analogread=pin -?mousemove=xVal;yPos;wheel -?mouseclickleft -?mouseclickright -?mouseclickmiddle -?mousepressleft -?mousepressright -?mousepressmiddle -?mouserelease -?keyboardpress=keycode1;keycode2;keycode3;presstime -?keyboardprint=str1 -?keyboardprintln=str1 -?keyboardwrite=keycode - -Keyboard Modifiers (keyboardpress) -https://www.arduino.cc/en/Reference/KeyboardModifiers -*/ - - -#include -SoftwareSerial mySerial(10, 11); // Arduino RX:10, TX:11 - -String ReceiveData="", command="",cmd="",str1="",str2="",str3="",str4="",str5="",str6="",str7="",str8="",str9=""; - -#include -#include - -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 (cmd=="yourcmd") - { - //you can do anything - //SendData(cmd+"="+str1+";"+str2); - } - else if (cmd=="inputpullup") - { - pinMode(str1.toInt(), INPUT_PULLUP); - SendData(command); - } - else if (cmd=="pinmode") - { - pinMode(str1.toInt(), str2.toInt()); - SendData(command); - } - else if (cmd=="digitalwrite") - { - pinMode(str1.toInt(), OUTPUT); - digitalWrite(str1.toInt(),str2.toInt()); - SendData(command); - } - else if (cmd=="digitalread") - { - SendData(String(digitalRead(str1.toInt()))); - } - else if (cmd=="analogwrite") - { - pinMode(str1.toInt(), OUTPUT); - analogWrite(str1.toInt(),str2.toInt()); - SendData(command); - } - else if (cmd=="analogread") - { - SendData(String(analogRead(str1.toInt()))); - } - else if (cmd=="mousemove") - { - Mouse.move(str1.toInt(), str2.toInt(), str3.toInt()); - SendData(command); - } - else if (cmd=="mouseclickleft") - { - Mouse.click(MOUSE_LEFT); - SendData(command); - } - else if (cmd=="mouseclickright") - { - Mouse.click(MOUSE_RIGHT); - SendData(command); - } - else if (cmd=="mouseclickmiddle") - { - Mouse.click(MOUSE_MIDDLE); - SendData(command); - } - else if (cmd=="mousepressleft") - { - Mouse.press(MOUSE_LEFT); - SendData(command); - } - else if (cmd=="mousepressright") - { - Mouse.press(MOUSE_RIGHT); - SendData(command); - } - else if (cmd=="mousepressmiddle") - { - Mouse.press(MOUSE_MIDDLE); - SendData(command); - } - else if (cmd=="mouserelease") - { - Mouse.release(); - SendData(command); - } - 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(); - SendData(command); - } - else if (cmd=="keyboardprint") - { - Keyboard.print(str1); - SendData(command); - } - else if (cmd=="keyboardprintln") - { - Keyboard.println(str1); - SendData(command); - } - else if (cmd=="keyboardwrite") - { - Keyboard.write(char(str1.toInt())); - SendData(command); - } - else - { - SendData("command is not defined"); - } -} - -void setup() -{ - Serial.begin(9600); - mySerial.begin(9600); - mySerial.setTimeout(10); -} - -void loop() -{ - getCommand(); - - if (ReceiveData.indexOf("?")==0) - { - executecommand(); - } -} - -void SendData(String data) -{ - mySerial.print(data); -} - -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; - } - } - Serial.println(ReceiveData); - } -} diff --git a/Bluetooth_MyFirmata.ino b/Bluetooth_MyFirmata.ino deleted file mode 100644 index c7a25c6eb..000000000 --- a/Bluetooth_MyFirmata.ino +++ /dev/null @@ -1,213 +0,0 @@ -/* -Arduino Uno(Uart) + Bluetooth - -Author : ChungYi Fu (Kaohsiung, Taiwan) 2018-3-19 09:30 - -Command Format : -?cmd=str1;str2;str3;str4;str5;str6;str7;str8;str9 - -?inputpullup=pin -?pinmode=pin;value -?digitalwrite=pin;value -?analogwrite=pin;value -?digitalread=pin -?analogread=pin -?car=pinL1;pinL2;pinR1;pinR2;L_speed;R_speed;Delay;state -*/ - - -#include -SoftwareSerial mySerial(10, 11); // Arduino RX:10, TX:11 - -String ReceiveData="", command="",cmd="",str1="",str2="",str3="",str4="",str5="",str6="",str7="",str8="",str9=""; - -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 (cmd=="yourcmd") - { - //you can do anything - //SendData(cmd+"="+str1+";"+str2); - } - else if (cmd=="inputpullup") - { - pinMode(str1.toInt(), INPUT_PULLUP); - SendData(command); - } - else if (cmd=="pinmode") - { - pinMode(str1.toInt(), str2.toInt()); - SendData(command); - } - else if (cmd=="digitalwrite") - { - pinMode(str1.toInt(), OUTPUT); - digitalWrite(str1.toInt(),str2.toInt()); - SendData(command); - } - else if (cmd=="digitalread") - { - SendData(String(digitalRead(str1.toInt()))); - } - else if (cmd=="analogwrite") - { - pinMode(str1.toInt(), OUTPUT); - analogWrite(str1.toInt(),str2.toInt()); - SendData(command); - } - else if (cmd=="analogread") - { - SendData(String(analogRead(str1.toInt()))); - } - else if (cmd=="car") // ?car=pinL1;pinL2;pinR1;pinR2;L_speed;R_speed;Delay;state - { - pinMode(str1.toInt(), OUTPUT); - pinMode(str2.toInt(), OUTPUT); - pinMode(str3.toInt(), OUTPUT); - pinMode(str4.toInt(), OUTPUT); - digitalWrite(str1.toInt(), 0); - digitalWrite(str2.toInt(), 0); - digitalWrite(str3.toInt(), 0); - digitalWrite(str4.toInt(), 0); - delay(10); - - if (str8=="S") - { - analogWrite(str1.toInt(),0); - analogWrite(str2.toInt(),0); - analogWrite(str3.toInt(),0); - analogWrite(str4.toInt(),0); - } - else if (str8=="F") - { - analogWrite(str1.toInt(),str5.toInt()); - analogWrite(str2.toInt(),0); - analogWrite(str3.toInt(),0); - analogWrite(str4.toInt(),str6.toInt()); - if ((str7!="")&&(str7!="0")) - { - delay(str7.toInt()); - analogWrite(str1.toInt(),0); - analogWrite(str2.toInt(),0); - analogWrite(str3.toInt(),0); - analogWrite(str4.toInt(),0); - } - } - else if (str8=="B") - { - analogWrite(str1.toInt(),0); - analogWrite(str2.toInt(),str5.toInt()); - analogWrite(str3.toInt(),str6.toInt()); - analogWrite(str4.toInt(),0); - if ((str7!="")&&(str7!="0")) - { - delay(str7.toInt()); - analogWrite(str1.toInt(),0); - analogWrite(str2.toInt(),0); - analogWrite(str3.toInt(),0); - analogWrite(str4.toInt(),0); - } - } - else if (str8=="L") - { - analogWrite(str1.toInt(),0); - analogWrite(str2.toInt(),str5.toInt()); - analogWrite(str3.toInt(),0); - analogWrite(str4.toInt(),str6.toInt()); - if ((str7!="")&&(str7!="0")) - { - delay(str7.toInt()); - analogWrite(str1.toInt(),0); - analogWrite(str2.toInt(),0); - analogWrite(str3.toInt(),0); - analogWrite(str4.toInt(),0); - } - } - else if (str8=="R") - { - analogWrite(str1.toInt(),str5.toInt()); - analogWrite(str2.toInt(),0); - analogWrite(str3.toInt(),str6.toInt()); - analogWrite(str4.toInt(),0); - if ((str7!="")&&(str7!="0")) - { - delay(str7.toInt()); - analogWrite(str1.toInt(),0); - analogWrite(str2.toInt(),0); - analogWrite(str3.toInt(),0); - analogWrite(str4.toInt(),0); - } - } - } - else - { - SendData("command is not defined"); - } -} - -void setup() -{ - Serial.begin(9600); - mySerial.begin(9600); - mySerial.setTimeout(10); -} - -void loop() -{ - getCommand(); - - if (ReceiveData.indexOf("?")==0) - { - executecommand(); - } -} - -void SendData(String data) -{ - mySerial.print(data); -} - -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; - } - } - Serial.println(ReceiveData); - } -} diff --git a/ESP01_Leonardo_MyFirmata.ino b/ESP01_Leonardo_MyFirmata.ino deleted file mode 100644 index ca32129e6..000000000 --- a/ESP01_Leonardo_MyFirmata.ino +++ /dev/null @@ -1,454 +0,0 @@ -/* -Arduino Leonardo(Uart) + ESP8266 ESP-01 (1MB Flash, V2.0_AT_Firmware) - -Author : ChungYi Fu (Taiwan) 2018-04-20 22:00 - -Update AT Firmware -https://www.youtube.com/watch?v=QVhWVu8NnZc -http://www.electrodragon.com/w/File:V2.0_AT_Firmware(ESP).zip - -nodemcu-flasher -https://github.com/nodemcu/nodemcu-flasher -(Baudrate:115200, Flash size:1MByte, Flash speed:26.7MHz, SPI Mode:QIO) - -Expanding Arduino Serial Port Buffer Size -https://internetofhomethings.com/homethings/?p=927 - -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/?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=xVal;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 -https://github.com/fustyles/webduino/blob/master/ESP8266_MyFirmata.html - -Keyboard Modifiers (keyboardpress) -https://www.arduino.cc/en/Reference/KeyboardModifiers -*/ - -// Check your Wi-Fi Router's Settings -String WIFI_SSID = ""; //your network SSID -String WIFI_PWD = ""; //your network password - -#include -SoftwareSerial mySerial(10, 11); // Arduino RX:10, TX:11 - -String ReceiveData="", command="",cmd="",str1="",str2="",str3="",str4="",str5="",str6="",str7="",str8="",str9=""; -String APIP="",APMAC="",STAIP="",STAMAC="",CID=""; - -#include -#include - -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 (cmd=="yourcmd") - { - //you can do anything - - //Feedback(CID,""+cmd+"="+str1+";"+str2+";"+str3+"",0); --> HTML - //Feedback(CID,cmd+"="+str1+";"+str2+";"+str3,1); --> XML - //Feedback(CID,cmd+"="+str1+";"+str2+";"+str3,2); --> JSON - //Feedback(CID,""+cmd+"="+str1+";"+str2+";"+str3+"",3); --> Custom definition - } - else if (cmd=="ip") - { - Feedback(CID,"APIP: "+APIP+"
STAIP: "+STAIP+"",3); - } - else if (cmd=="mac") - { - Feedback(CID,"APMAC: "+APMAC+"
STAMAC: "+STAMAC+"",3); - } - else if (cmd=="resetwifi") - { - Feedback(CID,""+str1+","+str2+"",3); - delay(3000); - SendData("AT+CWQAP",2000); - SendData("AT+CWJAP_CUR=\""+str1+"\",\""+str2+"\"",5000); - } - else if (cmd=="restart") - { - Feedback(CID,""+command+"",3); - delay(3000); - SendData("AT+RST",2000); - delay(2000); - setup(); - initial(); - } - else if (cmd=="at") // ?cmd=str1 -> ?at=AT+RST - { - Feedback(CID,""+WaitReply(3000)+"",3); - delay(1000); - mySerial.println(str1); - mySerial.flush(); - } - else if (cmd=="tcp") // ?tcp=domain;port;request - { - Feedback(CID,""+command+"",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 126? - */ - 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); - Feedback(CID,""+command+"",3); - } - else if (cmd=="pinmode") - { - pinMode(str1.toInt(), str2.toInt()); - Feedback(CID,""+command+"",3); - } - else if (cmd=="digitalwrite") - { - pinMode(str1.toInt(), OUTPUT); - digitalWrite(str1.toInt(),str2.toInt()); - Feedback(CID,""+command+"",3); - } - else if (cmd=="digitalread") - { - Feedback(CID,""+String(digitalRead(str1.toInt()))+"",3); - } - else if (cmd=="analogwrite") - { - pinMode(str1.toInt(), OUTPUT); - analogWrite(str1.toInt(),str2.toInt()); - Feedback(CID,""+command+"",3); - } - else if (cmd=="analogread") - { - Feedback(CID,""+String(analogRead(str1.toInt()))+"",3); - } - else if (cmd=="mousemove") - { - Mouse.move(str1.toInt(), str2.toInt(), str3.toInt()); - Feedback(CID,""+command+"",3); - } - else if (cmd=="mouseclickleft") - { - Mouse.click(MOUSE_LEFT); - Feedback(CID,""+command+"",3); - } - else if (cmd=="mouseclickright") - { - Mouse.click(MOUSE_RIGHT); - Feedback(CID,""+command+"",3); - } - else if (cmd=="mouseclickmiddle") - { - Mouse.click(MOUSE_MIDDLE); - Feedback(CID,""+command+"",3); - } - else if (cmd=="mousepressleft") - { - Mouse.press(MOUSE_LEFT); - Feedback(CID,""+command+"",3); - } - else if (cmd=="mousepressright") - { - Mouse.press(MOUSE_RIGHT); - Feedback(CID,""+command+"",3); - } - else if (cmd=="mousepressmiddle") - { - Mouse.press(MOUSE_MIDDLE); - Feedback(CID,""+command+"",3); - } - else if (cmd=="mouserelease") - { - Mouse.release(); - Feedback(CID,""+command+"",3); - } - 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(); - Feedback(CID,""+command+"",3); - } - else if (cmd=="keyboardprint") - { - Keyboard.print(str1); - Feedback(CID,""+command+"",3); - } - else if (cmd=="keyboardprintln") - { - Keyboard.println(str1); - Feedback(CID,""+command+"",3); - } - else if (cmd=="keyboardwrite") - { - Keyboard.write(char(str1.toInt())); - Feedback(CID,""+command+"",3); - } - else - { - Feedback(CID,"Command is not defined",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_id+"\",\""+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)); - 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,"It can't work!Check the length of command.",3); - } - } - else if ((ReceiveData.indexOf("/?")==-1)&&(ReceiveData.indexOf(" HTTP")!=-1)) - { - CID=String(ReceiveData.charAt(ReceiveData.indexOf("+IPD,")+5)); - Feedback(CID,"Hello World",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! - Expanding Arduino Serial Port Buffer Size - https://internetofhomethings.com/homethings/?p=927 - If you change buffer size to 256, response length must be less than or equal to 126? - */ - if (datatype==0) - { - Response=""+Response+""; - } - else if (datatype==1) - { - Response=""+Response+""; - } - 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())); - ReceiveData=ReceiveData+mySerial.readStringUntil('\r'); - } - //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); - } - } -} diff --git a/ESP01_MyFirmata.ino b/ESP01_MyFirmata.ino deleted file mode 100644 index 36384f276..000000000 --- a/ESP01_MyFirmata.ino +++ /dev/null @@ -1,384 +0,0 @@ -/* -Arduino Uno(Uart) + ESP8266 ESP-01 (1MB Flash) - -Author : ChungYi Fu (Taiwan) 2018-04-20 22:00 - -Update AT Firmware(V2.0_AT_Firmware) -https://www.youtube.com/watch?v=QVhWVu8NnZc -http://www.electrodragon.com/w/File:V2.0_AT_Firmware(ESP).zip - -nodemcu-flasher -https://github.com/nodemcu/nodemcu-flasher -(Baudrate:115200, Flash size:1MByte, Flash speed:26.7MHz, SPI Mode:QIO) - -Expanding Arduino Serial Port Buffer Size -https://internetofhomethings.com/homethings/?p=927 - -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 -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 - -STAIP: -Query: http://192.168.4.1/?ip -Link:http://192.168.4.1/?resetwifi=ssid;password - -Control Page (http) -https://github.com/fustyles/webduino/blob/master/ESP8266_MyFirmata.html -*/ - -// Check your Wi-Fi Router's Settings -String WIFI_SSID = ""; //your network SSID -String WIFI_PWD = ""; //your network password - -#include -SoftwareSerial mySerial(10, 11); // Arduino RX:10, TX:11 - -String ReceiveData="", command="",cmd="",str1="",str2="",str3="",str4="",str5="",str6="",str7="",str8="",str9=""; -String APIP="",APMAC="",STAIP="",STAMAC="",CID=""; - -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 (cmd=="yourcmd") - { - //you can do anything - - //Feedback(CID,""+cmd+"="+str1+";"+str2+";"+str3+"",0); --> HTML - //Feedback(CID,cmd+"="+str1+";"+str2+";"+str3,1); --> XML - //Feedback(CID,cmd+"="+str1+";"+str2+";"+str3,2); --> JSON - //Feedback(CID,""+cmd+"="+str1+";"+str2+";"+str3+"",3); --> Custom definition - } - else if (cmd=="ip") - { - Feedback(CID,"APIP: "+APIP+"
STAIP: "+STAIP+"",3); - } - else if (cmd=="mac") - { - Feedback(CID,"APMAC: "+APMAC+"
STAMAC: "+STAMAC+"",3); - } - else if (cmd=="resetwifi") - { - Feedback(CID,""+str1+","+str2+"",3); - delay(3000); - SendData("AT+CWQAP",2000); - SendData("AT+CWJAP_CUR=\""+str1+"\",\""+str2+"\"",5000); - } - else if (cmd=="restart") - { - Feedback(CID,""+command+"",3); - delay(3000); - SendData("AT+RST",2000); - delay(2000); - setup(); - initial(); - } - else if (cmd=="at") // ?cmd=str1 -> ?at=AT+RST - { - Feedback(CID,""+WaitReply(3000)+"",3); - delay(1000); - mySerial.println(str1); - mySerial.flush(); - } - else if (cmd=="tcp") - { - Feedback(CID,""+command+"",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 126? - */ - 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); - Feedback(CID,""+command+"",3); - } - else if (cmd=="pinmode") - { - pinMode(str1.toInt(), str2.toInt()); - Feedback(CID,""+command+"",3); - } - else if (cmd=="digitalwrite") - { - pinMode(str1.toInt(), OUTPUT); - digitalWrite(str1.toInt(),str2.toInt()); - Feedback(CID,""+command+"",3); - } - else if (cmd=="digitalread") - { - Feedback(CID,""+String(digitalRead(str1.toInt()))+"",3); - } - else if (cmd=="analogwrite") - { - pinMode(str1.toInt(), OUTPUT); - analogWrite(str1.toInt(),str2.toInt()); - Feedback(CID,""+command+"",3); - } - else if (cmd=="analogread") - { - Feedback(CID,""+String(analogRead(str1.toInt()))+"",3); - } - else - { - Feedback(CID,"Command is not defined",3); - } -} - -void setup() -{ - Serial.begin(9600); - - //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_id+"\",\""+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)); - 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,"It can't work!Check command length.",3); - } - } - else if ((ReceiveData.indexOf("/?")==-1)&&(ReceiveData.indexOf(" HTTP")!=-1)) - { - CID=String(ReceiveData.charAt(ReceiveData.indexOf("+IPD,")+5)); - Feedback(CID,"Hello World",3); - } - - /* - if (SensorValue>LimitValue) - { - cmd="yourcmd"; - str1="yourstr1"; - str2="yourstr2"; - str3="yourstr3"; - ... - str9="yourstr9"; - ExecuteCommand(); - delay(10000); - } - */ -} - -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! - Expanding Arduino Serial Port Buffer Size - https://internetofhomethings.com/homethings/?p=927 - If you change buffer size to 256, response length must be less than or equal to 126? - */ - if (datatype==0) - { - Response=""+Response+""; - } - else if (datatype==1) - { - Response=""+Response+""; - } - 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())); - ReceiveData=ReceiveData+mySerial.readStringUntil('\r'); - } - //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); - } - } -} diff --git a/ESP01_Server.ino b/ESP01_Server.ino deleted file mode 100644 index a4383f82c..000000000 --- a/ESP01_Server.ino +++ /dev/null @@ -1,316 +0,0 @@ -/* -Arduino Uno(Uart) + ESP8266 ESP-01 (1MB Flash) - -Author : ChungYi Fu (Taiwan) 2018-04-20 22:00 - -Update AT Firmware(V2.0_AT_Firmware) -https://www.youtube.com/watch?v=QVhWVu8NnZc -http://www.electrodragon.com/w/File:V2.0_AT_Firmware(ESP).zip - -nodemcu-flasher -https://github.com/nodemcu/nodemcu-flasher -(Baudrate:115200, Flash size:1MByte, Flash speed:26.7MHz, SPI Mode:QIO) - -Expanding Arduino Serial Port Buffer Size -https://internetofhomethings.com/homethings/?p=927 - -Command Format : -http://APIP/?cmd=str1;str2;str3;str4;str5;str6;str7;str8;str9 - -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/?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 -*/ - -String apssid = "MyFirmata ESP01"; -String appassword = "12345678"; //AP password require at least 8 characters. -String APIP="192.168.4.1"; -String APMAC="",CID=""; - -#include -SoftwareSerial mySerial(10, 11); // Arduino RX:10, TX:11 - -String ReceiveData="", command="",cmd="",str1="",str2="",str3="",str4="",str5="",str6="",str7="",str8="",str9=""; - -void setup() -{ - Serial.begin(9600); - - //You must change baud rate to 9600. - mySerial.begin(115200); //Default baud rate -> 19200,38400,57600,74880,115200 - SendData("AT+UART_CUR=9600,8,1,0,0",2000); //Change baud rate to 9600 - mySerial.begin(9600); //you will get more stable data without junk chars. - mySerial.setTimeout(10); - - SendData("AT+CWMODE_CUR=2",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=\""+APIP+"\"",2000); - SendData("AT+CWSAP_CUR=\""+apssid+"\",\""+appassword+"\",3,4",2000); - getAPIP(); -} - -void loop() -{ - getCommand(); - - if ((ReceiveData.indexOf("/?")!=-1)&&(ReceiveData.indexOf(" HTTP")!=-1)) - { - CID=String(ReceiveData.charAt(ReceiveData.indexOf("+IPD,")+5)); - 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,"It can't work!Check command length.",3); - } - } - else if ((ReceiveData.indexOf("/?")==-1)&&(ReceiveData.indexOf(" HTTP")!=-1)) - { - CID=String(ReceiveData.charAt(ReceiveData.indexOf("+IPD,")+5)); - Feedback(CID,"Hello World",3); - } - - /* - if (SensorValue>LimitValue) - { - cmd="yourcmd"; - str1="yourstr1"; - str2="yourstr2"; - str3="yourstr3"; - ... - str9="yourstr9"; - ExecuteCommand(); - delay(10000); - } - */ -} - -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 (cmd=="yourcmd") - { - //you can do anything - - //Feedback(CID,""+cmd+"="+str1+";"+str2+";"+str3+"",0); --> HTML - //Feedback(CID,cmd+"="+str1+";"+str2+";"+str3,1); --> XML - //Feedback(CID,cmd+"="+str1+";"+str2+";"+str3,2); --> JSON - //Feedback(CID,""+cmd+"="+str1+";"+str2+";"+str3+"",3); --> Custom definition - } - else if (cmd=="ip") - { - Feedback(CID,"APIP: "+APIP+"",3); - } - else if (cmd=="mac") - { - Feedback(CID,"APMAC: "+APMAC+"",3); - } - else if (cmd=="restart") - { - Feedback(CID,""+command+"",3); - delay(3000); - SendData("AT+RST",2000); - delay(2000); - setup(); - } - else if (cmd=="at") // ?cmd=str1 -> ?at=AT+RST - { - Feedback(CID,""+WaitReply(3000)+"",3); - delay(1000); - mySerial.println(str1); - mySerial.flush(); - } - else if (cmd=="inputpullup") - { - pinMode(str1.toInt(), INPUT_PULLUP); - Feedback(CID,""+command+"",3); - } - else if (cmd=="pinmode") - { - pinMode(str1.toInt(), str2.toInt()); - Feedback(CID,""+command+"",3); - } - else if (cmd=="digitalwrite") - { - pinMode(str1.toInt(), OUTPUT); - digitalWrite(str1.toInt(),str2.toInt()); - Feedback(CID,""+command+"",3); - } - else if (cmd=="digitalread") - { - Feedback(CID,""+String(digitalRead(str1.toInt()))+"",3); - } - else if (cmd=="analogwrite") - { - pinMode(str1.toInt(), OUTPUT); - analogWrite(str1.toInt(),str2.toInt()); - Feedback(CID,""+command+"",3); - } - else if (cmd=="analogread") - { - Feedback(CID,""+String(analogRead(str1.toInt()))+"",3); - } - else - { - Feedback(CID,"Command is not defined",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! - Expanding Arduino Serial Port Buffer Size - https://internetofhomethings.com/homethings/?p=927 - If you change buffer size to 256, response length must be less than or equal to 126? - */ - if (datatype==0) - { - Response=""+Response+""; - } - else if (datatype==1) - { - Response=""+Response+""; - } - 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())); - ReceiveData=ReceiveData+mySerial.readStringUntil('\r'); - } - //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); - } -} - -void getAPIP() -{ - APIP="";APMAC=""; - int apipreadstate=0,apmacreadstate=0,j=0; - mySerial.println("AT+CIFSR"); - mySerial.flush(); - long int StartTime=millis(); - while( (StartTime+2000) > millis()) - { - 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 (APMAC!="") Serial.println("APIP: "+APIP+"\nAPMAC: "+APMAC); -} - diff --git a/ESP01_Station.ino b/ESP01_Station.ino deleted file mode 100644 index 88b453e7b..000000000 --- a/ESP01_Station.ino +++ /dev/null @@ -1,164 +0,0 @@ -/* -Arduino Uno(Uart) + ESP8266 ESP-01 (1MB Flash) - -Author : ChungYi Fu (Taiwan) 2018-04-22 22:00 - -Update AT Firmware(V2.0_AT_Firmware) -https://www.youtube.com/watch?v=QVhWVu8NnZc -http://www.electrodragon.com/w/File:V2.0_AT_Firmware(ESP).zip - -nodemcu-flasher -https://github.com/nodemcu/nodemcu-flasher -(Baudrate:115200, Flash size:1MByte, Flash speed:26.7MHz, SPI Mode:QIO) - -Expanding Arduino Serial Port Buffer Size -https://internetofhomethings.com/homethings/?p=927 -*/ - -String WIFI_SSID = ""; //your network SSID -String WIFI_PWD = ""; //your network password - -#include -SoftwareSerial mySerial(10, 11); // Arduino RX:10, TX:11 - -String ReceiveData="",STAIP="",STAMAC=""; - -void setup() -{ - Serial.begin(9600); - - //You must change uart baud rate of ESP-01 to 9600. - mySerial.begin(115200); //Default uart baud rate - SendData("AT+UART_CUR=9600,8,1,0,0",2000); //Change uart baud rate to 9600 - mySerial.begin(9600); // you will get more stable data. - mySerial.setTimeout(10); - - SendData("AT+CIPSERVER=0",2000); - SendData("AT+CWMODE_CUR=1",2000); - SendData("AT+CIPMUX=0",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() -{ - if (STAIP=="") - { - getSTAIP(); - - if (STAIP!="") - { - pinMode(13,1); - for (int i=0;i<20;i++) - { - digitalWrite(13,1); - delay(50); - digitalWrite(13,0); - delay(50); - } - } - } - else - { - int SensorData = rand()%100; //humidity - - //Send sensor data to web page(php...) and save data to database - String Domain="192.168.201.10"; - /* - 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 126? - */ - String request = "GET /?humidity="+String(SensorData)+" HTTP/1.1\r\nHost: "+Domain+"\r\n\r\n"; - - SendData("AT+CIPSTART=\"TCP\",\""+Domain+"\",80", 4000); - SendData("AT+CIPSEND=" + String(request.length()+2), 2000); - SendData(request, 4000); - delay(1); - SendData("AT+CIPCLOSE",2000); - - Serial.println(SensorData); - delay(5000); - } -} - -void SendData(String data,int TimeLimit) -{ - mySerial.println(data); - mySerial.flush(); - delay(10); - WaitReply(TimeLimit); -} - -String WaitReply(long int TimeLimit) -{ - ReceiveData=""; - long int StartTime=millis(); - while( (StartTime+TimeLimit) > millis()) - { - if (mySerial.available()) - { - delay(4); - while(mySerial.available()) - { - //ReceiveData=ReceiveData+String(char(mySerial.read())); - ReceiveData=ReceiveData+mySerial.readStringUntil('\r'); - } - Serial.println(ReceiveData); - if (ReceiveData.indexOf("OK")!=-1) return ReceiveData; - } - } - return ReceiveData; -} - -void getSTAIP() -{ - ReceiveData=""; - - if (mySerial.available()) - { - while (mySerial.available()) - { - char c=mySerial.read(); - ReceiveData=ReceiveData+String(c); - } - - if (ReceiveData.indexOf("WIFI GOT IP")!=-1) - { - while(!mySerial.find('OK')){} - delay(10); - - int staipreadstate=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) - staipreadstate=1; - else if (j==2) - staipreadstate=0; - if ((staipreadstate==1)&&(t!="\"")) STAIP=STAIP+t; - - if (j==3) - stamacreadstate=1; - else if (j==4) - stamacreadstate=0; - if ((stamacreadstate==1)&&(t!="\"")) STAMAC=STAMAC+t; - - delay(1); - } - Serial.println("STAIP: "+STAIP+"\nSTAMAC: "+STAMAC+"\n"); - } - } -} diff --git a/ESP01_Station_IFTTT_ThinkSpeak.ino b/ESP01_Station_IFTTT_ThinkSpeak.ino deleted file mode 100644 index 767b61366..000000000 --- a/ESP01_Station_IFTTT_ThinkSpeak.ino +++ /dev/null @@ -1,192 +0,0 @@ -/* -Arduino Uno(Uart) + ESP8266 ESP-01 (1MB Flash) - -Author : ChungYi Fu (Taiwan) 2018-04-24 17:00 - -Update AT Firmware(V2.0_AT_Firmware) -https://www.youtube.com/watch?v=QVhWVu8NnZc -http://www.electrodragon.com/w/File:V2.0_AT_Firmware(ESP).zip - -nodemcu-flasher -https://github.com/nodemcu/nodemcu-flasher -(Baudrate:115200, Flash size:1MByte, Flash speed:26.7MHz, SPI Mode:QIO) - -Expanding Arduino Serial Port Buffer Size -https://internetofhomethings.com/homethings/?p=927 -*/ - -String WIFI_SSID = ""; //your network SSID -String WIFI_PWD = ""; //your network password - -#include -SoftwareSerial mySerial(10, 11); // Arduino RX:10, TX:11 - -String ReceiveData="",STAIP="",STAMAC=""; - -void setup() -{ - Serial.begin(9600); - - //You must change baud rate to 9600. - mySerial.begin(115200); //Default baud rate - SendData("AT+UART_CUR=9600,8,1,0,0",2000); //Change baud rate to 9600 - mySerial.begin(9600); // you will get more stable data without junk chars. - mySerial.setTimeout(10); - - SendData("AT+CIPSERVER=0",2000); - SendData("AT+CWMODE_CUR=1",2000); - SendData("AT+CIPMUX=0",2000); - - if (WIFI_SSID!="") - SendData("AT+CWJAP_CUR=\""+WIFI_SSID+"\",\""+WIFI_PWD+"\"",5000); - else - Serial.print("Please check your network SSID and password settings"); -} - -void loop() -{ - if (STAIP=="") - { - getSTAIP(); - - if (STAIP!="") - { - pinMode(13,1); - for (int i=0;i<20;i++) - { - digitalWrite(13,1); - delay(50); - digitalWrite(13,0); - delay(50); - } - } - } - else - { - //Sensor Data - int SensorTemperature = rand()%40; - int SensorHumidity = rand()%100; - - //ThingSpeak - String domain="api.thingspeak.com"; - String key="xxxxxxxxxxxxxxxxxxxx"; - String field1=String(SensorTemperature); - String field2=String(SensorHumidity); - /* - 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 bytes, request length must be less than or equal to 128. - */ - String request="GET /update?api_key="+key+"&field1="+field1+"&field2="+field2+" HTTP/1.1\r\nHost: "+domain+"\r\n\r\n"; - Serial.println(request); - - SendData("AT+CIPSTART=\"TCP\",\""+domain+"\",80", 4000); - SendData("AT+CIPSEND=" + String(request.length()+2), 2000); //Serial.println(request) -> request+"\r\n" -> request.length()+2 - SendData(request, 4000); - SendData("AT+CIPCLOSE",2000); - - if ((SensorTemperature<10)||(SensorHumidity<20)) - { - delay(1000); - //IFTTT - String domain="maker.ifttt.com"; - String event="xxxxxx"; - String key="xxxxxxxxxxxxxxxxxxxx"; - String value1=String(SensorTemperature); - String value2=String(SensorHumidity); - /* - 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 bytes, request length must be less than or equal to 128. - */ - request = "GET /trigger/"+event+"/with/key/"+key+"?value1="+value1+"&value2="+value2+" HTTP/1.1\r\nHost: "+domain+"\r\n\r\n"; - Serial.println(request); - - SendData("AT+CIPSTART=\"TCP\",\""+domain+"\",80", 4000); - SendData("AT+CIPSEND=" + String(request.length()+2), 2000); //Serial.println(request) -> request+"\r\n" -> request.length()+2 - SendData(request, 4000); - SendData("AT+CIPCLOSE",2000); - } - - delay(20000); // Time interval should be more than or equal to 15 seconds.(ThingSpeak) - } -} - -void SendData(String data,int TimeLimit) -{ - mySerial.println(data); - mySerial.flush(); - delay(10); - WaitReply(TimeLimit); -} - -String WaitReply(long int TimeLimit) -{ - ReceiveData=""; - long int StartTime=millis(); - while( (StartTime+TimeLimit) > millis()) - { - if (mySerial.available()) - { - delay(4); - while(mySerial.available()) - { - //ReceiveData=ReceiveData+String(char(mySerial.read())); - ReceiveData=ReceiveData+mySerial.readStringUntil('\r'); - } - Serial.println(ReceiveData); - if (ReceiveData.indexOf("OK")!=-1) return ReceiveData; - } - } - return ReceiveData; -} - -void getSTAIP() -{ - ReceiveData=""; - - if (mySerial.available()) - { - while (mySerial.available()) - { - char c=mySerial.read(); - ReceiveData=ReceiveData+String(c); - } - - if (ReceiveData.indexOf("WIFI GOT IP")!=-1) - { - while(!mySerial.find('OK')){} - delay(10); - - int staipreadstate=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) - staipreadstate=1; - else if (j==2) - staipreadstate=0; - if ((staipreadstate==1)&&(t!="\"")) STAIP=STAIP+t; - - if (j==3) - stamacreadstate=1; - else if (j==4) - stamacreadstate=0; - if ((stamacreadstate==1)&&(t!="\"")) STAMAC=STAMAC+t; - - delay(1); - } - Serial.println("STAIP: "+STAIP+"\nSTAMAC: "+STAMAC+"\n"); - } - } -} diff --git a/ESP12E_DFPlayer.ino b/ESP12E_DFPlayer.ino deleted file mode 100644 index a123bed47..000000000 --- a/ESP12E_DFPlayer.ino +++ /dev/null @@ -1,516 +0,0 @@ -/* -NodeMCU (ESP12E) + DFPlayer Mini MP3 - -Author : ChungYi Fu (Taiwan) 2018-4-19 20:00 - -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 -*/ - -#include - -const char* ssid = ""; //your network SSID -const char* password = ""; //your network password - -const char* apssid = "MyFirmata ESP12E"; -const char* appassword = "12345678"; //AP password require at least 8 characters. - -WiFiServer server(80); - -#include "Arduino.h" -#include "SoftwareSerial.h" -#include "DFRobotDFPlayerMini.h" - -SoftwareSerial mySoftwareSerial(13, 15); // RX(D7), TX(D8) -DFRobotDFPlayerMini myDFPlayer; -void printDetail(uint8_t type, int value); - -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() -{ - 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(""); - - myDFPlayer.pause(); - delay(300); - - if (cmd=="your cmd") - { - // You can do anything - // Feedback="Hello World"; - } - 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 if (cmd=="volume") - { - if (str1.toInt()>30) - str1="30"; - else if (str1.toInt()<0) - str1="0"; - myDFPlayer.volume(str1.toInt()); - - Feedback=Command; - } - else if (cmd=="volumeUp") - { - myDFPlayer.volumeUp(); - Feedback=Command; - } - else if (cmd=="volumeDown") - { - myDFPlayer.volumeDown(); - Feedback=Command; - } - else if (cmd=="EQ") - { - if (str1=="NORMAL") - myDFPlayer.EQ(DFPLAYER_EQ_NORMAL); - else if (str1=="POP") - myDFPlayer.EQ(DFPLAYER_EQ_POP); - else if (str1=="ROCK") - myDFPlayer.EQ(DFPLAYER_EQ_ROCK); - else if (str1=="JAZZ") - myDFPlayer.EQ(DFPLAYER_EQ_JAZZ); - else if (str1=="CLASSIC") - myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC); - else if (str1=="BASS") - myDFPlayer.EQ(DFPLAYER_EQ_BASS); - - Feedback=Command; - } - else if (cmd=="DEVICE") - { - if (str1=="U_DISK") - myDFPlayer.outputDevice(DFPLAYER_DEVICE_U_DISK); - else if (str1=="SD") - myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD); - else if (str1=="AUX") - myDFPlayer.outputDevice(DFPLAYER_DEVICE_AUX); - else if (str1=="SLEEP") - myDFPlayer.outputDevice(DFPLAYER_DEVICE_SLEEP); - else if (str1=="FLASH") - myDFPlayer.outputDevice(DFPLAYER_DEVICE_FLASH); - - Feedback=Command; - } - else if (cmd=="sleep") - { - myDFPlayer.sleep(); - Feedback=Command; - } - else if (cmd=="reset") - { - myDFPlayer.reset(); - Feedback=Command; - } - else if (cmd=="enableDAC") - { - myDFPlayer.enableDAC(); - Feedback=Command; - } - else if (cmd=="disableDAC") - { - myDFPlayer.disableDAC(); - Feedback=Command; - } - else if (cmd=="outputSetting") - { - myDFPlayer.outputSetting(str1.toInt(), str2.toInt()); - Feedback=Command; - } - else if (cmd=="next") - { - myDFPlayer.next(); - Feedback=Command; - } - else if (cmd=="previous") - { - myDFPlayer.previous(); - Feedback=Command; - } - else if (cmd=="play") - { - myDFPlayer.play(str1.toInt()); - Feedback=Command; - } - else if (cmd=="loop") - { - myDFPlayer.loop(str1.toInt()); - Feedback=Command; - } - else if (cmd=="pause") - { - myDFPlayer.pause(); - Feedback=Command; - } - else if (cmd=="start") - { - myDFPlayer.start(); - Feedback=Command; - } - else if (cmd=="playFolder") - { - myDFPlayer.playFolder(str1.toInt(), str2.toInt()); - Feedback=Command; - } - else if (cmd=="enableLoopAll") - { - myDFPlayer.enableLoopAll(); - Feedback=Command; - } - else if (cmd=="disableLoopAll") - { - myDFPlayer.disableLoopAll(); - Feedback=Command; - } - else if (cmd=="playMp3Folder") - { - myDFPlayer.playMp3Folder(str1.toInt()); - Feedback=Command; - } - else if (cmd=="advertise") - { - myDFPlayer.advertise(str1.toInt()); - Feedback=Command; - } - else if (cmd=="stopAdvertise") - { - myDFPlayer.stopAdvertise(); - Feedback=Command; - } - else if (cmd=="playLargeFolder") - { - myDFPlayer.playLargeFolder(str1.toInt(), str2.toInt()); - Feedback=Command; - } - else if (cmd=="loopFolder") - { - myDFPlayer.loopFolder(str1.toInt()); - Feedback=Command; - } - else if (cmd=="randomAll") - { - myDFPlayer.randomAll(); - Feedback=Command; - } - else if (cmd=="enableLoop") - { - myDFPlayer.enableLoop(); - Feedback=Command; - } - else if (cmd=="disableLoop") - { - myDFPlayer.disableLoop(); - Feedback=Command; - } - else - { - Feedback="Command is not defined"; - } - - delay(300); - myDFPlayer.start(); - delay(300); -} - -void setup() -{ - Serial.begin(115200); - delay(10); - - WiFi.mode(WIFI_AP_STA); - - WiFi.softAP(apssid, appassword); - - //WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0)); - - delay(1000); - Serial.println(""); - Serial.println("APIP address: "); - Serial.println(WiFi.softAPIP()); - - //WiFi.config(IPAddress(192, 168, 201, 100), IPAddress(192, 168, 201, 2), IPAddress(255, 255, 255, 0)); - - WiFi.begin(ssid, password); - - 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; - } - - Serial.println(""); - Serial.println("STAIP address: "); - Serial.println(WiFi.localIP()); - - server.begin(); - - - mySoftwareSerial.begin(9600); - - Serial.println(); - Serial.println(F("DFRobotDemo")); - Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)")); - - if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3. - Serial.println(F("Unable to begin:")); - Serial.println(F("1.Please recheck the connection!")); - Serial.println(F("2.Please insert the SD card!")); - } - else { - Serial.println(F("DFPlayer Mini online.")); - myDFPlayer.volume(10); //Set volume value. From 0 to 30 - - pinMode(2, OUTPUT); - for (int i=0;i<10;i++) - { - digitalWrite(2,LOW); - delay(100); - digitalWrite(2,HIGH); - delay(100); - } - } -} - -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) - { - Feedback+="

"; - Feedback+="
"; - Feedback+="cmd:"; - Feedback+=""; - Feedback+="

str1:"; - Feedback+=""; - Feedback+="

str2:"; - Feedback+=""; - Feedback+="

"; - Feedback+=""; - Feedback+="
"; - - 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(""); - client.println(""); - client.println(""); - client.println(""); - client.println(""); - client.println(""); - client.println(Feedback); - client.println(""); - 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; - } -} - -/* -if (myDFPlayer.available()) { - printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states. -} -*/ - -void printDetail(uint8_t type, int value){ - switch (type) { - case TimeOut: - Serial.println(F("Time Out!")); - break; - case WrongStack: - Serial.println(F("Stack Wrong!")); - break; - case DFPlayerCardInserted: - Serial.println(F("Card Inserted!")); - break; - case DFPlayerCardRemoved: - Serial.println(F("Card Removed!")); - break; - case DFPlayerCardOnline: - Serial.println(F("Card Online!")); - break; - case DFPlayerPlayFinished: - Serial.print(F("Number:")); - Serial.print(value); - Serial.println(F(" Play Finished!")); - break; - case DFPlayerError: - Serial.print(F("DFPlayerError:")); - switch (value) { - case Busy: - Serial.println(F("Card not found")); - break; - case Sleeping: - Serial.println(F("Sleeping")); - break; - case SerialWrongStack: - Serial.println(F("Get Wrong Stack")); - break; - case CheckSumNotMatch: - Serial.println(F("Check Sum Not Match")); - break; - case FileIndexOut: - Serial.println(F("File Index Out of Bound")); - break; - case FileMismatch: - Serial.println(F("Cannot Find File")); - break; - case Advertise: - Serial.println(F("In Advertise")); - break; - default: - break; - } - break; - default: - break; - } -} diff --git a/ESP12E_MyFirmata.ino b/ESP12E_MyFirmata.ino deleted file mode 100644 index f0d1da0c4..000000000 --- a/ESP12E_MyFirmata.ino +++ /dev/null @@ -1,392 +0,0 @@ -/* -NodeMCU (ESP12E) - -Author : ChungYi Fu (Taiwan) 2018-04-04 07:00 - -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 -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 -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 - -STAIP: -Query: http://192.168.4.1/?ip -Link:http://192.168.4.1/?resetwifi=ssid;password - -Control Page (http) -https://github.com/fustyles/webduino/blob/master/ESP8266_MyFirmata.html -*/ - -#include - -const char* ssid = ""; //your network SSID -const char* password = ""; //your network password - -const char* apssid = "MyFirmata ESP12E"; -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() -{ - 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="Hello World"; - } - 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 if (cmd=="inputpullup") - { - pinMode(str1.toInt(), INPUT_PULLUP); - Feedback=Command; - } - else if (cmd=="pinmode") - { - if (str2.toInt()==1) - pinMode(str1.toInt(), OUTPUT); - else - pinMode(str1.toInt(), INPUT); - Feedback=Command; - } - else if (cmd=="digitalwrite") - { - pinMode(str1.toInt(), OUTPUT); - digitalWrite(str1.toInt(), str2.toInt()); - Feedback=Command; - } - else if (cmd=="digitalread") - { - Feedback=String(digitalRead(str1.toInt())); - } - else if (cmd=="analogwrite") - { - pinMode(str1.toInt(), OUTPUT); - analogWrite(str1.toInt(), str2.toInt()); - Feedback=Command; - } - else if (cmd=="analogread") - { - Feedback=String(analogRead(str1.toInt())); - } - else if (cmd=="tcp") - { - String domain=str1; - String request ="/" + str3; - int port=str2.toInt(); - tcp(domain,request,port); - } - else if (cmd=="ifttt") - { - String domain="maker.ifttt.com"; - String request = "/trigger/" + str1 + "/with/key/" + str2; - request += "?value1="+str3+"&value2="+str4+"&value3="+str5; - tcp(domain,request,80); - } - 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; - tcp(domain,request,80); - } - else - { - Feedback="Command is not defined"; - } -} - -void setup() -{ - Serial.begin(115200); - delay(10); - - WiFi.mode(WIFI_AP_STA); - - WiFi.softAP(apssid, appassword); - - //WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0)); - - delay(1000); - Serial.println(""); - Serial.println("APIP address: "); - Serial.println(WiFi.softAPIP()); - - //WiFi.config(IPAddress(192, 168, 201, 100), IPAddress(192, 168, 201, 2), IPAddress(255, 255, 255, 0)); - - WiFi.begin(ssid, password); - - 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.localIP().toString()!="0.0.0.0") - { - pinMode(2, OUTPUT); - for (int i=0;i<5;i++) - { - digitalWrite(2,HIGH); - delay(100); - digitalWrite(2,LOW); - delay(100); - } - } - - Serial.println(""); - Serial.println("STAIP address: "); - Serial.println(WiFi.localIP()); - - server.begin(); -} - -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(""); - client.println(""+Feedback+""); - client.println(); - */ - - Feedback+="

"; - Feedback+="
"; - Feedback+="cmd:"; - Feedback+=""; - Feedback+="

str1:"; - Feedback+=""; - Feedback+="

str2:"; - Feedback+=""; - Feedback+="

str3:"; - Feedback+=""; - Feedback+="
(str3;str4;str5;str6;str7;str8;str9)

"; - Feedback+=""; - Feedback+="
"; - - 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(""); - client.println(""); - client.println(""); - client.println(""); - client.println(""); - client.println(""); - client.println(Feedback); - client.println(""); - 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(); - } - - //if (SensorValue>LimitValue) - //{ - // cmd="yourcmd"; - // str1="yourstr1"; - // str2="yourstr2"; - // str3="yourstr3"; - // ... - // str9="yourstr9"; - // 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))) 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; - } -} - -void tcp(String domain,String request,int port) -{ - WiFiClient client_tcp; - - if (client_tcp.connect(domain, port)) - { - Serial.println("GET " + request); - client_tcp.println("GET " + request + " HTTP/1.1"); - client_tcp.print("Host: "); - client_tcp.println(domain); - client_tcp.println("Connection: close"); - client_tcp.println(); - - long StartTime = millis(); - while ((StartTime+5000) > millis()) - { - while (client_tcp.available()) - { - char c = client_tcp.read(); - if (c == '\n') - { - if (Feedback.length() == 0) - break; - else - Feedback = ""; - } - else if (c != '\r') - Feedback += c; - } - if (Feedback.length()!= 0) break; - } - client_tcp.stop(); - } - else - Feedback="Connection failed"; -} diff --git a/ESP32_MyFirmata.ino b/ESP32_MyFirmata.ino deleted file mode 100644 index bec055ac1..000000000 --- a/ESP32_MyFirmata.ino +++ /dev/null @@ -1,400 +0,0 @@ -/* -NodeMCU (ESP32) - -Author : ChungYi Fu (Taiwan) 2018-04-04 07:00 - -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 -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/?touchread=pin -http://192.168.4.1/?tcp=domain;port;request -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 - -STAIP: -Query: http://192.168.4.1/?ip -Link:http://192.168.4.1/?resetwifi=ssid;password - -Control Page (http) -https://github.com/fustyles/webduino/blob/master/ESP8266_MyFirmata.html -*/ - -#include - -const char* ssid = ""; //your network SSID -const char* password = ""; //your network password - -const char* apssid = "MyFirmata ESP32"; -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() -{ - 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="Hello World"; - } - 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 if (cmd=="inputpullup") - { - pinMode(str1.toInt(), INPUT_PULLUP); - Feedback=Command; - } - else if (cmd=="pinmode") - { - if (str2.toInt()==1) - pinMode(str1.toInt(), OUTPUT); - else - pinMode(str1.toInt(), INPUT); - Feedback=Command; - } - else if (cmd=="digitalwrite") - { - ledcDetachPin(str1.toInt()); - pinMode(str1.toInt(), OUTPUT); - digitalWrite(str1.toInt(), str2.toInt()); - Feedback=Command; - } - else if (cmd=="digitalread") - { - Feedback=String(digitalRead(str1.toInt())); - } - else if (cmd=="analogwrite") - { - ledcAttachPin(str1.toInt(), 1); - ledcSetup(1, 5000, 8); - ledcWrite(1,str2.toInt()); - Feedback=Command; - } - else if (cmd=="analogread") - { - Feedback=String(analogRead(str1.toInt())); - } - else if (cmd=="touchread") - { - Feedback=String(touchRead(str1.toInt())); - } - else if (cmd=="tcp") - { - String domain=str1; - String request ="/" + str3; - int port=str2.toInt(); - tcp(domain,request,port); - } - else if (cmd=="ifttt") - { - String domain="maker.ifttt.com"; - String request = "/trigger/" + str1 + "/with/key/" + str2; - request += "?value1="+str3+"&value2="+str4+"&value3="+str5; - tcp(domain,request,80); - } - 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; - tcp(domain,request,80); - } - else - { - Feedback="Command is not defined"; - } -} - -void setup() -{ - Serial.begin(115200); - delay(10); - - WiFi.mode(WIFI_AP_STA); - - WiFi.softAP(apssid, appassword); - - //WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0)); - - delay(1000); - Serial.println(""); - Serial.println("APIP address: "); - Serial.println(WiFi.softAPIP()); - - //WiFi.config(IPAddress(192, 168, 201, 100), IPAddress(192, 168, 201, 2), IPAddress(255, 255, 255, 0)); - - WiFi.begin(ssid, password); - - 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.localIP().toString()!="0.0.0.0") - { - pinMode(2, OUTPUT); - for (int i=0;i<5;i++) - { - digitalWrite(2,HIGH); - delay(100); - digitalWrite(2,LOW); - delay(100); - } - } - - Serial.println(""); - Serial.println("STAIP address: "); - Serial.println(WiFi.localIP()); - - server.begin(); -} - -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(""); - client.println(""+Feedback+""); - client.println(); - */ - - Feedback+="

"; - Feedback+="
"; - Feedback+="cmd:"; - Feedback+=""; - Feedback+="

str1:"; - Feedback+=""; - Feedback+="

str2:"; - Feedback+=""; - Feedback+="

str3:"; - Feedback+=""; - Feedback+="
(str3;str4;str5;str6;str7;str8;str9)

"; - Feedback+=""; - Feedback+="
"; - - 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(""); - client.println(""); - client.println(""); - client.println(""); - client.println(""); - client.println(""); - client.println(Feedback); - client.println(""); - 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(); - } - /* - if (SensorValue>LimitValue) - { - cmd="yourcmd"; - str1="yourstr1"; - str2="yourstr2"; - str3="yourstr3"; - ... - str9="yourstr9"; - 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))) 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; - } -} - -void tcp(String domain,String request,int port) -{ - WiFiClient client_tcp; - - if (client_tcp.connect(domain.c_str(), port)) - { - Serial.println("GET " + request); - client_tcp.println("GET " + request + " HTTP/1.1"); - client_tcp.print("Host: "); - client_tcp.println(domain); - client_tcp.println("Connection: close"); - client_tcp.println(); - - long StartTime = millis(); - while ((StartTime+5000) > millis()) - { - while (client_tcp.available()) - { - char c = client_tcp.read(); - if (c == '\n') - { - if (Feedback.length() == 0) - break; - else - Feedback = ""; - } - else if (c != '\r') - Feedback += c; - } - if (Feedback.length()!= 0) break; - } - client_tcp.stop(); - } - else - Feedback="Connection failed"; -} diff --git a/ESP8266_MyFirmata.html b/ESP8266_MyFirmata.html deleted file mode 100644 index 2233bcc12..000000000 --- a/ESP8266_MyFirmata.html +++ /dev/null @@ -1,96 +0,0 @@ - - - - - - - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - -
ESP8266  IP:
Command
- - -
- - -
str1:
str2:
str3:
         
If request length is too long, ESP-01 can't work.
-
-Format:
-http://APIP/?cmd=str1;str2;str3;~~~;str7;str8;str9
-http://STAIP/?cmd=str1;str2;str3;~~~;str7;str8;str9 - -