2016年9月2日 星期五

ESP8266 WIFI(5) -Set LED ON/OFF from WEB browser


 這篇文章介紹如何在瀏覽器透過ESP8266 wifi連線控制Arduino上的LED 開關.


<圖1>概念圖,非完整接線圖


<圖2>實際工作狀態



首先請先參閱之前的ESP8266介紹完成基本接線及測試,

ESP8266 Wifi (1)---Connected with PC.

http://arbu00.blogspot.tw/2015/12/esp8266-wifi-1-connected-with-pc.html
ESP8266 WIFI(2) -update firmware and modify baud rate.

http://arbu00.blogspot.tw/2015/12/esp8266-wifi4-soilhumidity-sensor.html


影音筆記完整示範說明:






<圖3>打開瀏覽器在位址列輸入ESP8266 連上的IP:192.168.xxx.xxx及Port :9000
           帶入/?pin=底下參數即可控制LED 開關.
           *1./?pin=參數  為自定義,只要能讓Arduino程式可以從字串解析出涵義即可
           *2.IP:192.168.xxx.xxx 為ESP8266連上家中無線AP時得到的IP,數值會不固定
                請依自己的實際狀況設定
            *3 port =9000 為自定義,需與ESP8266 的port設定一致

<圖4>將圖3在位址列的指令格式透過JAVAScript & JQuery 實現GET request method 
          做成一個HTML檔,請參閱 <圖五>HTML source code.
          底下按下按鈕Arduino 端的LED 即會做出相對應動作.



 <圖五>HTML source code.實作出6個按鈕.透過JAVAScript & JQuery 實現GET request method. 


<圖6-1>在Arduino IDE serial監控視窗查看ESP8266連線狀態

<圖6-2>在Arduino IDE serial監控視窗查看ESP8266連線狀態




<Arduino 完整範例 source code>

/*=========================================================
 * ESP8266 example To set LED ON/OFF
 * 阿布拉機
 * http://arbu00.blogspot.tw/
 * 
 * Ashing tsai
 * 2016/09/01
 =========================================================*/
