Skip to content

Commit 059d034

Browse files
committed
good sensibility sensor
1 parent 6c13c56 commit 059d034

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
Serial.begin(115200);
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+
if ((ceil(concentrationPM10) != lastDUSTPM10)&&((long)concentrationPM10>0)) {
101+
gw.send(dustMsgPM10.set((long)ceil(concentrationPM10)));
102+
lastDUSTPM10 = ceil(concentrationPM10);
103+
}
104+
105+
//sleep to save on radio
106+
gw.sleep(SLEEP_TIME);
107+
108+
}
109+
110+
111+
long getPM(int DUST_SENSOR_DIGITAL_PIN) {
112+
113+
starttime = millis();
114+
115+
while (1) {
116+
117+
duration = pulseIn(DUST_SENSOR_DIGITAL_PIN, LOW);
118+
lowpulseoccupancy += duration;
119+
endtime = millis();
120+
121+
if ((endtime-starttime) > sampletime_ms)
122+
{
123+
ratio = (lowpulseoccupancy-endtime+starttime)/(sampletime_ms*10.0); // Integer percentage 0=>100
124+
long concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
125+
//Serial.print("lowpulseoccupancy:");
126+
//Serial.print(lowpulseoccupancy);
127+
//Serial.print("\n");
128+
//Serial.print("ratio:");
129+
//Serial.print(ratio);
130+
//Serial.print("\n");
131+
//Serial.print("DSM501A:");
132+
//Serial.println(concentration);
133+
//Serial.print("\n");
134+
135+
lowpulseoccupancy = 0;
136+
return(concentration);
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)