{"id":1447,"date":"2013-02-04T10:32:39","date_gmt":"2013-02-04T08:32:39","guid":{"rendered":"http:\/\/sickel.net\/blogg\/?p=1447"},"modified":"2013-04-02T10:53:27","modified_gmt":"2013-04-02T08:53:27","slug":"store-data-from-temperature-server-in-couchdb","status":"publish","type":"post","link":"http:\/\/sickel.net\/blogg\/?p=1447","title":{"rendered":"Store data from temperature server in couchdb"},"content":{"rendered":"<p>I am setting up a quick and dirty couchdb database to store data from an <a href=\"http:\/\/sickel.net\/blogg\/?p=1425\">arduino temperature server<\/a> running on http:\/\/192.168.0.177. (only visible on my local network)<\/p>\n<p>All the code below is run under bash 4.2 on the debian system where the database resides.<\/p>\n<p>The db is called arduino and is created by <\/p>\n<pre>\r\ncurl -X PUT http:\/\/127.0.0.1:5984\/arduino\r\n<\/pre>\n<p>Pulling out the data:<\/p>\n<pre>\r\nGET http:\/\/192.168.0.177\/json\r\n<\/pre>\n<p>Storing it in the database:<\/p>\n<pre>\r\ncurl -X POST http:\/\/localhost:5984\/arduino -H 'Content-Type: application\/json' -d &lt;jsonstring&gt;\r\n<\/pre>\n<p>This can be linked together:<\/p>\n<pre>\r\nGET http:\/\/192.168.0.177\/json | curl -X POST http:\/\/localhost:5984\/arduino -H Content-Type: application\/json' -d @-\r\n<\/pre>\n<p>Note the last @- that makes curl read from standard input.<\/p>\n<p>What we miss now is the timestamp. We can use sed to insert a timestamp in the json string:<\/p>\n<pre>sed -e \"s\/\\\"mil\/\\\"time\\\":\\\"$dte\\\",\\\"mil\/\"<\/pre>\n<p>Will insert the string &#8220;time&#8221;: and the content of the variable $dte just before the &#8220;millis&#8221; in the json.<\/p>\n<pre>export dte=`date +\"%F %T %z\"`\r\nGET 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 @-\r\n<\/pre>\n<p>And to make it run each 2nd minute put it all into a file:<\/p>\n<pre>\r\n#!\/bin\/bash\r\nwhile true; do\r\nexport dte=`date +\"%F %T %z\"`\r\nGET 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 @-\r\nsleep 120\r\ndone\r\n<\/pre>\n<p>and run this with a nohup<\/p>\n<p>To keep an eye on what goes in:<\/p>\n<pre>\r\nid=`tail -n1 nohup.out | awk -F'[,:\\\"]' '{ print $9 }'`;curl -X GET http:\/\/localhost:5984\/arduino\/$id\r\n<\/pre>\n<p>In the longer run, I am pretty sure these data should be stored in postgres &#8211; 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.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fetching json data from an arduino server, adding a time stamp and stashing it all into a couchdb. <a href=\"http:\/\/sickel.net\/blogg\/?p=1447\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[33],"tags":[],"class_list":["post-1447","post","type-post","status-publish","format-standard","hentry","category-arduino"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnVtD-nl","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/1447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1447"}],"version-history":[{"count":10,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/1447\/revisions"}],"predecessor-version":[{"id":1528,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/1447\/revisions\/1528"}],"wp:attachment":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1447"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}