Skip to content

Commit 8c307e6

Browse files
committed
add a circutpython MQTT version.
1 parent 4d2a37d commit 8c307e6

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

circutpythonMQTT/code.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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()

circutpythonMQTT/settings.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CIRCUITPY_WIFI_SSID="yourssid"
2+
CIRCUITPY_WIFI_PASSWORD="wifipassword"

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# analoguEnvoy
2-
This project contains three variations of scripts that can display Solar Production and Power Consumption as measured by an Enphase Envoy Communications Gateway.
2+
This project contains four variations of scripts that can display Solar Production and Power Consumption as measured by an Enphase Envoy Communications Gateway.
33

44
## Python MQTT
55
[solar.py](/pythonMQTT/solar.py) connectes to an MQTT server and subscribes to production and consumption messages published by [homeassistant](https://www.home-assistant.io/) and collected by a [custom envoy integration](https://github.com/briancmpbll/home_assistant_custom_envoy). This variation was developed on a Raspberry Pi Zero W.
@@ -10,6 +10,9 @@ This project contains three variations of scripts that can display Solar Product
1010
## CircutPython http
1111
Written in [CircutPython](https://circuitpython.org/), [code.py](/circutpython/code.py) reads [settings.toml](/circutpython/settings.toml) for an ssid and password to make a wifi connecton. Simiar to the python version your username, password and serial number are used to obtain a token from `enlighten` which is used to query `envoy` locally for consumption and producion information. This variation was developed on a [Raspberry Pi Pico W](https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html#raspberry-pi-pico-w-and-pico-wh).
1212

13+
## CircutPython MQTT
14+
Written in [CircutPython](https://circuitpython.org/), [code.py](/circutpythonMQTT/code.py) reads [settings.toml](/circutpython/settings.toml) for an ssid and password to make a wifi connecton. Similar to the python MQTT vairant it subscribes to production and consumption messages published by homeassistant and collected by a custom envoy integration. This variation was developed on a [Raspberry Pi Pico W](https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html#raspberry-pi-pico-w-and-pico-wh).
15+
1316
## PWM
1417
All three versions use PWM to drive analogue gauges with a 3v range.
1518

0 commit comments

Comments
 (0)