4

A mate and I are doing a project at the moment that hosts a webpage on a raspberry pi which I would like to send a message via MQTT to the arduino which reads sensors and then sends the values back to the pi to be displayed on the web page.

I am hoping to use Mosquito on the pi as a broker and treat the arduino and the webpage as clients.

I am just having a lot of trouble working out how it all fits together.

A lot of examples I have seen use web sockets and node.js and MQTT.io and I'm just so confused..

Could someone please help me piece it all together and explain how everything fits and what it does?

Thanks in advance!

Lee Wiggins
  • 351
  • 3
  • 11

2 Answers2

5

Websockets allows a web browser to open a bi-directional communication pipe to a server.

For websockets to work you need a websocket enabled mqtt broker. At the moment you have to install this seperately, some examples of what is out there are here. Mosquitto will have websockets enabled in the next version, 1.4, you can compile it if you wish.

An example of what can be done, and what I am doing atm with mqtt and websockets is;

  1. publish temperature data to mqtt broker from an arduino.
  2. subscribe to the topic that the arduino is publishing to and populate the data into a mysql database using python scripts.
  3. have a live website subscribe to the topic and show the current temperature as a line graph.
Community
  • 1
  • 1
Matt.
  • 1,043
  • 1
  • 12
  • 20
  • Thanks Matt! So all web-sockets does is provide a way for the browser to communicate with the MQTT broker? I had thought of doing it with mySQL but would really like to try and do it purely with MQTT if possible. Could I use node.js to replace web sockets? is it a similar thing? – Lee Wiggins Aug 28 '14 at 08:31
  • Pretty much, it acts as a client that can subscribe/publish to topics. I just use mysql for historical data. Node.js, don't know much about that sorry but I have seen this example though that might help http://blog.hekkers.net/2012/10/13/realtime-data-with-mqtt-node-js-mqtt-js-and-socket-io/ – Matt. Aug 28 '14 at 11:44
4

We did tutorials on building applications with MQTT and websockets in the past. Slides (+ Code) can be found on Slideshare.

If you're interested in how MQTT over websockets works in detail, this blog post may help you. Also, a full example websockets/MQTT application tutorial can be found in this blog post.

Dominik Obermaier
  • 5,610
  • 4
  • 34
  • 45
  • does hivemq run on raspberry pi? – Matt. Aug 28 '14 at 11:28
  • Hi! Thanks for your reply! I appreciate it very much! we really wanted to use mosquito as a broker al though the HiveMQ is really cool! I think now that mosquito has websockets support that I will go that way! those tutorials really helped my understanding :-) – Lee Wiggins Aug 28 '14 at 11:33
  • @Matt. HiveMQ runs perfectly on Raspberry Pi. It's designed for multi-core servers, though, but for simple installations with less than 1000 connections, Raspberry Pis work great. Make sure to install a current JDK before installing HiveMQ for the best experience. – Dominik Obermaier Aug 28 '14 at 11:46
  • Thx dominik wasn't 100% if it was supported. @LeeWiggins there you go, install hivemq on your pi and you will have mqtt/websockets all in one, hivemq is pretty easy to install. – Matt. Aug 28 '14 at 11:49