I made an attempt at combining the DallasTemperatureSensor and RelayActuator sketches, and after a couple tries I thought I had gotten it right - everything works as intended, except for one thing. If I try to reboot the node from MYSController (either individually or as a broadcast to all nodes), it shuts down (relays click off) but never starts back up. Node has no problem rebooting with a different sketch loaded. Also, it starts working fine again after a power cycle or manual reset.
What did I miss? It's driving me nuts.
Thanks in advance,
Josh
#include <MySensor.h>
#include <SPI.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 3 // Pin where dallas sensor is connected
#define MAX_ATTACHED_DS18B20 16
#define RELAY_1 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 2 // Total number of attached relays
#define RELAY_ON 0 // GPIO value to write to turn on attached relay
#define RELAY_OFF 1 // GPIO value to write to turn off attached relay
unsigned long SLEEP_TIME = 10000; // Sleep time between reads (in milliseconds)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
MySensor gw;
// int node_id = 99;
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors = 0;
boolean receivedConfig = false;
boolean metric = false;
// Initialize temperature message
MyMessage msg(5, V_TEMP);
void setup()
{
// Startup OneWire
sensors.begin();
// Initialize library and add callback for incoming messages
// gw.begin(incomingMessage, node_id, true);
gw.begin(incomingMessage, AUTO, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Relay plus Temp", "1.6");
// Fetch relay status
for (int sensor = 1, 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(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, gw.loadState(sensor) ? RELAY_ON : RELAY_OFF);
}
// Fetch the number of attached temperature sensors
numSensors = sensors.getDeviceCount();
// Present all sensors to controller
for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) {
gw.present(i, S_TEMP);
}
}
void loop()
{
// Alway process incoming messages whenever possible
gw.process();
// 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>((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(msg.setSensor(i).set(temperature, 1));
lastTemperature[i] = temperature;
}
}
gw.wait(SLEEP_TIME);
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type == V_LIGHT) {
// Change relay state
digitalWrite(message.sensor - 1 + RELAY_1, 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:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}