Arduino uno接收温湿度传感器数据发送到esp01s
时间: 2024-02-05 10:06:16 浏览: 164
Arduino 测量温湿度
好的,这是一个关于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. 编写Arduino代码,实现从DHT11传感器读取数据并将其发送到远程服务器上,可以参考以下代码:
```
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define DHTPIN 2
#define DHTTYPE DHT11
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* serverName = "your_SERVER_NAME";
DHT dht(DHTPIN, DHTTYPE);
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");
}
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();
}
delay(5000);
}
```
5. 将代码上传到Arduino Uno板子上。
到这里,你已经完成了将温湿度传感器数据发送到远程服务器的过程。如果你有一个Web服务器,你可以将其部署到服务器上并收集数据。
阅读全文