Mosquittos on the couch

What: Put mosquitto messages to the couch
Why: Using mosquitto broker as relay between your IoT devices and a database backend
How: Use mosquitto, curl and some linux magic


You need couchdb (assumming it runs locally on port 5984 for this example) and mosquitto (also assuming it runs locally for this example). If you dont have it on your system, have a look at my other blog entry. Additionally, you need curl and the bash.

Set up a simple publisher

Create a simple script, which will publish messages periodically to the mosquitto broker under the topic test:

Change the permission for this script in such a way that you can execut it.

Create a test database

Connect mosquitto and couch via curl

Mosquitto and couchdb can be connected via a simple shell pipe:

Note: You could think about piping mosquitto directly to couch, if your message is already a json string. Something like this:

This will not work, because curl starts reading the input after it is complete (after the stream from mosquitto is closed). You need the while read line construction like shown above.

Run the test publisher script and verify results

Run the script:

Wait some seconds. Now query the database and you should have some documents there:

The result should look like: