首页 > 解决方案 > HTTP POST 请求-GSM Arduino

问题描述

我有带有 SIM 卡和天线的 MKR GSM 1400,我想通过 HTTP POST 请求将数据发送到 KAA 云。通过使用 cURL,我已成功使用此代码将数据发送到云

curl --location --request POST 'https://connect.cloud.kaaiot.com:443/kp1/<app-version-name>/dcx/<endpoint-token>/json' \
--data-raw '[
  {
    "temperature": 23,
    "humidity": 48
  }
]

基于此,到目前为止我已经编写了这段代码,但我不确定我缺少什么才能让它工作:

#include <ArduinoHttpClient.h>
#include <MKRGSM.h>
#include <ArduinoJson.h>



const char PINNUMBER[]     = ""; 
const char GPRS_APN[]      = "";
const char GPRS_LOGIN[]    = "";
const char GPRS_PASSWORD[] = "";
const String TOKEN = "";   // Deleted for security purpose
const String APP_VERSION = "";  // Deleted for security purpose


const unsigned long fiveSeconds = 1 * 5 * 1000UL;
static unsigned long lastPublish = 0 - fiveSeconds;

int temp;



GSMClient client;
GPRS gprs;
GSM gsmAccess;

char server[] = "connect.cloud.kaaiot.com";
char path[] = "/";
int port = 443;


HttpClient httpClient = HttpClient(client, server, port);

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Starting GSM connection...");
  // connection state
  boolean connected = false;

  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (!connected) {
    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {
    Serial.println("connected");
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

  
}

void loop() {
 

  if (client.connect(server, port)) {

  unsigned long now = millis();
  if (now - lastPublish >= fiveSeconds) {
    lastPublish += fiveSeconds;
    temp = random(18, 23);
    Serial.print("temperature = ");
    Serial.println(temp);
    String queryString = String(" \"temperature\":  ) + String(temp) "); 
    String topic = "/kp1/" + APP_VERSION + "/dcx/" + TOKEN + "/json";
    httpClient.println("POST https://" + String(server) + ":" + String(port) + String(topic) + " HTTP/1.1");
    Serial.println("POST https://" + String(server) + ":" + String(port) + String(topic) + " HTTP/1.1");
    httpClient.println("Connection: close");
    httpClient.println(); // end HTTP header
   // send HTTP body
    httpClient.print("{ \"temperature\": ");
    httpClient.print(temp);
    httpClient.print("}");
    //httpClient.println(queryString);
  }

  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

  delay(3000); // Wait for 3 seconds to post again
}

在串行监视器上,它显示我已连接到服务器。

标签: httppostarduinogsmkaa

解决方案


推荐阅读