写1个基于esp32的环境监测系统,包含温湿度,空气质量检测,并使用tcp进行传输到onenet平台
时间: 2024-05-03 15:21:47 浏览: 80
以下是基于ESP32的环境监测系统的代码:
```c++
#include <WiFi.h>
#include <HTTPClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASSWORD "your_wifi_password"
#define ONE_NET_API_KEY "your_onenet_api_key"
#define ONE_NET_DEVICE_ID "your_onenet_device_id"
#define DHT_PIN 4
#define DHT_TYPE DHT11
#define DS18B20_PIN 5
DHT dht(DHT_PIN, DHT_TYPE);
OneWire oneWire(DS18B20_PIN);
DallasTemperature ds18b20(&oneWire);
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
float temperature, humidity;
int airQuality;
float airQualityVoltage;
// Read temperature and humidity from DHT sensor
humidity = dht.readHumidity();
temperature = dht.readTemperature();
// Read temperature from DS18B20 sensor
ds18b20.requestTemperatures();
float temperature2 = ds18b20.getTempCByIndex(0);
// Read air quality from MQ-135 sensor
int sensorValue = analogRead(A0);
airQualityVoltage = sensorValue / 1024.0 * 5.0;
airQuality = map(sensorValue, 0, 1023, 0, 500);
// Print sensor data
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\tHumidity: ");
Serial.print(humidity);
Serial.print(" %\tTemperature 2: ");
Serial.print(temperature2);
Serial.print(" °C\tAir Quality: ");
Serial.print(airQuality);
Serial.print(" ppm\tAir Quality Voltage: ");
Serial.print(airQualityVoltage);
Serial.println(" V");
// Send sensor data to OneNet platform
HTTPClient http;
http.begin("http://api.heclouds.com/devices/" + String(ONE_NET_DEVICE_ID) + "/datapoints");
http.addHeader("api-key", ONE_NET_API_KEY);
http.addHeader("Content-Type", "application/json");
String data = "{\"datastreams\":[";
data += "{\"id\":\"temperature\",\"datapoints\":[{\"value\":\"" + String(temperature) + "\"}]},";
data += "{\"id\":\"humidity\",\"datapoints\":[{\"value\":\"" + String(humidity) + "\"}]},";
data += "{\"id\":\"temperature2\",\"datapoints\":[{\"value\":\"" + String(temperature2) + "\"}]},";
data += "{\"id\":\"airQuality\",\"datapoints\":[{\"value\":\"" + String(airQuality) + "\"}]},";
data += "{\"id\":\"airQualityVoltage\",\"datapoints\":[{\"value\":\"" + String(airQualityVoltage) + "\"}]}";
data += "]}";
int httpResponseCode = http.POST(data);
if (httpResponseCode == 200) {
Serial.println("Data sent to OneNet platform");
} else {
Serial.print("Error sending data to OneNet platform. HTTP Response code: ");
Serial.println(httpResponseCode);
}
http.end();
delay(5000);
}
```
在这个代码中,我们使用了DHT11温湿度传感器,DS18B20温度传感器和MQ-135空气质量传感器。我们通过WiFi连接到网络,并将传感器数据发送到OneNet平台。在发送数据之前,我们需要在OneNet平台上创建一个设备并获取API密钥和设备ID,然后将它们替换到代码中的ONE_NET_API_KEY和ONE_NET_DEVICE_ID变量中。
在loop()函数中,我们首先读取传感器数据并将其打印到串口。然后,我们将传感器数据格式化为JSON格式,并将其通过HTTP POST请求发送到OneNet平台。
我们使用了HTTPClient库来进行HTTP通信,使用了WiFi库来连接到网络,使用了DHT库和DallasTemperature库来读取温湿度和温度数据。
在发送数据之前,我们还需要在OneNet平台上创建数据流,以便将传感器数据与设备关联起来。在本例中,我们创建了名为“temperature”、“humidity”、“temperature2”、“airQuality”和“airQualityVoltage”的数据流,分别对应温度、湿度、第二个温度传感器、空气质量和空气质量传感器的电压值。
代码中的“delay(5000)”表示每5秒发送一次传感器数据。您可以根据需要更改此值。
阅读全文