• <table id="0ws66"></table>
  • <noscript id="0ws66"><source id="0ws66"></source></noscript>

    遠程控制通訊——基于掌控板【esp32】控制LED燈并返回控制結果

    作者:kevin_Lv | 更新時間:2019-01-21 | 瀏覽量:2345

    一、目標

    掌控板【esp32】連接貝殼物聯服務,實現遠程控制LED燈亮滅,并將結果反饋至控制界面。

    二、硬件

    三、接線

    四、代碼下載

    所有公開代碼托管于碼云,方便大家使用和共同參與完善,地址:https://gitee.com/hejinlv/ESP32/tree/master

    點擊上方鏈接,進入如下界面:

    1.點擊 ESP32_bigiot_LED 進入:

    2.點擊下載。

    五、代碼使用

    1.下載代碼后解壓

    2、用Arduino IDE打開

    ESP32_bigiot_LED/ESP32_bigiot_LED.ino

      修改其中的DEVICEID、APIKEY兩個參數,將代碼上傳至Arduino開發板。

     

    之后的詳細操作步驟可參照http://www.embanju.org/help/2.html

    #include <WiFi.h>
    #include <aJSON.h>
    #include <MPython.h>

    const char* ssid     = "WLJY";
    const char* password = "steam666";

    const char* host = "www.embanju.org";
    const int httpPort = 8181;

    unsigned long lastCheckInTime = 0; //記錄上次報到時間
    const unsigned long postingInterval = 40000; // 每隔40秒向服務器報到一次

    //=============  此處必須修該============

    String DEVICEID="9159"; // 你的設備編號   ==
    String  APIKEY = "58710677c"; // 設備密碼==
    //=======================================

    void setup()
    {
        Serial.begin(115200);
        delay(10);
        mPython.begin();
        // We start by connecting to a WiFi network

        Serial.println();
        Serial.println();
        Serial.print("Connecting to ");
        Serial.println(ssid);
        display.setCursorXY(36,22);
        display.print(ssid);
        WiFi.begin(ssid, password);

        while (WiFi.status() != WL_CONNECTED) {
            delay(500);
            Serial.print(".");
        }
       delay(3000);
        Serial.println("");
        Serial.println("WiFi connected");
        Serial.println("IP address: ");
        Serial.println(WiFi.localIP());
       
        pinMode(P0, OUTPUT);
       // pinMode(P1, OUTPUT);
       display.fillScreen(0);
       display.setCursorXY(16, 22);
       display.print("connected: OK");
       delay(1000);
    }
    WiFiClient client;

    void loop()
    {

    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.print(".");
      }

      // Use WiFiClient class to create TCP connections
      if (!client.connected()) {
        if (!client.connect(host, httpPort)) {
          Serial.println("connection failed");
          delay(5000);
          return;
        }
      }

      if(millis() - lastCheckInTime > postingInterval || lastCheckInTime==0) {
        checkIn();
      }
      
      // Read all the lines of the reply from server and print them to Serial
      if (client.available()) {
        String inputString = client.readStringUntil('\n');
        inputString.trim();
        Serial.println(inputString);
        int len = inputString.length()+1;
        if(inputString.startsWith("{") && inputString.endsWith("}")){
          char jsonString[len];
          inputString.toCharArray(jsonString,len);
          aJsonObject *msg = aJson.parse(jsonString);
          processMessage(msg);
          aJson.deleteItem(msg);          
        }
      }
    }

    void processMessage(aJsonObject *msg){
      aJsonObject* method = aJson.getObjectItem(msg, "M");
      aJsonObject* content = aJson.getObjectItem(msg, "C");     
      aJsonObject* client_id = aJson.getObjectItem(msg, "ID");
      if (!method) {
        return;
      }
        String M = method->valuestring;
        if(M == "say"){
          String C = content->valuestring;
          String F_C_ID = client_id->valuestring;
          if(C == "play"){
              digitalWrite(P0, HIGH);
              //digitalWrite(P1, HIGH);
              display.fillScreen(0);
             display.setCursorXY(24, 22);
             display.print("LED: On");
            sayToClient(F_C_ID,"LED All on!");    
          }
          if(C == "stop"){
               digitalWrite(P0, LOW);
              //digitalWrite(P1, LOW);
              display.setCursorXY(24, 22);
             display.print("LED: OFF");
            sayToClient(F_C_ID,"LED All off!");    
          }
        }
    }

    void checkIn() {
        String msg = "{\"M\":\"checkin\",\"ID\":\"" + DEVICEID + "\",\"K\":\"" + APIKEY + "\"}\n";
        client.print(msg);
        lastCheckInTime = millis(); 
    }

    void sayToClient(String client_id, String content){
      String msg = "{\"M\":\"say\",\"ID\":\"" + client_id + "\",\"C\":\"" + content + "\"}\n";
      client.print(msg);
      lastCheckInTime = millis();
    }


    評論:共3條

    ma-fa 評論于:2019-01-21 16:52:17
    好例子!Thank you very much!
    I wrote a blog post about this great API and also added a fork of ESP32 Camera: https://fasani.de/2019/01/21/simple-iot-image-logging-using-bigiot-net/
    bigiot 評論于:2019-01-25 11:18:15
    不錯分享!
    haoyu 評論于:2020-05-07 19:11:19
    贊贊
    返回頂部
    国产综合在线