commit 09ef8e2f1a0e90d1814a670e8722f2b2c52a05d1 Author: Konstantin Koslowski Date: Sun Nov 3 14:56:56 2019 +0100 homecontrol-bh1750: initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b97241 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Pipfile.lock +.idea/* diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..f685d32 --- /dev/null +++ b/Pipfile @@ -0,0 +1,14 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +pyserial = ">=3.3,<4.0" +docopt = ">=0.6.2,<0.7" +adafruit-ampy = ">=1.0.5,<1.1" + +[requires] +python_version = "3.7" diff --git a/bh1750/__init__.py b/bh1750/__init__.py new file mode 100644 index 0000000..81594ca --- /dev/null +++ b/bh1750/__init__.py @@ -0,0 +1,62 @@ +""" +Micropython BH1750 ambient light sensor driver. +from https://github.com/PinkInk/upylib +""" + +from utime import sleep_ms + + +class BH1750(): + """Micropython BH1750 ambient light sensor driver.""" + + PWR_OFF = 0x00 + PWR_ON = 0x01 + RESET = 0x07 + + # modes + CONT_LOWRES = 0x13 + CONT_HIRES_1 = 0x10 + CONT_HIRES_2 = 0x11 + ONCE_HIRES_1 = 0x20 + ONCE_HIRES_2 = 0x21 + ONCE_LOWRES = 0x23 + + # default addr=0x23 if addr pin floating or pulled to ground + # addr=0x5c if addr pin pulled high + def __init__(self, bus, addr=0x23): + self.bus = bus + self.addr = addr + self.off() + self.reset() + + def off(self): + """Turn sensor off.""" + self.set_mode(self.PWR_OFF) + + def on(self): + """Turn sensor on.""" + self.set_mode(self.PWR_ON) + + def reset(self): + """Reset sensor, turn on first if required.""" + self.on() + self.set_mode(self.RESET) + + def set_mode(self, mode): + """Set sensor mode.""" + self.mode = mode + self.bus.writeto(self.addr, bytes([self.mode])) + + def luminance(self, mode): + """Sample luminance (in lux), using specified sensor mode.""" + # continuous modes + if mode & 0x10 and mode != self.mode: + self.set_mode(mode) + # one shot modes + if mode & 0x20: + self.set_mode(mode) + # earlier measurements return previous reading + sleep_ms(24 if mode in (0x13, 0x23) else 180) + data = self.bus.readfrom(self.addr, 2) + factor = 2.0 if mode in (0x11, 0x21) else 1.0 + return (data[0]<<8 | data[1]) / (1.2 * factor) \ No newline at end of file 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..ff357e5 --- /dev/null +++ b/main.py @@ -0,0 +1,58 @@ +import machine +import utime as time +import urequests as requests +import uuid +import ujson +import wifi +import bh1750 + +CONFIG = "config.json" +ADDRESS="http://192.168.11.21:5000" +TIMEOUT_UPDATE = 5 + +class SensorBH1750: + 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)) + + update_config = False + if not self.config.get("id"): + update_config = True + self.config["id"] = uuid.uuid4().hex + if update_config: + with open(CONFIG, "w") as config_file: + ujson.dump(self.config, config_file) + self.id_s = self.config["id"] + + self.i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) + self.s = bh1750.BH1750(self.i2c) + self.s.on() + + def update(self): + mode=bh1750.BH1750.ONCE_HIRES_1 + l = float(self.s.luminance(mode)) + print("luminance: %f" % l) + self.send_update(self.id_s, "luminance", l) + + 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 + 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() + sensor = SensorBH1750() + + while True: + sensor.update() + time.sleep(TIMEOUT_UPDATE) \ 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())