MySensors Library & Examples  2.3.2
DustSensor.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-2019 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  * REVISION HISTORY
22  * Version 1.0 - epierre
23  * Converted to 1.4 by Henrik Ekblad
24  *
25  * DESCRIPTION
26  * Arduino Dust Sensort
27  *
28  * connect the sensor as follows :
29  *
30  * VCC >>> 5V
31  * A >>> A0
32  * GND >>> GND
33  *
34  * Based on: http://www.dfrobot.com/wiki/index.php/Sharp_GP2Y1010AU
35  * Authors: Cyrille Médard de Chardon (serialC), Christophe Trefois (Trefex)
36  *
37  * http://www.mysensors.org/build/dust
38  *
39  */
40 
41 // Enable debug prints
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 
50 #include <MySensors.h>
51 
52 #define CHILD_ID_DUST 0
53 #define DUST_SENSOR_ANALOG_PIN 1
54 
55 uint32_t SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
56 //VARIABLES
57 int val = 0; // variable to store the value coming from the sensor
58 float valDUST =0.0;
59 float lastDUST =0.0;
60 int samplingTime = 280;
61 int deltaTime = 40;
62 int sleepTime = 9680;
63 float calcVoltage = 0;
64 float dustDensity = 0;
65 
66 MyMessage dustMsg(CHILD_ID_DUST, V_LEVEL);
67 
69 {
70  // Send the sketch version information to the gateway and Controller
71  sendSketchInfo("Dust Sensor", "1.1");
72 
73  // Register all sensors to gateway (they will be created as child devices)
74  present(CHILD_ID_DUST, S_DUST);
75 }
76 
77 void loop()
78 {
79  uint16_t voMeasured = analogRead(DUST_SENSOR_ANALOG_PIN);// Get DUST value
80 
81  // 0 - 5V mapped to 0 - 1023 integer values
82  // recover voltage
83  calcVoltage = voMeasured * (5.0 / 1024.0);
84 
85  // linear equation taken from http://www.howmuchsnow.com/arduino/airquality/
86  // Chris Nafis (c) 2012
87  dustDensity = (0.17 * calcVoltage - 0.1)*1000;
88 
89  Serial.print("Raw Signal Value (0-1023): ");
90  Serial.print(voMeasured);
91 
92  Serial.print(" - Voltage: ");
93  Serial.print(calcVoltage);
94 
95  Serial.print(" - Dust Density: ");
96  Serial.println(dustDensity); // unit: ug/m3
97 
98  if (ceil(dustDensity) != lastDUST) {
99  send(dustMsg.set((int16_t)ceil(dustDensity)));
100  lastDUST = ceil(dustDensity);
101  }
102 
103  sleep(SLEEP_TIME);
104 }
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
loop
void loop()
Main loop.
Definition: DustSensor.ino:77
presentation
void presentation()
Node presentation.
Definition: DustSensor.ino:68
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)
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:289