summaryrefslogtreecommitdiff
path: root/temp0
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
downloadesp8266-master.tar.gz
esp8266-master.zip
new commit-history root - cleaned off passwordsHEADmaster
Diffstat (limited to 'temp0')
-rw-r--r--temp0/Kconfig16
-rw-r--r--temp0/boot.py2
-rw-r--r--temp0/main.py121
-rwxr-xr-xtemp0/make.sh5
4 files changed, 144 insertions, 0 deletions
diff --git a/temp0/Kconfig b/temp0/Kconfig
new file mode 100644
index 0000000..afb1c7a
--- /dev/null
+++ b/temp0/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/temp0/boot.py b/temp0/boot.py
new file mode 100644
index 0000000..0d30893
--- /dev/null
+++ b/temp0/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/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()
+
diff --git a/temp0/make.sh b/temp0/make.sh
new file mode 100755
index 0000000..8266585
--- /dev/null
+++ b/temp0/make.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+PY_SOURCES="main.py boot.py .config"
+
+. ${0%/*}/../scripts/make.inc.sh
+