From dcb0babe63085366155388cb04d4bc6962f73af2 Mon Sep 17 00:00:00 2001 From: Henrik Ekblad Date: Sun, 17 Aug 2014 19:57:58 +0200 Subject: [PATCH] Dust sensor added. Thanks epierre. --- libraries/MySensors/MyMessage.h | 5 +- .../examples/DustSensor/DustSensor.ino | 80 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 libraries/MySensors/examples/DustSensor/DustSensor.ino diff --git a/libraries/MySensors/MyMessage.h b/libraries/MySensors/MyMessage.h index cd2d7b2..dddfcbe 100644 --- a/libraries/MySensors/MyMessage.h +++ b/libraries/MySensors/MyMessage.h @@ -38,7 +38,8 @@ typedef enum { V_RAINRATE, V_WIND, V_GUST, V_DIRECTION, V_UV, V_WEIGHT, V_DISTANCE, V_IMPEDANCE, V_ARMED, V_TRIPPED, V_WATT, V_KWH, V_SCENE_ON, V_SCENE_OFF, V_HEATER, V_HEATER_SW, V_LIGHT_LEVEL, V_VAR1, V_VAR2, V_VAR3, V_VAR4, V_VAR5, - V_UP, V_DOWN, V_STOP, V_IR_SEND, V_IR_RECEIVE, V_FLOW, V_VOLUME, V_LOCK_STATUS + V_UP, V_DOWN, V_STOP, V_IR_SEND, V_IR_RECEIVE, V_FLOW, V_VOLUME, V_LOCK_STATUS, + V_DUST_LEVEL } data; // Type of internal messages (for internal messages) @@ -53,7 +54,7 @@ typedef enum { typedef enum { S_DOOR, S_MOTION, S_SMOKE, S_LIGHT, S_DIMMER, S_COVER, S_TEMP, S_HUM, S_BARO, S_WIND, S_RAIN, S_UV, S_WEIGHT, S_POWER, S_HEATER, S_DISTANCE, S_LIGHT_LEVEL, S_ARDUINO_NODE, - S_ARDUINO_REPEATER_NODE, S_LOCK, S_IR, S_WATER, S_AIR_QUALITY, S_CUSTOM + S_ARDUINO_REPEATER_NODE, S_LOCK, S_IR, S_WATER, S_AIR_QUALITY, S_CUSTOM, S_DUST } sensor; // Type of data stream (for streamed message) diff --git a/libraries/MySensors/examples/DustSensor/DustSensor.ino b/libraries/MySensors/examples/DustSensor/DustSensor.ino new file mode 100644 index 0000000..4ebe804 --- /dev/null +++ b/libraries/MySensors/examples/DustSensor/DustSensor.ino @@ -0,0 +1,80 @@ +/* + Arduino Dust Sensort + + connect the sensor as follows : + + VCC >>> 5V + A >>> A0 + GND >>> GND + + Based on: http://www.dfrobot.com/wiki/index.php/Sharp_GP2Y1010AU + Authors: Cyrille Médard de Chardon (serialC), Christophe Trefois (Trefex) + Contribution: epierre + COnverted to 1.4 by Henrik Ekblad + + The dust sensor used (see purchase guide for latest link): + http://rover.ebay.com/rover/1/711-53200-19255-0/1?icep_ff3=2&pub=5575069610&toolid=10001&campid=5337433187&customid=&icep_item=171259125886&ipn=psmain&icep_vectorid=229466&kwid=902099&mtid=824&kw=lg + +*/ + +#include +#include + +#define CHILD_ID_DUST 0 +#define DUST_SENSOR_ANALOG_PIN 1 + +unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds) +//VARIABLES +int val = 0; // variable to store the value coming from the sensor +float valDUST =0.0; +float lastDUST =0.0; +int samplingTime = 280; +int deltaTime = 40; +int sleepTime = 9680; +float voMeasured = 0; +float calcVoltage = 0; +float dustDensity = 0; + +MySensor gw; +MyMessage dustMsg(CHILD_ID_DUST, V_DUST_LEVEL); + +void setup() +{ + gw.begin(); + + // Send the sketch version information to the gateway and Controller + gw.sendSketchInfo("Dust Sensor", "1.1"); + + // Register all sensors to gateway (they will be created as child devices) + gw.present(CHILD_ID_DUST, S_DUST); + +} + +void loop() +{ + uint16_t voMeasured = analogRead(DUST_SENSOR_ANALOG_PIN);// Get DUST value + + // 0 - 5V mapped to 0 - 1023 integer values + // recover voltage + calcVoltage = voMeasured * (5.0 / 1024.0); + + // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/ + // Chris Nafis (c) 2012 + dustDensity = (0.17 * calcVoltage - 0.1)*1000; + + Serial.print("Raw Signal Value (0-1023): "); + Serial.print(voMeasured); + + Serial.print(" - Voltage: "); + Serial.print(calcVoltage); + + Serial.print(" - Dust Density: "); + Serial.println(dustDensity); // unit: ug/m3 + + if (ceil(dustDensity) != lastDUST) { + gw.send(dustMsg.set((int)ceil(dustDensity))); + lastDUST = ceil(dustDensity); + } + + gw.sleep(SLEEP_TIME); +}