esp32 数据上传到 onenet 案例
时间: 2023-05-20 21:02:36 浏览: 216
ESP8266通过MQTT订阅主题并推送数据到ONENET
3星 · 编辑精心推荐
可以使用以下代码将 ESP32 的数据上传到 OneNet:
```c
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YourSSID";
const char* password = "YourPassword";
const char* apiKey = "YourAPIKey";
const char* deviceId = "YourDeviceId";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Set up the HTTP client
HTTPClient http;
http.begin("http://api.heclouds.com/devices/" + String(deviceId) + "/datapoints");
http.addHeader("api-key", apiKey);
http.addHeader("Content-Type", "application/json");
// Set up the data to be sent
String data = "{\"datastreams\":[{\"id\":\"temperature\",\"datapoints\":[{\"value\":25.0}]}]}";
// Send the data
int httpResponseCode = http.POST(data);
// Check for errors
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Disconnect from the server
http.end();
}
void loop() {
// Do nothing
}
```
这个代码将 ESP32 的温度数据上传到 OneNet 上的一个名为 "temperature" 的数据流中。你需要将代码中的 "YourSSID"、"YourPassword"、"YourAPIKey" 和 "YourDeviceId" 替换为你自己的值。
阅读全文