ESP8266 http通信 連接onenet 上傳數據 下發指令

網站鏈接:https://open.iot.10086.cn/iotbox/appsquare/appview?openid=d9729c3b93457ce42016c09f92dda0cc

網站效果圖:網站發佈待審覈中,可能某些數據暫時沒有計時更新

 

 程序源碼

void setup() 
{
    WiFi.mode(WIFI_AP_STA);               //設置工作模式 -- set work mode:  WIFI_AP /WIFI_STA /WIFI_AP_STA
    Serial.begin(115200);
    pinMode(LED, OUTPUT);
    delay(10);

    Serial.println("");
    Serial.print("Trying to connect to ");
    Serial.println(ssid);
    // 連接到wifi -- We start by connecting to a wifi network
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) 
    {
      delay(500);
      Serial.print(".");
    }
  
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

}

void loop() 
{
    delay(2000);
    //建立連接並判斷 -- Connecting to server
    if (!client.connect(OneNetServer, tcpPort)) 
    {
      Serial.println("connection failed");
      return;
    }
    //上傳數據 -- post value
    postData(DeviceId, temp, humi,330,123);
    Serial.println("closing connection");
    delay(1000);
    //建立連接並判斷 -- Connecting to server
    if (!client.connect(OneNetServer, tcpPort)) 
    {
      Serial.println("connection failed");
      return;
    }
    //從雲端獲取值並存於stream1 -- get data from server
    stream1=readData(DeviceId, DataStreams);
    if(stream1 == 1)
    {
      GPIO_OUTPUT_SET(4, 0);
      }
     else
     {
      GPIO_OUTPUT_SET(4, 1);
      }
    Serial.println("closing connection");
}

Arduino IDE 串口監視器輸出數據

138
Get the data from Internet!
{"errno":0,"data":{"count":1,"datastreams":[{"datapoints":[{"at":"2020-05-06 21:05:23.000","value":0}],"id":"led_state"}]},"error":"succ"}
Read Over!
Recived Value : 0	 The last update time : 2020-05-06 21:05:23.000
errno : 0	 error : succ
Data parse OK!
closing connection
POST /devices/*****/datapoints?type=3 HTTP/1.1
api-key:************************************
Host:api.heclouds.com
Content-Length: 43
Connection: close

{"TEM":0.00,"HUM":0.00,"CO2":330,"LUX":123}
closing connection
GET /devices/*****/datapoints?datastream_id=led_state HTTP/1.1
api-key:**************************
Host:api.heclouds.com
Connection: close

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章