Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Thomas Mørch
2015-03-28 23:20:03 +01:00
2 changed files with 109 additions and 67 deletions
@@ -1,5 +1,7 @@
#include <SPI.h>
#include <MySensor.h>
/* /*
Vera Arduino UVM-30A Arduino UVM-30A
connect the sensor as follows : connect the sensor as follows :
@@ -7,44 +9,55 @@
- >>> GND - >>> GND
out >>> A0 out >>> A0
Contribution: epierre, bulldoglowell Contribution: epierre, bulldoglowell, gizmocuz
Converted to 1.4 by Henrik Ekblad
Index table taken from: http://www.elecrow.com/sensors-c-111/environment-c-111_112/uv-sensor-moduleuvm30a-p-716.html
Because this table is pretty lineair, we can calculate a UVI with one decimal
License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0) License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
*/ */
#include <MySensor.h> #include <MySensor.h>
#include <SPI.h> #include <SPI.h>
#define CHILD_ID_UV 0
#define UV_SENSOR_ANALOG_PIN 0 #define UV_SENSOR_ANALOG_PIN 0
#define CHILD_ID_UV 0
unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds) unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
MySensor gw; MySensor gw;
MyMessage uvMsg(CHILD_ID_UV, V_UV); MyMessage uvMsg(CHILD_ID_UV, V_UV);
int lastUV = -1;
int uvIndexValue [13] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170, 3000}; unsigned long lastSend =0;
int uvIndex; float uvIndex;
float lastUV = -1;
int uvIndexValue [12] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170};
void setup() void setup()
{ {
gw.begin(); gw.begin();
// Send the sketch version information to the gateway and Controller // Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("UV Sensor", "1.1"); gw.sendSketchInfo("UV Sensor", "1.2");
// Register all sensors to gateway (they will be created as child devices) // Register all sensors to gateway (they will be created as child devices)
gw.present(CHILD_ID_UV, S_UV); gw.present(CHILD_ID_UV, S_UV);
} }
void loop() void loop()
{ {
uint16_t uv = analogRead(0);// Get UV value unsigned long currentTime = millis();
Serial.print("Uv reading: ");
Serial.println(uv); uint16_t uv = analogRead(UV_SENSOR_ANALOG_PIN);// Get UV value
for (int i = 0; i < 13; i++) if (uv>1170)
uv=1170;
//Serial.print("UV Analog reading: ");
//Serial.println(uv);
int i;
for (i = 0; i < 12; i++)
{ {
if (uv <= uvIndexValue[i]) if (uv <= uvIndexValue[i])
{ {
@@ -52,11 +65,21 @@ void loop()
break; break;
} }
} }
Serial.print("Uv index: ");
Serial.println(uvIndex);
if (uvIndex != lastUV) { //calculate 1 decimal if possible
gw.send(uvMsg.set(uvIndex)); if (i>0) {
float vRange=uvIndexValue[i]-uvIndexValue[i-1];
float vCalc=uv-uvIndexValue[i-1];
uvIndex+=(1.0/vRange)*vCalc-1.0;
}
//Serial.print("UVI: ");
//Serial.println(uvIndex,2);
//Send value to gateway if changed, or at least every 5 minutes
if ((uvIndex != lastUV)||(currentTime-lastSend >= 5*60*1000)) {
lastSend=currentTime;
gw.send(uvMsg.set(uvIndex,2));
lastUV = uvIndex; lastUV = uvIndex;
} }
@@ -13,14 +13,22 @@
#include <MySensor.h> #include <MySensor.h>
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!) #define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your sensor. (Only 2 and 3 generates interrupt!)
#define SENSOR_INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define PULSE_FACTOR 1000 // Nummber of blinks per m3 of your meter (One rotation/liter) #define PULSE_FACTOR 1000 // Nummber of blinks per m3 of your meter (One rotation/liter)
#define SLEEP_MODE false // flowvalue can only be reported when sleep mode is false. #define SLEEP_MODE false // flowvalue can only be reported when sleep mode is false.
#define MAX_FLOW 40 // Max flow (l/min) value to report. This filetrs outliers.
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway) #define MAX_FLOW 40 // Max flow (l/min) value to report. This filters outliers.
#define CHILD_ID 5 // Id of the sensor child
#define CHILD_ID 1 // Id of the sensor child
unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't want to spam the gateway. unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
MySensor gw; MySensor gw;
MyMessage flowMsg(CHILD_ID,V_FLOW);
MyMessage volumeMsg(CHILD_ID,V_VOLUME);
MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
double ppl = ((double)PULSE_FACTOR)/1000; // Pulses per liter double ppl = ((double)PULSE_FACTOR)/1000; // Pulses per liter
@@ -31,78 +39,86 @@ boolean pcReceived = false;
unsigned long oldPulseCount = 0; unsigned long oldPulseCount = 0;
unsigned long newBlink = 0; unsigned long newBlink = 0;
double oldflow = 0; double oldflow = 0;
double volume; double volume =0;
double oldvolume; double oldvolume =0;
unsigned long lastSend; unsigned long lastSend =0;
unsigned long lastPulse; unsigned long lastPulse =0;
unsigned long currentTime;
boolean metric;
MyMessage flowMsg(CHILD_ID,V_FLOW);
MyMessage volumeMsg(CHILD_ID,V_VOLUME);
MyMessage pcMsg(CHILD_ID,V_VAR1);
void setup() void setup()
{ {
gw.begin(incomingMessage); gw.begin(incomingMessage);
// Send the sketch version information to the gateway and Controller // Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Water Meter", "1.0"); gw.sendSketchInfo("Water Meter", "1.2");
// Register this device as Waterflow sensor // Register this device as Waterflow sensor
gw.present(CHILD_ID, S_WATER); gw.present(CHILD_ID, S_WATER);
pulseCount = oldPulseCount = 0;
// Fetch last known pulse count value from gw // Fetch last known pulse count value from gw
gw.request(CHILD_ID, V_VAR1); gw.request(CHILD_ID, V_VAR1);
attachInterrupt(INTERRUPT, onPulse, RISING); lastSend = lastPulse = millis();
lastSend = millis();
attachInterrupt(SENSOR_INTERRUPT, onPulse, RISING);
} }
void loop() void loop()
{ {
gw.process(); gw.process();
currentTime = millis(); unsigned long currentTime = millis();
// Only send values at a maximum frequency or woken up from sleep // Only send values at a maximum frequency or woken up from sleep
bool sendTime = currentTime - lastSend > SEND_FREQUENCY; if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY))
if (pcReceived && (SLEEP_MODE || sendTime)) { {
// New flow value has been calculated lastSend=currentTime;
if (!pcReceived) {
//Last Pulsecount not yet received from controller, request it again
gw.request(CHILD_ID, V_VAR1);
return;
}
if (!SLEEP_MODE && flow != oldflow) { if (!SLEEP_MODE && flow != oldflow) {
oldflow = flow;
Serial.print("l/min:");
Serial.println(flow);
// Check that we dont get unresonable large flow value. // Check that we dont get unresonable large flow value.
// could hapen when long wraps or false interrupt triggered // could hapen when long wraps or false interrupt triggered
if (flow<((unsigned long)MAX_FLOW)) { if (flow<((unsigned long)MAX_FLOW)) {
gw.send(flowMsg.set(flow, 2)); // Send flow value to gw gw.send(flowMsg.set(flow, 2)); // Send flow value to gw
} }
//Serial.print("l/min:");
//Serial.println(flow);
oldflow = flow;
} }
// No Pulse count in 2min // No Pulse count received in 2min
if(currentTime - lastPulse > 120000){ if(currentTime - lastPulse > 120000){
flow = 0; flow = 0;
} }
// Pulse count has changed // Pulse count has changed
if (pulseCount != oldPulseCount) { if (pulseCount != oldPulseCount) {
gw.send(pcMsg.set(pulseCount)); // Send volumevalue to gw VAR1
double volume = ((double)pulseCount/((double)PULSE_FACTOR));
oldPulseCount = pulseCount; oldPulseCount = pulseCount;
Serial.print("pulsecount:");
Serial.println(pulseCount);
gw.send(lastCounterMsg.set(pulseCount)); // Send pulsecount value to gw in VAR1
double volume = ((double)pulseCount/((double)PULSE_FACTOR));
if (volume != oldvolume) { if (volume != oldvolume) {
gw.send(volumeMsg.set(volume, 3)); // Send volume value to gw
oldvolume = volume; oldvolume = volume;
Serial.print("volume:");
Serial.println(volume, 3);
gw.send(volumeMsg.set(volume, 3)); // Send volume value to gw
} }
} }
lastSend = currentTime;
} else if (sendTime && !pcReceived) {
// No count received. Try requesting it again
gw.request(CHILD_ID, V_VAR1);
lastSend=currentTime;
} }
if (SLEEP_MODE) { if (SLEEP_MODE) {
gw.sleep(SEND_FREQUENCY); gw.sleep(SEND_FREQUENCY);
} }
@@ -110,29 +126,32 @@ void loop()
void incomingMessage(const MyMessage &message) { void incomingMessage(const MyMessage &message) {
if (message.type==V_VAR1) { if (message.type==V_VAR1) {
pulseCount = oldPulseCount = message.getLong(); unsigned long gwPulseCount=message.getULong();
pulseCount += gwPulseCount;
Serial.print("Received last pulse count from gw:"); Serial.print("Received last pulse count from gw:");
Serial.println(pulseCount); Serial.println(pulseCount);
pcReceived = true; pcReceived = true;
} }
} }
void onPulse() void onPulse()
{ {
if (!SLEEP_MODE) { if (!SLEEP_MODE)
{
unsigned long newBlink = micros(); unsigned long newBlink = micros();
unsigned long interval = newBlink-lastBlink; unsigned long interval = newBlink-lastBlink;
lastPulse = millis();
if (interval<500000L) { if (interval!=0)
// Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min) {
return; lastPulse = millis();
if (interval<500000L) {
// Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min)
return;
}
flow = (60000000.0 /interval) / ppl;
} }
flow = (60000000.0 /interval) / ppl;
lastBlink = newBlink; lastBlink = newBlink;
} }
pulseCount++; pulseCount++;
} }