Skip to content

Instantly share code, notes, and snippets.

@suya55
Forked from gerald-kim/co2.ino
Created July 26, 2019 05:03
Show Gist options
  • Save suya55/ae5bbfa64bae65d8fd29ae4dd45fd594 to your computer and use it in GitHub Desktop.
Save suya55/ae5bbfa64bae65d8fd29ae4dd45fd594 to your computer and use it in GitHub Desktop.
home air
#include "MHZ19.h" // include main library
#include "SoftwareSerial.h"
#include "EspMQTTClient.h"
#define RX_PIN D6 // Rx pin which the MHZ19 Tx pin is attached to
#define TX_PIN D7 // Tx pin which the MHZ19 Rx pin is attached to
#define BAUDRATE 9600
char* ID = "sensor_1";
String TOPIC = "sensors/sensor_1";
EspMQTTClient client(
"SYDNEY", // wifi
"*******", // wifi password
"192.168.3.21", // MQTT Broker server ip
"", // Can be omitted if not needed
"", // Can be omitted if not needed
ID // Client name that uniquely identify your device
);
MHZ19 myMHZ19;
SoftwareSerial mySerial(RX_PIN, TX_PIN); // Uno example
void setup() {
Serial.begin(115200);
Serial.setTimeout(2000);
mySerial.begin(BAUDRATE);
myMHZ19.begin(mySerial);
}
void onConnectionEstablished() {
client.subscribe(TOPIC + "/cmd", [] (const String &payload) {
Serial.println(payload);
if(payload == "RESET") {
Serial.println("RESET!!!!!!!");
myMHZ19.calibrateZero();
}
});
}
int count = 0;
void loop() {
send();
client.loop();
count++;
if(count > 5) {
deepSleep();
}
}
void deepSleep() {
mySerial.end();
Serial.end();
ESP.deepSleep(3 * 60e6);
}
void send() {
String co2 = String(myMHZ19.getCO2());
client.publish(TOPIC + "/co2", co2);
delay(2000);
}
connection adafruit-light-sensor
address io.adafruit.com:1883
#bridge_atetempt_unsubscribe false
bridge_protocol_version mqttv311
cleansession false
notifications false
try_private false
remote_username ******
remote_password ********
start_type automatic
topic co2 out 0 sensors/sensor_1/ ********/feeds/sensor1-
topic aqi out 0 sensors/mi_air_pro2/ ********/feeds/mi-air-pro2-
topic temperature out 0 sensors/mi_air_pro2/ ********/feeds/mi-air-pro2-
topic humidity out 0 sensors/mi_air_pro2/ ********/feeds/mi-air-pro2-
topic aqi out 0 sensors/mi_air_max/ ********/feeds/mi-air-max-
topic temperature out 0 sensors/mi_air_max/ ********/feeds/mi-air-max-
topic humidity out 0 sensors/mi_air_max/ ********/feeds/mi-air-max-
#topic sensors/sensor_1/co2 out 0 sensor1-co2
import miio
from math import ceil
import paho.mqtt.client as mqtt
mi_air_max = miio.airpurifier.AirPurifier('192.168.3.193', '************')
mi_air_pro2 = miio.airpurifier.AirPurifier('192.168.3.41', '************')
client = mqtt.Client()
client.connect("localhost")
for name in ["max", "pro2"]:
air = locals()[f"mi_air_{name}"]
status = air.status()
air.set_favorite_level(min([9, ceil(status.aqi / 3)+1]))
air.set_mode(miio.airpurifier.OperationMode.Favorite)
client.publish(f"sensors/mi_air_{name}/aqi", status.aqi)
client.publish(f"sensors/mi_air_{name}/temperature", status.temperature)
client.publish(f"sensors/mi_air_{name}/humidity", status.humidity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment