Hi, yout post is quit old but I changed your code a bit. Now it sends the status of both relays:
long double last_heartbeat_time = millis();
long double HEARTBEAT_TIME = 30000;
int oldValue = 0;
bool state;
MyMessage msg1a(1, V_STATUS);
MyMessage msg1b;
MyMessage msg2a(2, V_STATUS);
MyMessage msg2b;
void before() {
for (int sensor = 1, pin = RELAY_1; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, loadState(sensor) ? RELAY_ON : RELAY_OFF);
}
}
void setup() {
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Relay", "1.0");
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)
present(sensor, S_BINARY);
}
}
void loop()
{
long double temp = (millis() - last_heartbeat_time);
if (temp > HEARTBEAT_TIME) {
// If it exceeds the heartbeat time then send a heartbeat
sendHeartbeat();
send(msg1a.set(msg1b.getBool() ? 1 : 0));
send(msg2a.set(msg2b.getBool() ? 1 : 0));
last_heartbeat_time = millis();
Serial.println("Sent heartbeat" );
}
}
void receive(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type == V_STATUS) {
// Change relay state
digitalWrite(message.sensor - 1 + RELAY_1, message.getBool() ? RELAY_ON : RELAY_OFF);
// Store state in eeprom
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());
if (message.sensor == 1) {
msg1b = message;
}
if (message.sensor == 2) {
msg2b = message;
}
}
}
rg Denis