#define sensorpin 34 #define DHTPIN 2 #include <BH1750.h> BH1750 lightMeter; #include <Wire.h> #include <DHT.h> DHT dht(DHTPIN, DHT11); #include<WiFi.h> #include<PubSubClient.h> char* ssid ="17group";//此处需要改成你的wifi名称 const char*password = "hhj20011019"; //你的wifi密码 //以下四行参数勿动 const char*mqttServer = "39.106.6.44"; const int mqttPort =1886; const char*mqttUser = "iotlab"; const char*mqttPassword = "iot20121013"; WiFiClient espClient; PubSubClient client(espClient); char msg[50]; //用于存储向外发送的消息 //回调函数,topic是主题,payload就是收到的信息 void callback(char*topic, byte* payload, unsigned int length) { Serial.print("Messagearrived in topic: "); Serial.println(topic); Serial.print("Message:"); for (int i = 0; i< length; i++) { Serial.print((char)payload[i]); } Serial.println(); Serial.println("-----------------------"); } void setup() { Serial.begin(115200); WiFi.begin(ssid,password); while (WiFi.status()!= WL_CONNECTED) { delay(500); Serial.println("Connectingto WiFi.."); } Serial.println("Connectedto the WiFi network"); client.setServer(mqttServer,mqttPort); client.setCallback(callback); while (!client.connected()){ Serial.println("Connectingto MQTT..."); //请将cs202112345后面的数字替换成自己的学号 if (client.connect("cs225150340",mqttUser, mqttPassword )) { Serial.println("connected"); }else { Serial.print("failedwith state "); Serial.print(client.state()); delay(2000); } } client.subscribe("sub225150340");//请将后面的数字替换成自己的学号 { Serial.begin(115200); // Initialize the I2C bus (BH1750 library doesn't do this automatically) Wire.begin(); // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); // For Wemos / Lolin D1 Mini Pro and the Ambient Light shield use // Wire.begin(D2, D1); lightMeter.begin(); } } void loop() { client.loop();//此句为循环监测是否有消息过来,勿删 //以下是发送一组数据的例子,实际场景替换成读取的传感器数值 float hum = dht.readHumidity(); float temp = dht.readTemperature(); float lux = lightMeter.readLightLevel(); int soilhum =analogRead(sensorpin); snprintf(msg,50,"%.2f,%.2f,%.1f,%d",hum,temp,lux,soildhum); Serial.println(msg); //这行只是为了调试用 client.publish("pub225150430", msg); //请将后面的数字替换成自己的学号 delay(1000); } 如何接收 #define sensorpin 34 int svalue; void setup(){ Serial.begin(9600); } void loop(){ svalue=analogRead(sensorpin); Serial.println(svalue); delay(1000); } 上的数据
时间: 2024-01-22 12:04:17 浏览: 70
您可以使用 MQTT 客户端订阅相应主题,接收通过 MQTT 发布的数据。在代码中,传感器数据通过以下语句发布到 MQTT 主题 "pub225150430" 中:
```
client.publish("pub225150430", msg);
```
因此,您可以使用相同的 MQTT 客户端对象 `client`,在回调函数中订阅 "pub225150430" 主题并解析接收到的数据。
以下是示例代码:
```
#include <PubSubClient.h>
WiFiClient wifiClient;
PubSubClient client(wifiClient);
char receivedMsg[50];
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
receivedMsg[i] = (char)payload[i];
Serial.print(receivedMsg[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Connect to MQTT broker
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("cs225150340", mqttUser, mqttPassword)) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// Subscribe to "pub225150430" topic
client.subscribe("pub225150430");
}
void loop() {
client.loop();
// Parse received message
String msgStr = String(receivedMsg);
float hum, temp, lux;
int soilhum;
sscanf(msgStr.c_str(), "%f,%f,%f,%d", &hum, &temp, &lux, &soilhum);
// Do something with received data
Serial.print("Humidity: ");
Serial.println(hum);
Serial.print("Temperature: ");
Serial.println(temp);
Serial.print("Lux: ");
Serial.println(lux);
Serial.print("Soil Humidity: ");
Serial.println(soilhum);
delay(1000);
}
```
阅读全文