Arduino ESP8266 使用 POST 将 DHT11 传感器数据以 JSON 格式发送到服务器
Arduino ESP8266 使用 POST 将 DHT11 传感器数据以 JSON 格式发送到服务器
以下是 Arduino ESP8266 使用 POST 将 DHT11 传感器数据以 JSON 格式发送到服务器的代码:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <DHT.h>
#define DHTPIN 2 // DHT11 传感器的数字引脚
#define DHTTYPE DHT11 // DHT11 传感器类型
#define WIFI_SSID 'your_wifi_ssid' // WiFi SSID
#define WIFI_PASSWORD 'your_wifi_password' // WiFi 密码
#define SERVER_URL 'http://your_server_url' // 服务器地址
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print('Connecting to WiFi');
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print('.');
}
Serial.println();
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println('Failed to read data from DHT sensor');
delay(2000);
return;
}
StaticJsonDocument<200> doc;
doc['temperature'] = temperature;
doc['humidity'] = humidity;
String jsonStr;
serializeJson(doc, jsonStr);
HTTPClient http;
http.begin(client, SERVER_URL);
http.addHeader('Content-Type', 'application/json');
int httpResponseCode = http.POST(jsonStr);
if (httpResponseCode > 0) {
Serial.println('Data sent successfully');
} else {
Serial.println('Error sending data');
}
http.end();
delay(10000);
}
代码解释:
- 引入所需的库:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <DHT.h>
- 定义 DHT11 传感器的数字引脚、类型、WiFi SSID、密码和服务器地址:
#define DHTPIN 2
#define DHTTYPE DHT11
#define WIFI_SSID 'your_wifi_ssid'
#define WIFI_PASSWORD 'your_wifi_password'
#define SERVER_URL 'http://your_server_url'
- 初始化 DHT11 传感器和 WiFi 连接:
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print('Connecting to WiFi');
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print('.');
}
Serial.println();
}
- 循环读取 DHT11 传感器数据,将其存储为 JSON 格式,并发送到服务器:
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println('Failed to read data from DHT sensor');
delay(2000);
return;
}
StaticJsonDocument<200> doc;
doc['temperature'] = temperature;
doc['humidity'] = humidity;
String jsonStr;
serializeJson(doc, jsonStr);
HTTPClient http;
http.begin(client, SERVER_URL);
http.addHeader('Content-Type', 'application/json');
int httpResponseCode = http.POST(jsonStr);
if (httpResponseCode > 0) {
Serial.println('Data sent successfully');
} else {
Serial.println('Error sending data');
}
http.end();
delay(10000);
}
在循环中,首先使用 dht.readTemperature() 和 dht.readHumidity() 函数读取温度和湿度数据。然后,检查数据是否有效。如果数据无效,则延迟 2 秒并返回。否则,使用 StaticJsonDocument 将温度和湿度数据存储为 JSON 格式,然后使用 HTTPClient 将 JSON 数据 POST 到服务器。最后,等待 10 秒钟并重复此过程。
注意:
- 替换代码中的
your_wifi_ssid、your_wifi_password和your_server_url为您的实际值。 - 确保您已安装所需的库,包括 ESP8266WiFi.h、ESP8266HTTPClient.h、ArduinoJson.h 和 DHT.h。
- 为了确保代码正常工作,请在服务器上配置一个接受 JSON 数据的 API 接口。
通过此代码,您就可以将 DHT11 传感器的数据以 JSON 格式发送到服务器,以便进行数据分析和处理。
原文地址: https://www.cveoy.top/t/topic/mFx9 著作权归作者所有。请勿转载和采集!