Newbie question: how to read status of relay?

  • Hi,

    After reading a lot on this site, I think I understand the serial protocol at least at the basic level. I use the MQTT ethernet gateway sketch, and use only MQTT.FX as client to mosquitto. So for now there is not the complication of a controller in the field.
    Now I am trying to alter the Relay Actuator sketch. But investigating this sketch and very much more type of sketches, I don't understand how receiving of messages is handled in the code.

    I mean, in the Relay Actuator code, this is the relevant code: (hard coded node-id of 250)

    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(", New status: ");

    I tested this code with MQTT.FX by sending
    mygateway1-out/250/1/1/0/2 with a payload of 1

    And then the led which represents the relay is lighted up.

    But when I request the value of the relay with:
    mygateway1-out/250/1/2/0/2 with a payload of 1

    the led is also lightened up. This seams false to me because the command value equals 2 , which stands for request.
    So: how can I check in the sketch code not only message.type, but also for the used command?

    I need this because I want to create a node that can be placed on the normal light points at the ceilings. So that via a variable the actual light can be switched. And therefore I think I need to be able to ask the node to the status of the light.

    In the documentation I can see how to read the type (message.type) and the child-sensor-id (message.sensor). But how to get the other variables as mentioned in the serial protocol?

  • Mod

    Why do you need to ask status? Just send on or off command according to the needs

  • Because there will be a local override. Mainly for not interrupting the normal behaviour of the lights in the house, but with the addition of being controlled by a controller.

    To be more specific, imagine the case:
    controller light switch on
    physical light switch off
    light is on

    and now the physical light switch is used so the physical signal will be high on the wire, and still I want the light be off because of the change of switch position.

    So to know the real status of the node output I need to be able to read the output status.

  • Mod

    I'm not sure if I understood your setup... How would you measure the node output? The easiest way to control the relay node would be to use temporary buttons so when you press it it will just tell the node to switch the relay either on or off and you would still have control from the home automation controller

  • Also not sure, if I got the idea right. Imo, it is easier to track Relay status on the node itself and inform controller about changes that are due to switches or PIRs.
    Example from a PIR/Relay node (no MQTT, but shouldn't make a difference) that turns the relay on in case it is not already switched on; you may implement this slightly different using a toggle logic:

    if (digitalRead(RELAY_PIN) == RELAY_OFF ) {
              // Turn on and send in the new status
              digitalWrite(RELAY_PIN, RELAY_ON);
              send(RelayMsg.set(true)); // Send new state 

  • Hero Member

    @Psilin Have a look at this post for some info about dealing with requests. Look at the example for the use of message.getCommand()

  • Well, the suggested forum thread certainly has valueable information regarding my question. Somehow I think this kind of information should be part of the API description.
    I really appreciate the total of Mysensors. It seems like a real solution with high adaption possibilities. The documentation and the given examples unfortuanate are not of the best quality. The learning curve to be able to use the Mysensors solution is therefore quite steep.

    To the question of what I want to accomplish: I am inspired by the Fibaro solution for a double switch. Were Fibaro puts this sensor/actuator behind the wall switch, I want to place it at the position of the light bulb. For now I am trying to make a proof of concept in software, and the hardware is a junk of breadboard and dupont cables. For a real working node I have to solve also some hardware questions. (Low power AC node, AC sensing signal, footprint size, housing etc)

  • Mod

    Have you ever considered a simple sonoff?

  • Hero Member

    @Psilin Do remember that the MySensors project is open source and developed by volunteers. Like all of us they have many other areas of their lives that also demand their time so sometimes you may have to dig a bit to find the information you need.

    As to your switch you may also find the sketch in this post of some use. It is for a push button type switch but could easily be modded for a toggle type.

    Enjoy your MySensors journey 🙂

  • I am well aware of the facts that all these wonderful stuff is created by volunteers. If it was not clear: I really want to thank all of the people who made this possible. It is a great and remarkable project.

    That said: since I am at the very very ( did I mention very?) early stage of playing with the MySensors, I am not in the position to create, add or edit any content at this moment. But I hope since this all is new to me, that if I find something strange, or something that I cannot find, to mention it. It is not criticism, but a honest view of what a newbie like me faces when starting with the project. Perhaps in a later stage I will try to add/change for example the documentation. But that goal for now are a few yards too far away.

    So also,I really want to thank the people who are replying to, perhaps perceived as dumb questions', at this forum. That information really got me some further.

    To read the fields used in the serial protocol in the receive function of the node:

    serial field format:
    node id;child_sensor_id;command;ack;type;payload
    void receive(const MyMessage &message)
      message.getBool();     //in case of variable type boolean

    Regarding using of Sonoff's: I've seen some video's on youtube. It is absolute an incredible featured device with the correct firmware on it. Perhaps in the future I will be using it, but for now I want to focus on the open structure of the mySensors.

    Next step is to try to connect a controller mechanism. Because of the fact I want to use only MQTT as transport protocol, mainly to keep things open, I thought of putting MyController and OpenHab together. Primary to use OpenHab as controller ( also my knowledge of this system is about zero), and use MyController only to be able to upload new firmware. After reading the suggested forum threads, I am scared this won't work because of the fact that when a node ask something to the controller, the node would get up to 2 answers. Only the answer of OpenHab should be used, but as far as I know right now there is no filter or selection option possible.

Log in to reply

Suggested Topics