MySensors Library & Examples  2.3.2-62-ge298769
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-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  * 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 //#define MY_PJON
50 
51 #include <MySensors.h>
52 
53 #define CHILD_ID_DUST 0
54 #define DUST_SENSOR_ANALOG_PIN 1
55 
56 uint32_t SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
57 //VARIABLES
58 int val = 0; // variable to store the value coming from the sensor
59 float valDUST =0.0;
60 float lastDUST =0.0;
61 int samplingTime = 280;
62 int deltaTime = 40;
63 int sleepTime = 9680;
64 float calcVoltage = 0;
65 float dustDensity = 0;
66 
67 MyMessage dustMsg(CHILD_ID_DUST, V_LEVEL);
68 
70 {
71  // Send the sketch version information to the gateway and Controller
72  sendSketchInfo("Dust Sensor", "1.1");
73 
74  // Register all sensors to gateway (they will be created as child devices)
75  present(CHILD_ID_DUST, S_DUST);
76 }
77 
78 void loop()
79 {
80  uint16_t voMeasured = analogRead(DUST_SENSOR_ANALOG_PIN);// Get DUST value
81 
82  // 0 - 5V mapped to 0 - 1023 integer values
83  // recover voltage
84  calcVoltage = voMeasured * (5.0 / 1024.0);
85 
86  // linear equation taken from http://www.howmuchsnow.com/arduino/airquality/
87  // Chris Nafis (c) 2012
88  dustDensity = (0.17 * calcVoltage - 0.1)*1000;
89 
90  Serial.print("Raw Signal Value (0-1023): ");
91  Serial.print(voMeasured);
92 
93  Serial.print(" - Voltage: ");
94  Serial.print(calcVoltage);
95 
96  Serial.print(" - Dust Density: ");
97  Serial.println(dustDensity); // unit: ug/m3
98 
99  if (ceil(dustDensity) != lastDUST) {
100  send(dustMsg.set((int16_t)ceil(dustDensity)));
101  lastDUST = ceil(dustDensity);
102  }
103 
104  sleep(SLEEP_TIME);
105 }
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
loop
void loop()
Main loop.
Definition: DustSensor.ino:78
presentation
void presentation()
Node presentation.
Definition: DustSensor.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)
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