Mosquitto and Web Sockets

This entry is about ThinkStudio (thingstud.io), Web Sockets and Mosquitto in my quest for ESP nirvana.

So TODAY I was alerted to an early version of a new service which I think you might like… so check out http://www.thingstud.io/ 

Clarification: This is ALPHA software but already, provided they keep at it I think this has the potential to replace the rather slow-moving NETIO app which provides a nice user interface for IOT. 

mqttThis software uses MQTT which is right up my street.

Well, always one for a challenge, I went to the site, set up an account and…. nothing – not a thing. I put in my MOSQUITTO (MQTT) credentials and got absolutely no-where apart from having a nice pretty interface. They have a nice pop up live help (pretty amazing for an alpha product) and before long I was happily chatting away to Michael Karliner. It turns out that the product needs MQTT to have Websocket support which my Mosquitto on the Raspberry Pi does not.  I’ve been putting it off because every explanation I’ve seen up to now involves compilers and all sorts of horrible things.

I was pointed to this link… blog.thingstud.io which Michale had just completed – and in there is an article about installing Mosquitto with websockets. I was in one of those “devil may care” moods and so figured there was no reason why this could no upgrade my existing Mosquitto.

So I did – I followed the instructions from the start right up to but not including the bit about installing Node-Red as I’ve already done that.

WELL BLOW ME!!! it worked. Well… not quite. It over-wrote my mosquitto.conf file with a virgin one so now my security was bust and my local boards could not talk to anyone.

I opened the virgin mosquitto.conf which had everything commented out and added these lines as per the blog

listener 9001

protocol websockets

