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

Requirements

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 test.sh, which will publish messages periodically to the mosquitto broker under the topic test:

counter=0
while true; do
    sleep 3
    mosquitto_pub -t test -m "$counter"
    counter=$(($counter+1))
done

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

Create a test database

curl -X PUT http://localhost:5984/testdb

Connect mosquitto and couch via curl

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

mosquitto_sub -t test | while read line;do curl -H 'Content-Type:application/json' -d "{\"date\":\"$(date +%Y-%m-%dT%H:%M:%SZ)\", \"value\":$line}" http://localhost:5984/testdb;done &

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

mosquitto_sub -t test | while read line;do curl -H 'Content-Type:application/json' -d -http://localhost:5984/testdb;done

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:

./test.sh

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

curl http://localhost:5984/testdb/_all_docs?include_docs=true

The result should look like:

{"total_rows":8,"offset":0,"rows":[
{"id":"13e93448d1256a98a3fa76f889000414","key":"13e93448d1256a98a3fa76f889000414","value":{"rev":"1-589430e0693f8f209655122fa934c440"},"doc":{"_id":"13e93448d1256a98a3fa76f889000414","_rev":"1-589430e0693f8f209655122fa934c440","date":"2017-06-08T20:47:01Z","value":0}},
{"id":"13e93448d1256a98a3fa76f889000bab","key":"13e93448d1256a98a3fa76f889000bab","value":{"rev":"1-0ebf14c49eab17f786c5f03c7c89acbb"},"doc":{"_id":"13e93448d1256a98a3fa76f889000bab","_rev":"1-0ebf14c49eab17f786c5f03c7c89acbb","date":"2017-06-08T20:47:04Z","value":1}},
{"id":"13e93448d1256a98a3fa76f8890010ee","key":"13e93448d1256a98a3fa76f8890010ee","value":{"rev":"1-8ded0d7b84da764a9fbe3d51bf27db6c"},"doc":{"_id":"13e93448d1256a98a3fa76f8890010ee","_rev":"1-8ded0d7b84da764a9fbe3d51bf27db6c","date":"2017-06-08T20:47:07Z","value":2}},
{"id":"13e93448d1256a98a3fa76f889001e53","key":"13e93448d1256a98a3fa76f889001e53","value":{"rev":"1-d2f32251185308fbca46873999022bfd"},"doc":{"_id":"13e93448d1256a98a3fa76f889001e53","_rev":"1-d2f32251185308fbca46873999022bfd","date":"2017-06-08T20:47:10Z","value":3}},
{"id":"13e93448d1256a98a3fa76f88900238e","key":"13e93448d1256a98a3fa76f88900238e","value":{"rev":"1-307f1ccd43f10642bddf3f8bf5f4646a"},"doc":{"_id":"13e93448d1256a98a3fa76f88900238e","_rev":"1-307f1ccd43f10642bddf3f8bf5f4646a","date":"2017-06-08T20:47:13Z","value":4}},
{"id":"13e93448d1256a98a3fa76f8890032ae","key":"13e93448d1256a98a3fa76f8890032ae","value":{"rev":"1-47d81b8b99058883550ba27088474e70"},"doc":{"_id":"13e93448d1256a98a3fa76f8890032ae","_rev":"1-47d81b8b99058883550ba27088474e70","date":"2017-06-08T20:47:16Z","value":5}},
{"id":"13e93448d1256a98a3fa76f889003c49","key":"13e93448d1256a98a3fa76f889003c49","value":{"rev":"1-33a785b3a53e9b80f4aedc19f0dc5bc8"},"doc":{"_id":"13e93448d1256a98a3fa76f889003c49","_rev":"1-33a785b3a53e9b80f4aedc19f0dc5bc8","date":"2017-06-08T20:47:19Z","value":6}},
{"id":"13e93448d1256a98a3fa76f889004701","key":"13e93448d1256a98a3fa76f889004701","value":{"rev":"1-63238f12e3b566a2329ef60afbd663e2"},"doc":{"_id":"13e93448d1256a98a3fa76f889004701","_rev":"1-63238f12e3b566a2329ef60afbd663e2","date":"2017-06-08T20:47:22Z","value":7}}
]}