added some message builder helpers. Optimized libray a bit using inline functions and macros.
Converted Dallas example to use new library api.
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
|
|
||||||
MyMessage::MyMessage() {
|
MyMessage::MyMessage() {
|
||||||
|
destination = 0; // Gateway is default destination
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -60,6 +61,7 @@ uint8_t MyMessage::getByte() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MyMessage::getBool() {
|
bool MyMessage::getBool() {
|
||||||
return !(data[0]-'0'==0);
|
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
|
// Set payload
|
||||||
MyMessage MyMessage::set(void* value, uint8_t length) {
|
MyMessage MyMessage::set(void* value, uint8_t length) {
|
||||||
miSetPayloadType(P_CUSTOM);
|
miSetPayloadType(P_CUSTOM);
|
||||||
|
|||||||
@@ -100,8 +100,8 @@ typedef enum {
|
|||||||
|
|
||||||
|
|
||||||
// Check and fetch data from special messages
|
// Check and fetch data from special messages
|
||||||
#define isTimeResponse(_msg) (mGetCommand(_msg)==C_INTERN && _msg.type==I_TIME)
|
#define isTimeResponse(_msg) (mGetCommand(_msg)==C_INTERNAL && _msg.type==I_TIME)
|
||||||
#define isConfigResponse(_msg) (mGetCommand(_msg)==C_INTERN && _msg.type==I_CONFIG)
|
#define isConfigResponse(_msg) (mGetCommand(_msg)==C_INTERNAL && _msg.type==I_CONFIG)
|
||||||
#define mIsMetric(_msg) (_msg.getByte() == 'M')
|
#define mIsMetric(_msg) (_msg.getByte() == 'M')
|
||||||
|
|
||||||
|
|
||||||
@@ -126,6 +126,10 @@ public:
|
|||||||
int getInt();
|
int getInt();
|
||||||
unsigned int getUInt();
|
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
|
// Setters for payload
|
||||||
MyMessage set(void* payload, uint8_t length);
|
MyMessage set(void* payload, uint8_t length);
|
||||||
|
|||||||
@@ -11,6 +11,27 @@
|
|||||||
|
|
||||||
#include "MySensor.h"
|
#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) {
|
MySensor::MySensor(uint8_t _cepin, uint8_t _cspin) : RF24(_cepin, _cspin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +130,7 @@ void MySensor::findParentNode() {
|
|||||||
s.distance = 255;
|
s.distance = 255;
|
||||||
|
|
||||||
// Send ping message to BROADCAST_ADDRESS (to which all relaying nodes and gateway listens and should reply to)
|
// 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);
|
sendWrite(BROADCAST_ADDRESS, msg, true);
|
||||||
|
|
||||||
unsigned long enter = millis();
|
unsigned long enter = millis();
|
||||||
|
|||||||
@@ -56,22 +56,6 @@
|
|||||||
|
|
||||||
#define FIND_RELAY_AFTER_FAILED_TRANSMISSIONS 5
|
#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
|
class MySensor : public RF24
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,61 +1,93 @@
|
|||||||
|
// Example sketch showing how to send in OneWire temperature readings
|
||||||
|
#include <MySensor.h>
|
||||||
#include <Sleep_n0m1.h>
|
#include <Sleep_n0m1.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <EEPROM.h>
|
|
||||||
#include <DallasTemperature.h>
|
#include <DallasTemperature.h>
|
||||||
#include <OneWire.h>
|
#include <OneWire.h>
|
||||||
#include <RF24.h>
|
#include <RF24.h>
|
||||||
#include <Sensor.h>
|
|
||||||
|
|
||||||
#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected
|
#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected
|
||||||
#define MAX_ATTACHED_DS18B20 16
|
#define MAX_ATTACHED_DS18B20 16
|
||||||
unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds)
|
unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds)
|
||||||
|
|
||||||
OneWire oneWire(ONE_WIRE_BUS);
|
OneWire oneWire(ONE_WIRE_BUS);
|
||||||
DallasTemperature sensors(&oneWire);
|
DallasTemperature sensors(&oneWire);
|
||||||
Sensor gw;
|
MySensor gw;
|
||||||
Sleep sleep;
|
Sleep sleep;
|
||||||
float lastTemperature[MAX_ATTACHED_DS18B20];
|
float lastTemperature[MAX_ATTACHED_DS18B20];
|
||||||
int numSensors=0;
|
int numSensors=0;
|
||||||
|
boolean receivedConfig = false;
|
||||||
boolean metric = true;
|
boolean metric = true;
|
||||||
|
MyMessage out;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
// Startup OneWire
|
||||||
sensors.begin();
|
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
|
// 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();
|
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<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
|
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
|
||||||
gw.sendSensorPresentation(i, S_TEMP);
|
gw.present(i, S_TEMP);
|
||||||
}
|
}
|
||||||
metric = gw.isMetricSystem();
|
|
||||||
|
// 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
|
||||||
|
out.type = V_TEMP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
sensors.requestTemperatures(); // Fetch temperatures from Dallas
|
// Process incoming messages
|
||||||
delay(100);
|
gw.process();
|
||||||
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
|
|
||||||
// Fetch and round temperature to one decimal
|
if (receivedConfig) {
|
||||||
float temperature = static_cast<float>(static_cast<int>((metric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
|
// Fetch temperatures from Dallas sensors
|
||||||
// Only send data if temperature has changed and no error
|
sensors.requestTemperatures();
|
||||||
if (lastTemperature[i] != temperature && temperature != -127.00) {
|
|
||||||
gw.powerUp(); // Powerup introduces a small delay (which is missing in radio.write powerup)
|
// Read temperatures and send them to controller
|
||||||
// Send variable (using registered shortcut) to gw
|
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
|
||||||
gw.sendVariable(i, V_TEMP, temperature, 1);
|
|
||||||
lastTemperature[i]=temperature;
|
// Fetch and round temperature to one decimal
|
||||||
|
float temperature = static_cast<float>(static_cast<int>((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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
// Example sketch showing how to control physical relays.
|
// Example sketch showing how to control physical relays.
|
||||||
|
|
||||||
#include <MyConfig.h>
|
|
||||||
#include <MyMessage.h>
|
|
||||||
#include <MySensor.h>
|
#include <MySensor.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <RF24.h>
|
#include <RF24.h>
|
||||||
|
|||||||
Reference in New Issue
Block a user