-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
154 lines (127 loc) · 5.38 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import ipaddress
import ssl
import wifi
import socketpool
import adafruit_requests
import time
from adafruit_magtag.magtag import MagTag
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from secrets import secrets
# URLs to fetch from
magtag = MagTag(
)
HA_STATES_ENDPOINT_ROOT = "http://YOUR_HA_IP:8123/api/states/"
MQTT_SERVER = "YOUR_MQTT_SERVER"
MQTT_PORT=1883
BATT_MQTT_TOPIC = secrets["BATT_MQTT_TOPIC"]
TIME_BETWEEN_REFRESHES_SEC = secrets["TIME_BETWEEN_REFRESHES_SEC"] # 1200 sec = 20min / 300 sec = 5 min
use_live = True #Live means we load via APIs, not live means we mock data (wifi errors).
errorstring = ""
wifi_channel = "66"
wifi_rssi = "-99"
location = secrets.get("timezone", None)
TIME_URL = "https://io.adafruit.com/api/v2/%s/integrations/time/strftime?x-aio-key=%s&tz=%s" % (secrets["aio_username"], secrets["aio_key"], location)
TIME_URL += "&fmt=%25Y-%25m-%25d+%25H%3A%25M%3A%25S.%25L+%25j+%25u+%25z+%25Z"
headers = { "Authorization": "Bearer " + secrets["ha_api_token"]};
batt = "%s" % round(float(magtag.peripherals.battery),1)
if use_live:
try:
wifi.radio.connect(ssid= secrets["ssid"],
password=secrets["password"],
channel=secrets["wifi_channel_force"],
)
pool = socketpool.SocketPool(wifi.radio)
wifi_rssi = wifi.radio.ap_info.rssi
wifi_channel = wifi.radio.ap_info.channel
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response = requests.get(HA_STATES_ENDPOINT_ROOT + "sensor.netatmo_outdoor",headers=headers)
outdoor_temp = round(float(response.json()["state"]),1)
response = requests.get(HA_STATES_ENDPOINT_ROOT + "sensor.netatmo_magamistuba",headers=headers)
magamistuba_temp = round(float(response.json()["state"]),1)
response = requests.get(HA_STATES_ENDPOINT_ROOT + "sensor.netatmo_allkorrus",headers=headers)
allkorrus_temp = round(float(response.json()["state"]),1)
response = requests.get(HA_STATES_ENDPOINT_ROOT + "sensor.brigitta_tuba_temperature",headers=headers)
brigittatuba_temp = round(float(response.json()["state"]),1)
response = requests.get(HA_STATES_ENDPOINT_ROOT + "sensor.tarmo_tuba_temperature",headers=headers)
kontorituba_temp = round(float(response.json()["state"]),1)
response = requests.get(HA_STATES_ENDPOINT_ROOT + "sensor.power_meter_active_power",headers=headers)
grid_input_kW = round(float(response.json()["state"])/1000,1)
response = requests.get(HA_STATES_ENDPOINT_ROOT + "sensor.inverter_active_power",headers=headers)
solar_output_kW = round(float(response.json()["state"])/1000,1)
#get time when we updated
response = requests.get(TIME_URL)
updated_at = response.text[11:16]
#updated_at = " "
#Send battery parameters
mqtt_client = MQTT.MQTT(
broker=MQTT_SERVER,
port=MQTT_PORT,
#username="", #do not define if you don't have them, it passes as empty string and resulting in ERRCONNRESET
#password="",
socket_pool=pool,
is_ssl=False,
#ssl_context=ssl.create_default_context(),
)
mqtt_client.connect()
mqtt_client.publish(BATT_MQTT_TOPIC, batt, True) #Retain to True
mqtt_client.disconnect()
#except ConnectionError:
# magtag.exit_and_deep_sleep(5) #retry
except Exception as e:
est = str(e)
#print(" ", est)
magtag.set_text(est, 0, True); #Refresh screen with last
magtag.exit_and_deep_sleep(15) #retry
else:
outdoor_temp = round(float("-5"),1)
magamistuba_temp = round(float("21.29"),1)
allkorrus_temp = round(float("23.55"),1)
brigittatuba_temp = round(float("21.22"),1)
kontorituba_temp = round(float("19.54"),1)
grid_input_kW = round(float("-2725")/1000,1)
solar_output_kW = round(float("1500")/1000,1)
ha_last_update = "2016-05-30T21:50:30.529465+00:00"
updated_at = ha_last_update[11:16] # string from 11 to 16
# ÕUES
magtag.add_text(
#text_font="/fonts/epilogue18.bdf",
text_position=(10, 15), text_scale=2,
)
magtag.set_text("Oues: " + str(outdoor_temp) + " C", 0, False);
# ALLKORRUS
magtag.add_text(
#text_font="/fonts/epilogue18.bdf",
text_position=(10, 35), text_scale=2,
)
magtag.set_text("Allkorrus: " + str(allkorrus_temp) + " C", 1, False);
# MAGAMISTUBA
magtag.add_text(
#text_font="/fonts/epilogue18.bdf",
text_position=(10, 55), text_scale=2,
)
magtag.set_text("Magamistuba: " + str(magamistuba_temp) + " C", 2, False);
# Brigitta tuba
magtag.add_text(
#text_font="/fonts/epilogue18.bdf",
text_position=(10, 75), text_scale=2,
)
magtag.set_text("Brigitta tuba: " + str(brigittatuba_temp) + " C", 3, False);
# Kontor
magtag.add_text(
#text_font="/fonts/epilogue18.bdf",
text_position=(10, 95), text_scale=2,
)
magtag.set_text("Kontorituba: " + str(kontorituba_temp) + " C", 4, False); #Refresh screen with last
# Kontor
magtag.add_text(
#text_font="/fonts/epilogue18.bdf",
text_position=(10, 115), text_scale=2,
)
magtag.set_text("" + str(grid_input_kW) + " | " + str(solar_output_kW) + " kW", 5, False); #Refresh screen with last
# Last updated and batt
magtag.add_text(
#text_font="/fonts/epilogue18.bdf",
text_position=(190, 118), text_scale=1,
)
magtag.set_text(updated_at +" "+ batt + "V " + str(wifi_rssi) + " " + str(wifi_channel), 6, True); #Refresh screen with last
magtag.exit_and_deep_sleep(TIME_BETWEEN_REFRESHES_SEC)