Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Newbie question: how to read status of relay?

Newbie question: how to read status of relay?

Scheduled Pinned Locked Moved Development
10 Posts 4 Posters 2.6k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Psilin
    wrote on last edited by Psilin
    #1

    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(message.sensor);
            Serial.print(", New status: ");
            Serial.println(message.getBool());
        }
    

    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?

    Boots33B 1 Reply Last reply
    0
    • gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Psilin
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • gohanG Offline
          gohanG Offline
          gohan
          Mod
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • rejoe2R Offline
            rejoe2R Offline
            rejoe2
            wrote on last edited by
            #5

            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 
            }
            

            Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

            1 Reply Last reply
            0
            • P Psilin

              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(message.sensor);
                      Serial.print(", New status: ");
                      Serial.println(message.getBool());
                  }
              

              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?

              Boots33B Offline
              Boots33B Offline
              Boots33
              Hero Member
              wrote on last edited by Boots33
              #6

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

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Psilin
                wrote on last edited by
                #7

                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)

                Boots33B 1 Reply Last reply
                0
                • gohanG Offline
                  gohanG Offline
                  gohan
                  Mod
                  wrote on last edited by
                  #8

                  Have you ever considered a simple sonoff?

                  1 Reply Last reply
                  0
                  • P Psilin

                    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)

                    Boots33B Offline
                    Boots33B Offline
                    Boots33
                    Hero Member
                    wrote on last edited by
                    #9

                    @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 :)

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Psilin
                      wrote on last edited by
                      #10

                      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)
                      {
                          //child_sensor_id
                          message.sensor;
                      
                         //command
                         message.getCommand();
                      
                        //ack
                        message.isAck();
                      
                      //type
                        message.type;
                      
                      //payload
                        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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      17

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.0k

                      Posts


                      Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular