MQTT is a machine-to-machine (M2M) protocol heavily used for the Internet of Things (IoT) because it is an extremely lightweight publish/subscribe messaging transport. The Things Network as well uses MQTT to exchange messages between devices and their online backends — or applications as we call them.
The Rise of APIs
In the meanwhile, online platforms have seen the rise of APIs. No longer do you need to manually export and import data between services. Once you’ve allowed one service to access another’s API on your behalf, it can take care of that for you.
Connecting Passive APIs
While MQTT provides full two-way communication, the popular REST APIs do not. In the example above, the first service is the client who takes the initiative (requests), while the other is the master and merely responds. Active APIs do exist, like Twitter’s Streaming APIs.
Fortunately, services like Zapier and IFTTT allow you to connect APIs even if they don’t have direct integrations with each other. Services only need to integrate with them, either by providing passive polling endpoints or actively calling webhooks on different events. These can then be used as triggers to make requests to pretty much any other APIs.
Connecting MQTT and HTTP APIs
What has puzzled me about the rise of MQTT and APIs is how obvious yet limited the options are to cross-over. I can think of countless use cases where hardware events trigger software APIs and visa versa.
The only platform I know off that really does this right is Node-RED. This visual tool was made specifically to wire IoT to complex flows and MQTT is one of its core trigger aka input nodes. It has lots of contributed input and output nodes, but can’t top the 700+ services and easy of use of Zapier, which by the way also introduced multi-step zaps (flows) this year.
IFTTT Maker Channel
IFTTT has a Maker Channel, but really isn’t so much about the hardware marker culture at all. It simply allows you to receive or make web requests, just like Zapier’s Webhooks. It doesn’t allow you to directly subscribe or publish via MQTT.
So, I decided to play around a bit and present to you: MQTT2HTTP. A simple, yet highly configurable Node.js service to forward MQTT messages to a HTTP API or simple webhook. Simply configure and deploy to Heroku or anywhere else and forget about it.
Here’s an example:
- Go to https://github.com/FokkeZB/mqtt2http.
- Click Deploy to Heroku.
- Configure it via Environment Variables.
Here’s how I configured it for a temperature sensor I have on The Things Network:
- Click Deploy.
- Create a Zappier Zap with a Catch Hook as trigger.
Here’s an example my test result after setting the Pick off a Child Key option to
- Add one or more actions using the data retrieved via the hook.
I used Create Spreadsheet Row to store the sensor data:
- Forget about the MQTT2HTTP process and enjoy endless automation!
Here’s how my spreadsheet looked a few minutes later:
If I find time I might add the return journey as well. The script would simply start a webserver and allow you to configure HTTP requests to listen for and how these should translate publishing MQTT messages.
Of course I still hope Zapier and IFTTT will someday add native support for MQTT and other protocols as trigger and action.
If you use The Things Network, then don’t sweat it. We’re working on some very cool stuff to make this even easier!
For more blog posts on automation and other stuff that interests me: fokkezb.nl