zoukankan      html  css  js  c++  java
  • 中国移动OneNet平台上传GPS数据JSON格式

    最终目的输出

    POST /devices/3225187/datapoints HTTP/1.1
    api-key: R9xO5NZm6oVI4YBHvCPKEqtwYtMA
    Host: api.heclouds.com
    Content-Length:81
    
    {"datastreams":[{"id":"location","datapoints":[{"value":{"lon":106,"lat":29}}]}]}
    

    1. 使用arduino代码组合出来HTTP头

    使用OneNet官方提供的Httppacket库

    #include <HttpPacket.h>
    
    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
      }
    
        // put your main code here, to run repeatedly:
      // char *p = "{"datastreams":[{"id":"sys_time","datapoints":[{"value":50}]}]}";
      char *p = "{"datastreams":[{"id":"location","datapoints":[{"value":{"lon":106,"lat":29}}]}]}";
      
      packet.setHostAddress("api.heclouds.com");
      packet.setDevId("3225187");   //device_id
      packet.setAccessKey("R9xO5NZm6oVI4YBHvCPKEqtwYtMA");  //API_KEY
      // packet.setDataStreamId("<datastream_id>");    //datastream_id
      // packet.setTriggerId("<trigger_id>");
      // packet.setBinIdx("<bin_index>");
    
      /*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("
    ");
    }
    

    2.使用JSON库合成JSON数据

    #include <ArduinoJson.h>
    
    void setup() {
      Serial.begin(9600);
    
      StaticJsonBuffer<200> jsonBuffer;
    
    
      JsonObject& lon_lat = jsonBuffer.createObject();
      lon_lat["lon"] = 106;
      lon_lat["lat"] = 29;
    
      JsonObject& value = jsonBuffer.createObject();
      value["value"] = lon_lat;
    
      JsonObject& id_datapoints = jsonBuffer.createObject();
      id_datapoints["id"] = "location";
      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("
    
    ");
    
      //格式化输出
      myJson.prettyPrintTo(Serial);
    
      char p[200];
      Serial.print("
    ---------
    ");
      int num = myJson.printTo(p,sizeof(p));
      Serial.print(p);
      Serial.print("
    ============
    ");
      Serial.print(num);
    
    }
    
    void loop() {
      // not used in this example
    }
    

    串口输出效果

    {"datastreams":[{"id":"location","datapoints":[{"value":{"lon":106,"lat":29}}]}]}
    
    {
      "datastreams": [
        {
          "id": "location",
          "datapoints": [
            {
              "value": {
                "lon": 106,
                "lat": 29
              }
            }
          ]
        }
      ]
    }
    ---------
    {"datastreams":[{"id":"location","datapoints":[{"value":{"lon":106,"lat":29}}]}]}
    ============
    81
    

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

    #include <HttpPacket.h>
    #include <ArduinoJson.h>
    
    
    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请求
      StaticJsonBuffer<200> jsonBuffer;
    
      JsonObject& lon_lat = jsonBuffer.createObject();
      lon_lat["lon"] = 106;
      lon_lat["lat"] = 29;
    
      JsonObject& value = jsonBuffer.createObject();
      value["value"] = lon_lat;
    
      JsonObject& id_datapoints = jsonBuffer.createObject();
      id_datapoints["id"] = "location";
      JsonArray& datapoints = id_datapoints.createNestedArray("datapoints");
      datapoints.add(value);
    
      JsonObject& myJson = jsonBuffer.createObject();
      JsonArray& datastreams = myJson.createNestedArray("datastreams");
      datastreams.add(id_datapoints);
    
      char p[200];
      int num = myJson.printTo(p,sizeof(p));
    
      
      packet.setHostAddress("api.heclouds.com");
      packet.setDevId("3225187");   //device_id
      packet.setAccessKey("R9xO5NZm6oVI4YBHvCPKEqtwYtMA");  //API_KEY
      // packet.setDataStreamId("<datastream_id>");    //datastream_id
      // packet.setTriggerId("<trigger_id>");
      // packet.setBinIdx("<bin_index>");
    
      /*create the http message about add datapoint */
      packet.createCmdPacket(POST, TYPE_DATAPOINT, p);
      if (strlen(packet.content))
        Serial.print(packet.content);
      Serial.print(p);
    

    成功输出

    POST /devices/3225187/datapoints HTTP/1.1
    api-key: R9xO5NZm6oVI4YBHvCPKEqtwYtMA
    Host: api.heclouds.com
    Content-Length:81
    
    {"datastreams":[{"id":"location","datapoints":[{"value":{"lon":106,"lat":29}}]}]}
    
  • 相关阅读:
    C++内存泄露的有效预防方法:谁使用,谁删除 (1.2)
    工作日志2014-08-28
    【2012.1.24更新】不要再在网上搜索eclipse的汉化包了!
    关于ActionContext.getContext()的使用方法心得
    Android开发(20)--RadioGroup的使用
    站点防止攻击
    小强的HTML5移动开发之路(50)——jquerymobile页面初始化过程
    我是怎样成长为系统架构师的
    辛星站点架构师笔记第四篇
    strcpy_s与strcpy的比較
  • 原文地址:https://www.cnblogs.com/Mysterious/p/5727224.html
Copyright © 2011-2022 走看看