diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2018-08-02 17:21:14 +0200 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2018-08-02 17:59:06 +0200 |
commit | 74973624deed05d1cc8b297fb1d877cadad50969 (patch) | |
tree | 8304000f99a4dc0e0fad263fc9928d8e5ad6366c /stromzähler3 | |
download | esp8266-master.tar.gz esp8266-master.zip |
Diffstat (limited to 'stromzähler3')
-rw-r--r-- | stromzähler3/Kconfig | 16 | ||||
-rw-r--r-- | stromzähler3/boot.py | 2 | ||||
-rw-r--r-- | stromzähler3/main.py | 105 | ||||
-rwxr-xr-x | stromzähler3/make.sh | 5 |
4 files changed, 128 insertions, 0 deletions
diff --git a/stromzähler3/Kconfig b/stromzähler3/Kconfig new file mode 100644 index 0000000..afb1c7a --- /dev/null +++ b/stromzähler3/Kconfig @@ -0,0 +1,16 @@ +mainmenu "configuration" + +source ../scripts/Kconfig-wifi + +menu "Influxdb" + +config INFLUXDB_HOST + string "Hostname of influxdb server" + +config INFLUXDB_PORT + int "Port number" + +config INFLUXDB_PATH + string "Path element for POST request" + +endmenu diff --git a/stromzähler3/boot.py b/stromzähler3/boot.py new file mode 100644 index 0000000..0d30893 --- /dev/null +++ b/stromzähler3/boot.py @@ -0,0 +1,2 @@ +config = dict(map(lambda x: (x[0], int(x[1]) if x[1].isdigit() else x[1][1:-1] if x[1].startswith('"') and x[1].endswith('"') else x[1]), map(lambda x: x.strip().split("=", 1), filter(lambda x: '=' in x, open(".config").readlines())))) + diff --git a/stromzähler3/main.py b/stromzähler3/main.py new file mode 100644 index 0000000..65499ff --- /dev/null +++ b/stromzähler3/main.py @@ -0,0 +1,105 @@ +#!micropython +print("Start main.py") + +import os +import machine +import socket +import time +import micropython as mp + + +HOST = config['CONFIG_INFLUXDB_HOST'] +PORT = config['CONFIG_INFLUXDB_PORT'] +PATH = config['CONFIG_INFLUXDB_PATH'] + +H_REQUEST = bytes('POST /%s HTTP/1.1\r\n' % PATH, 'utf8') +H_HOST = bytes('Host: %s\r\n' % HOST, 'utf8') +H_CONTENT_TYPE = bytes('Content-Type: application/x-www-urlencoded\r\n', 'utf8') +H_CONTENT_LENGTH = bytes('Content-Length: %s\r\n', 'utf8') +H_CONNECTION = bytes('Connection: close\r\n', 'utf8') +H_NL = bytes('\r\n', 'utf8') + +def post(data): + print('[%d] Send HTTP POST: %s. Response: ' % (time.ticks_ms(), data), end='') + addr = socket.getaddrinfo(HOST, PORT)[0][-1] + data = bytes(data, 'utf8') + s = socket.socket() + s.connect(addr) + s.send(H_REQUEST) + s.send(H_HOST) + s.send(H_CONTENT_TYPE) + s.send(H_CONTENT_LENGTH % len(data)) + s.send(H_CONNECTION) + s.send(H_NL) + s.send(data) + + first_data = s.recv(100) + if first_data: + line, *_ = first_data.split(b'\r\n') + print(line, end='') + while data: + data = s.recv(100) + s.close() + print('') + +d4_total = 0 +d4_count = 0 + +d1_total = 0 +d1_count = 0 + +def update(t): + global d4_total, d4_count, d1_total, d1_count + d4 = d4_count + d1 = d1_count + post('stromzaehler3 d4_count=%d,d4_total=%d,d1_count=%d,d1_total=%d' % ( + d4, d4_total, d1, d1_total)) + d4_count -= d4 + d1_count -= d1 + +timer1 = machine.Timer(-1) +timer1.init(period=10000, mode=machine.Timer.PERIODIC, callback=update) + +def persist(t): + write_persistent_int("d4_total.txt", d4_total) + write_persistent_int("d1_total.txt", d1_total) + + +# Import +def cb_d4(p): + global d4_total, d4_count + v = p.value() + if v != 1: # skip rising edge + d4_total += 1 + d4_count += 1 + +d4 = machine.Pin(2, machine.Pin.IN, pull=machine.Pin.PULL_UP) +d4.irq(handler=lambda p: mp.schedule(cb_d4, p)) + +# Export +def cb_d1(p): + global d1_total, d1_count + v = p.value() + if v != 1: # skip rising edge + d1_count += 1 + d1_total += 1 + +d1 = machine.Pin(5, machine.Pin.IN) +d1.irq(handler=lambda p: mp.schedule(cb_d1, p)) + +# Pumpe +d3_last_value = 0 + +def cb_d3(p): + global d3_last_value + print('[%d] %s = %d' % (time.ticks_ms(), repr(p), p.value())) + v = 1 - p.value() + if v != d3_last_value: + post('stromzaehler3.Pumpe Status=%s' % v) + d3_last_value = v + +d3 = machine.Pin(0, machine.Pin.IN, pull=machine.Pin.PULL_UP) +d3.irq(handler=lambda p: mp.schedule(cb_d3, p)) + +print("main.py finished") + diff --git a/stromzähler3/make.sh b/stromzähler3/make.sh new file mode 100755 index 0000000..a76ff1a --- /dev/null +++ b/stromzähler3/make.sh @@ -0,0 +1,5 @@ +#!/bin/sh +PY_SOURCES="boot.py main.py .config" + +. ${0%/*}/../scripts/make.inc.sh + |