我是靠谱客的博主 帅气樱桃,最近开发中收集的这篇文章主要介绍oneNet之http请求上传云平台数据,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include<ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
 
const char* ssid     = "Xiaomi_1EDE";       // 连接 WiFi名 ,修改为自己的WiFi名                                      
const char* password = "1234567890";          // 连接 WiFi密码,修改为自己的WiFi密码
const char* host = "api.heclouds.com";
const char* url = "/devices/634373264/datapoints";   //634373264需要修改为自己的 设备ID
const char* apiKey = "nbrcxcO1PxNC9PnNBcYpxAMmFm0=";  //API KEY

HTTPClient httpClient;          //1、创建http的客户端
 
void setup() {
  Serial.begin(115200);  //启动串口,并设置波特率为115200
  connectWiFi();       //连接WiFi
}
 
void loop() {
   ESP.wdtFeed();
   httpClientRequest();  //通过客户端发送http请求,并响应
   delay(2000);
}

String serializeData(){
  //序列化数据
  const size_t capacity = 3*JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(1) + 7*JSON_OBJECT_SIZE(2);
  DynamicJsonDocument doc(capacity);
  
  JsonArray datastreams = doc.createNestedArray("datastreams");
  
  JsonObject datastreams_0 = datastreams.createNestedObject();
  datastreams_0["id"] = "temperature";
  
  JsonArray datastreams_0_datapoints = datastreams_0.createNestedArray("datapoints");
  
  JsonObject datastreams_0_datapoints_0 = datastreams_0_datapoints.createNestedObject();
  datastreams_0_datapoints_0["at"] = "2013-04-22T00:35:43";
  datastreams_0_datapoints_0["value"] = "bacd";
  
  JsonObject datastreams_0_datapoints_1 = datastreams_0_datapoints.createNestedObject();
  datastreams_0_datapoints_1["at"] = "2013-04-22T00:55:43";
  datastreams_0_datapoints_1["value"] = 84;
  
  JsonObject datastreams_1 = datastreams.createNestedObject();
  datastreams_1["id"] = "key";
  
  JsonArray datastreams_1_datapoints = datastreams_1.createNestedArray("datapoints");
  
  JsonObject datastreams_1_datapoints_0 = datastreams_1_datapoints.createNestedObject();
  datastreams_1_datapoints_0["at"] = "2013-04-22T00:35:43";
  
  JsonObject datastreams_1_datapoints_0_value = datastreams_1_datapoints_0.createNestedObject("value");
  datastreams_1_datapoints_0_value["x"] = 123;
  datastreams_1_datapoints_0_value["y"] = 123.994;
  
  JsonObject datastreams_1_datapoints_1 = datastreams_1_datapoints.createNestedObject();
  datastreams_1_datapoints_1["at"] = "2013-04-22T00:35:43";
  datastreams_1_datapoints_1["value"] = 23.001;
  String data;
  serializeJson(doc, data);
  return data;
}
 
void httpClientRequest(){ 
  httpClient.begin(host,80,url);       //2、通过begin函数配置请求地址。
  httpClient.addHeader("api-key",apiKey);
  String data =serializeData();   //序列化json数据
  int httpCode = httpClient.POST(data);  //3、通过GET函数启动连接并发送HTTP请求
  if (httpCode > 0) {
    Serial.printf("[HTTP] POST... code: %dn", httpCode);
    if (httpCode == HTTP_CODE_OK) {
      String payload = httpClient.getString();    //获取响应体
      Serial.println(payload);
    } else {
      Serial.printf("[HTTP] POST... failed, error: %sn", httpClient.errorToString(httpCode).c_str());
    }
  } else {
    Serial.printf("[HTTP] Unable to connectn");
  }
  httpClient.end();
}
 
void connectWiFi(){
  Serial.println();
  Serial.print("Connecting to");
  Serial.println(ssid);
  WiFi.begin(ssid,password);                  //启动WIFI
  while(WiFi.status()!=WL_CONNECTED){         //判断WiFi的连接状态,如果没有连接成功,等待
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address:");
  Serial.println(WiFi.localIP());    //获取本机的IP
}

 

最后

以上就是帅气樱桃为你收集整理的oneNet之http请求上传云平台数据的全部内容,希望文章能够帮你解决oneNet之http请求上传云平台数据所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部