Advertisement
einsupershop

MH-Z14A CO2 Sensor

Jan 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <MySensor.h>  
  2. #include <SPI.h>
  3.  
  4. #define CHILD_ID 0
  5. #define CO2_SENSOR_PWM_PIN 2
  6.  
  7. unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
  8.  
  9. float valAIQ =0.0;
  10. float lastAIQ =0.0;
  11. unsigned long duration;
  12. long ppm;
  13. MySensor gw;
  14. MyMessage msg(CHILD_ID, V_LEVEL);
  15.  
  16. void setup()  
  17. {
  18.   gw.begin();
  19.  
  20.   // Send the sketch version information to the gateway and Controller
  21.   gw.sendSketchInfo("AIQ Sensor CO2 MH-Z14A", "1.0");
  22.  
  23.   // Register all sensors to gateway (they will be created as child devices)
  24.   gw.present(CHILD_ID, S_AIR_QUALITY);  
  25.  
  26.   pinMode(CO2_SENSOR_PWM_PIN, INPUT);
  27.    
  28. }
  29.  
  30. void loop() {
  31.  
  32.   while(digitalRead(CO2_SENSOR_PWM_PIN) == HIGH) {;}
  33.   //wait for the pin to go HIGH and measure HIGH time
  34.   duration = pulseIn(CO2_SENSOR_PWM_PIN, HIGH, 2000000);
  35.   ppm = 5000 * ((duration/1000) - 2)/1000;
  36.   Serial.print(ppm);
  37.   if ((ppm != lastAIQ)&&(abs(ppm-lastAIQ)>=10)) {
  38.       gw.send(msg.set((long)ceil(ppm)));
  39.       lastAIQ = ceil(ppm);
  40.   }
  41.  
  42.   gw.sleep(SLEEP_TIME); //sleep for: sleepTime
  43. }
  44.  
  45. Credit to alexsh1
  46. https://forum.mysensors.org/topic/3821/mh-z14a-co2-sensor/4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement