8

I working with several arduino board and now I need to control them via a web interface.

Via web interface I want to activate GPIO.

I have two ideas:

  1. Each arduino acts as web server and I can control the GPIO via the Arduino web page. Basically one browser tab for each arduino.
  2. Use the MQTT protocol to exchange message with the arduino boards. Furthermore I think to use an raspberry as web server and as MQTT broker. Each arduino board is subscribed to a specific topic and through a web page, hosted on the raspberry, I can control the Arduino GPIOs.

The first solution I very quicly and simple.

Regarding the second option, I don't know how to send MQTT message via a web page. I read that I need to use Websocket. Is it right? Need I to write code in Javascript or what?

My second question is: Can the MQTT broker manage both MQTT and MQTT over Websocket at the same time? Otherwise I need to use the Websocket also on Arduino.

Another option is to built a Python script with GUI that allow to send MQTT messages to Arduino.

Is there a best way?

Thanks for the help!

Federico
  • 183
  • 4

1 Answers1

5

What you've written all seems reasonable to me.

MQTT traditionally runs over TCP1, but your browser does not allow webpages to open a raw TCP socket. There are proposals to allow that, but I doubt they'll be implemented any time soon. So, your browser can't connect to a MQTT broker only supporting TCP connections.

The solution is, as you've identified, to use a WebSocket—these are supported by the browser and so some JavaScript code can be used to connect to an MQTT broker through a web page. HiveMQ have an example you can play with, or you can try a library such as MQTT.js which supports WebSocket communication with an MQTT broker.

Most brokers—and all I'm aware of—won't care about whether a client is a WebSocket or TCP client. You can happily connect both to one broker, and you can find instructions on how to configure a Mosquitto broker on Stack Overflow2.

With regards to a best way... it's up to you. If you are happy with JavaScript, then there's no problem using that. If Python's easier, do that (you wouldn't need to set up WebSockets support that way). You could even just use pre-built client software if you didn't care about the UI too much.


1 MQTT 3.1.1 does allow for TLS or WebSocket connections also; see section 4.2 of the specification. There is a variant, MQTT-SN, where the requirement for TCP is relaxed. Either way, you're probably not worried about MQTT-SN for your use case.

2 Note that on Windows, the Mosquitto build does not have WebSocket support enabled. You will need to build Mosquitto yourself if you want to use it on Windows. Alternatively, you could try a different broker that doesn't restrict you in this way.

Aurora0001
  • 18,520
  • 13
  • 55
  • 169