Experiments with PHP and MQTT

Over the past few days I’ve been playing around with combining lightweight messaging and PHP. There are a couple of reasons for this, but the primary one is that I’d like to extend my prototype iPhone CurrentCost monitoring web application to display more up-to-date information about the state of my home energy usage. I’d planned to do this for a while, but recently Mark Taylor created his own version of the iPhone interface (PDF link) and he has got current readings on the front page. Clearly, I have to compete 🙂

Actually, in my system, I’d like to do things a different way. The heart of my setup is a Really Small Message Broker. At the moment, data from the CurrentCost meter comes in over the USB connection and is then published in pieces, or on topics, to the RSMB (temperature and energy readings are separate). These published messages are then read by a script which is subscribing to the topics and squirrelling the historical data into an rrdtool database; and also being pushed up to our IBM broker “in the interweb cloud” via an MQTT broker bridge connection.

So in theory, having the up-to-date information in the web UI should be a simple case of grabbing the MQTT publications on each topic and displaying them. The way I’ve coded things (and would prefer to do things), this involves having the ability to subscribe to MQTT publications from PHP.

I’m not at the end of the road yet, but I do have a starting point.


I’ve got a front-end test page which currently uses Prototype to send an Ajax request to a server-side PHP script (yes, I have had jQuery recommended to me, and I may well look into that instead of Prototype, but this works).

The server-side script uses the Simple Asynchronous Messaging PHP library. SAM is a wrapper which enables a variety of messaging transports to be supported in PHP, such as MQTT, WebSphere MQ or WebSphere Platform Messaging. Just one thing: I found that in order to get the most recent SAM release to work on Ubuntu on my MPC-L, I had to install IBM’s XMS client SupportPac (for some reason, it won’t build without it, even though it is “optional”) and I also had to delete a spurious empty line from the end of /usr/share/php/SAM/php_sam.php to prevent header issues. Other than that, it was all good.

The script is really simple and basically uses all of the defaults to create a connection to my local RSMB over MQTT. The advantage of this being server-side is that I don’t have to open my RSMB to the Internet, the PHP code can connect via localhost. Once that’s done, it creates a subscription on the topic I’ve asked for, and receives the first data that comes along, then echoes it back to the front-end. I could make it auto-updating with Ajax.PeriodicalUpdater too, but there’s no need to put a load on my server.

Wanna see a quick demo? 😉

I’m quite pleased with the way this is working. There’s some more plumbing to do, and I’ll almost certainly extend the server-side piece to allow two-way communications (publish as well as subscribe) as well as finer-grained control over the options. As a proof-of-concept though, I think this is looking good.

8 thoughts on “Experiments with PHP and MQTT”

  1. Hi Andy,

    Why no sMash or Dojo? I am attacking Dojo for the first time on a commerical project (also power/utilities but on a bigger scale 🙂 ) and was wondering if you have opinions on Dojo/Prototype/jQuery. Maybe chat internally about this?


  2. Thanks for the article. Any clues on installing IBM’s XMS client SupportPac? I trip up here, as I’ve almost zero clue when it comes to all these rpms and compiling from source nonsense…


  3. Hi Justin – re: Dojo, I played around with different Javascript libraries and ended up with Prototype, but didn’t spend long on anything – this was a quick-and-dirty option. I might try using Dojo in a future iteration.

    Martin – XMS (SupportPac IA94) is available from http://www-01.ibm.com/support/docview.wss?uid=swg24007092 and if you’re using Ubuntu like me, you’ll want the Linux ia32 package. You will end up with a .tar.gz file… unpack that (tar -zxvf filename) and there’s an installer shipped inside the bundle (setuplinuxia32) that you can just run at the command line. No compiling or RPMs involved.

  4. Given that I work for them, I’m obviously sorry that you found it unusable – I don’t have any direct influence but I’ll be sure to pass the comment on. Good luck. BTW if you want to use it with MQTT then you might find some edits are required to some of the SAM files – mostly trailing linefeeds/crs in the include files I think.

  5. in my case the library is not working, does it work with mosquitto.org? and how to send the personalized message to mqtt on a personalized topic

Leave a Reply