Analog input with usb soundcard on Linux [Part3]

[<< 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 This script will simply launch arecord each 60 seconds and save it under ram directory (/run/).

$ arecord -d 10 -f S16_LE -c1 -r4000 -D plughw:1 -t wav foobar.wav

The script must be launched at raspberry pi boot. We just have to add the command in crontab with command crontab -e

@reboot cd /opt/house_power/ && python3 -d /run/hdata &

Current calculation

Two things with : calculate RMS current from wavefile recorded by, store value and time in csv and database sqlite3.

Then we must have to create a sqlite3 file to store value with sqlite3 command :

pi@raspberrypi:/opt/house_power $ sqlite3 housepower.db
SQLite version 2014-10-29 13:59:56
Enter ".help" for usage hints.
sqlite> CREATE TABLE rms (id INTEGER, recordtype VARCHAR(20), rmsvalue INTEGER, recordtime INTEGER);

To calculate rms, don’t forget that Python3 is provided with « batteries included » then there is a module for that named audioop. This module include a method simply named rms().

As, the script is launched with crontab command :

@reboot cd /opt/house_power/ && python3 -d /run/hdata &

Web page display

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 and need an html template named graph.html.

Flask can be installed with apt :

sudo apt-get install python3-flask

And bokeh with pip3

sudo pip3 install bokeh

And launch it also with crontab :

@reboot cd /opt/house_power/web/ && python3 &

The graphes of electricity current house consumption is then accessible on following local networks address : (of course, if your raspberrypi address is

This give us graph like it :

All codes are available on my github repository named housepower. It’s fully functionnal but not finished yet. I have to increase website functionnality. Adding days mean consumption and maybe more.

Ce contenu a été publié dans embarqué, informatique, Non classé, avec comme mot(s)-clé(s) , , , , , , , , . Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *