This code
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
//Comento la siguiente linea para que no necesite radio
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Set LOW transmit power level as default, if you have an amplified NRF-module and
// power your radio separately with a good regulator you can turn up PA level.
#define MY_RF24_PA_LEVEL RF24_PA_LOW
// Enable serial gateway
#define MY_GATEWAY_SERIAL
// Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
#if F_CPU == 8000000L
#define MY_BAUD_RATE 38400
#endif
// Flash leds on rx/tx/err
#define MY_LEDS_BLINKING_FEATURE
// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300
// Inverses the behavior of leds
//#define MY_WITH_LEDS_BLINKING_INVERSE
// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
#define MY_INCLUSION_BUTTON_FEATURE
// Inverses behavior of inclusion button (if using external pullup)
//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
#define MY_INCLUSION_MODE_BUTTON_PIN 3
// Uncomment to override default HW configurations
//#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
//#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED
#include <SPI.h>
#include <MySensors.h>
//Para el sensor de temperatura
#ifdef TEMP
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
MyMessage msg(0,V_TEMP);
#endif
//Structura de reles
typedef struct {
int pin;
char desc[20];
bool ON;
bool OFF;
} sRELE;
sRELE Rele [] = { {13 , "LED", HIGH, LOW},
{31 , "RELE ASPERSOR PORCHE", LOW, HIGH},
{33 , "RELE TURBINAS", LOW, HIGH},
{35 , "RELE GOTEROS BAJOS", LOW, HIGH}
};
#define NUMBER_OF_RELAYS 4 // Total number of attached relays
void setup() {
// Setup locally attached sensors
#ifdef TEMP
//Para el sensor de temperatura
sensors.begin();
#endif
//Para los reles
Serial.println("start call Setup");
for (int sensor=0 ; sensor<NUMBER_OF_RELAYS;sensor++) {
// Poner el rele en output mode
pinMode(Rele[sensor].pin, OUTPUT);
// Poner el rele en el ultimo estado conocido (usando eeprom storage)
digitalWrite(Rele[sensor].pin, loadState(sensor)?Rele[sensor].ON:Rele[sensor].OFF);
}
Serial.println("End call Setup");
presentation();
}
void presentation() {
// Presentar los sensores y actuadores locales
Serial.println("start call presentation");
// Mandar la info del sketch
sendSketchInfo("Arduino USB", "1.0");
//Presentar los reles
for (int rele=0; rele<NUMBER_OF_RELAYS;rele++) {
// Registrar todos los reles al gw
present(rele, S_LIGHT,Rele[rele].desc);
}
Serial.println("End call presentation");
#ifdef TEMP
//Presento el sensor de temperatura
present(9,S_TEMP,"SENSOR_TEMP");
#endif
}
bool STATUS;
void loop() {
// Send locally attached sensor data here
#ifdef TEMP
Serial.print("Solicitando temperaturas...");
sensors.requestTemperatures(); // Send the command to get temperatures
float temperatura = sensors.getTempCByIndex(0);
send(msg.setSensor(9).set(temperatura,1));
Serial.println("DONE");
sleep(2000);
#endif
}
void receive(const MyMessage &message) {
// Solo esperamos mensajes V_LIGTH de momento, pero lo chequeamos por si acaso.
if (message.type==V_LIGHT) {
// Cambiar estado del rele
digitalWrite(Rele[message.sensor].pin, message.getBool()?Rele[message.sensor].ON:Rele[message.sensor].OFF);
// Almacenar estado en la eeprom
saveState(message.sensor, message.getBool());
// Escribir informacion de debug
Serial.print("Cambio entrante para sensor:");
Serial.print(message.sensor);
Serial.print(", Nuevo status: ");
Serial.println(message.getBool());
}
}
works ok for me. As you can see I define the relays in an structure to do it configurable. The only trick for me is to call the presentation function in the setup function. In the ethernet gateway it isn't needed.
A note for the structure sRELE. yo can see the pin, description and the state of the pin that mean ON or OFF. It's so because my relay boards have an inverse logic. When the pin is in low state, the relay is on. This relay boards are opto coupled and have an isolated circuit between arduino pins and relays, this is the reason for that.
The sample code also have some lines to read temperature from a dallas sensor.
I hope that this help you.