Unable to get relay status from Domoticz
-
I did edit the receive function with this:
void receive(const MyMessage &msg_relay) { Serial.println("-------------- DEBUG receive() ---------------"); Serial.print("msg_relay.type value is "); Serial.println(msg_relay.type); Serial.println("------------------------------------------"); Serial.print("msg_relay.getCommand() value is "); Serial.println(msg_relay.getCommand()); Serial.println("------------------------------------------"); Serial.println("--------------------------------------------"); }Receive is NOT called at first start ( I have no debug in monitor). Receive is called only when from DOMOTICZ I poweron or poweroff the relay.
In effect this is the debug monitor:
-------------- DEBUG getStateFromController() --------------- request( CHILD_ID_RELAY , V_STATUS) value is 1 Uplink available! Relay in controller is marked as 0 ------------------------------------------------------------- -------------- DEBUG receive() --------------- msg_relay.type value is 2 ------------------------------------------ msg_relay.getCommand() value is 1 ------------------------------------------ -------------------------------------------- -------------- DEBUG receive() --------------- msg_relay.type value is 2 ------------------------------------------ msg_relay.getCommand() value is 1 ------------------------------------------ --------------------------------------------Last two receive() debug called only when I did poweron or poweroff the relay.
-
You could try editing out all the ifs etc in the getStateFromController and only request the state (surrounded by a few serial prints to see which bits execute). If it works in simplest form then you could build up the logic around it to see what is failing
void getStateFromController() { Serial.println(" starting GetStateFromController"); request( CHILD_ID_RELAY , V_STATUS); wait (WAIT_RETURN); serial.print("Waiting"); } -
You could try editing out all the ifs etc in the getStateFromController and only request the state (surrounded by a few serial prints to see which bits execute). If it works in simplest form then you could build up the logic around it to see what is failing
void getStateFromController() { Serial.println(" starting GetStateFromController"); request( CHILD_ID_RELAY , V_STATUS); wait (WAIT_RETURN); serial.print("Waiting"); }@Ben-Andrewes request return every time "1". This is not the state of relay in Domoticz . The following state from Domoticz is "0", either if in Domoticz relay need to be on or off ....
-
@gohan to mantain them in sync after a power failure.
But you did put me on right way. I could simply put the relay off on Arduino start and send off to Domoticz.
Every 5 minutes I could send the corrent state ti Domoticz (Imagine a gateway Power failure instead).
In this mode I delegate all logic to Domoticz, when the relay need to be on or off....
-
Ok,
I don't know if it is necessary to open a new post.I imagine now it's a problem of Domoticz.
I did change all my logic. NOw on startup and every X minutes, the node send to the Domoticz the status of relay, to mantain it in sync.
Errrr...... the status is received but not displayed correctly :disappointed_relieved:
I attach an image to explain better.

