I am setting up a quick and dirty couchdb database to store data from an arduino temperature server running on http://192.168.0.177. (only visible on my local network)
All the code below is run under bash 4.2 on the debian system where the database resides.
The db is called arduino and is created by
curl -X PUT http://127.0.0.1:5984/arduino
Pulling out the data:
GET http://192.168.0.177/json
Storing it in the database:
curl -X POST http://localhost:5984/arduino -H 'Content-Type: application/json' -d <jsonstring>
This can be linked together:
GET http://192.168.0.177/json | curl -X POST http://localhost:5984/arduino -H Content-Type: application/json' -d @-
Note the last @- that makes curl read from standard input.
What we miss now is the timestamp. We can use sed to insert a timestamp in the json string:
sed -e "s/\"mil/\"time\":\"$dte\",\"mil/"
Will insert the string “time”: and the content of the variable $dte just before the “millis” in the json.
export dte=`date +"%F %T %z"` GET 192.168.0.177/json | sed -e "s/\"mil/\"time\":\"$dte\",\"mil/" | curl -X POST http://localhost:5984/arduino -H 'Content-Type: application/json' -d @-
And to make it run each 2nd minute put it all into a file:
#!/bin/bash while true; do export dte=`date +"%F %T %z"` GET 192.168.0.177/json | sed -e "s/\"mil/\"time\":\"$dte\",\"mil/" | curl -X POST http://localhost:5984/arduino -H 'Content-Type: application/json' -d @- sleep 120 done
and run this with a nohup
To keep an eye on what goes in:
id=`tail -n1 nohup.out | awk -F'[,:\"]' '{ print $9 }'`;curl -X GET http://localhost:5984/arduino/$id
In the longer run, I am pretty sure these data should be stored in postgres – but for the first run testing, couchdb works fine. (Although, I could have rewritten the server to return sql-strings and run them through psql.)
Pingback: Arduino – temperature webserver | Mortens meninger