* Converted time example. Callback for time request was the only clean solution. Now global callback for incoming messages only needs to care about SET commands coming from other nodes or gateway.

* New api for storing states. RelayActator now stores state in eeprom, making it more standalone.
* Controller config is now fetched in the background without needing any special tratment in sketch. Last received config is stored in eeprom:
This commit is contained in:
Henrik Ekblad
2014-05-16 14:48:18 +02:00
parent 9291a97d45
commit 891c826a0c
7 changed files with 270 additions and 256 deletions
@@ -25,10 +25,10 @@ void setup()
sensors.begin();
// Startup and initialize MySensors library. Set callback for incoming messages.
gw.begin(response);
gw.begin();
// Send the sketch version information to the gateway and Controller
gw.sketchInfo("Temperature Sensor", "1.0");
gw.sendSketchInfo("Temperature Sensor", "1.0");
// Fetch the number of attached temperature sensors
numSensors = sensors.getDeviceCount();
@@ -38,56 +38,39 @@ void setup()
gw.present(i, S_TEMP);
}
// Request configuration from controller (use this to determine if we're supposed to send in Ceclscius or Farenheit)
gw.requestConfig();
// Initialize outgoing message to always send temperature
// Initialize outgoing message to always send temperature
out.type = V_TEMP;
}
void loop()
{
// Process incoming messages
// Process incoming messages (like config from server)
gw.process();
if (receivedConfig) {
// Fetch temperatures from Dallas sensors
sensors.requestTemperatures();
// Fetch temperatures from Dallas sensors
sensors.requestTemperatures();
// Read temperatures and send them to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
// Read temperatures and send them to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
// Fetch and round temperature to one decimal
float temperature = static_cast<float>(static_cast<int>((metric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
// Fetch and round temperature to one decimal
float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
// Only send data if temperature has changed and no error
if (lastTemperature[i] != temperature && temperature != -127.00) {
// Only send data if temperature has changed and no error
if (lastTemperature[i] != temperature && temperature != -127.00) {
// Send in the new temperature
gw.send(out.setSensor(i).set(temperature,1));
lastTemperature[i]=temperature;
}
// Send in the new temperature
gw.send(out.setSensor(i).set(temperature,1));
lastTemperature[i]=temperature;
}
// Power down the radio and sleep Arduino to save batteries.
delay(500);
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
} else {
// We're still missing the configuration. Wait a little bit and request it again.
delay(1000);
gw.requestConfig();
}
}
void response(MyMessage msg) {
// Check that received message is a configuration
if (isConfigResponse(msg)) {
// Read config from message
metric = mIsMetric(msg);
receivedConfig = true;
}
// Power down the radio and sleep Arduino to save batteries.
delay(500);
gw.powerDown();
sleep.pwrDownMode(); //set sleep mode
sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime
}
@@ -1,4 +1,5 @@
// Example sketch showing how to control physical relays.
// This example will remember relay state even after power failure.
#include <MySensor.h>
#include <SPI.h>
@@ -17,29 +18,23 @@ void setup()
// Initialize library and add callback for incoming messages
gw.begin(incomingMessage, false);
// Send the sketch version information to the gateway and Controller
gw.sketchInfo("Relay", "1.0");
gw.sendSketchInfo("Relay", "1.0");
// Register all sensors to gw (they will be created as child devices)
for (int i=0; i<NUMBER_OF_RELAYS;i++) {
gw.present(RELAY_1+i, S_LIGHT);
}
// Fetch relay status
for (int i=0; i<NUMBER_OF_RELAYS;i++) {
// Make sure relays are off when starting up
digitalWrite(RELAY_1+i, RELAY_OFF);
for (int sensor=0, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(RELAY_1+i, OUTPUT);
// Request and wait for relay status from controller
gw.request(RELAY_1+i, V_LIGHT);
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, gw.loadState(sensor));
}
}
void loop()
{
// Process incoming messages
// Alway process incoming messages whenever possible
gw.process();
}
@@ -48,6 +43,8 @@ void incomingMessage(MyMessage message) {
if (message.type==V_LIGHT) {
// Change relay state
digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
gw.saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
@@ -1,66 +0,0 @@
// Example sketch showing how to request time from vera
// and how to set a cutom variable on a sensor
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
#include <Sensor.h>
#include <Time.h>
#define CHILD_ID 0 // Id of the sensor child
Sensor gw;
void setup()
{
gw.begin();
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Clock", "1.0");
// Register any sensortype. This example we just create a motion sensor.
gw.sendSensorPresentation(CHILD_ID, S_MOTION);
setSyncProvider(getTime);
setSyncInterval(3600); // get time from gw every hour
}
unsigned long getTime() {
return gw.getTime(); // Vera is time provider
}
void loop()
{
// long time = gw.getTime();
// setTime(time);
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
// Ok now send time back to gw and store in in Variable1
// of the motion sensor device
gw.sendVariable(CHILD_ID, V_VAR1, minute());
// wait 10 seconds...
delay(10000);
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
@@ -0,0 +1,56 @@
// Example sketch showing how to request time from controller
#include <SPI.h>
#include <RF24.h>
#include <MySensor.h>
#include <Time.h>
MySensor gw;
boolean timeReceived = false;
void setup()
{
gw.begin();
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Clock", "1.0");
// Request time from controller.
gw.requestTime(receiveTime);
}
// This is called when a new time value was received
void receiveTime(unsigned long time) {
// Ok, set incoming time
setTime(time);
timeReceived = true;
}
void loop()
{
gw.process();
// Print time
if (timeReceived) {
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}