• 專注電子技術學習與研究
    當前位置:單片機教程網 >> Arduino >> 瀏覽文章

    arduino與andriod

    作者:未知   來源:互聯網   點擊數:  更新時間:2014年07月31日   【字體:

     剛玩了一下arduino與andriod手機的連接,非常棒?梢栽谑謾C上直接控制arduino了。具體的操作如下。

    本實驗需要一個arduino,一個ethernet shield,一個andriod的手機,一個無線路由器。
    在arduino端寫入程序:
    #include <SPI.h>
    #include <Ethernet.h>
    #define action_none -1
    #define action_out_all 0
    #define action_on_light 1
    #define action_off_light 2
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xBE }; //physical mac address
    byte ip[] = { 192, 168, 1, 222 };   // 填寫ip,設置arduino服務器地址,但要和路由器的地址一致
    byte gateway[] = { 192, 168, 1, 1 };   // 這是你的默認網關
    byte subnet[] = { 255, 255, 255, 0 };    //子網掩碼
    Server server(80);        //服務器端口
    String readString = String(30); //string for fetching data from address
    // arduino out
    int pinOutPlight = 4;//把led燈插到pin 4口
     
    // incoming GET command
    String r_pinOnLight = "GET /?out=4&status=1";
    String r_pinOffLight = "GET /?out=4&status=0";
    String r_out_all = "GET /?out=all";
    // current action
    int current_action;
    void setup(){
      //start Ethernet
      Ethernet.begin(mac, ip, gateway, subnet);
      delay(1000);
      pinMode(pinOutPlight, OUTPUT);
      digitalWrite(pinOutPlight, LOW);
      //enable serial datada print
      Serial.begin(9600);
      current_action = -1;
    }
    void loop(){
      current_action = -1;
      // Create a client connection
      Client client = server.available();
        if (client) {
     while (client.connected()) {
      if (client.available()) {
       char c = client.read();
       //read char by char HTTP request
       if (readString.length() < 30)
       {
         //store characters to string
         readString = readString + c;
       }
       //output chars to serial port
       //Serial.print(c);
       //if HTTP request has ended
       if (c == '\n') {
        Serial.print(readString);
        // ****************************************************
         if(readString.startsWith(r_pinOnLight))
         {
         Serial.print("\n ON UP \n");
         current_action = action_on_light;
         }
         else if(readString.startsWith(r_pinOffLight))
         {
          Serial.print("\n OFF UP \n");
          current_action = action_off_light;
         }
         else if(readString.startsWith(r_out_all))
         {
      Serial.print("\n ALL\n");
      current_action = action_out_all;
         }
         else
         {
      Serial.print("\n None \n");
      current_action = action_none;
         }
        // ****************************************************
         // now output HTML data starting with standart header
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();
        char buf[12];
        switch(current_action)
        {
        case action_out_all:
      client.print("{\"ip\" : \"192.168.1.222\", \"devices\" : [{ \"type\" : \"light\", \"name\" : \"LED\", \"out\" : \"");
      client.print(pinOutPlight);
      client.print("\"}");
      client.print("]}");
          break;
        case action_on_light:
          digitalWrite(pinOutPlight, HIGH);
          client.print("{\"status\" : \"1\" , \"out\" : \"");
          client.print(pinOutPlight);
          client.print("\"}");
          break;
        case action_off_light:
          digitalWrite(pinOutPlight, LOW);
          client.print("{\"status\" : \"0\" , \"out\" : \"");
          client.print(pinOutPlight);
          client.print("\"}");
          break;
        default:
          current_action = action_none;
        }
     
        // ****************************************************
         //clearing string for next read
         readString="";
         //stopping client
         client.stop();
       }
     }
        }
      }
    }
    ————————————————————————————————————————
    手機端安裝軟件domotichome,電子市場上有下載。
    copy完后,在手機上打開domotichome,點擊菜單鍵-settings,輸入剛才在sketch中設置的那個ip地址,我上面填的是192.168.1.222,下面的port當中填寫80,最后save,然后菜單鍵-sync,在界面上會出現light(1),點擊會有LED,左右兩邊有兩個鎖的圖標,一個控制開,一個控制關。
    關閉窗口

    相關文章

    国产农村精品一级毛片视频_亚洲Av无码专区国产乱码在线观看_亚洲综合国产Av_天天影视网天天综合色