#include
#include
#include
#include
#include
WiFiClient net;
MQTTClient client;
DHT_Unifieddht(D4, DHT22);
SoftwareSerialpmSerial(D1, D2);
constcharssid[] ="你的WIFI网络名称";
constcharpass[] ="你的WIFI密码";
#defineLENG 31
unsignedcharbuf[LENG];
intPM01Value =0;
intPM2_5Value =0;
intPM10Value =0;
unsignedlonglastMillis =0;
voidconnect();
voidsetup(){
Serial.begin(115200);
pmSerial.begin(9600);
pmSerial.setTimeout(1500);
dht.begin();
sensor_tsensor;
dht.temperature().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Temperature");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
Serial.println("------------------------------------");
dht.humidity().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Humidity");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
Serial.println("------------------------------------");
WiFi.begin(ssid, pass);
client.begin("MQTT服务器地址", net);
client.onMessage(messageReceived);
connect();
}
voidconnect(){
Serial.print("checking wifi...");
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while(!client.connect("arduino","try","try")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
}
voidloop(){
client.loop();
delay(10);
if(!client.connected()) {
connect();
}
if(millis() - lastMillis >5000) {
lastMillis = millis();
client.publish("/home/livingroom/brightness","{\"value\": "+ String(1024- analogRead(A0)) +"}");
sensors_event_tevent;
dht.temperature().getEvent(&event);
if(isnan(event.temperature)) {
Serial.println("Error reading temperature!");
}
else{
client.publish("/home/livingroom/temperature","{\"value\": "+ String(event.temperature) +"}");
}
dht.humidity().getEvent(&event);
if(isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
}
else{
client.publish("/home/livingroom/humidity","{\"value\": "+ String(event.relative_humidity) +"}");
}
if(pmSerial.find(0x42)) {
pmSerial.readBytes(buf, LENG);
if(buf[0] ==0x4d) {
if(checkValue(buf, LENG)) {
PM01Value = transmitPM01(buf);
PM2_5Value = transmitPM2_5(buf);
PM10Value = transmitPM10(buf);
client.publish("/home/livingroom/air","{\"pm1\":"+ String(PM01Value) +", \"pm25\": "+ String(PM2_5Value) +", \"pm10\": "+ String(PM10Value) +"}");
}
}
}
}
}
voidmessageReceived(String &topic, String &payload){
Serial.println("incoming: "+ topic +" - "+ payload);
}
charcheckValue(unsignedchar*thebuf,charleng)
{
charreceiveflag =0;
intreceiveSum =0;
for(inti =0; i < (leng -2); i++) {
receiveSum = receiveSum + thebuf[i];
}
receiveSum = receiveSum +0x42;
if(receiveSum == ((thebuf[leng -2] <<8) + thebuf[leng -1]))
{
receiveSum =0;
receiveflag =1;
}
returnreceiveflag;
}
inttransmitPM01(unsignedchar*thebuf)
{
intPM01Val;
PM01Val = ((thebuf[3] <<8) + thebuf[4]);
returnPM01Val;
}
inttransmitPM2_5(unsignedchar*thebuf)
{
intPM2_5Val;
PM2_5Val = ((thebuf[5] <<8) + thebuf[6]);
returnPM2_5Val;
}
inttransmitPM10(unsignedchar*thebuf)
{
intPM10Val;
PM10Val = ((thebuf[7] <<8) + thebuf[8]);
returnPM10Val;
}