diff --git a/main.py b/main.py index bc6ef57..f151b6c 100644 --- a/main.py +++ b/main.py @@ -7,8 +7,10 @@ import ujson import wifi CONFIG = "config.json" +ADDRESS="http://192.168.11.21:5000" +TIMEOUT_UPDATE = 5 -class Node: +class SensorDHT: def __init__(self): self.config = {} try: @@ -17,10 +19,18 @@ class Node: 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 + update_config = False + if not self.config.get("id_t"): + update_config = True + self.config["id_t"] = uuid.uuid4().hex + if not self.config.get("id_h"): + update_config = True + self.config["id_h"] = uuid.uuid4().hex + if update_config: with open(CONFIG, "w") as config_file: ujson.dump(self.config, config_file) + self.id_t = self.config["id_t"] + self.id_h = self.config["id_h"] self.d = dht.DHT22(machine.Pin(2)) @@ -29,12 +39,12 @@ class Node: 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) + self.send_update(self.id_t, "temperature", t) + self.send_update(self.id_h, "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} + def send_update(self, id_s, type_s, value): + url = "%s/sensors/update" % ADDRESS + data = {"id": id_s, "type": type_s, "value": value} try: r = requests.post(url, json=data) # remember to close @@ -45,8 +55,8 @@ class Node: if __name__ == "__main__": w = wifi.wifi() w.connect() - node = Node() + sensor = SensorDHT() while True: - node.update() - time.sleep(5) \ No newline at end of file + sensor.update() + time.sleep(TIMEOUT_UPDATE) \ No newline at end of file