Skip to content

pixavier/mqtt4snap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MQTT4Snap !

MQTT4Snap ! is a Snap ! library for using MQTT in Snap ! (in the cloud) and Snap4Arduino (in the edge).

MQTT is an open and secure publish-subscribe standard protocol for the Internet of Things (ISO/IEC 20922), suitable for industrial applications and Digital Twins.

There are several MQTT public free server/brokers for rapid testing purpose, provide by Mosquitto (test.mosquitto.org), Eclipse Projects (mqtt.eclipseprojects.io), HiveMQ (broker.hivemq.com) or EMQX (www.emqx.com/en/mqtt/public-mqtt5-broker). Never use these servers for production. One way to start is using Mosquitto on your own server, which is a popular open-source option available for Linux, Windows and Mac, fast and easy to install. For accessing directly to a MQTT broker from a browser, WebSockets support must be activated on the broker. To monitor de broker activity, MQTT Explorer is an excellent free tool.

Snap ! can be considered a block-based low-code programming language, unlike Node-RED, that can be regarded as a flow-based low-code tool.

This Hello World! example shows how Snap ! can help to implement PubSub-based architectures with frictionless user experience. The example can be extended with IoT devices like this one based on ESP32 (device Digital Twin in this case).

Blocks and usage

MQTT Extension

This demo shows step by step the basic functionalities. If you use secured pages you will need the first option, else, for non secured pages, you will need the second one (without SSL):

(The new extension is available in the Snap ! library list as "MQTT" and does not need the activation of JavaScript extensions)

Usually, the default brokers configuration only works with http pages (no https). Using http you can connect to a default local Mosquitto imstallation at localhost.

Here you can see a minimum example (please consult the blocks help):

Minimal example

WebSocket protocol (ws:// or wss://) can be specified in the broker field. If not specified, if the Snap ! webpage is http:// type, then ws:// is used, and if it is https:// type then wss:// is used.

The standard "split" block supports JSON decodification, which can be used in conjunction with with the "item" block.

We can combine Snap ! programs with a remote joystick via MQTT (in LateralProjects).

Old library demo (JavaScript extensions must be ticked with the old version when using old library)

Old library demo (without SSL)

If you want to import the old MQTT4Snap ! blocks library, copy the following URL to the clipboard and paste it when importing into Snap !:

https://raw.githubusercontent.com/pixavier/mqtt4snap/master/LateralProjects/old_dist/mqtt-standalone.xml

pub / sub blocks

The pub and sub blocks are suitable for PubSub architecture based implementations.

pub sub blocks

If you get a message when subscribing to a topic is because there is retained note. To clean it, publish an empty message to the topic with the retain flag to false.

request / response blocks

The request and response blocks are suitable for an asynchronous client-server architectural approach on a PubSub based infrastructure. We add the response topic at the end of the calling topic, behind the last @ sign, acting as an call identifier. It enables the responder to publish the response to the response topic. An example of a topic is "topicName/readx/@9876.1", in which the idCall is 9876.1. The idCall corresponds to the responseTopic field introduced in MQTT 5. We use the 3.1.1 MQTT version to keep compatibility with microcontroller implementations such as the PubSubClient library.

request response blocks

request reporter block

The request reporter block is suitable for a synchronous client-server architectural approach on a PubSub based infrastructure, and it is the synchronous version of the previous asynchronous request block based on the response topic. Some similar idea implementations such as the Node.js Replyer module are available. Find here a minimum Python server and client implementations of a synchronous MQTT 3.1.1 echo server for the request block reporter testing. Find here a minimum synchronous MQTT 3.1.1 callback function implemented on an ESP32 microcontroller.

The example MQTT response report block combined with the response block shows the same echo example fully implemented in Snap !.

request reporter block

PubSub architectures can be considered as a generalization of client-server architectures:

  • An asynchronous request-response can be converted into synchronous with a response waiting semaphore based mechanism.
  • Synchronous request-response can be emulated in PubSub with a one-time subscription (one-to-one) and a waiting semaphore.
  • PubSub can be emulated in client-server if all agents can play both client and server roles.

The waiting semaphore can be improved by adding timeout management.

Synchronous and asynchronous client-server can be represented in UML, regarding the arrowheads and the activation boxes:

Synchronous and asynchronous client-server

unsubscribe / disconnect blocks

The unsubscribe block cancels a specific subscription. The disconnect block accepts the value all as a parameter, and then it closes all existing connections to the MQTT brokers.

Note: This library always works with MQTT Quality of Service (QoS) level 0.

Example: Controlling IoT electronic devices

Snap ! can be used for controling and testing IoT devices. Here you can see a demo with a physical twin at the Snap!Con 2021.

Wokwi is an online Digital Twin for IoT developments. You can see an executable MQTT Python example here or a C/C++ example here, which can be controlled and tested from Snap !.

Command-line mode

You can run Snap ! in command-line mode using a headless browser. To run MQTT4Snap ! in a headless browser, the JavaScript extensions must be enabled by default. It can be achieved with an own Snap ! installation (Download source), adding this line of code "Process.prototype.enableJS = true;" in the snap.html script block of code.

MQTT.js library

MQTT4Snap ! is based on the MQTT.js library.

Deprectaed: The "load MQTT library" block loads the module on the fly from the UNPKG CDN repo.

load MQTT library

Acknowledgents

This project wouldn't exist without:

Thanks to Simon Walters for his Snap ! MQTT Extension proposal.

MQTT4Snap ! was presented in the Snap!Con 2020 Conference.

About

MQTT for Snap!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published