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:
Henrik Ekblad
2014-05-16 10:35:26 +02:00
parent 7080520a3f
commit 9291a97d45
6 changed files with 104 additions and 48 deletions
@@ -1,61 +1,93 @@
// Example sketch showing how to send in OneWire temperature readings
#include <MySensor.h>
#include <Sleep_n0m1.h>
#include <SPI.h>
#include <EEPROM.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <RF24.h>
#include <Sensor.h>
#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<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()
{
sensors.requestTemperatures(); // Fetch temperatures from Dallas
delay(100);
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.;
// 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<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.;
// 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.
#include <MyConfig.h>
#include <MyMessage.h>
#include <MySensor.h>
#include <SPI.h>
#include <RF24.h>