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