Reversed Relay



  • Hi, I was wondering if anyone can tell me what's going on here?

    I've got a relay node setup but as soon as it's registered by HASS is flicks the relay to on even though the web GUI shows it as off? Flicking the switch in the GUI to on turns the relay off... I'm sure it must be something obvious but I can't see it.

    Here's the sketch on the node:

    /*
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * http://www.mysensors.org/build/relay
     */
    
    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define MY_REPEATER_FEATURE
    //#define MY_NODE_ID 1
    #include <SPI.h>
    #include <MySensors.h>
    
    #define RELAY_PIN  3
    #define CHILD_ID 1
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    bool state = false;
    bool initialValueSent = false;
    
    MyMessage msg(CHILD_ID, V_STATUS);
    
    void setup()
    {
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      pinMode(RELAY_PIN, OUTPUT);
    }
    
    void presentation()  {
      sendSketchInfo("MyRelay", "1.0");
      present(CHILD_ID, S_BINARY);
    }
    
    void loop()
    {
      if (!initialValueSent) {
        Serial.println("Sending initial value");
        send(msg.set(state?RELAY_ON:RELAY_OFF));
        Serial.println("Requesting initial value from controller");
        request(CHILD_ID, V_STATUS);
        wait(2000, C_SET, V_STATUS);
      }
    }
    
    void receive(const MyMessage &message) {
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_STATUS) {
        if (!initialValueSent) {
          Serial.println("Receiving initial value from controller");
          initialValueSent = true;
        }
        // Change relay state
        state = (bool)message.getInt();
        digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
        send(msg.set(state?RELAY_ON:RELAY_OFF));
      }
    }
    

    When the Node presents itself to HASS I get the following:

    12/5/2016 11:15:19 PM16-12-05 23:15:19 INFO (Thread-12) [homeassistant.components.mysensors] Adding new devices: <Entity MyRelay 1 1: off>
    12/5/2016 11:15:19 PM16-12-05 23:15:19 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=switch.myrelay_1_1, old_state=None, new_state=<state switch.myrelay_1_1=unavailable; friendly_name=MyRelay 1 1 @ 2016-12-05T23:15:19.403632+00:00>>
    12/5/2016 11:15:19 PM16-12-05 23:15:19 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=group.all_switches, old_state=<state group.all_switches=off; auto=True, entity_id=('switch.wswitch1',), friendly_name=all switches, hidden=True, order=1 @ 2016-12-05T23:14:34.823591+00:00>, new_state=None>
    12/5/2016 11:15:19 PM16-12-05 23:15:19 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=group.all_switches, old_state=None, new_state=<state group.all_switches=off; auto=True, entity_id=('switch.myrelay_1_1', 'switch.wswitch1'), friendly_name=all switches, hidden=True, order=1 @ 2016-12-05T23:15:19.406471+00:00>>
    12/5/2016 11:15:19 PM16-12-05 23:15:19 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=switch.myrelay_1_1, old_state=<state switch.myrelay_1_1=unavailable; friendly_name=MyRelay 1 1 @ 2016-12-05T23:15:19.403632+00:00>, new_state=<state switch.myrelay_1_1=off; description=, node_id=1, friendly_name=MyRelay 1 1, V_STATUS=off, device=172.16.23.30, battery_level=0, child_id=1 @ 2016-12-05T23:15:19.410796+00:00>>]
    

    But no other mentions of switches or states.

    Any ideas??


  • Plugin Developer

    I suggest either inverting the high/low logic when writing to the relay, or checking and possibly changing the wiring of the relay. Some relays "default" to on (active low).



  • Thanks, that never occurred to me and your're absolutely correct, the relay does default to on.

    Ill have a play with the sketch and see if I can figure out how to reverse it, I'm pretty new to this though so I may be back asking for help before long.

    Thanks again.


  • Mod

    @Rooster replace

    #define RELAY_ON 1
    #define RELAY_OFF 0
    

    with

    #define RELAY_ON 0
    #define RELAY_OFF 1
    

    to reverse. It's that simple 🙂


  • Plugin Developer

    @mfalkvidd

    Only doing that will also reverse the reporting to home assistant, I think. You can maybe add two new defines for either the reporting or the the writing to the relay.



  • Thanks for the help I actually fixed the scketch by reversing the RELAY_ON:RELAY_OFF in the digital write. However I was hoping to expand the script to include two relays. After comparing this sketch with the RelayActuator example it was pretty clear that my coding skills would fall short of being able to modify the first sketch.

    Instead I've modified the RelayActuator example (which by default doesn't send the initial states required by HASS).

    This sketch is working well although I think it would have a problem if I wanted the child ID of the first relay to be anything but 1 (but I dont so thats ok for now 🙂 )

    As I said I'm pretty new to this so if there are any glaring problems with how im doing this I'd welcome the feedback.

    Thanks again for the help.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Example sketch showing how to control physical relays. 
     * This example will remember relay state after power failure.
     * http://www.mysensors.org/build/relay
     */ 
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    #include <MySensors.h>
    
    #define RELAY_1  3  // 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 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    bool initialValueSent = false;
    
    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() 
    {
      if (!initialValueSent) {
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        MyMessage msg(sensor, V_STATUS);
        Serial.println("Sending initial value");
        send(msg.set(0));
        wait(1000);
        }
      initialValueSent = true;
      }
    }
    
    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_OFF:RELAY_ON);
         // 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());
         // Tell Controller about it
         MyMessage msg(message.sensor-1+RELAY_1, V_STATUS);
         send(msg.set(message.getBool()?RELAY_OFF:RELAY_ON));
       } 
    }
    

  • Admin

    @Rooster If you only want to send the initial values once, you can move the logic to setup(). Setup is called after the radio transport is initialized however you may need the 2.1 beta.


  • Plugin Developer

    In mysensors 2.0 setup is called before registering the sensors at the gateway, so values can't be sent during setup in 2.0. This is changed in 2.1.


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 17
  • 5
  • 2

8
Online

11.4k
Users

11.1k
Topics

112.7k
Posts