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. General Discussion
  3. Help work out correct MQTT message to activate relay

Help work out correct MQTT message to activate relay

Scheduled Pinned Locked Moved General Discussion
11 Posts 3 Posters 3.5k Views 3 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.
  • ahmedadelhosniA Offline
    ahmedadelhosniA Offline
    ahmedadelhosni
    wrote on last edited by
    #2

    Can you please post your GW sketch ?

    Check this also.
    http://forum.mysensors.org/topic/2378/how-can-i-set-the-payload-for-mqtt-v1-6

    Another note regarding the above link. Some replies are talking about a bug which was fixed. But in all cases check the first replies concerning setting the message structure.

    T 1 Reply Last reply
    0
    • T Offline
      T Offline
      TommySharp
      wrote on last edited by
      #3

      As far as I know I am just using the default MySensors relay sketch with 1 relay.

      // Example sketch showing how to control physical relays. 
      // This example will remember relay state even after power failure.
      
      #include <MySensor.h>
      #include <SPI.h>
      
      #define RELAY_1  6  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
      #define NUMBER_OF_RELAYS 1 // 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
      
      MySensor gw;
      
      void setup()  
      {   
        // Initialize library and add callback for incoming messages
        gw.begin(incomingMessage, AUTO, true);
        // Send the sketch version information to the gateway and Controller
        gw.sendSketchInfo("Relay 1.0");
      
        // Fetch relay status
        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)
          gw.present(sensor, S_LIGHT);
          // Then set relay pins in output mode
          pinMode(pin, OUTPUT);   
          // Set relay to last known state (using eeprom storage) 
          digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
      }
      
      
      void loop() 
      {
        // Alway process incoming messages whenever possible
        gw.process();
      }
      
      void incomingMessage(const MyMessage &message) {
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type==V_LIGHT) {
           // Change relay state
           digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
           // Store state in eeprom
           gw.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());
         } 
      }
      
      
      1 Reply Last reply
      0
      • ahmedadelhosniA ahmedadelhosni

        Can you please post your GW sketch ?

        Check this also.
        http://forum.mysensors.org/topic/2378/how-can-i-set-the-payload-for-mqtt-v1-6

        Another note regarding the above link. Some replies are talking about a bug which was fixed. But in all cases check the first replies concerning setting the message structure.

        T Offline
        T Offline
        TommySharp
        wrote on last edited by
        #4

        @ahmedadelhosni

        Ahhh by GW sketch I guess you mean gateway sketch? I set that up well over a year ago and I actually wouldn't even have a clue where the original sketch is... My temp/humidity sensors have been working perfectly for over a year now. So just diving back into the arduino stuff to see if I can get the relay going.

        I'm even using a different PC to compile and upload the sketches so I might not even have the original GW sketch....

        ahmedadelhosniA 1 Reply Last reply
        0
        • T TommySharp

          @ahmedadelhosni

          Ahhh by GW sketch I guess you mean gateway sketch? I set that up well over a year ago and I actually wouldn't even have a clue where the original sketch is... My temp/humidity sensors have been working perfectly for over a year now. So just diving back into the arduino stuff to see if I can get the relay going.

          I'm even using a different PC to compile and upload the sketches so I might not even have the original GW sketch....

          ahmedadelhosniA Offline
          ahmedadelhosniA Offline
          ahmedadelhosni
          wrote on last edited by
          #5

          @TommySharp Yeah I mean the gateway.

          You say your gateway is running for a year now, but now you want to use MQTT so you have to set this MQTT gateway. I am not familiar with MYsensor library 1.5 or previous versions. I am using the development branch ( https://github.com/mysensors/Arduino )
          It has a sketch for setting MQTT gateway and you can test sending a payload data using mosquitto as explained in my first link in the previous reply. Also there is a new sketch for relayactuator example.

          It works okay and I have tested it.

          T 1 Reply Last reply
          0
          • ahmedadelhosniA ahmedadelhosni

            @TommySharp Yeah I mean the gateway.

            You say your gateway is running for a year now, but now you want to use MQTT so you have to set this MQTT gateway. I am not familiar with MYsensor library 1.5 or previous versions. I am using the development branch ( https://github.com/mysensors/Arduino )
            It has a sketch for setting MQTT gateway and you can test sending a payload data using mosquitto as explained in my first link in the previous reply. Also there is a new sketch for relayactuator example.

            It works okay and I have tested it.

            T Offline
            T Offline
            TommySharp
            wrote on last edited by
            #6

            @ahmedadelhosni
            Yes my gateway is the MQTT gateway and it has been running for well over a year. It happily posts temp/humidity which my OpenHAB server subscribes to and reads in the sensor readings.

            I just need to work out now how to activate a relay :-)

            martinhjelmareM 1 Reply Last reply
            0
            • T TommySharp

              @ahmedadelhosni
              Yes my gateway is the MQTT gateway and it has been running for well over a year. It happily posts temp/humidity which my OpenHAB server subscribes to and reads in the sensor readings.

              I just need to work out now how to activate a relay :-)

              martinhjelmareM Offline
              martinhjelmareM Offline
              martinhjelmare
              Plugin Developer
              wrote on last edited by
              #7

              @TommySharp

              Hi!

              According to the mqtt gateway sketch in 1.5.3, topic structure is:

              Topicprefix/node_id/child_id/v_type

              Mysensors payload is mqtt payload.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                TommySharp
                wrote on last edited by
                #8

                So when my relay sensor announces itself to the gateway below.... Does it announce the child ID anywhere so I can be sure I'm using the right one?

                send: 24-24-0-0 s=255,c=3,t=11,pt=0,l=5,st=ok:Relay
                send: 24-24-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                send: 24-24-0-0 s=1,c=0,t=3,pt=0,l=5,st=ok:1.4.1

                1 Reply Last reply
                0
                • martinhjelmareM Offline
                  martinhjelmareM Offline
                  martinhjelmare
                  Plugin Developer
                  wrote on last edited by
                  #9

                  The last line shows s=1, so child id is 1.

                  Read this post for details on topic structure:
                  http://forum.mysensors.org/topic/303/mqtt-broker-gateway/17

                  Also read the text at the top of the mqtt gateway sketch for more info.

                  I think you should test with this topic:
                  prefix/24/1/V_LIGHT

                  Replace prefix with your set topic prefix.

                  You can set payload with the -m option if you're using mosquitto.

                  1 Reply Last reply
                  0
                  • martinhjelmareM Offline
                    martinhjelmareM Offline
                    martinhjelmare
                    Plugin Developer
                    wrote on last edited by
                    #10

                    Be aware though, that the mqtt broker gateway sketch will me removed in the next release, so if you want the best support I would think about changing to the mqtt client gateway. This is currently only available in the dev branch at github, but there is a sticky guide in the forum, on how to set it up.

                    1 Reply Last reply
                    1
                    • T Offline
                      T Offline
                      TommySharp
                      wrote on last edited by
                      #11

                      Awesome! I'd much rather use a client gateway so might just hold off until the next release of MySensors and then use the MQTT Client Gateway.

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


                      20

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.1k

                      Posts


                      Copyright 2025 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