hero
Home About Hardware Guide

Categories

Archive

July 2024

June 2024

May 2024

April 2024

March 2024

February 2024

January 2024

December 2023

November 2023

October 2023

September 2023

August 2023

July 2023

June 2023

May 2023

April 2023

March 2023

February 2023

January 2023

December 2022

back to home

Building an emergency button with Shelly Button1 and MQTT

27 February 2023 • 3 mins

In another article, we talked about some basics around Shelly products.

In this article, we will use a Shelly Button1 to build an alarming system. The button press can send two levels of emergency to a Peakboard application, and then reset to a normal state. In a professional environment, it would be possible to use this pattern to give an end user the option to call for help, call for missing material at an assembly line, etc….

The web interface of Shelly Button1

Please refer to the official documentation for how to integrate the Shelly Button into your local Wi-Fi. Then, access the web interface of the Button for the configuration. When you click on Actions, you can put HTTP calls behind the different button press events (single, double, triple, long). This would be perfect for letting the Shelly Button call an external Peakboard function—then, we’d be done within minutes. The problem is: When calling an external Peakboard function, we must use an HTTP POST call, but Shelly only supports sending HTTP GET calls. Unfortunately, this is an unsolvable problem, unless you use an additional layer that transforms the HTTP GET call into an HTTP POST call. So we look for another option and ignore this feature.

image

Configuring Shelly’s MQTT

The second best option is to use MQTT to let Peakboard and Shelly communicate. In Shelly’s web interface, you can access the MQTT pane through Internet & Security and ADVANCED - DEVELOPER SETTINGS. As you can see in this screenshot, we are using the Mosquitto MQTT broker to exchange messages and enable MQTT.

image

When MQTT is enabled, the button sends state messages to the MQTT broker. The topic is shellies/shellybutton1-<deviceid>/input_event/0. The device ID depends on the button; you can check Settings -> Device Info to find it. The JSON message looks like this:

{
    "event":"S",
    "event_cnt":2
}

The events can be S for a single click, SS for a double click, or L for a long click. The attribute event_cnt counts the number of clicks since the last reboot of the button. That’s all the information we need to proceed.

Building the Peakboard application

On the Peakboard side, the magic happens in an MQTT data source. We connect to the known MQTT broker. The topic to subscribe to is explained above, and we provide a path that points to the event-Node within the JSON, so we don’t need to worry about how to turn the JSON into usable information. As you see in the preview data, the SS value for the double click is extracted and shown.

image

The actual visualization is done with an Icon control. The switching between the three states is done with Conditional Formatting, so there’s no coding, no scripts, and no blocks.

image image image image

Finally, here you can see how the board works in real life….

Building an emergency button with Shelly Button1 and MQTT

Newsletter

Want to get notified when a new article is published? Sign up below!