summaryrefslogtreecommitdiff
path: root/temp0/main.py
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2018-08-02 17:21:14 +0200
committerYves Fischer <yvesf-git@xapek.org>2018-08-02 17:59:06 +0200
commit74973624deed05d1cc8b297fb1d877cadad50969 (patch)
tree8304000f99a4dc0e0fad263fc9928d8e5ad6366c /temp0/main.py
downloadesp8266-74973624deed05d1cc8b297fb1d877cadad50969.tar.gz
esp8266-74973624deed05d1cc8b297fb1d877cadad50969.zip
new commit-history root - cleaned off passwordsHEADmaster
Diffstat (limited to 'temp0/main.py')
-rw-r--r--temp0/main.py121
1 files changed, 121 insertions, 0 deletions
diff --git a/temp0/main.py b/temp0/main.py
new file mode 100644
index 0000000..ac1de7d
--- /dev/null
+++ b/temp0/main.py
@@ -0,0 +1,121 @@
+#!micropython
+import time
+print("[{}] Start main.py".format(time.ticks_ms()))
+
+import os
+import gc
+import socket
+import machine
+import onewire
+import ds18x20
+import network
+
+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('')
+
+
+
+pins = [5,4,0,2,14]
+ow_list = list(map(onewire.OneWire, map(machine.Pin, pins)))
+ds_list = list(map(ds18x20.DS18X20, ow_list))
+
+
+def onewire_name(ba):
+ return "-".join(map(lambda x: "{:02X}".format(x), ba))
+
+
+def update(func):
+ global ds_list
+ data = []
+
+ for i in range(len(pins)):
+ pin_id = pins[i]
+ print("[{}] Read pin {}".format(time.ticks_ms(), pin_id))
+ ds = ds_list[i]
+
+ roms = ds.scan()
+ if len(roms) > 0:
+ time.sleep_ms(500)
+ ds.convert_temp()
+ time.sleep_ms(1000) #750
+
+ for rom in roms:
+ value = ds.read_temp(rom)
+ data.append("temp0,pin={},sensor={} value={}".format(pin_id, onewire_name(rom), value))
+ time.sleep_ms(100)
+
+ data.append("temp0.ticks,pin={} value={}".format(pin_id, time.ticks_ms()))
+ func("\n".join(data))
+ data = []
+
+
+wlan = network.WLAN(network.STA_IF)
+
+
+def activate_wlan():
+ global wlan
+ print("[{}] Connect to WLAN".format(time.ticks_ms()))
+ wlan.active(True)
+ wlan.connect()
+ while not wlan.isconnected():
+ print("[{}] Connecting to WLAN".format(time.ticks_ms()))
+ time.sleep_ms(500)
+ print("[{}] Connected to WLAN".format(time.ticks_ms()))
+
+#####
+
+activate_wlan()
+
+print("[{}] Connected. Run loop".format(time.ticks_ms()))
+
+try:
+ while True:
+ gc.collect()
+ gc.disable()
+ print("[{}] Run update".format(time.ticks_ms()))
+ update(post)
+ gc.enable()
+ time.sleep_ms(2000)
+except KeyboardInterrupt:
+ print("[{}] Loop aborted".format(time.ticks_ms()))
+except Exception as e:
+ msg = repr(e)
+ print("[{}] {}".format(time.ticks_ms(), msg))
+ try:
+ msg_esc = msg.replace("\"", "").replace("'", "").replace("\\", "")
+ post("temp0.exception value=\"{}\"".format(msg_esc))
+ finally:
+ time.sleep_ms(1000)
+ machine.reset()
+