帮我写一段arduino使用esp8266将数据上传至onenet
时间: 2023-05-16 09:06:09 浏览: 216
当然可以,以下是一段示例代码:
#include <SoftwareSerial.h>
SoftwareSerial esp8266(2, 3); // RX, TX
void setup() {
Serial.begin(9600);
esp8266.begin(9600);
delay(1000);
Serial.println("Connecting to WiFi...");
esp8266.println("AT+CWJAP=\"SSID\",\"password\"");
delay(5000);
if (esp8266.find("OK")) {
Serial.println("WiFi connected!");
} else {
Serial.println("WiFi connection failed!");
}
}
void loop() {
float temperature = 25.0; // replace with your own data
float humidity = 50.0; // replace with your own data
String data = "{\"temperature\":" + String(temperature) + ",\"humidity\":" + String(humidity) + "}";
Serial.println("Sending data to OneNet...");
esp8266.println("AT+CIPSTART=\"TCP\",\"api.heclouds.com\",80");
delay(5000);
esp8266.println("AT+CIPSEND=" + String(data.length() + 41));
delay(5000);
esp8266.println("POST /devices/device_id/datapoints HTTP/1.1");
esp8266.println("Host: api.heclouds.com");
esp8266.println("Content-Type: application/json");
esp8266.println("api-key: your_api_key");
esp8266.println("Connection: close");
esp8266.println("Content-Length: " + String(data.length()));
esp8266.println();
esp8266.println(data);
delay(5000);
esp8266.println("AT+CIPCLOSE");
delay(5000);
Serial.println("Data sent to OneNet!");
delay(60000); // send data every minute
}
阅读全文