http - 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
}
在串行监视器上,它显示我已连接到服务器。
解决方案
推荐阅读
- r - 在 R 中,当字符索引是零长度字符串 ("") 时 [[<- 的行为是否可靠?
- ajax - Vuejs 帖子返回 json 数据但不会分配给 Vues 数据对象
- can-bus - 使用 CAPL 在 CANalyzer 中发送/接收/配置 TCP/IP 协议套件请求/响应数据包
- servlets - Karaf 切换到另一个版本捆绑
- c++ - Function 的调用重载不明确
- python - 自动将散点图的标签放置在点周围,以免它们相互覆盖
- c# - 通过使用我的 Nuget 包的 VS2015 构建 .NET 项目不会复制 .config 文件
- docker - 重新启动在 Docker 容器中运行的应用程序会破坏堆栈网络
- java - 我想在 android 中生成四个不同的随机数,但它们的总数不应大于指定范围,如 0 到 69
- mysql - 我无法用 MYSQL 中的更新、设置、替换替换注册表中的部分名称