Merge pull request #165 from empierre/master

New sensors examples
This commit is contained in:
Henrik Ekblad
2015-08-12 21:02:32 +02:00
4 changed files with 491 additions and 33 deletions
@@ -0,0 +1,100 @@
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* DESCRIPTION
*
* MH-Z14 CO2 sensor
*
* Wiring:
* Pad 1, Pad 5: Vin (Voltage input 4.5V-6V)
* Pad 2, Pad 3, Pad 12: GND
* Pad 6: PWM output ==> pin 6
*
* From: http://davidegironi.blogspot.fr/2014/01/co2-meter-using-ndir-infrared-mh-z14.html
* MH-Z14 has a PWM output, with a sensitivity range of 0ppm to 2000ppm CO2, an accurancy of ±200ppm.
* 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:
* CO2ppm = 2000 * (Th - 2ms) /(Th + Tl - 4ms)
* From: http://airqualityegg.wikispaces.com/Sensor+Tests
* - response time is less than 30 s
* - 3 minute warm up time
* datasheet: http://www.futurlec.com/Datasheet/Sensor/MH-Z14.pdf
* Contributor: epierre
**/
#include <MySensor.h>
#include <SPI.h>
#define CHILD_ID_AIQ 0
#define AIQ_SENSOR_ANALOG_PIN 6
unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
float valAIQ =0.0;
float lastAIQ =0.0;
MySensor gw;
MyMessage msg(CHILD_ID_AIQ, V_LEVEL);
MyMessage msg2(CHILD_ID_AIQ, V_UNIT_PREFIX);
void setup()
{
gw.begin();
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("AIQ Sensor CO2 MH-Z14", "1.0");
// Register all sensors to gateway (they will be created as child devices)
gw.present(CHILD_ID_AIQ, S_AIR_QUALITY);
gw.send(msg2.set("ppm"));
gw.sleep(SLEEP_TIME);
pinMode(AIQ_SENSOR_ANALOG_PIN, INPUT);
}
void loop() {
//unsigned long duration = pulseIn(AIQ_SENSOR_ANALOG_PIN, HIGH);
while(digitalRead(AIQ_SENSOR_ANALOG_PIN) == HIGH) {;}
//wait for the pin to go HIGH and measure HIGH time
unsigned long duration = pulseIn(AIQ_SENSOR_ANALOG_PIN, HIGH);
//Serial.print(duration/1000); Serial.println(" ms ");
//from datasheet
//CO2 ppm = 2000 * (Th - 2ms) / (Th + Tl - 4ms)
// given Tl + Th = 1004
// Tl = 1004 - Th
// = 2000 * (Th - 2ms) / (Th + 1004 - Th -4ms)
// = 2000 * (Th - 2ms) / 1000 = 2 * (Th - 2ms)
long co2ppm = 2 * ((duration/1000) - 2);
//Serial.print(co2ppm);
if ((co2ppm != lastAIQ)&&(abs(co2ppm-lastAIQ)>=10)) {
gw.send(msg.set((long)ceil(co2ppm)));
lastAIQ = ceil(co2ppm);
}
//Serial.println();
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
gw.sleep(SLEEP_TIME); //sleep for: sleepTime
}
@@ -0,0 +1,144 @@
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* DESCRIPTION
*
* Dust Sensor for SamYoung DSM501
* connect the sensor as follows :
* Pin 2 of dust sensor PM1 -> Digital 3 (PMW)
* Pin 3 of dust sensor -> +5V
* Pin 4 of dust sensor PM2.5 -> Digital 6 (PWM)
* Pin 5 of dust sensor -> Ground
* Datasheet: http://www.samyoungsnc.com/products/3-1%20Specification%20DSM501.pdf
* Contributor: epierre
**/
#include <MySensor.h>
#include <SPI.h>
#define CHILD_ID_DUST_PM10 0
#define CHILD_ID_DUST_PM25 1
#define DUST_SENSOR_DIGITAL_PIN_PM10 6
#define DUST_SENSOR_DIGITAL_PIN_PM25 3
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 valDUSTPM25 =0.0;
float lastDUSTPM25 =0.0;
float valDUSTPM10 =0.0;
float lastDUSTPM10 =0.0;
unsigned long duration;
unsigned long starttime;
unsigned long endtime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
long concentrationPM25 = 0;
long concentrationPM10 = 0;
MySensor gw;
MyMessage dustMsgPM10(CHILD_ID_DUST_PM10, V_LEVEL);
MyMessage msgPM10(CHILD_ID_DUST_PM10, V_UNIT_PREFIX);
MyMessage dustMsgPM25(CHILD_ID_DUST_PM25, V_LEVEL);
MyMessage msgPM25(CHILD_ID_DUST_PM25, V_UNIT_PREFIX);
void setup()
{
gw.begin();
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Dust Sensor DSM501", "1.4");
// Register all sensors to gateway (they will be created as child devices)
gw.present(CHILD_ID_DUST_PM10, S_DUST);
gw.send(msgPM10.set("ppm"));
gw.present(CHILD_ID_DUST_PM25, S_DUST);
gw.send(msgPM25.set("ppm"));
pinMode(DUST_SENSOR_DIGITAL_PIN_PM10,INPUT);
pinMode(DUST_SENSOR_DIGITAL_PIN_PM25,INPUT);
}
void loop()
{
//get PM 2.5 density of particles over 2.5 μm.
concentrationPM25=(long)getPM(DUST_SENSOR_DIGITAL_PIN_PM25);
Serial.print("PM25: ");
Serial.println(concentrationPM25);
Serial.print("\n");
if ((concentrationPM25 != lastDUSTPM25)&&(concentrationPM25>0)) {
gw.send(dustMsgPM25.set((long)ceil(concentrationPM25)));
lastDUSTPM25 = ceil(concentrationPM25);
}
//get PM 1.0 - density of particles over 1 μm.
concentrationPM10=getPM(DUST_SENSOR_DIGITAL_PIN_PM10);
Serial.print("PM10: ");
Serial.println(concentrationPM10);
Serial.print("\n");
//ppmv=mg/m3 * (0.08205*Tmp)/Molecular_mass
//0.08205 = Universal gas constant in atm·m3/(kmol·K)
int temp=20; //external temperature, if you can replace this with a DHT11 or better
long ppmv=(concentrationPM10*0.0283168/100/1000) * (0.08205*temp)/0.01;
if ((ceil(concentrationPM10) != lastDUSTPM10)&&((long)concentrationPM10>0)) {
gw.send(dustMsgPM10.set((long)ppmv));
lastDUSTPM10 = ceil(concentrationPM10);
}
//sleep to save on radio
gw.sleep(SLEEP_TIME);
}
long getPM(int DUST_SENSOR_DIGITAL_PIN) {
starttime = millis();
while (1) {
duration = pulseIn(DUST_SENSOR_DIGITAL_PIN, LOW);
lowpulseoccupancy += duration;
endtime = millis();
if ((endtime-starttime) > sampletime_ms)
{
ratio = (lowpulseoccupancy-endtime+starttime)/(sampletime_ms*10.0); // Integer percentage 0=>100
long concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
//Serial.print("lowpulseoccupancy:");
//Serial.print(lowpulseoccupancy);
//Serial.print("\n");
//Serial.print("ratio:");
//Serial.print(ratio);
//Serial.print("\n");
//Serial.print("DSM501A:");
//Serial.println(concentration);
//Serial.print("\n");
lowpulseoccupancy = 0;
return(concentration);
}
}
}
@@ -18,48 +18,168 @@
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik Ekblad
*
* DESCRIPTION
* Example sketch sending soil moisture alarm to controller
* http://www.mysensors.org/build/moisture
*/
*
* Arduino soil moisture based on gypsum sensor/resistive sensor to avoid electric catalyse in soil
* Required to interface the sensor: 2 * 4.7kOhm + 2 * 1N4148
*
* Gypsum sensor and calibration:
* DIY: See http://vanderleevineyard.com/1/category/vinduino/1.html
* Built: Davis / Watermark 200SS
* http://www.cooking-hacks.com/watermark-soil-moisture-sensor?_bksrc=item2item&_bkloc=product
* http://www.irrometer.com/pdf/supportmaterial/sensors/voltage-WM-chart.pdf
* cb (centibar) http://www.irrometer.com/basics.html
* 0-10 Saturated Soil. Occurs for a day or two after irrigation
* 10-20 Soil is adequately wet (except coarse sands which are drying out at this range)
* 30-60 Usual range to irrigate or water (except heavy clay soils).
* 60-100 Usual range to irrigate heavy clay soils
* 100-200 Soil is becoming dangerously dry for maximum production. Proceed with caution.
*
* Connection:
* D6, D7: alternative powering to avoid sensor degradation
* A0, A1: alternative resistance mesuring
*
* Based on:
* "Vinduino" portable soil moisture sensor code V3.00
* Date December 31, 2012
* Reinier van der Lee and Theodore Kaskalis
* www.vanderleevineyard.com
* Contributor: epierre
**/
// Copyright (C) 2015, Reinier van der Lee
// www.vanderleevineyard.com
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
#include <math.h> // Conversion equation from resistance to %
#include <SPI.h>
#include <MySensor.h>
#include <MySensor.h>
#define DIGITAL_INPUT_SOIL_SENSOR 3 // Digital input did you attach your soil sensor.
#define INTERRUPT DIGITAL_INPUT_SOIL_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID 0 // Id of the sensor child
// Setting up format for reading 3 soil sensors
#define NUM_READS 10 // Number of sensor reads for filtering
#define CHILD_ID 0
MySensor gw;
MyMessage msg(CHILD_ID, V_TRIPPED);
int lastSoilValue = -1;
MySensor gw; // Arduino initialization
MyMessage msg(CHILD_ID, V_LEVEL);
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
void setup()
{
long buffer[NUM_READS];
int index;
typedef struct { // Structure to be used in percentage and resistance values matrix to be filtered (have to be in pairs)
int moisture;
long resistance;
} values;
const long knownResistor = 4700; // Constant value of known resistor in Ohms
int supplyVoltage; // Measured supply voltage
int sensorVoltage; // Measured sensor voltage
values valueOf[NUM_READS]; // Calculated moisture percentages and resistances to be sorted and filtered
int i; // Simple index variable
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(115200);
gw.begin();
gw.sendSketchInfo("Soil Moisture Sensor Reverse Polarity", "1.0");
gw.present(CHILD_ID, S_HUM);
// initialize the digital pins as an output.
// Pin 6,7 is for sensor 1
// initialize the digital pin as an output.
// Pin 6 is sense resistor voltage supply 1
pinMode(6, OUTPUT);
// initialize the digital pin as an output.
// Pin 7 is sense resistor voltage supply 2
pinMode(7, OUTPUT);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Soil Moisture Sensor", "1.0");
// sets the soil sensor digital pin as input
pinMode(DIGITAL_INPUT_SOIL_SENSOR, INPUT);
// Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID, S_MOTION);
}
void loop()
{
// Read digital soil value
int soilValue = digitalRead(DIGITAL_INPUT_SOIL_SENSOR); // 1 = Not triggered, 0 = In soil with water
if (soilValue != lastSoilValue) {
Serial.println(soilValue);
gw.send(msg.set(soilValue==0?1:0)); // Send the inverse to gw as tripped should be when no water in soil
lastSoilValue = soilValue;
void loop() {
measure(6,7,1);
Serial.print ("\t");
Serial.println (average());
long read1 = average();
measure(7,6,0);
Serial.print ("\t");
Serial.println (average());
long read2= average();
long sensor1 = (read1 + read2)/2;
Serial.print ("resistance bias =" );
Serial.println (read1-read2);
Serial.print ("sensor bias compensated value = ");
Serial.println (sensor1);
Serial.println ();
//send back the values
gw.send(msg.set((long int)ceil(sensor1)));
// delay until next measurement (msec)
gw.sleep(SLEEP_TIME);
}
void measure (int phase_b, int phase_a, int analog_input)
{
// read sensor, filter, and calculate resistance value
// Noise filter: median filter
for (i=0; i<NUM_READS; i++) {
// Read 1 pair of voltage values
digitalWrite(phase_a, HIGH); // set the voltage supply on
delayMicroseconds(25);
supplyVoltage = analogRead(analog_input); // read the supply voltage
delayMicroseconds(25);
digitalWrite(phase_a, LOW); // set the voltage supply off
delay(1);
digitalWrite(phase_b, HIGH); // set the voltage supply on
delayMicroseconds(25);
sensorVoltage = analogRead(analog_input); // read the sensor voltage
delayMicroseconds(25);
digitalWrite(phase_b, LOW); // set the voltage supply off
// Calculate resistance
// the 0.5 add-term is used to round to the nearest integer
// Tip: no need to transform 0-1023 voltage value to 0-5 range, due to following fraction
long resistance = (knownResistor * (supplyVoltage - sensorVoltage ) / sensorVoltage) ;
delay(1);
addReading(resistance);
Serial.print (resistance);
Serial.print ("\t");
}
// Power down the radio and arduino until digital input changes.
gw.sleep(INTERRUPT,CHANGE);
}
// Averaging algorithm
void addReading(long resistance){
buffer[index] = resistance;
index++;
if (index >= NUM_READS) index = 0;
}
long average(){
long sum = 0;
for (int i = 0; i < NUM_READS; i++){
sum += buffer[i];
}
return (long)(sum / NUM_READS);
}
@@ -0,0 +1,94 @@
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* DESCRIPTION
*
* Vibration Sensor
*
* connect the sensor as follows :
*
* VCC >>> 5V
* S >>> D3
* GND >>> GND
*
* Based on: http://www.dfrobot.com/wiki/index.php/DFRobot_Digital_Vibration_Sensor_V2_SKU:DFR0027
* Contributor: epierre
**/
#include <MySensor.h>
#include <SPI.h>
#include <Wire.h>
#define CHILD_ID_VIBRATION 0
#define VIBRATION_SENSOR_DIGITAL_PIN 3
#define SensorLED 13
unsigned long SLEEP_TIME = 10*1000; // Sleep time between reads (in seconds)
//VARIABLES
int val = 0; // variable to store the value coming from the sensor
float valVIBRATION =0.0;
float lastVIBRATION =0.0;
unsigned char state = 0;
MySensor gw;
MyMessage vibrationMsg(CHILD_ID_VIBRATION, V_LEVEL);
void setup()
{
gw.begin();
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("VIBRATION Sensor", "1.0");
// Register all sensors to gateway (they will be created as child devices)
gw.present(CHILD_ID_VIBRATION, S_VIBRATION);
pinMode(VIBRATION_SENSOR_DIGITAL_PIN, INPUT);
attachInterrupt(1, blink, FALLING);// Trigger the blink function when the falling edge is detected
pinMode(SensorLED, OUTPUT);
}
void loop()
{
if(state>=40){ // basically below 40 so ignire basic level
gw.send(vibrationMsg.set(int(state)));
state = 0;
digitalWrite(SensorLED,HIGH);
} else {
state = 0;
digitalWrite(SensorLED,LOW);
}
// Power down the radio. Note that the radio will get powered back up
// on the next write() call.
delay(1000); //delay to allow serial to fully print before sleep
gw.sleep(SLEEP_TIME); //sleep for: sleepTime
}
void blink()//Interrupts function
{
state++;
}