So now I could run a websocket on port 9001 (I tested that by redirecting the port on my external address then trying  http://www.hivemq.com/demos/websocket-client/ – worked a treat)

That allowed the websockets to work – on port 9001. But… my normal port would not work any more – 1883.. so I added this.

listener 1883

protocol mqtt

That got both pipes listening – all that was needed was to restore my user access.

allow_anonymous false

password_file /etc/mosquitto/passwords.txt

And Bob’s your uncle – the latest Mosquitto (which is just excellent) and now websockets.. which means I can write my own web pages to access MQTT if I want to!!! And no doubt I will – but if ThingStud.io works out –  I might not have to. Ensure you are registered to get updates in here or follow my usual Twitter and Facebook accounts to make sure you don’t miss out – I have a hunch about this one.

Update: Oh I found another link with instructions for installing websockets – and a little info on the mosquitto.conf file…

http://harizanov.com/wiki/wiki-home/raspberry-pi/how-to-rasbperry-pi-install-mosquitto-with-websockets-enable/

19 thoughts on “Mosquitto and Web Sockets

  1. Sorry to jumpstart an old thread but I’ve recently started using thingstud.io and it works really well on my PC but for some reason It will not connect to the MQTT server using with Chrome on my Android phone with a secure websocket to cloudmqtt. (phone is lollipop and latest version of Chrome)

    I’ve tried a couple of Android phones with no joy and my wife’s Iphone on which it works!

    Anyone come across this before

  2. Hi Peter, you don’t need to compile Mosquitto yourself anymore. Simply add the repo from mosquitto.org and install from that.

    That version includes websockets, all you need to do is to add a conf file.

    I’ve put some information on my blog to this effect.

  3. Hi Pete,
    Mike from thingstud.io noticed that I was running mosquitto manually. When doing so it is necessary to add the configuration file to the command, as in:

    mosquitto -c configfile

    After that I could connect and did not even need to enable port forwarding.

    As Murf said the connection is from the desktop to the Raspberry…

    Regards,
    GIlson

    1. Yes I have the connection running. Actually after help from Mike and much reading I’ve finally gotten Mosquitto auto-running as well as Node-Red so the whole lot comes up from boot. Just have to make sure now that my various bits of code run on this new Pi2. The first time I did this I’d just bought my first Pi2 and I loaded the kitchen sink into it. This time I’ve been careful to load only what I need and to get rid of the default stuff on the Pi for which I have no earthly use – the games etc and WolfRam which is massive. The result, my new image is barely over 3.5Gig. I also took the opportunity to buy a fast (Sandisk Extreme) MicroSD and a fast USB3 adaptor (Anker) so I can back that up in a matter of minutes. Compared to my cheap Ebay chip and USB2 adaptor this is a different world.

        1. Hi

          Well, in etc/init.d I have a file called mosquitto and inside that it has this..

          # /etc/init.d/mosquitto: start and stop the mosquitto MQTT message broker

          test -x ${DAEMON} || exit 0

          umask 022

          . /lib/lsb/init-functions

          # Are we running from init?
          run_by_init() {
          ([ “$previous” ] && [ “$runlevel” ]) || [ “$runlevel” = S ]
          }

          export PATH=”${PATH:+$PATH:}/usr/sbin:/sbin”

          case “$1” in
          start)
          if init_is_upstart; then
          exit 1
          fi
          log_daemon_msg “Starting Mosquitto message broker:” “mosquitto”
          if start-stop-daemon –start –quiet –oknodo –background –make-pidfile –pidfile ${PIDFILE} –exec ${DAEMON} — -c /etc/mosquitto/mosquitto.conf ; then
          log_end_msg 0
          else
          log_end_msg 1
          fi
          ;;
          stop)
          if init_is_upstart; then
          exit 0
          fi
          log_daemon_msg “Stopping Mosquitto message broker:” “mosquitto”
          if start-stop-daemon –stop –quiet –oknodo –pidfile ${PIDFILE}; then
          log_end_msg 0
          rm -f ${PIDFILE}
          else
          log_end_msg 1
          fi
          ;;

          reload|force-reload)
          if init_is_upstart; then
          exit 1
          fi
          log_daemon_msg “Reloading Mosquitto message broker:” “mosquitto”
          if start-stop-daemon –stop –signal HUP –quiet –oknodo –pidfile $PIDFILE; then
          log_end_msg 0
          else
          log_end_msg 1
          fi
          ;;

          restart)
          if init_is_upstart; then
          exit 1
          fi
          log_daemon_msg “Restarting Mosquitto messae broker:” “mosquitto”
          if start-stop-daemon –stop –quiet –oknodo –retry 30 –pidfile ${PIDFILE}; then
          rm -f ${PIDFILE}
          fi
          if start-stop-daemon –start –quiet –oknodo –background –make-pidfile –pidfile ${PIDFILE} –exec ${DAEMON} — -c /etc/mosquitto/mosquitto.conf ; then
          log_end_msg 0
          else
          log_end_msg 1
          fi
          ;;

          try-restart)
          if init_is_upstart; then
          exit 1
          fi
          log_daemon_msg “Restarting Mosquitto message broker” “mosquitto”
          set +e
          start-stop-daemon –stop –quiet –retry 30 –pidfile ${PIDFILE}
          RET=”$?”
          set -e
          case $RET in
          0)
          # old daemon stopped
          rm -f ${PIDFILE}
          if start-stop-daemon –start –quiet –oknodo –background –make-pidfile –pidfile ${PIDFILE} –exec ${DAEMON} — -c /etc/mosquitto/mosquitto.conf ; then
          log_end_msg 0
          else
          log_end_msg 1
          fi
          ;;
          1)
          # daemon not running
          log_progress_msg “(not running)”
          log_end_msg 0
          ;;
          *)
          # failed to stop
          log_progress_msg “(failed to stop)”
          log_end_msg 1
          ;;
          esac
          ;;

          status)
          if init_is_upstart; then
          exit 1
          fi
          status_of_proc -p ${PIDFILE} ${DAEMON} mosquitto && exit 0 || exit $?
          ;;

          *)
          log_action_msg “Usage: /etc/init.d/mosquitto {start|stop|reload|force-reload|restart|try-restart|status}”
          exit 1
          esac

          exit 0

  4. The Hive-MQ MQTT client is javascript based. So all the connections made are from your desktop to your mqtt-server. No need to forward any ports if you are in the same network segment (aka home…)

    I’ve been running mosquitto with websockets for some time. I did go the “compile yourself” route though. Mostly for debugging since there has not been any good java-apps (before mqttspy).

  5. Hi Pete,
    Have you already been able to create any screen?
    I still can’t create a connection from thingstud.io to the Pi with mqtt 1.4, where I can see the error message:
    1428979794: New connection from 192.168.1.204 on port 1883.
    1428979794: Socket error on client , disconnecting.
    Any clues?
    Thanks,
    Gilson

    1. You can’t use port 1883. You need to enable Websockets on your MQTT broker and that will be a different port. It’s in the blog. Top right of ThingStud.io – is a circle – it is red. When you are connected it will go green.

      1. Hi Pete,
        I did just that, built 1.4 with websockets and enabled it to port 9001. Then configured the connection to the same port and protocol but the circle remained red.

        Maybe I captured the error message when I was trying other combinations to see if the error description would change, but it did not work for me with 9001 for sure.

        Is there any other way I can test/debug websokets on mqtt?

        1. oguime,
          Did you enable port forwarding on your router so the service can connect to your server on port 9001?

          1. Yes I did – after putting a user and password in as well of course – done via the mosquitto.conf file.

  6. Very nice catch!!! Looks like NetIO has been setting on their laurels and been scooped. This is indeed a nice piece of the puzzle that has been missing!

Comments are closed.