ESP8266 http通信 连接onenet 上传数据 下发指令

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

网站效果图:网站发布待审核中,可能某些数据暂时没有计时更新

ESP8266 http通信 连接onenet 上传数据 下发指令_第1张图片

 

 程序源码

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

 

你可能感兴趣的:(浩瀚物联,ESP8266)