Arduino uno接收温湿度传感器数据发送到esp01s并上传至阿里云服务器
时间: 2024-02-12 15:04:16 浏览: 151
好的,这是一个关于Arduino Uno和ESP-01S模块的项目,主要用于接收DHT11温湿度传感器的数据并将其通过ESP-01S模块上传到阿里云服务器上。以下是具体的步骤:
1. 将DHT11温湿度传感器连接到Arduino Uno上,可以参考以下连接方式:
DHT11 VCC - Arduino 5V
DHT11 GND - Arduino GND
DHT11 DATA - Arduino Digital Pin 2
2. 下载并安装ESP8266库,这里我们使用的是ESP8266WiFi库和ESP8266HTTPClient库。打开Arduino IDE,选择菜单栏:Sketch -> Include Library -> Manage Libraries,搜索并安装这两个库。
3. 连接ESP-01S模块到Arduino Uno上,可以参考以下连接方式:
ESP-01S VCC - Arduino 3.3V
ESP-01S GND - Arduino GND
ESP-01S RX - Arduino Digital Pin 3
ESP-01S TX - Arduino Digital Pin 4
4. 在阿里云上创建一个IoT实例并添加一个设备,记录下设备的三元组(ProductKey、DeviceName、DeviceSecret)。
5. 在阿里云上创建一个Topic,用于接收设备上传的数据。
6. 编写Arduino代码,实现从DHT11传感器读取数据并将其上传到阿里云服务器上,可以参考以下代码:
```
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
#define DHTPIN 2
#define DHTTYPE DHT11
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* serverName = "your_SERVER_NAME";
const char* productKey = "your_PRODUCT_KEY";
const char* deviceName = "your_DEVICE_NAME";
const char* deviceSecret = "your_DEVICE_SECRET";
const char* mqttServer = productKey ".iot-as-mqtt.cn-shanghai.aliyuncs.com";
int mqttPort = 1883;
const char* mqttClientId = "clientId";
const char* mqttUsername = deviceName "&" productKey;
const char* mqttPassword = "your_MQTT_PASSWORD";
const char* mqttTopic = "/sys/" productKey "/" deviceName "/thing/event/property/post";
DHT dht(DHTPIN, DHTTYPE);
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
mqttClient.setServer(mqttServer, mqttPort);
}
void loop() {
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temp) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("C, Humidity: ");
Serial.print(humidity);
Serial.println("%");
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String serverPath = serverName + "/temperature=" + String(temp) + "&humidity=" + String(humidity);
http.begin(serverPath.c_str());
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
} else {
Serial.println("Error on HTTP request");
}
http.end();
}
if (!mqttClient.connected()) {
String clientId = mqttClientId;
clientId += String(random(0xffff), HEX);
Serial.print("Connecting to MQTT server ");
Serial.print(mqttServer);
Serial.println("...");
if (mqttClient.connect(clientId.c_str(), mqttUsername, mqttPassword)) {
Serial.println("Connected to MQTT server");
} else {
Serial.print("Failed to connect to MQTT server, rc=");
Serial.println(mqttClient.state());
delay(2000);
return;
}
}
String payload = "{\"id\":\"1\",\"params\":{\"Temperature\":" + String(temp) + ",\"Humidity\":" + String(humidity) + "},\"method\":\"thing.event.property.post\"}";
mqttClient.publish(mqttTopic, payload.c_str());
Serial.println("Published message");
delay(5000);
}
```
在代码中,我们使用了MQTT协议将传感器数据上传到阿里云服务器上。需要注意的是,MQTT连接需要使用三元组中的DeviceName、ProductKey和DeviceSecret,以及阿里云IoT账号的AccessKey和SecretKey生成MQTT密码,可以在阿里云控制台中找到。
7. 将代码上传到Arduino Uno板子上。
到这里,你已经完成了将温湿度传感器数据发送到阿里云服务器的过程。如果你在阿里云IoT控制台中创建了一个Topic,则可以在控制台中查看设备上传的数据。
阅读全文