From 8c14db6565ee747da099f204b3f5123f5d886fe2 Mon Sep 17 00:00:00 2001 From: Konstantin Koslowski Date: Wed, 23 Oct 2019 00:13:22 +0200 Subject: [PATCH] homecontrol-dht: baby steps --- .gitignore | 3 +++ Pipfile | 14 ++++++++++++++ boot.py | 1 + main.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ uuid.py | 28 ++++++++++++++++++++++++++++ wifi.py | 22 ++++++++++++++++++++++ 6 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 Pipfile create mode 100644 boot.py create mode 100644 main.py create mode 100644 uuid.py create mode 100644 wifi.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e2e9dc9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +Pipfile.lock +/credentials.py diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..d2a98b3 --- /dev/null +++ b/Pipfile @@ -0,0 +1,14 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +pyserial = "<4.0,>=3.3" +docopt = ">=0.6.2,<0.7" +adafruit-ampy = "<1.1,>=1.0.5" + +[requires] +python_version = "3.7" diff --git a/boot.py b/boot.py new file mode 100644 index 0000000..13df088 --- /dev/null +++ b/boot.py @@ -0,0 +1 @@ +# not doing anything \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..bc6ef57 --- /dev/null +++ b/main.py @@ -0,0 +1,52 @@ +import dht +import machine +import utime as time +import urequests as requests +import uuid +import ujson +import wifi + +CONFIG = "config.json" + +class Node: + def __init__(self): + self.config = {} + try: + config_file = open(CONFIG, "r") + self.config = ujson.load(config_file) + except Exception as ex: + print("Exception\n\ttype: %s\n\targs: %s" % (type(ex).__name__, ex.args)) + + if not self.config.get("nodeid"): + self.config["nodeid"] = uuid.uuid4().hex + with open(CONFIG, "w") as config_file: + ujson.dump(self.config, config_file) + + self.d = dht.DHT22(machine.Pin(2)) + + def update(self): + self.d.measure() + t = float(self.d.temperature()) + h = float(self.d.humidity()) + print("T: %f, H: %f" % (t, h)) + self.send_value("temperature", t) + self.send_value("humidity", h) + + def send_value(self, vtype, value): + url = "http://192.168.11.21:5000/node/add_value" + data = {"nodeid": self.config["nodeid"], "type": vtype, "value": value} + try: + r = requests.post(url, json=data) + # remember to close + r.close() + except Exception as ex: + print("Exception\n\tdata: %s\n\ttype: %s\n\targs: %s" % (data, type(ex).__name__, ex.args)) + +if __name__ == "__main__": + w = wifi.wifi() + w.connect() + node = Node() + + while True: + node.update() + time.sleep(5) \ No newline at end of file diff --git a/uuid.py b/uuid.py new file mode 100644 index 0000000..e675f5d --- /dev/null +++ b/uuid.py @@ -0,0 +1,28 @@ +import uos +import ubinascii + + +class UUID: + def __init__(self, bytes): + if len(bytes) != 16: + raise ValueError('bytes arg must be 16 bytes long') + self._bytes = bytes + + @property + def hex(self): + return ubinascii.hexlify(self._bytes).decode() + + def __str__(self): + h = self.hex + return '-'.join((h[0:8], h[8:12], h[12:16], h[16:20], h[20:32])) + + def __repr__(self): + return "" % str(self) + + +def uuid4(): + """Generates a random UUID compliant to RFC 4122 pg.14""" + random = bytearray(uos.urandom(16)) + random[6] = (random[6] & 0x0F) | 0x40 + random[8] = (random[8] & 0x3F) | 0x80 + return UUID(bytes=random) \ No newline at end of file diff --git a/wifi.py b/wifi.py new file mode 100644 index 0000000..6dbe6ee --- /dev/null +++ b/wifi.py @@ -0,0 +1,22 @@ +import network +from credentials import * +# NET_SSID="xxx" +# NET_PASS="yyy" + +class wifi: + def __init__(self): + self.if_ap = network.WLAN(network.AP_IF) + self.if_sta = network.WLAN(network.STA_IF) + + def status(self): + return self.if_sta.isconnected() + + def connect(self): + self.if_ap.active(False) + self.if_sta.active(True) + if not self.if_sta.isconnected(): + print("connect: reconnecting...") + self.if_sta.connect(NET_SSID, NET_PASS) + while not self.if_sta.isconnected(): + pass + print("network config: ", self.if_sta.ifconfig())