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. Controllers
  3. OpenHAB
  4. Openhab MQTT Example

Openhab MQTT Example

Scheduled Pinned Locked Moved OpenHAB
openhab mqtt ex
49 Posts 8 Posters 68.8k Views 1 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.
  • T Offline
    T Offline
    tboha
    wrote on last edited by
    #14

    I stripped my Openhab Installation to the bones and tried to run the example - now I'm getting strange errors like "InstantiationException on org.openhab.core.library.types.StringType"

    It will take a while to check out the reasons this behavior.

    If I change the MQTT-Topics by hand (e.g. with mosquitto_pub from the raspberry) with some String, they show up correct, if it's a number it won't. Strange.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tboha
      wrote on last edited by
      #15

      A little update. I couldn't figure out the reasons of this strange behavior, bus at least there exist a kind of workaround.

      File mqtt.sitemap

      sitemap mqtt label="MQTT-Test"
      {
       Frame label="MQTT" 
         {	
          Switch item=mqttsw1 label="MQTT Switch 1 (text)" 
          Text item=mqttsw1s   
          Switch item=mqttsw2 label="MQTT Switch 2 (raw)"
          Text item=mqttsw2s 
          Switch item=mqttsw3 label="MQTT Switch 3 (string)" 
          Text item=mqttsw3s
          Text item=sketch20
         }
      }
      

      File mqtt.items

      Switch mqttsw1 "MQTT Switch 1" (all,mqtt) {mqtt=">[mysensor:/testsw/1:command:on:RED],>[mysensor:/testsw/1:command:off:Green]"}
      Switch mqttsw2 "MQTT Switch 2" (all,mqtt) {mqtt=">[mysensor:/testsw/2:command:off:default],>[mysensor:/testsw/2:command:on:default]"}
       Switch mqttsw3 "MQTT Switch 3" (all,mqtt) {mqtt=">[mysensor:/testsw/3:command:*:Switch ${itemName} was turned ${command}]"}
      
      String mqttsw1s "MQtt Switch 1 Status [%s]" (all,mqtt) {mqtt="<[mysensor:/testsw/1:state:default]"}
       String mqttsw2s "MQtt Switch 2 Status [%s]" (all,mqtt) {mqtt="<[mysensor:/testsw/2:state:default]"}
      String mqttsw3s "MQtt Switch 3 Status [%s]" (all,mqtt) {mqtt="<[mysensor:/testsw/3:state:default]"}
                                         
       String sketch20 "Sketch name 20 [%s]"  (all,mqtt) {mqtt="<[mysensor:MyMQTT/20/255/V_SKETCH_NAME:state:default]"}
      

      Switch 1 : default is replaced with RED/Green and this text is output to the topic /testsw/1.
      Toggling this switch will result in RED/Green in the status field.

      Switch 2: the original statement.
      Toggling this switch will result in ON/OFF messages shown in the broker (e.g. MyMQTT)

      Switch 3: copied from manual : https://github.com/openhab/openhab/wiki/MQTT-Binding
      Toggling this switch will result in a well formed Message in status and broker.

      This line resolves the secret of the 2 publishers : OpenHab counts every ">" as a separate publisher.

      Furthermore the browser used seems to affect the results. It is mentioned somewhere in the documentation that browsers behave different. I'm using Opera 12.17 - for some items
      manual refreshing is required. Chrome does this automatically.

      However - the line "String sketch20....." should have been functional despite of all this. I can't see an obvious explanation for failure.

      Maybe you can adapt the above skeleton files for your needs.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Marcus
        wrote on last edited by
        #16

        Good morning.

        I did what u said. But i dont see the status of my switch.

         Switch mqttsw1 "MQTT Switch 1" (all,mqtt) {mqtt=">[mysensor:MyMQTT/20/3/V_TRIPPED:command:on:RED],>[mysensor:MyMQTT/20/3/V_TRIPPED:command:off:Green]"}
        Switch mqttsw2 "MQTT Switch 2" (all,mqtt) {mqtt=">[mysensor:MyMQTT/20/3/V_TRIPPED:command:off:default],>[mysensor:MyMQTT/20/3/V_TRIPPED:command:on:default]"}
        Switch mqttsw3 "MQTT Switch 3" (all,mqtt) {mqtt=">[mysensor:MyMQTT/20/3/V_TRIPPED:command:*:Switch ${itemName} was turned ${command}]"}
        
        String mqttsw1s "MQtt Switch 1 Status [%s]" (all,mqtt) {mqtt="<[mysensor:MyMQTT/20/3/V_TRIPPED:state:default]"}
        String mqttsw2s "MQtt Switch 2 Status [%s]" (all,mqtt) {mqtt="<[mysensor:MyMQTT/20/3/V_TRIPPED:state:default]"}
        String mqttsw3s "MQtt Switch 3 Status [%s]" (all,mqtt) {mqtt="<[mysensor:MyMQTT/20/3/V_TRIPPED:state:default]"}
        
        String sketch20 "Sketch name 20 [%s]"  (all,mqtt) {mqtt="<[mysensor:MyMQTT/20/3/V_TRIPPED:state:default]"}
        

        The sitemap is the same as yours.

        mqtt-test.JPG

        As u can see there is no red/green and no well formed text-message.

        Thanks anyway! Would be very nice if we get this to work.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tboha
          wrote on last edited by
          #17

          It seems your broker does not response to OpenHab or the binding is not effective.

          Your installation is different from mine - you are running OpenHab on your raspberry , I'm running OpenHab on my desktop.
          We both use OpenHab 1.6.1.
          Probably you are running your MQTT Broker (presumably mosquitto?) on your raspberry - so do I.
          You are running MyMQTT on Android, so do I.
          (by the way -- I am not able to publish something from MyMQTT despite MyMQTT says "Message published" - nothing arrives at the broker. You got similar experiences?)

          For a basic debugging environment I use mosquitto_sub in an extra terminal on my Raspberry with

          mosquitto_sub -t /#
          

          which should show all messages arriving at the broker.
          First test with mosquitto_pub in another terminal:

          mosquitto_pub -t /testsw/1 -m "switch1"
          

          If this Message shows up we got our topic instantiated (and the broker is working).

          mosquitto_pub -t /testsw/1 -m "ON" -r
          

          Now sending a retained message. If OpenHab connects to the broker, this message will be sent immediately

          in the OpenHab log you should see this:

          11:15:07.844 INFO  runtime.busevents[:26] - mqttsw1s state updated to switch1.
          11:16:21.289 INFO  runtime.busevents[:26] - mqttsw1s state updated to ON
          

          Toggling some switch should give the followin log:

          12:00:03.976 DEBUG o.o.b.m.i.MqttItemBinding[:44] - Publishing command OFF to /testsw/1
          12:00:03.991 DEBUG o.o.i.t.m.i.MqttBrokerConnection[:437] - Publishing message 6 to topic '/testsw/1'
          12:00:03.994 INFO  runtime.busevents[:22] - mqttsw1 received command OFF
          12:00:04.004 INFO  runtime.busevents[:26] - mqttsw1s state updated to Green
          

          what does your log show?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Marcus
            wrote on last edited by
            #18

            I use the MQTT Ethernet-Ethernet Gateway as broker http://www.mysensors.org/build/mqtt_gateway

            And with MyMQTT on my Smartphone i can see that its working.

            Later i will test the other thing.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tboha
              wrote on last edited by
              #19

              This may be the fault. The MQTT Gateway is not a broker - it is just a special form of client, it "only" translates messages from the MySensors network into MQTT acceptable forms an supplies Ethernet access. It does not perform any broker function.
              To install a broker you should spend a little time with Mosquitto (http://mosquitto.org/) or http://jpmens.net/2013/09/01/installing-mosquitto-on-a-raspberry-pi/
              In a short way: To install MQTT on your raspberry:

              sudo apt-get update
              sudo apt-get upgrade
              

              to be up to date.

              sudo apt-get install mosquitto mosquitto-clients
              

              this should be enough to get your broker up and running with default parameters.

              Replace the IP-adress in OpenHab accordingly and you should be done.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Marcus
                wrote on last edited by
                #20

                Its not a broker????

                "The MQTT gateway is basically a Ethernet Gateway with modified software which makes it act as a MQTT broker. "
                

                This text in the build instructions has to be fixed. In no minute i thought about installing mosquitto.

                But i hope the installation of mosquitto will get it to work.

                JohnJ 1 Reply Last reply
                0
                • M Marcus

                  Its not a broker????

                  "The MQTT gateway is basically a Ethernet Gateway with modified software which makes it act as a MQTT broker. "
                  

                  This text in the build instructions has to be fixed. In no minute i thought about installing mosquitto.

                  But i hope the installation of mosquitto will get it to work.

                  JohnJ Offline
                  JohnJ Offline
                  John
                  Plugin Developer
                  wrote on last edited by John
                  #21

                  @Marcus It does act as a broker, only with very limited functions, you do not have to subscribe and also no QOS is done. But it is a broker.

                  Just try to connect an mqtt client to it, and you will see messages being received by the client. (for example mqtt.fx)

                  My Domotica project: http://www.pidome.org

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Marcus
                    wrote on last edited by Marcus
                    #22

                    @John
                    I use MyMQTT on my smartphone is this a client? There are messages coming in.

                    Do i need mosquitto for make mysensors work with openhab or not. Thats the only thing that is interesting for me at the moment.

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tboha
                      wrote on last edited by
                      #23

                      Hi Marcus, I have to confess I mingled up the names and got a little lack of information. I referred to something I tried several months ago - i didn't fit my needs, so I changed back to the serial Interface - which performed reasonably good and i lost contact to the MySensors-MQTT side via Ethernet.

                      To my surprise i read there shall be some basic broker functionality in the Gateway sketch - which sounds really good. It will take a while to reactivate my Ethernetshield and give it a try. Sadly my holidays are over tomorrow and there will be not much spare time to test it.

                      Nevertheless trying Mosquitto would be interesting, e.g. to talk to the MySensors broker directly and to check the OpenHab->Broker connectivity.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Marcus
                        wrote on last edited by
                        #24

                        Hmm, now i am nearly at the beginning. It would be nice if i dont need mosquitto.

                        Maybe here are others who can help.

                        Thank u anyway for ur help.

                        Are we both the only who want to use openhab???

                        Is there no other way to check if openhab recieves the messages from the broker (ethernet gateway)?

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          C.r.a.z.y.
                          wrote on last edited by
                          #25

                          I am also using openHab ;) but serial way

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Marcus
                            wrote on last edited by
                            #26

                            Is there a big difference between serial and ethernet?

                            C 1 Reply Last reply
                            0
                            • M Marcus

                              Is there a big difference between serial and ethernet?

                              C Offline
                              C Offline
                              C.r.a.z.y.
                              wrote on last edited by
                              #27

                              @Marcus it works fine for me

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Marcus
                                wrote on last edited by
                                #28

                                @Crazy Do u need Mosquitto?

                                C 1 Reply Last reply
                                0
                                • C Offline
                                  C Offline
                                  ceburge
                                  wrote on last edited by
                                  #29

                                  I to have had this same problem using openhab and MQTT Gateway. I could toggle relays and such but could not receive any information back from the gateway things such as sketch info or versions. So I decided to pull from that and try PiDome. I like the way PiDome works with the Serial Gateway. Everything seems to be more dynamic. I did try PiDome with the MQTT Gateway and got the same results as with Openhab I could publish information to the gateway but couldn't receive for some reason. Would like to find out what could be happening myself. However right now I'm waiting on parts.

                                  1 Reply Last reply
                                  0
                                  • M Marcus

                                    @Crazy Do u need Mosquitto?

                                    C Offline
                                    C Offline
                                    C.r.a.z.y.
                                    wrote on last edited by
                                    #30

                                    @Marcus serial is working fine with piDome, openHab and Vera. No need for mosquito which i never installed and used before.

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      Marcus
                                      wrote on last edited by Marcus
                                      #31

                                      Thank u for your answers.

                                      I love the look of openhab. So i will try the it with mosquitto.

                                      If it will not work then i will try pidome and serial gateway.

                                      If i have new infos i will post them here!

                                      JohnJ 1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        Marcus
                                        wrote on last edited by
                                        #32

                                        Good morning community.

                                        Here is what i did now for installing mosquitto on my raspberry pi.

                                        This infos are from here (http://jpmens.net/2013/09/01/installing-mosquitto-on-a-raspberry-pi/).

                                          curl -O http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
                                        
                                          sudo apt-key add mosquitto-repo.gpg.key
                                        
                                          rm mosquitto-repo.gpg.key
                                        
                                          cd /etc/apt/sources.list.d/
                                        
                                          sudo curl -O http://repo.mosquitto.org/debian/mosquitto-repo.list
                                        
                                          sudo apt-get update
                                        
                                          sudo apt-get install mosquitto mosquitto-clients python-mosquitto
                                        

                                        Now mosquitto is running.

                                        With:

                                        sudo /etc/init.d/mosquitto stop
                                        

                                        I can stop it.

                                        With

                                        sudo /etc/init.d/mosquitto start
                                        

                                        I can start it.

                                        But what to do now???
                                        How do i connect mosquitto to my MQTT-Ethernet-Gateway?
                                        How do i connect mosquitto to Openhab?

                                        Please help!

                                        1 Reply Last reply
                                        0
                                        • b0rmannB Offline
                                          b0rmannB Offline
                                          b0rmann
                                          wrote on last edited by
                                          #33

                                          MQTT-Ethernet-Gateway is mqtt brocker, not client :(

                                          to connect mosquitto try use MQTTClientGateway http://forum.mysensors.org/topic/524/mqtt-client-gateway

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


                                          15

                                          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