First version
authorNirgal <contact_ampy@nirgal.com>
Sun, 30 Dec 2018 23:52:37 +0000 (23:52 +0000)
committerNirgal <contact_ampy@nirgal.com>
Sat, 12 Oct 2019 21:46:17 +0000 (21:46 +0000)
devices.py [new file with mode: 0755]
html/currentjson.php [new file with mode: 0644]
html/graph.php [new file with mode: 0644]
html/index.php [new file with mode: 0644]
readme [new file with mode: 0644]
record.py [new file with mode: 0644]
run.py [new file with mode: 0755]

diff --git a/devices.py b/devices.py
new file mode 100755 (executable)
index 0000000..20c3fa4
--- /dev/null
@@ -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 (file)
index 0000000..f3c8f31
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+$RRDFILE="/home/nirgal/ampy/power.rrd";
+
+$info=rrd_lastupdate($RRDFILE);
+if ($info === FALSE)
+       $result=[
+               'last_update' => '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 (file)
index 0000000..4675620
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+$RRDFILE="/home/nirgal/ampy/power.rrd";
+$IMGFILE="/tmp/power.svg";
+$REFRESH=30;
+
+$result=rrd_graph("$IMGFILE", [
+       "--imgformat", "SVG",
+       "-s", "now-1h",
+       #"-e", "now-8h",
+       "--width", "1000",
+       "--height", "500",
+       "--vertical-label", "watts",
+       "--lower-limit", "0",
+       #"--logarithmic",
+       "DEF:watts=${RRDFILE}:watts:AVERAGE",
+       "LINE1:watts#FF0000"
+]);
+if ($result === FALSE) {
+       echo("Error in rrd_graph");
+       die();
+}
+header("Content-Type: image/svg+xml");
+#header("Refresh: $REFRESH");
+readfile($IMGFILE);
+?>
diff --git a/html/index.php b/html/index.php
new file mode 100644 (file)
index 0000000..db29c6a
--- /dev/null
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<meta charset="UTF-8">
+<script src='/javascript/jquery/jquery.js'></script>
+<script>
+function refresh_power() {
+       $.ajax({
+               url : 'currentjson.php',
+               success : function(result) {
+                       document.getElementById('currentpower').innerHTML = result['watts'];
+               },
+               complete : function() {
+                       setTimeout(refresh_power, 1000);
+               }
+       });
+}
+</script>
+
+Puissance instannĂ©e: <span id=currentpower>unknown</span> watts<br>
+<script>refresh_power();</script>
+
+
+<img src="graph.php">
diff --git a/readme b/readme
new file mode 100644 (file)
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 (file)
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 (executable)
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()
+