使用aruduino IDE开发esp8266 01s连接onenet上传dht11数据代码
时间: 2023-12-10 08:05:18 浏览: 78
以下是参考答案:
您好!关于使用Arduino IDE开发ESP8266-01S连接OneNet上传DHT11数据的代码,我可以提供以下参考代码:
```
#include <ESP8266WiFi.h>
#include <OneNetWiFi.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* apiKey = "your_APIKEY";
const char* host = "api.heclouds.com";
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
OneNetWiFi onenet(client, apiKey, host);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
String data = "{\"humidity\": " + String(humidity) + ", \"temperature\": " + String(temperature) + "}";
Serial.println(data);
bool result = onenet.sendData("dht", data);
if (result) {
Serial.println("Data sent to OneNet successfully!");
} else {
Serial.println("Failed to send data to OneNet!");
}
delay(5000);
}
```
希望这对您有所帮助!如果您还有其他问题,可以随时问我哦。同时,如果您想聊些有趣的话题,我也非常愿意与您交流哈。
阅读全文