Skip to content

Commit 9d7d0cc

Browse files
committed
Merge pull request #165 from empierre/master
New sensors examples
2 parents d170b7b + 6f42c70 commit 9d7d0cc

File tree

4 files changed

+491
-33
lines changed

4 files changed

+491
-33
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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-2015 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/Arduino/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 accurancy 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+
42+
#include <MySensor.h>
43+
#include <SPI.h>
44+
45+
#define CHILD_ID_AIQ 0
46+
#define AIQ_SENSOR_ANALOG_PIN 6
47+
48+
unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
49+
50+
float valAIQ =0.0;
51+
float lastAIQ =0.0;
52+
53+
MySensor gw;
54+
MyMessage msg(CHILD_ID_AIQ, V_LEVEL);
55+
MyMessage msg2(CHILD_ID_AIQ, V_UNIT_PREFIX);
56+
57+
void setup()
58+
{
59+
gw.begin();
60+
61+
// Send the sketch version information to the gateway and Controller
62+
gw.sendSketchInfo("AIQ Sensor CO2 MH-Z14", "1.0");
63+
64+
// Register all sensors to gateway (they will be created as child devices)
65+
gw.present(CHILD_ID_AIQ, S_AIR_QUALITY);
66+
gw.send(msg2.set("ppm"));
67+
68+
gw.sleep(SLEEP_TIME);
69+
70+
pinMode(AIQ_SENSOR_ANALOG_PIN, INPUT);
71+
72+
}
73+
74+
void loop() {
75+
76+
//unsigned long duration = pulseIn(AIQ_SENSOR_ANALOG_PIN, HIGH);
77+
while(digitalRead(AIQ_SENSOR_ANALOG_PIN) == HIGH) {;}
78+
//wait for the pin to go HIGH and measure HIGH time
79+
unsigned long duration = pulseIn(AIQ_SENSOR_ANALOG_PIN, HIGH);
80+
81+
//Serial.print(duration/1000); Serial.println(" ms ");
82+
//from datasheet
83+
//CO2 ppm = 2000 * (Th - 2ms) / (Th + Tl - 4ms)
84+
// given Tl + Th = 1004
85+
// Tl = 1004 - Th
86+
// = 2000 * (Th - 2ms) / (Th + 1004 - Th -4ms)
87+
// = 2000 * (Th - 2ms) / 1000 = 2 * (Th - 2ms)
88+
long co2ppm = 2 * ((duration/1000) - 2);
89+
//Serial.print(co2ppm);
90+
if ((co2ppm != lastAIQ)&&(abs(co2ppm-lastAIQ)>=10)) {
91+
gw.send(msg.set((long)ceil(co2ppm)));
92+
lastAIQ = ceil(co2ppm);
93+
}
94+
95+
//Serial.println();
96+
97+
// Power down the radio. Note that the radio will get powered back up
98+
// on the next write() call.
99+
gw.sleep(SLEEP_TIME); //sleep for: sleepTime
100+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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-2015 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/Arduino/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+
* Dust Sensor for SamYoung DSM501
24+
* connect the sensor as follows :
25+
* Pin 2 of dust sensor PM1 -> Digital 3 (PMW)
26+
* Pin 3 of dust sensor -> +5V
27+
* Pin 4 of dust sensor PM2.5 -> Digital 6 (PWM)
28+
* Pin 5 of dust sensor -> Ground
29+
* Datasheet: http://www.samyoungsnc.com/products/3-1%20Specification%20DSM501.pdf
30+
* Contributor: epierre
31+
**/
32+
33+
34+
#include <MySensor.h>
35+
#include <SPI.h>
36+
37+
#define CHILD_ID_DUST_PM10 0
38+
#define CHILD_ID_DUST_PM25 1
39+
#define DUST_SENSOR_DIGITAL_PIN_PM10 6
40+
#define DUST_SENSOR_DIGITAL_PIN_PM25 3
41+
42+
unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
43+
//VARIABLES
44+
int val = 0; // variable to store the value coming from the sensor
45+
float valDUSTPM25 =0.0;
46+
float lastDUSTPM25 =0.0;
47+
float valDUSTPM10 =0.0;
48+
float lastDUSTPM10 =0.0;
49+
unsigned long duration;
50+
unsigned long starttime;
51+
unsigned long endtime;
52+
unsigned long sampletime_ms = 30000;
53+
unsigned long lowpulseoccupancy = 0;
54+
float ratio = 0;
55+
long concentrationPM25 = 0;
56+
long concentrationPM10 = 0;
57+
58+
MySensor gw;
59+
MyMessage dustMsgPM10(CHILD_ID_DUST_PM10, V_LEVEL);
60+
MyMessage msgPM10(CHILD_ID_DUST_PM10, V_UNIT_PREFIX);
61+
MyMessage dustMsgPM25(CHILD_ID_DUST_PM25, V_LEVEL);
62+
MyMessage msgPM25(CHILD_ID_DUST_PM25, V_UNIT_PREFIX);
63+
64+
void setup()
65+
{
66+
gw.begin();
67+
68+
// Send the sketch version information to the gateway and Controller
69+
gw.sendSketchInfo("Dust Sensor DSM501", "1.4");
70+
71+
// Register all sensors to gateway (they will be created as child devices)
72+
gw.present(CHILD_ID_DUST_PM10, S_DUST);
73+
gw.send(msgPM10.set("ppm"));
74+
gw.present(CHILD_ID_DUST_PM25, S_DUST);
75+
gw.send(msgPM25.set("ppm"));
76+
77+
pinMode(DUST_SENSOR_DIGITAL_PIN_PM10,INPUT);
78+
pinMode(DUST_SENSOR_DIGITAL_PIN_PM25,INPUT);
79+
80+
}
81+
82+
void loop()
83+
{
84+
85+
//get PM 2.5 density of particles over 2.5 μm.
86+
concentrationPM25=(long)getPM(DUST_SENSOR_DIGITAL_PIN_PM25);
87+
Serial.print("PM25: ");
88+
Serial.println(concentrationPM25);
89+
Serial.print("\n");
90+
91+
if ((concentrationPM25 != lastDUSTPM25)&&(concentrationPM25>0)) {
92+
gw.send(dustMsgPM25.set((long)ceil(concentrationPM25)));
93+
lastDUSTPM25 = ceil(concentrationPM25);
94+
}
95+
//get PM 1.0 - density of particles over 1 μm.
96+
concentrationPM10=getPM(DUST_SENSOR_DIGITAL_PIN_PM10);
97+
Serial.print("PM10: ");
98+
Serial.println(concentrationPM10);
99+
Serial.print("\n");
100+
//ppmv=mg/m3 * (0.08205*Tmp)/Molecular_mass
101+
//0.08205 = Universal gas constant in atm·m3/(kmol·K)
102+
int temp=20; //external temperature, if you can replace this with a DHT11 or better
103+
long ppmv=(concentrationPM10*0.0283168/100/1000) * (0.08205*temp)/0.01;
104+
105+
if ((ceil(concentrationPM10) != lastDUSTPM10)&&((long)concentrationPM10>0)) {
106+
gw.send(dustMsgPM10.set((long)ppmv));
107+
lastDUSTPM10 = ceil(concentrationPM10);
108+
}
109+
110+
//sleep to save on radio
111+
gw.sleep(SLEEP_TIME);
112+
113+
}
114+
115+
116+
long getPM(int DUST_SENSOR_DIGITAL_PIN) {
117+
118+
starttime = millis();
119+
120+
while (1) {
121+
122+
duration = pulseIn(DUST_SENSOR_DIGITAL_PIN, LOW);
123+
lowpulseoccupancy += duration;
124+
endtime = millis();
125+
126+
if ((endtime-starttime) > sampletime_ms)
127+
{
128+
ratio = (lowpulseoccupancy-endtime+starttime)/(sampletime_ms*10.0); // Integer percentage 0=>100
129+
long concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
130+
//Serial.print("lowpulseoccupancy:");
131+
//Serial.print(lowpulseoccupancy);
132+
//Serial.print("\n");
133+
//Serial.print("ratio:");
134+
//Serial.print(ratio);
135+
//Serial.print("\n");
136+
//Serial.print("DSM501A:");
137+
//Serial.println(concentration);
138+
//Serial.print("\n");
139+
140+
lowpulseoccupancy = 0;
141+
return(concentration);
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)