Domoticz is last update version, this is the current sketch:
/** * Sketch for a relay + termostat. * * In test from 11/08/2017 * * TERMINARE SE UPLINK NON è DISPONIBILE * * @version 2.0 * */ /** * DEFINE MYSENSORS SECTION */ #define MY_DEBUG // Enable Basic Debug //#define MY_DEBUG_VERBOSE_SIGNING // Enable Signing Debug #define MY_RADIO_NRF24 #define MY_SIGNING_SOFT #define MY_SIGNING_SOFT_RANDOMSEED_PIN 7 #define MY_SIGNING_REQUEST_SIGNATURES #define MY_SIGNING_NODE_WHITELISTING {{.nodeId = GATEWAY_ADDRESS,.serial = {0x3B,0xF4,0x61,0xDF,0x1E,0xFF,0xFF,0xFF,0xEC}}} #define MY_NODE_ID 10 // Default ID #define MY_DEFAULT_LED_BLINK_PERIOD 300 #define MY_WITH_LEDS_BLINKING_INVERSE #define MY_DEFAULT_ERR_LED_PIN 16 #define MY_DEFAULT_TX_LED_PIN 17 #define MY_DEFAULT_RX_LED_PIN 18 /** * Include libraries */ #include <SPI.h> #include <MySensors.h> #include <DallasTemperature.h> #include <OneWire.h> /** * Define section. * * // NAME */ #define SKETCH_NAME "Termostato+Relay" #define SKETCH_VERSION "3.0" // PIN #define PIN_DS18B20 2 #define PIN_RELAY 3 #define PIN_LED_STATUS 19 // CHILD ID #define CHILD_ID_TEMP 0 #define CHILD_ID_RELAY 1 // RELAY SECTION #define RELAY_ON 0 // if inverted, invert here #define RELAY_OFF 1 // if inverted, invert here // WAITs TIMES #define WAIT_LOOPS 1 // The status led flashes ON 10 ms and OFF 4990 ms == 5000 ms of loop. 5000 * WAIT_LOOPS = number of cycles before new read. 60 == 5 minutes //#define WAIT_RETURN 1500 // Milliseconds to wait for returns from gateway /* * One Wire Section */ OneWire oneWire(PIN_DS18B20); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. /** * Mysensors message */ // Initialize temperature message MyMessage msg_temperature(CHILD_ID_TEMP , V_TEMP); // Initialize relay message MyMessage msg_relay(CHILD_ID_RELAY , V_STATUS); /** * Before class */ void before() { // Startup up the OneWire library sensors.begin(); } /** * Presentation. * * Presents the child to the controller */ void presentation() { // send sketch name sendSketchInfo(SKETCH_NAME , SKETCH_VERSION); // Present all sensors to controller. // TEMPERATURE present(CHILD_ID_TEMP, S_TEMP); // RELAY present(CHILD_ID_RELAY , S_BINARY); } /** * Our Setup */ void setup() { // SETUP LED STATUS // Set the LED STATUS as OUTPUT and shutdown it. pinMode(PIN_LED_STATUS , OUTPUT); digitalWrite(PIN_LED_STATUS , LOW); // SETUP RELAY // Set the RELAY PIN as OUTPUT and LOW pinMode(PIN_RELAY , OUTPUT); digitalWrite(PIN_RELAY , RELAY_OFF); // initialize sensors sensors.setWaitForConversion(false); } /** * Our loop */ void loop() { // send the relay state to controller sendRelayStateToController(); // executed the reading loopTemperature(); // light the status led... so wait lightLedStatus(); } /* * Send the state of relay to controller. * * @since 3.0 */ void sendRelayStateToController() { Serial.println("-------------- DEBUG sendRelayStateToController() ---------------"); bool current_state = digitalRead(PIN_RELAY); if (current_state == 1) { current_state = RELAY_ON; } else { current_state = RELAY_OFF; } Serial.print("current_state value is "); Serial.println(current_state); send(msg_relay.set(current_state) , true); Serial.println("------------------------------------------------------------"); } /** * Loop temperature. * Executed inside loop() * * @since 2.0 */ void loopTemperature() { // Fetch temperatures from Dallas sensors // we need sensors.requestTemperatures() to NO BLOCK THE THREAD!!! sensors.requestTemperatures(); float temperature = readTemperature(); sendTemperatureToController(temperature); } /************************************************/ /**** VARIOUS FUNCTION TO SUPPORT *****/ /************************************************/ /** * Receive function * * @since 2.0 */ void receive(const MyMessage &message) { if ( !message.isAck() ) { if ( message.type == V_STATUS) { if ( message.getCommand() == 1 ) { bool received_state = message.getBool(); Serial.print("received_state value is "); Serial.println(received_state); if ( received_state == 1) { digitalWrite(PIN_RELAY , RELAY_ON); } else { digitalWrite(PIN_RELAY , RELAY_OFF); } } } } Serial.println("--------------------------------------------"); } /** * Read the temperature from DS18B20 * * @since 2.0 */ float readTemperature() { // query conversion time and sleep until conversion completed int16_t conversionTime = millisToWaitForConversion(sensors.getResolution()); wait(conversionTime); // Read temperatures and send them to controller // Fetch and round temperature to one decimal float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(CHILD_ID_TEMP):sensors.getTempFByIndex(CHILD_ID_TEMP)) * 10.)) / 10.; return temperature; } /** * Get the millis to wait before reading based on resolution. * * @since 2.0 */ int16_t millisToWaitForConversion(uint8_t bitResolution) { switch (bitResolution) { case 9: return 94; case 10: return 188; case 11: return 375; default: return 750; } } /** * Send the temperature to controller * * @since 2.0 */ void sendTemperatureToController(float temperature) { if (temperature != -127.00 && temperature != 85.00) { send(msg_temperature.setSensor(CHILD_ID_TEMP).set(temperature,1)); digitalWrite(MY_DEFAULT_ERR_LED_PIN , LOW); //Serial.println("-------------- DEBUG sendTemperatureToController() ---------------"); //Serial.println("No error on send temperature"); //Serial.println("------------------------------------------------------"); } else { // light the error led fixed! digitalWrite(MY_DEFAULT_ERR_LED_PIN , HIGH); //Serial.println("-------------- DEBUG sendTemperatureToController() ---------------"); //Serial.println("! ERROR sending temperature"); //Serial.println("------------------------------------------------------"); } } /** * Light the LED status * * @since 2.0 */ void lightLedStatus() { int k = 0; for ( k = 0; k < WAIT_LOOPS; k++) { digitalWrite(PIN_LED_STATUS,HIGH); wait(10); // 5 * 1000 millisends = 5 seconds; digitalWrite(PIN_LED_STATUS,LOW); wait(4990); } }