From 9291a97d452050d4fa329ed15c67ba676865bcd1 Mon Sep 17 00:00:00 2001 From: Henrik Ekblad Date: Fri, 16 May 2014 10:35:26 +0200 Subject: [PATCH] added some message builder helpers. Optimized libray a bit using inline functions and macros. Converted Dallas example to use new library api. --- libraries/MySensors/MyMessage.cpp | 17 ++++ libraries/MySensors/MyMessage.h | 8 +- libraries/MySensors/MySensor.cpp | 23 ++++- libraries/MySensors/MySensor.h | 16 ---- .../DallasTemperatureSensor.ino | 86 +++++++++++++------ .../examples/RelayActuator/RelayActuator.ino | 2 - 6 files changed, 104 insertions(+), 48 deletions(-) diff --git a/libraries/MySensors/MyMessage.cpp b/libraries/MySensors/MyMessage.cpp index e1a0db2..4834b46 100644 --- a/libraries/MySensors/MyMessage.cpp +++ b/libraries/MySensors/MyMessage.cpp @@ -4,6 +4,7 @@ MyMessage::MyMessage() { + destination = 0; // Gateway is default destination } @@ -60,6 +61,7 @@ uint8_t MyMessage::getByte() { return 0; } } + bool MyMessage::getBool() { return !(data[0]-'0'==0); } @@ -114,6 +116,21 @@ unsigned int MyMessage::getUInt() { } +MyMessage MyMessage::setType(uint8_t _type) { + type = _type; + return *this; +} + +MyMessage MyMessage::setSensor(uint8_t _sensor) { + sensor = _sensor; + return *this; +} + +MyMessage MyMessage::setDestination(uint8_t _destination) { + destination = _destination; + return *this; +} + // Set payload MyMessage MyMessage::set(void* value, uint8_t length) { miSetPayloadType(P_CUSTOM); diff --git a/libraries/MySensors/MyMessage.h b/libraries/MySensors/MyMessage.h index af9f294..27b16a2 100644 --- a/libraries/MySensors/MyMessage.h +++ b/libraries/MySensors/MyMessage.h @@ -100,8 +100,8 @@ typedef enum { // Check and fetch data from special messages -#define isTimeResponse(_msg) (mGetCommand(_msg)==C_INTERN && _msg.type==I_TIME) -#define isConfigResponse(_msg) (mGetCommand(_msg)==C_INTERN && _msg.type==I_CONFIG) +#define isTimeResponse(_msg) (mGetCommand(_msg)==C_INTERNAL && _msg.type==I_TIME) +#define isConfigResponse(_msg) (mGetCommand(_msg)==C_INTERNAL && _msg.type==I_CONFIG) #define mIsMetric(_msg) (_msg.getByte() == 'M') @@ -126,6 +126,10 @@ public: int getInt(); unsigned int getUInt(); + // Setters for building message "on the fly" + MyMessage setType(uint8_t type); + MyMessage setSensor(uint8_t sensor); + MyMessage setDestination(uint8_t destination); // Setters for payload MyMessage set(void* payload, uint8_t length); diff --git a/libraries/MySensors/MySensor.cpp b/libraries/MySensors/MySensor.cpp index e3dfc4f..41ae49d 100644 --- a/libraries/MySensors/MySensor.cpp +++ b/libraries/MySensors/MySensor.cpp @@ -11,6 +11,27 @@ #include "MySensor.h" +// Inline and macros +inline MyMessage build (MyMessage msg, uint8_t sender, uint8_t destination, uint8_t sensor, uint8_t command, uint8_t type) { + msg.sender = sender; + msg.destination = destination; + msg.sensor = sensor; + msg.type = type; + mSetCommand(msg,command); + return msg; +} + +// Created macros for these, compiler seems to do a bad job when doing sendRoute(build(...)) +#define buildSend(_msg, _sender, _destination, _sensor, _cmd, _type) \ + build(_msg, _sender, _destination, _sensor, _cmd, _type);\ + sendRoute(_msg); + +#define buildSendPayload(_msg, _sender, _destination, _sensor, _cmd, _type, _value) \ + build(_msg, _sender, _destination, _sensor, _cmd, _type).set(_value);\ + _msg.set(_value);\ + sendRoute(_msg); + + MySensor::MySensor(uint8_t _cepin, uint8_t _cspin) : RF24(_cepin, _cspin) { } @@ -109,7 +130,7 @@ void MySensor::findParentNode() { s.distance = 255; // Send ping message to BROADCAST_ADDRESS (to which all relaying nodes and gateway listens and should reply to) - build(msg, s.nodeId, BROADCAST_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_PING) + build(msg, s.nodeId, BROADCAST_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_PING); sendWrite(BROADCAST_ADDRESS, msg, true); unsigned long enter = millis(); diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index 38b3ac6..0b22504 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -56,22 +56,6 @@ #define FIND_RELAY_AFTER_FAILED_TRANSMISSIONS 5 -#define build(_msg, _sender, _destination, _sensor, _cmd, _type) \ - _msg.sender = _sender; \ - _msg.destination = _destination;\ - _msg.sensor = _sensor; \ - _msg.type = _type; \ - mSetCommand(_msg,_cmd); \ - -#define buildSend(_msg, _sender, _destination, _sensor, _cmd, _type) \ - build(_msg, _sender, _destination, _sensor, _cmd, _type) \ - sendRoute(_msg); - -#define buildSendPayload(_msg, _sender, _destination, _sensor, _cmd, _type, _value) \ - build(_msg, _sender, _destination, _sensor, _cmd, _type) \ - _msg.set(_value); \ - sendRoute(_msg); - class MySensor : public RF24 { diff --git a/libraries/MySensors/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino b/libraries/MySensors/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino index e4fe953..ddeeacc 100644 --- a/libraries/MySensors/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino +++ b/libraries/MySensors/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino @@ -1,61 +1,93 @@ +// Example sketch showing how to send in OneWire temperature readings +#include #include #include -#include #include #include #include -#include #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 16 unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds) - OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); -Sensor gw; +MySensor gw; Sleep sleep; float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; +boolean receivedConfig = false; boolean metric = true; +MyMessage out; void setup() { + // Startup OneWire sensors.begin(); - gw.begin(); + + // Startup and initialize MySensors library. Set callback for incoming messages. + gw.begin(response); // Send the sketch version information to the gateway and Controller - gw.sendSketchInfo("Temperature Sensor", "1.0"); + gw.sketchInfo("Temperature Sensor", "1.0"); - // Fetch the number of attached sensors + // Fetch the number of attached temperature sensors numSensors = sensors.getDeviceCount(); - // Register all sensors to gw (they will be created as child devices) + + // Present all sensors to controller for (int i=0; i(static_cast((metric?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) { - gw.powerUp(); // Powerup introduces a small delay (which is missing in radio.write powerup) - // Send variable (using registered shortcut) to gw - gw.sendVariable(i, V_TEMP, temperature, 1); - lastTemperature[i]=temperature; + // Process incoming messages + gw.process(); + + if (receivedConfig) { + // Fetch temperatures from Dallas sensors + sensors.requestTemperatures(); + + // Read temperatures and send them to controller + for (int i=0; i(static_cast((metric?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) { + + // 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. Note that the radio will get powered back up - // on the next write() call. - delay(500); - gw.powerDown(); - sleep.pwrDownMode(); //set sleep mode - sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime } diff --git a/libraries/MySensors/examples/RelayActuator/RelayActuator.ino b/libraries/MySensors/examples/RelayActuator/RelayActuator.ino index f4375d1..0d57c94 100644 --- a/libraries/MySensors/examples/RelayActuator/RelayActuator.ino +++ b/libraries/MySensors/examples/RelayActuator/RelayActuator.ino @@ -1,7 +1,5 @@ // Example sketch showing how to control physical relays. -#include -#include #include #include #include