MySensors Library & Examples  2.3.2-62-ge298769
CO2Sensor.ino
1 /*
2  * The MySensors Arduino library handles the wireless radio link and protocol
3  * between your home built sensors/actuators and HA controller of choice.
4  * The sensors forms a self healing radio network with optional repeaters. Each
5  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6  * network topology allowing messages to be routed to nodes.
7  *
8  * Created by Henrik Ekblad <[email protected]>
9  * Copyright (C) 2013-2022 Sensnology AB
10  * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11  *
12  * Documentation: http://www.mysensors.org
13  * Support Forum: http://forum.mysensors.org
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * version 2 as published by the Free Software Foundation.
18  *
19  *******************************
20  *
21  * DESCRIPTION
22  *
23  * MH-Z14 CO2 sensor
24  *
25  * Wiring:
26  * Pad 1, Pad 5: Vin (Voltage input 4.5V-6V)
27  * Pad 2, Pad 3, Pad 12: GND
28  * Pad 6: PWM output ==> pin 6
29  *
30  * From: http://davidegironi.blogspot.fr/2014/01/co2-meter-using-ndir-infrared-mh-z14.html
31  * MH-Z14 has a PWM output, with a sensitivity range of 0ppm to 2000ppm CO2, an accuracy of ±200ppm.
32  * The cycle is 1004ms±5%, given the duty cicle Th (pulse high), Tl is 1004-Th, we can convert it to CO2 value using the formula:
33  * CO2ppm = 2000 * (Th - 2ms) /(Th + Tl - 4ms)
34  * From: http://airqualityegg.wikispaces.com/Sensor+Tests
35  * - response time is less than 30 s
36  * - 3 minute warm up time
37  * datasheet: http://www.futurlec.com/Datasheet/Sensor/MH-Z14.pdf
38  * Contributor: epierre
39  */
40 
41 // Enable debug prints to serial monitor
42 #define MY_DEBUG
43 
44 // Enable and select radio type attached
45 #define MY_RADIO_RF24
46 //#define MY_RADIO_NRF5_ESB
47 //#define MY_RADIO_RFM69
48 //#define MY_RADIO_RFM95
49 //#define MY_PJON
50 
51 #include <MySensors.h>
52 
53 #define CHILD_ID_AIQ 0
54 #define AIQ_SENSOR_ANALOG_PIN 6
55 
56 uint32_t SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
57 
58 float valAIQ =0.0;
59 float lastAIQ =0.0;
60 
61 MyMessage msg(CHILD_ID_AIQ, V_LEVEL);
62 MyMessage msg2(CHILD_ID_AIQ, V_UNIT_PREFIX);
63 
64 void setup()
65 {
66  pinMode(AIQ_SENSOR_ANALOG_PIN, INPUT);
67 }
68 
70 {
71  // Send the sketch version information to the gateway and Controller
72  sendSketchInfo("AIQ Sensor CO2 MH-Z14", "1.0");
73 
74  // Register all sensors to gateway (they will be created as child devices)
75  present(CHILD_ID_AIQ, S_AIR_QUALITY);
76  send(msg2.set("ppm"));
77 }
78 
79 void loop()
80 {
81 
82  //uint32_t duration = pulseIn(AIQ_SENSOR_ANALOG_PIN, HIGH);
83  while(digitalRead(AIQ_SENSOR_ANALOG_PIN) == HIGH) {
84  ;
85  }
86  //wait for the pin to go HIGH and measure HIGH time
87  uint32_t duration = pulseIn(AIQ_SENSOR_ANALOG_PIN, HIGH);
88 
89  //Serial.print(duration/1000); Serial.println(" ms ");
90  //from datasheet
91  //CO2 ppm = 2000 * (Th - 2ms) / (Th + Tl - 4ms)
92  // given Tl + Th = 1004
93  // Tl = 1004 - Th
94  // = 2000 * (Th - 2ms) / (Th + 1004 - Th -4ms)
95  // = 2000 * (Th - 2ms) / 1000 = 2 * (Th - 2ms)
96  long co2ppm = 2 * ((duration/1000) - 2);
97  //Serial.print(co2ppm);
98  if ((co2ppm != lastAIQ)&&(abs(co2ppm-lastAIQ)>=10)) {
99  send(msg.set((int32_t)ceil(co2ppm)));
100  lastAIQ = ceil(co2ppm);
101  }
102 
103  //Serial.println();
104 
105  // Power down the radio. Note that the radio will get powered back up
106  // on the next write() call.
107  sleep(SLEEP_TIME); //sleep for: sleepTime
108 }
HIGH
#define HIGH
Definition: bcm2835.h:572
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
loop
void loop()
Main loop.
Definition: CO2Sensor.ino:79
presentation
void presentation()
Node presentation.
Definition: CO2Sensor.ino:69
MyMessage::set
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
send
bool send(MyMessage &msg, const bool requestEcho=false)
present
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
setup
void setup()
Called after node initialises but before main loop.
Definition: CO2Sensor.ino:64
sleep
int8_t sleep(const uint32_t sleepingMS, const bool smartSleep=false)
MySensors.h
API declaration for MySensors.
MyMessage
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:290