Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MySensor.h>
- #include <SPI.h>
- #define CHILD_ID 0
- #define CO2_SENSOR_PWM_PIN 2
- unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
- float valAIQ =0.0;
- float lastAIQ =0.0;
- unsigned long duration;
- long ppm;
- MySensor gw;
- MyMessage msg(CHILD_ID, V_LEVEL);
- void setup()
- {
- gw.begin();
- // Send the sketch version information to the gateway and Controller
- gw.sendSketchInfo("AIQ Sensor CO2 MH-Z14A", "1.0");
- // Register all sensors to gateway (they will be created as child devices)
- gw.present(CHILD_ID, S_AIR_QUALITY);
- pinMode(CO2_SENSOR_PWM_PIN, INPUT);
- }
- void loop() {
- while(digitalRead(CO2_SENSOR_PWM_PIN) == HIGH) {;}
- //wait for the pin to go HIGH and measure HIGH time
- duration = pulseIn(CO2_SENSOR_PWM_PIN, HIGH, 2000000);
- ppm = 5000 * ((duration/1000) - 2)/1000;
- Serial.print(ppm);
- if ((ppm != lastAIQ)&&(abs(ppm-lastAIQ)>=10)) {
- gw.send(msg.set((long)ceil(ppm)));
- lastAIQ = ceil(ppm);
- }
- gw.sleep(SLEEP_TIME); //sleep for: sleepTime
- }
- Credit to alexsh1
- https://forum.mysensors.org/topic/3821/mh-z14a-co2-sensor/4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement