The last examples converted to 1.4.

This commit is contained in:
Henrik Ekblad
2014-06-13 20:19:48 +02:00
parent af90d238d4
commit 8a9061aee7
5 changed files with 58 additions and 69 deletions
@@ -96,7 +96,7 @@ void loop()
void incomingMessage(MyMessage message) { void incomingMessage(MyMessage message) {
if (message.type==V_VAR1) { if (message.type==V_VAR1) {
pulseCount = oldPulseCount = message.getLong(); pulseCount = oldPulseCount = message.getLong();
Serial.print("Last pulse count from gw:"); Serial.print("Received last pulse count from gw:");
Serial.println(pulseCount); Serial.println(pulseCount);
pcReceived = true; pcReceived = true;
} }
@@ -39,7 +39,6 @@ void loop()
} }
void incomingMessage(MyMessage message) { void incomingMessage(MyMessage message) {
Serial.println(gw.getInternalTemp());
// We only expect one type of message from controller. But we better check anyway. // We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT) { if (message.type==V_LIGHT) {
// Change relay state // Change relay state
@@ -18,6 +18,7 @@
#define CHILD_ID 10 // Id of the sensor child #define CHILD_ID 10 // Id of the sensor child
MySensor gw; MySensor gw;
MyMessage msg(CHILD_ID, V_DIMMER);
Servo myservo; // create servo object to control a servo Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created Sensor gw(9,10); // a maximum of eight servo objects can be created Sensor gw(9,10);
unsigned long timeOfLastChange = 0; unsigned long timeOfLastChange = 0;
@@ -26,7 +27,8 @@ bool attachedServo = false;
void setup() void setup()
{ {
gw.begin(); // Attach method for incoming messages
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("Servo", "1.0"); gw.sendSketchInfo("Servo", "1.0");
@@ -34,42 +36,37 @@ void setup()
// Register all sensors to gw (they will be created as child devices) // Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID, S_COVER); gw.present(CHILD_ID, S_COVER);
// Fetch servo state at startup // Request last servo state at startup
gw.getStatus(CHILD_ID, V_DIMMER); gw.request(CHILD_ID, V_DIMMER);
setRelayStatus(gw.getMessage()); // Wait here until status message arrive from gw
} }
void loop() void loop()
{ {
if (gw.messageAvailable()) { gw.process();
// New messsage from gw
message_s message = gw.getMessage();
setRelayStatus(message);
}
if (attachedServo && millis() - timeOfLastChange > DETACH_DELAY) { if (attachedServo && millis() - timeOfLastChange > DETACH_DELAY) {
myservo.detach(); myservo.detach();
attachedServo = false; attachedServo = false;
} }
} }
void setRelayStatus(message_s message) { void incomingMessage(MyMessage message) {
myservo.attach(SERVO_DIGITAL_OUT_PIN); myservo.attach(SERVO_DIGITAL_OUT_PIN);
attachedServo = true; attachedServo = true;
if (message.header.type==V_DIMMER) { // This could be M_ACK_VARIABLE or M_SET_VARIABLE if (message.type==V_DIMMER) { // This could be M_ACK_VARIABLE or M_SET_VARIABLE
int val = atoi(message.data); int val = message.getInt();
myservo.write(SERVO_MAX + (SERVO_MIN-SERVO_MAX)/100 * val); // sets the servo position 0-180 myservo.write(SERVO_MAX + (SERVO_MIN-SERVO_MAX)/100 * val); // sets the servo position 0-180
// Write some debug info // Write some debug info
Serial.print("Servo changed. new state: "); Serial.print("Servo changed. new state: ");
Serial.println(val); Serial.println(val);
} else if (message.header.type==V_UP) { } else if (message.type==V_UP) {
Serial.println("Servo UP command"); Serial.println("Servo UP command");
myservo.write(SERVO_MIN); myservo.write(SERVO_MIN);
gw.sendVariable(CHILD_ID, V_DIMMER, 100); gw.send(msg.set(100));
} else if (message.header.type==V_DOWN) { } else if (message.type==V_DOWN) {
Serial.println("Servo DOWN command"); Serial.println("Servo DOWN command");
myservo.write(SERVO_MAX); myservo.write(SERVO_MAX);
gw.sendVariable(CHILD_ID, V_DIMMER, 0); gw.send(msg.set(0));
} else if (message.header.type==V_STOP) { } else if (message.type==V_STOP) {
Serial.println("Servo STOP command"); Serial.println("Servo STOP command");
myservo.detach(); myservo.detach();
attachedServo = false; attachedServo = false;
@@ -1,15 +1,13 @@
#include <Sleep_n0m1.h>
#include <SPI.h> #include <SPI.h>
#include <EEPROM.h>
#include <RF24.h> #include <RF24.h>
#include <Sensor.h> #include <MySensor.h>
#define DIGITAL_INPUT_SOIL_SENSOR 3 // Digital input did you attach your soil sensor. #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 INTERRUPT DIGITAL_INPUT_SOIL_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID 0 // Id of the sensor child #define CHILD_ID 0 // Id of the sensor child
Sensor gw; MySensor gw;
Sleep sleep; MyMessage msg(CHILD_ID, V_TRIPPED);
int lastSoilValue = -1; int lastSoilValue = -1;
void setup() void setup()
@@ -18,10 +16,10 @@ void setup()
// Send the sketch version information to the gateway and Controller // Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Soil Moisture Sensor", "1.0"); gw.sendSketchInfo("Soil Moisture Sensor", "1.0");
// sets the soil sensor digital pin as input
pinMode(DIGITAL_INPUT_SOIL_SENSOR, INPUT); // 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) // Register all sensors to gw (they will be created as child devices)
gw.sendSensorPresentation(CHILD_ID, S_MOTION); gw.present(CHILD_ID, S_MOTION);
} }
void loop() void loop()
@@ -30,14 +28,11 @@ void loop()
int soilValue = digitalRead(DIGITAL_INPUT_SOIL_SENSOR); // 1 = Not triggered, 0 = In soil with water int soilValue = digitalRead(DIGITAL_INPUT_SOIL_SENSOR); // 1 = Not triggered, 0 = In soil with water
if (soilValue != lastSoilValue) { if (soilValue != lastSoilValue) {
Serial.println(soilValue); Serial.println(soilValue);
gw.sendVariable(CHILD_ID, V_TRIPPED, soilValue==0?"1":"0"); // Send the inverse to gw as tripped should be when no water in soil gw.send(msg.set(soilValue==0?1:0)); // Send the inverse to gw as tripped should be when no water in soil
lastSoilValue = soilValue; lastSoilValue = soilValue;
} }
// Power down the radio. Note that the radio will get powered back up // Power down the radio and arduino until digital input changes.
// on the next write() call. gw.sleep(INTERRUPT,CHANGE);
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepInterrupt(INTERRUPT,CHANGE);
} }
@@ -8,15 +8,10 @@
// sleepmode. So we cannot make this sensor sleep if we also want // sleepmode. So we cannot make this sensor sleep if we also want
// to calculate/report flow. // to calculate/report flow.
// //
// Sensor on pin 3
#include <Relay.h>
#include <Sleep_n0m1.h>
#include <SPI.h> #include <SPI.h>
#include <EEPROM.h>
#include <RF24.h> #include <RF24.h>
#include <Sensor.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 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)
@@ -26,14 +21,14 @@
#define CHILD_ID 5 // Id of the sensor child #define CHILD_ID 5 // Id of the sensor child
unsigned long SEND_FREQUENCY = 20; // Minimum time between send (in seconds). We don't want to spam the gateway. unsigned long SEND_FREQUENCY = 20; // Minimum time between send (in seconds). We don't want to spam the gateway.
Sensor gw; MySensor gw;
Sleep sleep;
double ppl = ((double)PULSE_FACTOR)/1000; // Pulses per liter double ppl = ((double)PULSE_FACTOR)/1000; // Pulses per liter
volatile unsigned long pulseCount = 0; volatile unsigned long pulseCount = 0;
volatile unsigned long lastBlink = 0; volatile unsigned long lastBlink = 0;
volatile double flow = 0; volatile double flow = 0;
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;
@@ -43,21 +38,24 @@ unsigned long lastSend;
unsigned long lastPulse; unsigned long lastPulse;
unsigned long currentTime; unsigned long currentTime;
boolean metric; 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(); 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.0");
// Register this device as Waterflow sensor // Register this device as Waterflow sensor
gw.sendSensorPresentation(CHILD_ID, S_WATER); gw.present(CHILD_ID, S_WATER);
// Fetch last known pulse count value from gw // Fetch last known pulse count value from gw
pulseCount = oldPulseCount = atol(gw.getStatus(CHILD_ID, V_VAR1)); gw.request(CHILD_ID, V_VAR1);
//Serial.print("Last pulse count from gw:");
//Serial.println(pulseCount);
attachInterrupt(INTERRUPT, onPulse, RISING); attachInterrupt(INTERRUPT, onPulse, RISING);
lastSend = millis(); lastSend = millis();
} }
@@ -65,16 +63,18 @@ void setup()
void loop() void loop()
{ {
currentTime = millis(); gw.process();
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
if (SLEEP_MODE || currentTime - lastSend > 1000*SEND_FREQUENCY) { bool sendTime = currentTime - lastSend > 1000*SEND_FREQUENCY;
if (pcReceived && (SLEEP_MODE || sendTime)) {
// New flow value has been calculated // New flow value has been calculated
if (!SLEEP_MODE && flow != oldflow) { if (!SLEEP_MODE && flow != oldflow) {
// 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.sendVariable(CHILD_ID, V_FLOW, flow, 2); // Send flow value to gw gw.send(flowMsg.set(flow, 2)); // Send flow value to gw
} }
//Serial.print("l/min:"); //Serial.print("l/min:");
//Serial.println(flow); //Serial.println(flow);
@@ -82,39 +82,39 @@ void loop()
} }
// No Pulse count in 2min // No Pulse count in 2min
//Serial.print("currentTime");
//Serial.println(currentTime);
//Serial.print("lastPulse");
//Serial.println(lastPulse);
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.sendVariable(CHILD_ID, V_VAR1, pulseCount); // Send volumevalue to gw VAR1 gw.send(pcMsg.set(pulseCount)); // Send volumevalue to gw VAR1
double volume = ((double)pulseCount/((double)PULSE_FACTOR)); double volume = ((double)pulseCount/((double)PULSE_FACTOR));
oldPulseCount = pulseCount; oldPulseCount = pulseCount;
//Serial.print("Pulse count:");
//Serial.println(pulseCount);
if (volume != oldvolume) { if (volume != oldvolume) {
gw.sendVariable(CHILD_ID, V_VOLUME, volume, 3); // Send volume value to gw gw.send(volumeMsg.set(volume, 3)); // Send volume value to gw
//Serial.print("m3:");
//Serial.println(volume, 3);
oldvolume = volume; oldvolume = volume;
} }
} }
lastSend = currentTime; lastSend = currentTime;
} else if (sendTime) {
// No count received. Try requesting it again
gw.request(CHILD_ID, V_VAR1);
} }
if (SLEEP_MODE) { if (SLEEP_MODE) {
delay(300); //delay to allow serial to fully print before sleep delay(300); //delay to allow serial to fully print before sleep
gw.powerDown(); gw.sleep(SEND_FREQUENCY * 1000);
sleep.pwrDownMode(); //set sleep mode }
sleep.sleepDelay(SEND_FREQUENCY * 1000); //sleep for: sleepTime }
void incomingMessage(MyMessage message) {
if (message.type==V_VAR1) {
pulseCount = oldPulseCount = message.getLong();
Serial.print("Received last pulse count from gw:");
Serial.println(pulseCount);
pcReceived = true;
} }
} }
@@ -129,10 +129,8 @@ void onPulse()
// Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min) // Sometimes we get interrupt on RISING, 500000 = 0.5sek debounce ( max 120 l/min)
return; return;
} }
flow = (60000000.0 /interval) / ppl; flow = (60000000.0 /interval) / ppl;
lastBlink = newBlink; lastBlink = newBlink;
//Serial.println(flow, 4);
} }
pulseCount++; pulseCount++;
} }