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 /anzeige0/main.py | |
download | esp8266-master.tar.gz esp8266-master.zip |
Diffstat (limited to 'anzeige0/main.py')
-rw-r--r-- | anzeige0/main.py | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/anzeige0/main.py b/anzeige0/main.py new file mode 100644 index 0000000..c73e847 --- /dev/null +++ b/anzeige0/main.py @@ -0,0 +1,174 @@ +#!micropython +print("Start main.py") +import json +import time +import machine +import socket +import micropython as mp + +from nodemcu_gpio_lcd import GpioLcd +from machine import Pin, PWM +import network + +backlight = PWM(Pin(12), freq=500, duty=200) + +wlan = network.WLAN() + +lcd = GpioLcd(rs_pin=Pin(16), enable_pin=Pin(5), + d4_pin=Pin(4), d5_pin=Pin(0), d6_pin=Pin(2), d7_pin=Pin(14), + num_lines=2, num_columns=16) + +lcd.clear() +lcd.putstr("Verbinde mit:\n{}".format(wlan.config('essid'))) +while not wlan.isconnected(): + continue +lcd.clear() +lcd.putstr("Verbunden") + +HOST = b'www.localnet.cc' +HEADERS = b'Host: www.localnet.cc\r\nConnection: close\r\n\r\n' + +displays = [ + { # ################ ################ <- 16 chars + "name": "Zusammenfassung " + " ", + "text": "I: ____ E: ____ " + "W: ____ßS: ____ß", + "values" : [ + """SELECT sum("d4_count")*30 FROM "stromzaehler3" WHERE time > now() - 2m""", + """SELECT sum("d1_count")*30 FROM "stromzaehler3" WHERE time > now() - 2m""", + """SELECT last(value) FROM temp0 WHERE sensor = '28-8B-DC-E9-16-13-01-D5'""", + """SELECT last(value) FROM temp0 WHERE sensor = '28-7F-69-16-17-13-01-A6'""", + ] + }, + { # ################ ################ <- 16 chars + "name": "Stromz\xe1hler " + " Heute ", + "text": "\xf6 Import ____Wh" + "\xf6 Export ____Wh", + "values" : [ + "SELECT last(sum) FROM (SELECT sum(d4_count) FROM stromzaehler3 WHERE time > now() - 1d GROUP BY time(1d) tz('Europe/Berlin'))", + "SELECT last(sum) FROM (SELECT sum(d1_count) FROM stromzaehler3 WHERE time > now() - 1d GROUP BY time(1d) tz('Europe/Berlin'))", + ], + }, + { # ################ ################ <- 16 chars + "name": "Heizung Solar " + " ", + "text": "Von Dach: ____ß " + "Zum Dach: ____ß ", + "values" : [ + "SELECT last(value) FROM temp0 WHERE sensor = '28-7F-69-16-17-13-01-A6'", + "SELECT last(value) FROM temp0 WHERE sensor = '28-99-38-EB-16-13-01-22'", + ] + }, + { # ################ ################ <- 16 chars + "name": "BFT Rheinau " + " ", + "text": "S95-E5: __.___ " + "Diesel: __.___ ", + "values" : [ + """SELECT last(value) FROM "tankerkoenig.SP95-E5" WHERE "name" = 'Rheinau-Freistett - BFT - Freistett'""", + """SELECT last(value) FROM "tankerkoenig.Diesel" WHERE "name" = 'Rheinau-Freistett - BFT - Freistett'""", + ]}, + ] + +def query_one(db, select): + select_esc = select.replace(" ", "%20") + select_esc = select_esc.replace("\"", "%22") + addr = socket.getaddrinfo(HOST, 80)[0][-1] + s = socket.socket() + s.connect(addr) + s.send(b'GET /grafana/api/datasources/proxy/3/query?db={}&q={}&epoch=ms\r\n'.format(db, select_esc)) + s.send(HEADERS) + data = json.load(s) + s.close() + return data['results'][0]['series'][0]['values'][0][1] + +current_display = 0 +def update(): + global UPDATE_RUN, current_display, display, lcd + + lcd.move_to(0, 0) + text = displays[current_display]["text"] + + s = "" + cur = "" + pos = 0 + i = 0 + while i<len(text): + c = text[i] + cur_added = False + if c == '_' or ( len(cur) > 0 and c == '.' ): + cur += c + cur_added = True + + if len(cur) > 0 and (not cur_added or i == len(text) - 1): + value_query = displays[current_display]["values"][pos] + v = query_one("data", value_query) + if isinstance(v, float): + if "." in cur: + precision = len(cur) - cur.index(".") - 1 + else: + precision = 0 + fmt_string = "{{: >{}.{}f}}".format(len(cur), precision) + elif isinstance(v, int): + fmt_string = "{{: >{}d}}".format(len(cur)) + else: + raise Exception("Wrong type: {}".format(type(v))) + s += fmt_string.format(v) + lcd.putstr(s) + s = "" + cur = "" + pos += 1 + + if not cur_added: + s += c + + if not cur_added and i == len(text)-1: + lcd.putstr(s) + s = "" + + i += 1 + + lcd.putstr(s) + +last_update = 0 + +def schedule_update(timer=None, force=False): + global last_update + def ex(none): + try: + update() + except Exception as e: + lcd.clear() + msg = "Error: {}".format(str(e)[0:(32-7)]) + lcd.putstr(msg) + raise e + + now = time.ticks_ms() + if force: + last_update = now + ex(None) + elif now - last_update > 5000: + last_update = now + mp.schedule(ex, None) + +timer = machine.Timer(-1) + +def switch_display(none): + global current_display, displays + + current_display += 1 + current_display %= len(displays) + lcd.move_to(0,0) + lcd.putstr(displays[current_display]['name']) + + # trigger update in 1s + last_update = time.ticks_ms() - 4000 + +last_switch = 0 +def switch_pressed(pin): + global last_switch + now = time.ticks_ms() + if pin.value() == 0 and now - last_switch > 200: + mp.schedule(switch_display, None) + last_switch = now + +switch = machine.Pin(13, machine.Pin.IN) +switch.irq(switch_pressed) + +timer.init(period=500, mode=machine.Timer.PERIODIC, callback=schedule_update) + +print("main.py finished") |