|
| 1 | +import os |
| 2 | +import ipaddress |
| 3 | +import wifi |
| 4 | +import socketpool |
| 5 | +import ssl |
| 6 | +import adafruit_requests |
| 7 | +import supervisor |
| 8 | +import pwmio |
| 9 | +import board |
| 10 | +import time |
| 11 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 12 | + |
| 13 | + |
| 14 | +#https://stackoverflow.com/questions/1969240/mapping-a-range-of-values-to-another |
| 15 | +def translate(sensor_val, in_from, in_to, out_from, out_to): |
| 16 | + out_range = out_to - out_from |
| 17 | + in_range = in_to - in_from |
| 18 | + in_val = sensor_val - in_from |
| 19 | + val=(float(in_val)/in_range)*out_range |
| 20 | + out_val = out_from+val |
| 21 | + return int(out_val) |
| 22 | + |
| 23 | +#https://stackoverflow.com/questions/5996881/how-to-limit-a-number-to-be-within-a-specified-range-python |
| 24 | +def clamp(n, minn, maxn): |
| 25 | + return max(min(maxn, n), minn) |
| 26 | + |
| 27 | +def connect(mqtt_client, userdata, flags, rc): |
| 28 | + print("Connected to MQTT Broker!") |
| 29 | + print("Flags: {0}\n RC: {1}".format(flags, rc)) |
| 30 | + |
| 31 | +def subscribe(mqtt_client, userdata, topic, granted_qos): |
| 32 | + print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) |
| 33 | + |
| 34 | +def message(client, topic, message): |
| 35 | + if not("envoy_20223xxxxxxxx_current_power_" in topic): |
| 36 | + return |
| 37 | + print("New message on topic {0}: {1}".format(topic, message)) |
| 38 | + try: |
| 39 | + value = int(message) |
| 40 | + except: |
| 41 | + return |
| 42 | + if topic == "homeassistant/sensor/envoy_2022xxxxxxxx_current_power_production/state": |
| 43 | + productionPWM.duty_cycle = translate(clamp(value,0,9000),0,9000,0,65535) |
| 44 | + if topic == "homeassistant/sensor/envoy_2022xxxxxxxx_current_power_consumption/state": |
| 45 | + consumptionPWM.duty_cycle = translate(clamp(value,0,9000),0,9000,0,65535) |
| 46 | + |
| 47 | +productionPWM = pwmio.PWMOut(board.GP6, frequency=5000, duty_cycle=0) |
| 48 | +consumptionPWM = pwmio.PWMOut(board.GP7, frequency=5000, duty_cycle=0) |
| 49 | + |
| 50 | +print() |
| 51 | +print("Connecting to WiFi") |
| 52 | +wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) |
| 53 | + |
| 54 | +print("Connected to WiFi") |
| 55 | +print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address]) |
| 56 | + |
| 57 | +print("My IP address is", wifi.radio.ipv4_address) |
| 58 | + |
| 59 | +pool = socketpool.SocketPool(wifi.radio) |
| 60 | + |
| 61 | +mqtt_client = MQTT.MQTT( |
| 62 | + broker="192.168.1.50", |
| 63 | + socket_pool=pool, |
| 64 | + ssl_context=ssl.create_default_context(), |
| 65 | +) |
| 66 | + |
| 67 | +mqtt_client.on_connect = connect |
| 68 | +mqtt_client.on_subscribe = subscribe |
| 69 | +mqtt_client.on_message = message |
| 70 | + |
| 71 | +# long topic strings cause problems so we will just subscribe to _all_ the sensor states and filter them out later. |
| 72 | +# https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/issues/160 |
| 73 | +topics = [("homeassistant/sensor/+/state",0)] |
| 74 | + |
| 75 | +try: |
| 76 | + mqtt_client.connect() |
| 77 | + mqtt_client.subscribe(topics) |
| 78 | +except: |
| 79 | + supervisor.reload() |
| 80 | + |
| 81 | +while True: |
| 82 | + try: |
| 83 | + mqtt_client.loop() |
| 84 | + except: |
| 85 | + supervisor.reload() |
0 commit comments