"""
import os
-import pyaudio
-import wave
-import sys
import struct
import time
+import pyaudio
+
FORMAT = pyaudio.paInt32 # Format
STRUCTFORMAT = 'i' # Format for python struct module
-DEVICE = 3 # which alsa decive to read
+DEVICE = 5 # which alsa decive to read
RATE = 44100
RECORD_SECONDS = 0.5
VOLUME_CONSTANT = 132360.98315789475
-#amixer -c 1 contents
-# numid=23,iface=MIXER,name='Capture Volume'
-# ; type=INTEGER,access=rw---R--,values=2,min=0,max=46,step=0
-# : values=17,17
-# | dBscale-min=-16.00dB,step=1.00dB,mute=0
-# => Set volume to 18
-
# https://www.actutem.com/valeur-crete-moyenne-et-efficace-dune-tension-ac/
# math.pi/math.sqrt(2)/2 = 1.1107207345395915
# https://fr.wikipedia.org/wiki/%C3%89lectricit%C3%A9_domestique#Tension
# En france, ERDF fourni 230V EFFICACES (=> 207.0727527161344 moyenne)
-#raw average: 636207384.4738322 - min: -2147483648 - max: 2147483392
-#average 4806.6W
+# raw average: 636207384.4738322 - min: -2147483648 - max: 2147483392
+# average 4806.6W
+
def loop(optrecord, optstats):
p = pyaudio.PyAudio()
-
+
print("opened")
NSAMPLE = int(RATE * RECORD_SECONDS)
- stream = p.open(format=FORMAT,
- channels=1, # Our ampmeter always returns 0 on the second channel
- rate=RATE,
- input=True,
- input_device_index=DEVICE)
-
+ stream = p.open(
+ format=FORMAT,
+ channels=1, # Our ampmeter always returns 0 on the second channel
+ rate=RATE,
+ input=True,
+ input_device_index=DEVICE)
+
structformat = '<' + STRUCTFORMAT * NSAMPLE
while(True):
try:
maxvalue = 0
data = stream.read(NSAMPLE, exception_on_overflow=False)
values = struct.unpack(structformat, data)
- #print(values)
- #for x in range(100): print('**{:04x}**{}**'.format(values[x], values[x]))
+ # print(values)
+ # for x in range(100):
+ # print('**{:04x}**{}**'.format(values[x], values[x]))
for value in values:
if value > 0:
total += value
minvalue = value
avg = float(total) / NSAMPLE
if optstats:
- print("raw average: {} - min: {} - max: {}".format(avg, minvalue, maxvalue))
+ print("raw average: {} - min: {} - max: {}".format(
+ avg, minvalue, maxvalue))
watts = avg / VOLUME_CONSTANT
print("average {:.1f}W ".format(watts), end='\r')
if optrecord:
- #os.system("rrdtool update --daemon /var/run/rrdcached.sock power.rrd N:{}".format(watts))
- os.system("rrdtool update --daemon /var/run/rrdcached.sock /var/lib/rrdcached/db/power.rrd {}:{}".format(time.time(), watts))
+ os.system(
+ "rrdtool update --daemon /var/run/rrdcached.sock"
+ " /var/lib/rrdcached/db/power.rrd {}:{}".format(
+ time.time(), watts))
except KeyboardInterrupt:
print("Received KeyboardInterrupt: exiting")
break
-
+
stream.stop_stream()
stream.close()
p.terminate()
+
def main():
import argparse
loop(optrecord=args.record, optstats=args.stats)
+
if __name__ == '__main__':
main()