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

    物聯網之遠程控制家電開關

    作者:小東   來源:轉自小東   點擊數:  更新時間:2014年05月30日   【字體:

       基于Dccduino uno 結合Dcc ethernet W5100擴張在yeelink物聯網平臺控制家電開關的例子,本教程實現的功能通過PC端網頁服務器和手機APP的控制一個LED.   

     

    硬件準備:


         軟件準備:
    1.安裝Arduino開發環境,如果您的電腦上沒有安裝Arduino開發環境,點擊這里學習如何安裝Arduino
    2.成為Yeelink開發者,如果您還沒有注冊,點擊這里注冊。
    3.下載Yeelink移動客戶端APP,點擊這里下載


    在yeelink平臺添加設備和傳感器
    1.進入用戶中心

    2.增加新設備





    3.增加傳感器




    下一步往DCCduino上傳代碼:

    #include
    #include
    #include
    #include

    byte buff[2];

    // for yeelink api
    #define APIKEY         "xxxxxxxxxxxxxxxxxxxx" // 此處替換為你自己的API KEY
    #define DEVICEID       7003 // 此處替換為你的設備編號
    #define SENSORID1       10926 // 此處替換為你的傳感器編號


    // assign a MAC address for the ethernet controller.
    byte mac[] = { 0x00, 0x1D, 0x72, 0x82, 0x35, 0x9D};
    // initialize the library instance:
    EthernetClient client ;
    char server[] = "api.yeelink.net";   // name address for yeelink API

    unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
    boolean lastConnected = false;                 // state of the connection last time through the main loop
    const unsigned long postingInterval = 3*1000; // delay between 2 datapoints, 30s
    String returnValue = "";
    boolean ResponseBegin = false;

    void setup() {
      pinMode(5, OUTPUT);
      Wire.begin();
      // start serial port:
     Serial.begin(57600);
     
      // start the Ethernet connection with DHCP:
      if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
        for(;;)
          ;
      }
      else {
        Serial.println("Ethernet configuration OK");
      }
    }

    void loop() {
      // if there's incoming data from the net connection.
      // send it out the serial port.  This is for debugging
      // purposes only:

      if (client.available()) {
        char c = client.read();
       // Serial.print(c);
          if (c == '{')
            ResponseBegin = true;
          else if (c == '}')
            ResponseBegin = false;

          if (ResponseBegin)
            returnValue += c;  
      }
      if (returnValue.length() !=0 && (ResponseBegin == false))
      {
        Serial.println(returnValue);
       
        if (returnValue.charAt(returnValue.length() - 1) == '1') {
          Serial.println("turn on the LED");
          digitalWrite(5, HIGH);
        }
          else if(returnValue.charAt(returnValue.length() - 1) == '0') {
          Serial.println("turn off the LED");
          digitalWrite(5, LOW);
        }
         returnValue = "";
      }
      // if there's no net connection, but there was one last time
      // through the loop, then stop the client:
      if (!client.connected() && lastConnected) {
        Serial.println();
        Serial.println("disconnecting.");
        client.stop();
      }
     
      // if you're not connected, and ten seconds have passed since
      // your last connection, then connect again and send data:
      if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
        // read sensor data, replace with your code
        //int sensorReading = readLightSensor();
        Serial.print("yeelink:");
        //get data from server 
        getData();
      }
      // store the state of the connection for next time through
      // the loop:
      lastConnected = client.connected();
    }

     

    // this method makes a HTTP connection to the server and get data back
    void getData(void) {
      // if there's a successful connection:
      if (client.connect(server, 80)) {
        Serial.println("connecting...");
        // send the HTTP GET request:
       
        client.print("GET /v1.0/device/");
        client.print(DEVICEID);
        client.print("/sensor/");
        client.print(SENSORID1);
        client.print("/datapoints");
        client.println(" HTTP/1.1");
        client.println("Host: api.yeelink.net");
        client.print("Accept: *");
        client.print("/");
        client.println("*");
        client.print("U-ApiKey: ");
        client.println(APIKEY);
        client.println("Content-Length: 0");
        client.println("Connection: close");
        client.println();
        Serial.println("print get done.");
       
      }
      else {
        // if you couldn't make a connection:
        Serial.println("connection failed");
        Serial.println();
        Serial.println("disconnecting.");
        client.stop();
      }
       // note the time that the connection was made or attempted:
      lastConnectionTime = millis();
    }



    如上圖接線,前提是保證你的網線是鏈接到路由器上面能上網的,LED的端口鏈接到數字端口5腳,按照設想我們是在PC端或者手機APP遠程控制這個LED,好了,切換
    我們的設備管理器,控制我們的開關:

    可以看到LED是受控了,也就說PC端能控制了。

    再轉化到我們的手機APP,打開yeelink應用,找到你的設備進行操作



    大伙覺得就控制一個led不好玩是吧,看到桌面的小電風扇,直接拿開刀,話不多說,看視頻。

    關閉窗口

    相關文章

    亚洲一区二区制服在线|在绩专区欧美自拍日韩|青春娱乐网97超碰人人射|在线观看国产网址你懂的