{"id":1121,"date":"2017-10-24T22:24:39","date_gmt":"2017-10-24T20:24:39","guid":{"rendered":"http:\/\/www.fabienm.eu\/wordpress\/?p=1121"},"modified":"2017-11-12T15:53:01","modified_gmt":"2017-11-12T13:53:01","slug":"analog-input-with-usb-soundcard-on-linux-part3","status":"publish","type":"post","link":"https:\/\/www.fabienm.eu\/wordpress\/2017\/10\/24\/analog-input-with-usb-soundcard-on-linux-part3\/","title":{"rendered":"Analog input with usb soundcard on Linux [Part3]"},"content":{"rendered":"<p><a href=\"http:\/\/www.fabienm.eu\/wordpress\/?p=1097\">[&lt;&lt; part2]<\/a><\/p>\n<p>Time to get serious things now and put it all together.<\/p>\n<p><strong>Record waves<\/strong><\/p>\n<p>I will record audio waves with a first python script named <a href=\"https:\/\/github.com\/Martoni\/housepower\/blob\/master\/recordwaves.py\">recordwaves.py.<\/a> This script will simply launch arecord each 60 seconds and save it under ram directory (\/run\/).<\/p>\n<pre>$ arecord -d 10 -f S16_LE -c1 -r4000 -D plughw:1 -t wav foobar.wav\r\n<\/pre>\n<p>The script must be launched at raspberry pi boot. We just have to add the command in crontab with command crontab -e<\/p>\n<pre>@reboot <span class=\"pl-c1\">cd<\/span> \/opt\/house_power\/ <span class=\"pl-k\">&amp;&amp;<\/span> python3 recordwaves.py -d \/run\/hdata <span class=\"pl-k\">&amp;<\/span><\/pre>\n<p><strong>Current calculation<\/strong><\/p>\n<p>Two things with <a href=\"https:\/\/github.com\/Martoni\/housepower\/blob\/master\/waverms.py\">waverms.py <\/a>: calculate RMS current from wavefile recorded by recordwaves.py, store value and time in csv and database sqlite3.<\/p>\n<p>Then we must have to create a sqlite3 file to store value with sqlite3 command :<\/p>\n<pre>pi@raspberrypi:\/opt\/house_power $ sqlite3 housepower.db\r\nSQLite version 3.8.7.1 2014-10-29 13:59:56\r\nEnter \".help\" for usage hints.\r\nsqlite&gt; CREATE TABLE rms (id INTEGER, recordtype VARCHAR(20), rmsvalue INTEGER, recordtime INTEGER);\r\n<\/pre>\n<p>To calculate rms, don&rsquo;t forget that Python3\u00a0is provided with \u00ab\u00a0batteries included\u00a0\u00bb then there is a module for that named <a href=\"https:\/\/docs.python.org\/3\/library\/audioop.html\"><strong>audioop<\/strong><\/a>. This module include a method simply named <a href=\"https:\/\/docs.python.org\/3\/library\/audioop.html#audioop.rms\">rms()<\/a>.<\/p>\n<p>As recordwaves.py, the script waverms.py is launched with crontab command :<\/p>\n<pre>@reboot <span class=\"pl-c1\">cd<\/span> \/opt\/house_power\/ <span class=\"pl-k\">&amp;&amp;<\/span> python3 waverms.py -d \/run\/hdata <span class=\"pl-k\">&amp;<\/span><\/pre>\n<p><strong>Web page display<\/strong><\/p>\n<p>For displaying datas, we will keep python3 language using module flask for web server and bokeh for graphes generation. The python3 script server is named <a href=\"https:\/\/github.com\/Martoni\/housepower\/blob\/master\/web\/house_power.py\">house_power.py<\/a> and need an html template named <a href=\"https:\/\/github.com\/Martoni\/housepower\/blob\/master\/web\/templates\/graph.html\">graph.html.<\/a><\/p>\n<p>Flask can be installed with apt :<\/p>\n<pre>sudo apt-get install python3-flask<\/pre>\n<p>And bokeh with pip3<\/p>\n<pre>sudo pip3 install bokeh<\/pre>\n<p>And launch it also with crontab :<\/p>\n<pre>@reboot <span class=\"pl-c1\">cd<\/span> \/opt\/house_power\/web\/ <span class=\"pl-k\">&amp;&amp;<\/span> python3 house_power.py <span class=\"pl-k\">&amp;<\/span><\/pre>\n<p>The graphes of electricity current house consumption is then accessible on following local networks address : <a href=\"http:\/\/192.168.0.10:5000\/\">http:\/\/192.168.0.10:5000\/<\/a> (of course, if your raspberrypi address is 192.168.0.10).<\/p>\n<p>This give us graph like it :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1137\" src=\"http:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/10\/bokeh_scsh.png\" alt=\"\" width=\"1012\" height=\"503\" srcset=\"https:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/10\/bokeh_scsh.png 1012w, https:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/10\/bokeh_scsh-300x149.png 300w, https:\/\/www.fabienm.eu\/wordpress\/wp-content\/uploads\/2017\/10\/bokeh_scsh-768x382.png 768w\" sizes=\"auto, (max-width: 1012px) 100vw, 1012px\" \/><\/p>\n<p>All codes are available on my github repository named <a href=\"https:\/\/github.com\/Martoni\/housepower\">housepower<\/a>. It&rsquo;s fully functionnal but not finished yet. I have to increase website functionnality. Adding days mean consumption and maybe more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[&lt;&lt; part2] Time to get serious things now and put it all together. Record waves I will record audio waves with a first python script named recordwaves.py. This script will simply launch arecord each 60 seconds and save it under &hellip; <a href=\"https:\/\/www.fabienm.eu\/wordpress\/2017\/10\/24\/analog-input-with-usb-soundcard-on-linux-part3\/\">Continuer la lecture <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106,10,1],"tags":[181,176,179,83,182,180,183,173,127],"class_list":["post-1121","post","type-post","status-publish","format-standard","hentry","category-embarque","category-informatique","category-non-classe","tag-bokeh","tag-courant","tag-current","tag-electricite","tag-flask","tag-graph","tag-python3","tag-raspberrypi","tag-web"],"_links":{"self":[{"href":"https:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/posts\/1121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/comments?post=1121"}],"version-history":[{"count":15,"href":"https:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"predecessor-version":[{"id":1139,"href":"https:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/posts\/1121\/revisions\/1139"}],"wp:attachment":[{"href":"https:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fabienm.eu\/wordpress\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}