From dfc9ce437827a461b6c82c1321a1b7517e7b5d7b Mon Sep 17 00:00:00 2001 From: Nirgal Date: Sun, 30 Dec 2018 23:52:37 +0000 Subject: [PATCH] First version --- devices.py | 13 +++++++++++ html/currentjson.php | 17 ++++++++++++++ html/graph.php | 25 +++++++++++++++++++++ html/index.php | 22 ++++++++++++++++++ readme | 3 +++ record.py | 49 ++++++++++++++++++++++++++++++++++++++++ run.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 182 insertions(+) create mode 100755 devices.py create mode 100644 html/currentjson.php create mode 100644 html/graph.php create mode 100644 html/index.php create mode 100644 readme create mode 100644 record.py create mode 100755 run.py diff --git a/devices.py b/devices.py new file mode 100755 index 0000000..20c3fa4 --- /dev/null +++ b/devices.py @@ -0,0 +1,13 @@ +#!/usr/bin/python3 + +import pyaudio + +p = pyaudio.PyAudio() + +print('Listing api:') +for i in range(p.get_host_api_count()): + print(i, p.get_host_api_info_by_index(i)['name']) + +print('Listing devices:') +for i in range(p.get_device_count()): + print(i, p.get_device_info_by_index(i)['name']) diff --git a/html/currentjson.php b/html/currentjson.php new file mode 100644 index 0000000..f3c8f31 --- /dev/null +++ b/html/currentjson.php @@ -0,0 +1,17 @@ + 'unknown', + 'watts' => 'unknown' + ]; +else + $result=[ + 'last_update' => $info['last_update'], + 'watts' => $info['data'][0] + ]; +header("Content-Type: text/json"); +echo(json_encode($result)); +?> diff --git a/html/graph.php b/html/graph.php new file mode 100644 index 0000000..4675620 --- /dev/null +++ b/html/graph.php @@ -0,0 +1,25 @@ + diff --git a/html/index.php b/html/index.php new file mode 100644 index 0000000..db29c6a --- /dev/null +++ b/html/index.php @@ -0,0 +1,22 @@ + + + + + +Puissance instannée: unknown watts
+ + + + diff --git a/readme b/readme new file mode 100644 index 0000000..75c0776 --- /dev/null +++ b/readme @@ -0,0 +1,3 @@ +https://learn.openenergymonitor.org/electricity-monitoring/ct-sensors/yhdc-sct-013-000-ct-sensor-report + +rrdtool create power.rrd --start now-2h --step 1s DS:watts:GAUGE:1:0:6600 RRA:AVERAGE:0.5:1s:1d # Buggy: nécessite des updates toutes les 500 ms diff --git a/record.py b/record.py new file mode 100644 index 0000000..a113104 --- /dev/null +++ b/record.py @@ -0,0 +1,49 @@ +""" +PyAudio example: Record a few seconds of audio and save to a WAVE +file. +""" + +import pyaudio +import wave +import sys + +CHUNK = 1024 +FORMAT = pyaudio.paInt16 +CHANNELS = 2 +RATE = 44100 +RECORD_SECONDS = 5 +WAVE_OUTPUT_FILENAME = "output.wav" +DEVICE = 3 + +if sys.platform == 'darwin': + CHANNELS = 1 + +p = pyaudio.PyAudio() + +stream = p.open(format=FORMAT, + channels=CHANNELS, + rate=RATE, + input=True, + input_device_index=DEVICE, + frames_per_buffer=CHUNK) + +print("* recording") + +frames = [] + +for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): + data = stream.read(CHUNK, exception_on_overflow=False) + frames.append(data) + +print("* done recording") + +stream.stop_stream() +stream.close() +p.terminate() + +wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') +wf.setnchannels(CHANNELS) +wf.setsampwidth(p.get_sample_size(FORMAT)) +wf.setframerate(RATE) +wf.writeframes(b''.join(frames)) +wf.close() diff --git a/run.py b/run.py new file mode 100755 index 0000000..c4bbf79 --- /dev/null +++ b/run.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 +""" +PyAudio example: Record a few seconds of audio and save to a WAVE +file. +""" + +import os +import pyaudio +import wave +import sys +import struct + +DEVICE = 3 # which alsa decive to read +CHUNK = 1024 # how many bytes at a time? +FORMAT = pyaudio.paInt16 # Format +RATE = 44100 +RECORD_SECONDS = 0.5 + +VOLUME_CONSTANT = 11.975454545454545 + +p = pyaudio.PyAudio() + +stream = p.open(format=FORMAT, + channels=1, # Our ampmeter always returns 0 on the second channel + rate=RATE, + input=True, + input_device_index=DEVICE, + frames_per_buffer=CHUNK) + +while(True): + total = 0 + for i in range(0, int(RATE * RECORD_SECONDS / CHUNK)): + data = stream.read(CHUNK, exception_on_overflow=False) + values = struct.unpack('h'*CHUNK, data) + #print(values) + chunk_total = 0 + for value in values: + if value > 0: + chunk_total += value + else: + chunk_total -= value + chunk_total /= len(values) + #print(chunk_total) + total += chunk_total + avg = total / int(RATE * RECORD_SECONDS / CHUNK) + watts = avg / VOLUME_CONSTANT + print("average {:.0f}W".format(watts)) + os.system("rrdtool update power.rrd N:{:.0f}".format(watts)) + +stream.stop_stream() +stream.close() +p.terminate() + -- 2.30.2