ESP8266串口通信接收来自ESP32的温湿度信息,mqtt上传阿里云的代码
时间: 2024-05-10 21:18:08 浏览: 130
以下是使用ESP8266接收来自ESP32的温湿度信息并通过MQTT上传到阿里云的示例代码。
首先,需要在ESP8266上安装MQTT库,可以使用PubSubClient库,可以在Arduino IDE中进行安装。
然后,需要连接ESP8266和ESP32,可以使用串口通信或无线通信,这里以串口通信为例。
ESP32发送温湿度信息的代码示例:
```c
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <WiFi.h>
#include <AsyncMqttClient.h>
#define WIFI_SSID "your_ssid"
#define WIFI_PASSWORD "your_password"
#define MQTT_HOST "your_mqtt_host"
#define MQTT_PORT 1883
#define MQTT_USERNAME "your_mqtt_username"
#define MQTT_PASSWORD "your_mqtt_password"
#define MQTT_TOPIC "your_topic"
#define DHTPIN 27
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
WiFiClient wifiClient;
AsyncMqttClient mqttClient;
void connectToWifi() {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}
void setup() {
Serial.begin(115200);
dht.begin();
connectToWifi();
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.setCredentials(MQTT_USERNAME, MQTT_PASSWORD);
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C, Humidity: ");
Serial.print(humidity);
Serial.println("%");
String message = String(temperature) + "," + String(humidity);
mqttClient.publish(MQTT_TOPIC, 0, false, message.c_str());
delay(5000);
}
```
ESP8266接收并上传信息的代码示例:
```c
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASSWORD "your_wifi_password"
#define MQTT_SERVER "your_mqtt_server"
#define MQTT_PORT 1883
#define MQTT_USERNAME "your_mqtt_username"
#define MQTT_PASSWORD "your_mqtt_password"
#define MQTT_TOPIC "your_mqtt_topic"
SoftwareSerial espSerial(2, 3); // RX, TX
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
void setup() {
Serial.begin(115200);
espSerial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
Serial.println("Connected to WiFi");
mqttClient.setServer(MQTT_SERVER, MQTT_PORT);
mqttClient.setCallback(callback);
reconnect();
}
void loop() {
if (!mqttClient.connected()) {
reconnect();
}
mqttClient.loop();
}
void reconnect() {
while (!mqttClient.connected()) {
Serial.print("Connecting to MQTT server...");
if (mqttClient.connect("ESP8266-client", MQTT_USERNAME, MQTT_PASSWORD)) {
Serial.println("connected.");
mqttClient.subscribe(MQTT_TOPIC);
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" retrying in 5 seconds.");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String message = "";
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
Serial.println("Received message: " + message);
// 解析温湿度信息
int index = message.indexOf(",");
float temperature = message.substring(0, index).toFloat();
float humidity = message.substring(index + 1).toFloat();
// 将温湿度信息上传到阿里云
String data = "{\"Temperature\":" + String(temperature) + ",\"Humidity\":" + String(humidity) + "}";
espSerial.println("AT+CIPSTART=\"TCP\",\"your_aliyun_host\",your_aliyun_port");
if (espSerial.find("OK")) {
Serial.println("TCP connection established.");
espSerial.println("AT+CIPSEND=" + String(data.length() + 2));
if (espSerial.find(">")) {
Serial.println("Sending data...");
espSerial.println(data);
}
}
delay(5000);
}
```
在ESP8266的串口监视器中,可以看到接收到的温湿度信息和上传到阿里云的信息。
注意:需要将代码中的“your_ssid”,“your_password”,“your_mqtt_host”,“your_mqtt_username”,“your_mqtt_password”,“your_topic”,“your_aliyun_host”和“your_aliyun_port”替换为实际的值。
阅读全文