#include 
#include "Timer.h"
SoftwareSerial WifiSerial(2,3); /* RX:D2, TX:D3 */
Timer T1;
#define SSID        "Buffalo-G-2.4" 
//#define SSID_2        "Xperia C4" 
#define PASSWORD    "guitar100"
String cmdAT = "";
String data = ""; 
String StrWebMsg="";
int Wifi_State,delaytime;
byte connID;
String WebMsgIPD="";
int GetPin=0,GetPinH=0,GetPinL=0;
int PinState=0;
void setup(void)
{
      WifiSerial.begin(9600);
      Serial.begin(9600);
      Serial.print("setup begin\r\n");
      pinMode(10,OUTPUT);
      digitalWrite(10,LOW);
      pinMode(11,OUTPUT);
      digitalWrite(11,LOW);
      pinMode(12,OUTPUT);
      digitalWrite(12,LOW);
      
      T1.every(5, ESP8266_Main);

        //1.Reset esp8266
        // cmdAT ="AT+RST";
        // WifiSerial.println(cmdAT);
        sendATcmd("AT+RST",2000); // 重置ESP模組,等待2秒

        //2.TEST ESP8266
           for (int i=0;i<=10;i++){
              cmdAT ="AT";
              WifiSerial.println(cmdAT);
           if (WifiSerial.find("OK")) {  
            Serial.print("Start+++++++++++++++++++++++++++++++++++\r\n");
            Serial.print("ESP8266 Operation ok\r\n");
            break;
           } else {
            Serial.print("ESP8266 Operation error\r\n");
           }
          }
        //3.把ESP8266 的 Wifi連線模式設成station模式:
          for (int i=0;i<=10;i++){
              cmdAT ="AT+CWMODE=1";
              WifiSerial.println(cmdAT);
           if (WifiSerial.find("OK")) {  
              Serial.print("Operate to station ok\r\n");
            break;
           } else {
             Serial.print("Operate to station err\r\n");
           }
          }
         //4.連線到家裡的Wifi網路得到IP adr:
         //AT+CWJAP="Buffalo-G-2.4","guitar100"
            for (int i=0;i<=10;i++){
         cmdAT="AT+CWJAP=\"";
         cmdAT+=SSID;
         cmdAT+="\",\"";
         cmdAT+=PASSWORD;
         cmdAT+="\"";
              WifiSerial.println(cmdAT);
           if (WifiSerial.find("OK")) {  
              Serial.print("Join AP success \r\n");
              sendATcmd("AT+CIFSR",1000);  //AT+CIFSR     #印出 IP 狀態
            break;
           } else {
             Serial.print("Try to Connectto AP,Loading...\r\n");
           }
          }
  
         //5.把「多重連線」打開
         for (int i=0;i<=10;i++){
              cmdAT ="AT+CIPMUX=1";
              WifiSerial.println(cmdAT);
           if (Loding("AT+CWMODE=1")){  
              Serial.print("Set AT+CIPMUX=1 success\r\n");
            break;
           } else {
              Serial.print("Set AT+CIPMUX=1  failure\r\n");
           }
          }

          //6.讓 ESP8266進入伺服器模式,等候其他裝置與它連線:
           for (int i=0;i<=10;i++){
               cmdAT ="AT+CIPSERVER=1,9000";// 在9000埠啟動伺服器
               WifiSerial.println(cmdAT);
           if (Loding("AT+CIPSERVER=1")){  
              Serial.print("Set port 9000  success\r\n");
              Serial.print("ESP8266 configuration Done!\r\n");
              Serial.print("End====================================\r\n");
            break;
           } else {
              Serial.print("Set port 9000  failure\r\n");
              Serial.print("End====================================\r\n");
           }
          }
          //-END-Set to serve mode and connect to Home AP.

}
void loop() {
T1.update();
//chat();
}
void ESP8266_Main(){
     int Debugflag=0;
     if(WifiSerial.available()) {
          StrWebMsg="";
               //Just Debug  for get all message from client
               if (Debugflag==1) {
                   while(WifiSerial.available())
                  {
                   StrWebMsg+=(char)WifiSerial.read(); 
                   delay(2);
                  }
                   Serial.println(StrWebMsg);
                    connID=0;
                    cmdAT = "AT+CIPCLOSE=";
                    cmdAT+=connID;   // 附加連線編號
                    sendATcmd(cmdAT,1000);  // 送出「中斷連線」命令
                    connID=1;
                    cmdAT = "AT+CIPCLOSE=";
                    cmdAT+=connID;   // 附加連線編號
                    sendATcmd(cmdAT,1000);  // 送出「中斷連線」命令
                    
               }else if (WifiSerial.find("+IPD,")) {  // 若接收到"+IPD,",代表有用戶連線了…
                 delay(2);
                 connID = WifiSerial.read();// 讀取連線編號(1~5)並轉成數字[最多 5 個連線] ,48 is ASCII '0'
                 delay(2);
                 connID = connID-48;  // 讀取連線編號(1~5)並轉成數字[最多 5 個連線] ,48 is ASCII '0'
                while(WifiSerial.available())
                 {
                 StrWebMsg+=(char)WifiSerial.read(); 
                 delay(2);
                }
                Serial.print("connID=");
                Serial.println(connID);
                Serial.println(StrWebMsg);
                
                GetPinH=(char)StrWebMsg[16]-48;   //48 is ASCII '0', Pin :十位數
                GetPinL=(char)StrWebMsg[17]-48;   //48 is ASCII '0', Pin :個位數
                GetPin=GetPinH*10+GetPinL;
                
                Serial.print("Pin=");
                Serial.println(GetPin);

                PinState=(char)StrWebMsg[18]-48;   //48 is ASCII '0'
                Serial.print("PinStatus=");
                Serial.println(PinState);
                digitalWrite(GetPin,PinState);
  
                    cmdAT = "AT+CIPCLOSE=";
                    cmdAT+=connID;   // 附加連線編號
                    sendATcmd(cmdAT,500);  // 送出「中斷連線」命令

           }
     }
  
}

void chat(void) {
 
  if (WifiSerial.available()) {
    Serial.write(WifiSerial.read());
  }
 
  if (Serial.available()) {
    char chars = Serial.read();
    WifiSerial.write(chars);
  }
}
String get_response() {  //get esp responce without "Serial.find()".
  String response="";  
  char c; 
  while (WifiSerial.available()) {  
    c=WifiSerial.read();
    response.concat(c);   
    delay(10);  
    }
  response.trim();  
  return response;
}
boolean Loding(String state){
  String response=get_response();
  for (int timeout=0 ; timeout<30 -1="" change="" if="" no="" ok="" response.indexof="" serial.print="" timeout="">=2){
          Serial.print("This commend waste ");
          Serial.print(timeout/2);
          Serial.println(" sec.");
        }
        else Serial.println("");
        return 1; 
        break;
    }
    else if(timeout==29){ //after 15 sec for wait esp without responce.
      Serial.print(state);
      Serial.println(" fail...\nExit2");
      return 0; 
    }
    else{
      response=get_response(); //reget
      if(timeout==0)
        Serial.print("Wifi Loading.");
      else
        Serial.print(".");
      delay(500);
    }
  }
}
void sendATcmd(String cmd, unsigned int time){
    String response = "";  // 接收ESP回應值的變數  
    WifiSerial.println(cmd); // 送出AT命令到ESP模組
    unsigned long timeout = time + millis();
    
    while (WifiSerial.available() || millis() < timeout) {
      while(WifiSerial.available()) {
        char c = WifiSerial.read(); // 接收ESP傳入的字元
        response += c;
      }
    }

    Serial.print(response);  // 顯示ESP的回應
}



加入阿布拉機的3D列印與機器人的FB專頁
https://www.facebook.com/arbu00/

Scan QRcode to join FB Fans.



沒有留言:

張貼留言