基于Arduino上传JSON格式数据至中移OneNet平台

中移OneNet平台JSON格式:

POST /devices/39245655/datapoints HTTP/1.1
api-key:UAXd=EZif9opr36q3E6L7Q1J2mk=
Host:api.heclouds.com
Connection:close
Content-Length:66

{"datastreams":[{"id":"Temperature","datapoints":[{"value":27}]}]}

1. 使用Arduino输出HTTP报头

#include 

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

HttpPacketHead packet;

void loop() {
  // put your main code here, to run repeatedly:
  char *p = "{\"datastreams\":[{\"id\":\"Temperature\",\"datapoints\":[{\"value\":29}]}]}";

  packet.setHostAddress("api.heclouds.com");
  packet.setDevId("39245655");
  packet.setAccessKey("UAXd=EZif9opr36q3E6L7Q1J2mk=");
  packet.setDataStreamId("Temperature");
  // packet.setTriggerId("");
  // packet.setBinIdx("");

  /*create the http message about add datapoint */
  packet.createCmdPacket(POST, TYPE_DATAPOINT, p);
  if (strlen(packet.content))
    Serial.print(packet.content);
  Serial.print(p);
  Serial.println("\n");
}

2.使用JSON库合成JSON数据

#include 

void setup() {
  Serial.begin(9600);

  StaticJsonBuffer<200> jsonBuffer;

  JsonObject& value = jsonBuffer.createObject();
  value["value"] = 29;

  JsonObject& id_datapoints = jsonBuffer.createObject();
  id_datapoints["id"] = "Temperature";
  JsonArray& datapoints = id_datapoints.createNestedArray("datapoints");
  datapoints.add(value);

  JsonObject& myJson = jsonBuffer.createObject();
  JsonArray& datastreams = myJson.createNestedArray("datastreams");
  datastreams.add(id_datapoints);

  myJson.printTo(Serial);
  Serial.print("\r\n\r\n");

  //格式化输出
  myJson.prettyPrintTo(Serial);

  char p[200];
  Serial.print("\r\n---------\r\n");
  int num = myJson.printTo(p,sizeof(p));
  Serial.print(p);
  Serial.print("\r\n============\r\n");
  Serial.print(num);
}

void loop() {
  // not used in this example
}

串口输出效果

{"datastreams":[{"id":"Temperature","datapoints":[{"value":29}]}]}

{
  "datastreams": [
    {
      "id": "Temperature",
      "datapoints": [
        {
          "value": 29
        }
      ]
    }
  ]
}
---------
{"datastreams":[{"id":"Temperature","datapoints":[{"value":29}]}]}
============
66

3. 综合HTTP头和JSON输出需要的POST请求

#include 
#include 

HttpPacketHead packet;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  //合成POST请求
    Serial.begin(9600);

  StaticJsonBuffer<200> jsonBuffer;

  JsonObject& value = jsonBuffer.createObject();
  value["value"] = 29;

  JsonObject& id_datapoints = jsonBuffer.createObject();
  id_datapoints["id"] = "Temperature";
  JsonArray& datapoints = id_datapoints.createNestedArray("datapoints");
  datapoints.add(value);

  JsonObject& myJson = jsonBuffer.createObject();
  JsonArray& datastreams = myJson.createNestedArray("datastreams");
  datastreams.add(id_datapoints);

  myJson.printTo(Serial);
  Serial.print("\r\n\r\n");

  //格式化输出
  myJson.prettyPrintTo(Serial);
  char p[200];

  Serial.print("\r\n---------\r\n");
  int num = myJson.printTo(p,sizeof(p));
  Serial.print(p);
  Serial.print("\r\n============\r\n");
  Serial.print(num);
  Serial.println("\n");

  packet.setHostAddress("api.heclouds.com");
  packet.setDevId("39245655");
  packet.setAccessKey("UAXd=EZif9opr36q3E6L7Q1J2mk=");
  packet.setDataStreamId("Temperature");
  // packet.setTriggerId("");
  // packet.setBinIdx("");

  /*create the http message about add datapoint */
  packet.createCmdPacket(POST, TYPE_DATAPOINT, p);
  if (strlen(packet.content))
    Serial.print(packet.content);
  Serial.print(p);
  Serial.println("\n");
  }

void loop() {
  // not used in this example
}

成功输出

{"datastreams":[{"id":"Temperature","datapoints":[{"value":29}]}]}

{
  "datastreams": [
    {
      "id": "Temperature",
      "datapoints": [
        {
          "value": 29
        }
      ]
    }
  ]
}
---------
{"datastreams":[{"id":"Temperature","datapoints":[{"value":29}]}]}
============
66

POST /devices/39245655/datapoints HTTP/1.1
api-key:UAXd=EZif9opr36q3E6L7Q1J2mk=
Host:api.heclouds.com
Content-Length:66

{"datastreams":[{"id":"Temperature","datapoints":[{"value":29}]}]}
        感谢一直关注着禾灮成长进步的朋友们。你们的信任、支持和鼓励,鞭策着我们一路走到了今天。

        感谢所有的合作伙伴,我们相互促进,共同见证了彼此的成长。

        感谢所有曾经在禾灮彼此倚靠、相互鼓励、携手同心、砥砺同行的兄弟姐妹。这里承载了我们的青春与热血。

                    禾灮,感谢有你。

        未来,我们将一如既往,砥砺前行。

                                            禾灮·小楊
                                           2018.08.20

你可能感兴趣的:(Arduino,OneNET,Basic)