Compare commits

...

259 Commits

Author SHA1 Message Date
Gunnar Skjold
700f023292 Possibility to specify fixed energy price 2023-04-01 10:57:21 +02:00
Gunnar Skjold
ea43b2b632 Some changes after testing new HA features 2023-04-01 10:43:55 +02:00
Gunnar Skjold
c456e3fdf1 Fixed config loading for ha 2023-04-01 08:20:41 +02:00
Gunnar Skjold
4947f0ec7f Clear HA config in next config version 2023-04-01 08:02:12 +02:00
Gunnar Skjold
799c2f19d9 Changes after uahn merge 2023-04-01 08:00:15 +02:00
Gunnar Skjold
18dc188835 Merge pull request #481 from dbeinder/mosquito
Add HAN pullup config and mosquito board
2023-04-01 07:43:34 +02:00
Gunnar Skjold
7db745829e Merge branch 'master' into mosquito 2023-04-01 07:43:24 +02:00
Gunnar Skjold
81fedaaa6a Moved from postfix to prefix on HA sensor name tag 2023-04-01 07:38:24 +02:00
Gunnar Skjold
71e0f13f0e Various changes after testing new HA features 2023-04-01 07:31:40 +02:00
Gunnar Skjold
f214af3595 Added price API rate limit error 2023-03-31 13:57:38 +02:00
Gunnar Skjold
fb9ae2f5f6 Updated boot reason descriptions 2023-03-31 13:52:22 +02:00
Gunnar Skjold
4c73f39214 Fixed state class on sensors 2023-03-31 13:49:02 +02:00
Gunnar Skjold
1e7176af0b HA configuration 2023-03-30 14:54:10 +02:00
Gunnar Skjold
0d8c88b1fc Fixed DST 25hr day error on month plot 2023-03-30 12:25:07 +02:00
Gunnar Skjold
d189d904fe Fixed edge case where graph data sometimes goes crazy 2023-03-30 11:55:39 +02:00
Gunnar Skjold
3c86e824a2 Added boot time to status page 2023-03-30 09:44:41 +02:00
Gunnar Skjold
061f6433d6 Added another proprietary L&G 2023-03-30 09:00:12 +02:00
Gunnar Skjold
0d923e30d6 Added another proprietary L&G 2023-03-30 08:59:56 +02:00
Gunnar Skjold
51f761d25e Fixed incorrect change of HA current hour/day/month kwh state class 2023-03-29 08:53:13 +02:00
Gunnar Skjold
58fec70e7c Added detection for Kaifa through system title 2023-03-27 09:20:16 +02:00
Gunnar Skjold
dff55b4eee Remove copy/paste l2current substitution from DSMR parser 2023-03-27 08:46:41 +02:00
Gunnar Skjold
931f0d400b Only publish prices once when fetching today and tomorrow at the same time 2023-03-26 15:24:55 +02:00
Gunnar Skjold
0178dc4184 Various fixes for HA 2023-03-26 15:12:24 +02:00
Gunnar Skjold
be522b40f9 Fixed swapped voltage ranges, esp32 vs esp8266 2023-03-26 10:52:01 +02:00
Gunnar Skjold
068c55e7bb Fixed disabling encryption 2023-03-26 10:49:00 +02:00
Gunnar Skjold
8e88d7e11b Show boot reason on status page 2023-03-26 10:40:02 +02:00
Gunnar Skjold
e36acef1d4 Fixed DST offset in day graph 2023-03-26 10:14:10 +02:00
Gunnar Skjold
07d5481a72 Actual fix for DST change 2023-03-26 10:06:37 +02:00
Gunnar Skjold
dbd6205cca Fixed issue with price graph in relation to entering or leaving DST 2023-03-25 19:14:21 +01:00
david-beinder
35d47902c6 Add HAN pullup config and mosquito board 2023-03-18 23:39:55 +01:00
Gunnar Skjold
938f9f69d1 Fixed memory leak in baud autodetect 2023-03-11 10:17:44 +01:00
Gunnar Skjold
cd27472e2d Fixed checkbox tick and changed backup/restore label 2023-03-11 09:19:09 +01:00
Gunnar Skjold
64f8414217 Fixed precision on domoticz energy counter 2023-03-11 08:53:51 +01:00
Gunnar Skjold
a2c1250724 Some updates for esp32c3 2023-02-27 09:00:07 +01:00
Gunnar Skjold
4b7160b502 Only report realtime data on MQTT if EnergyAccounting is initialized 2023-02-27 08:30:56 +01:00
Gunnar Skjold
cc50457404 Fixed spikes in realtime production on top of hour 2023-02-27 08:04:29 +01:00
Gunnar Skjold
cd48192a74 Removed classic UI 2023-02-22 18:09:16 +01:00
Gunnar Skjold
cd031c33aa Added forced 2400 baud for Pow-K 2023-02-22 07:12:54 +01:00
Gunnar Skjold
6c59b15681 Added esp32c3 release build 2023-02-22 07:07:51 +01:00
Gunnar Skjold
7c8593122b Removed forced baud rate 2023-02-22 07:07:31 +01:00
Gunnar Skjold
6d8fd4e083 Merge pull request #460 from dbeinder/c3-generic-boards
Add generic boards for ESP32-C3
2023-02-22 06:56:09 +01:00
david-beinder
79d674710f Add generic boards for ESP32-C3 2023-02-21 23:25:14 +01:00
Gunnar Skjold
062068eacd Increased range of tariff thresholds 2023-02-21 15:29:10 +01:00
Gunnar Skjold
98309ea532 Allow extended ascii in ssid 2023-02-21 13:55:38 +01:00
Gunnar Skjold
0f75fa4a58 Fixed redirect issue after initial setup with DHCP on ESP8266 2023-02-21 13:40:55 +01:00
Gunnar Skjold
d08f75d9c3 Fixed error changing from static ip to dhcp 2023-02-21 12:44:43 +01:00
Gunnar Skjold
dd4a43c831 Better feedback from entso-e 2023-02-21 07:54:11 +01:00
Gunnar Skjold
1f5a04e606 Increased range for instant values 2023-02-14 20:23:23 +01:00
Gunnar Skjold
4d6e63a171 Fixed GPIO HAN select for ESP8266 2023-02-12 20:48:38 +01:00
Gunnar Skjold
0093410e05 Removed kicad backup 2023-02-10 18:57:50 +01:00
Gunnar Skjold
ee63a606e8 Fixed voltage read issue 2023-02-10 18:55:55 +01:00
Gunnar Skjold
d0ccd2d007 Minor ui adjustment 2023-02-10 17:28:19 +01:00
Gunnar Skjold
bb2f74d1ca Various UI improvements 2023-02-06 18:20:48 +01:00
Gunnar Skjold
dfef18fa09 Updated Entsoe endpoint 2023-02-06 11:32:53 +01:00
Gunnar Skjold
485c21dc69 Removed unnecessary build flag 2023-02-06 10:59:40 +01:00
Gunnar Skjold
9ceb84bc9c C3 build 2023-02-06 07:46:34 +01:00
Gunnar Skjold
24e68428c4 Commited gui dist files 2023-02-05 19:26:48 +01:00
Gunnar Skjold
c2c5855e6a Fixed issue where authentication key got corrupted from configfile upload 2023-02-02 16:15:03 +01:00
Gunnar Skjold
de19de2129 Fixed incorrect change in previous commit 2023-02-01 19:00:50 +01:00
Gunnar Skjold
1719263de0 Fixed config migration issue from v2.1 2023-02-01 18:46:22 +01:00
Gunnar Skjold
e70b872c98 Removed v2.0 config loading 2023-02-01 18:46:06 +01:00
Gunnar Skjold
b7d28238ab Extended cache for js and css 2023-01-31 17:16:18 +01:00
Gunnar Skjold
6c9a8b0692 Label fix 2023-01-31 17:04:46 +01:00
Gunnar Skjold
9f3dba3aab Fixed reconnect after setup 2023-01-30 20:58:08 +01:00
Gunnar Skjold
e4e4ad4107 Fixed autodetect 2023-01-30 18:32:17 +01:00
Gunnar Skjold
e8fb9570bb Chasing a reboot bug... 2023-01-30 16:52:12 +01:00
Gunnar Skjold
bc42099962 Fixing some bug reports 2023-01-30 16:07:08 +01:00
Gunnar Skjold
be71cbe609 Some changes after bug reports 2023-01-30 12:02:23 +01:00
Gunnar Skjold
0d6df03c94 Fixed reboot loop, changed temperature sensor behaviour and changed some debugging 2023-01-29 22:45:35 +01:00
Gunnar Skjold
d777040c0a Fixed loading error from previous version energy accounting 2023-01-29 17:04:04 +01:00
Gunnar Skjold
a5636a60f8 Some changes after testing 2023-01-28 20:24:18 +01:00
Gunnar Skjold
6f817b2ed5 Some changes after testing 2023-01-28 20:19:06 +01:00
Gunnar Skjold
e4fec4f4c2 Some final changes 2023-01-28 11:53:26 +01:00
Gunnar Skjold
3a25964ec4 Merge pull request #412 from UtilitechAS/dev-v2.2
v2.2
2023-01-28 10:54:20 +01:00
Gunnar Skjold
1227dff412 Changed autocomplete in setup form 2023-01-26 19:55:39 +01:00
Gunnar Skjold
26ee2e6efc Some changes after testing + changing hostname for hub 2023-01-26 19:42:19 +01:00
Gunnar Skjold
d6a8d31278 Some updates for documentation 2023-01-23 18:19:33 +01:00
Gunnar Skjold
3f2b534baa Merge branch 'master' into dev-v2.2 2023-01-23 13:59:38 +01:00
Gunnar Skjold
70f413013c Merge pull request #408 from aadnehovda/patch-1
Unknown HA entity units.
2023-01-23 13:57:33 +01:00
Ådne Hovda
6edcd174bb Unknown HA entity units.
It appears that HA units are case sensitive: https://developers.home-assistant.io/docs/core/entity/sensor/, so for SensorDeviceClass.REACTIVE_POWER it should be "var", not "VAr". Also for SensorDeviceClass.TEMPERATURE, the degree symbol is required: "°C".

    Entity sensor.ams_ed6e_qo (<class 'homeassistant.components.mqtt.sensor.MqttSensor'>) is using native unit of measurement 'VAr' which is not a valid unit for the device class ('reactive_power') it is using; Please update your configuration if your entity is manually configured, otherwise create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+mqtt%22
    Entity sensor.ams_ed6e_q (<class 'homeassistant.components.mqtt.sensor.MqttSensor'>) is using native unit of measurement 'VAr' which is not a valid unit for the device class ('reactive_power') it is using; Please update your configuration if your entity is manually configured, otherwise create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+mqtt%22
    Entity sensor.ams_ed6e_tqo (<class 'homeassistant.components.mqtt.sensor.MqttSensor'>) is using native unit of measurement 'kVArh' which is not a valid unit for the device class ('energy') it is using; Please update your configuration if your entity is manually configured, otherwise create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+mqtt%22
    Entity sensor.ams_ed6e_tqi (<class 'homeassistant.components.mqtt.sensor.MqttSensor'>) is using native unit of measurement 'kVArh' which is not a valid unit for the device class ('energy') it is using; Please update your configuration if your entity is manually configured, otherwise create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+mqtt%22
2023-01-23 11:09:47 +01:00
Gunnar Skjold
3170ef6ce9 c3 scripts 2023-01-21 13:45:47 +01:00
Gunnar Skjold
fb56f4d012 More changes for repo 2023-01-21 13:45:09 +01:00
Gunnar Skjold
c5756f0cba Renamed repo 2023-01-21 13:43:11 +01:00
Gunnar Skjold
95c9ecc8b2 Merge branch 'master' into dev-v2.2 2023-01-18 21:14:49 +01:00
Gunnar Skjold
bdee066c33 Fixed reboot loop 2023-01-18 21:14:40 +01:00
Gunnar Skjold
957039d0c0 Merge branch 'master' into dev-v2.2 2023-01-18 20:48:38 +01:00
Gunnar Skjold
dd23a0fa60 Fixed reboot look 2023-01-18 20:48:27 +01:00
Gunnar Skjold
7777a0a059 Merge branch 'master' into dev-v2.2 2023-01-18 17:11:10 +01:00
Gunnar Skjold
e8fc6d48bf Fixed issue where GPIO setup becomes invalid 2023-01-18 17:11:01 +01:00
Gunnar Skjold
c98148c886 Update readme 2023-01-17 19:56:28 +01:00
Gunnar Skjold
6ba2b4060e Update images for readme and wiki 2023-01-17 19:55:00 +01:00
Gunnar Skjold
762c17ca8e Updated readme and example ini file 2023-01-17 19:48:24 +01:00
Gunnar Skjold
fad6ada1e0 Updated release 2023-01-17 19:15:57 +01:00
Gunnar Skjold
fb1d343ee3 Removed debugging 2023-01-17 19:03:07 +01:00
Gunnar Skjold
1f3c32e80a Adding secrets to build 2023-01-17 18:57:11 +01:00
Gunnar Skjold
43fbca7099 Update release build 2023-01-17 18:42:09 +01:00
Gunnar Skjold
7a36082564 Trying stuff to get build working 2023-01-17 18:34:47 +01:00
Gunnar Skjold
fb2cfdfe01 Trying to fix build 2023-01-17 18:28:08 +01:00
Gunnar Skjold
6c3dca9344 Updated build 2023-01-17 18:15:32 +01:00
Gunnar Skjold
cce5d75fd7 Updated build 2023-01-17 18:13:54 +01:00
Gunnar Skjold
8fd411c1d6 Updated build 2023-01-17 18:13:20 +01:00
Gunnar Skjold
4b15ac74fc Update FUNDING.yml 2023-01-17 18:05:44 +01:00
Gunnar Skjold
508b2e6c45 Icon for safari 2023-01-17 17:51:50 +01:00
Gunnar Skjold
af630615db Changes after testing 2023-01-17 17:49:01 +01:00
Gunnar Skjold
222a4f13e2 Changes after testing 2023-01-17 17:48:42 +01:00
Gunnar Skjold
3bc6c75c5a Some more changes for L&G 2023-01-15 11:20:28 +01:00
Gunnar Skjold
dbb3eac709 Some changes for L&G 2023-01-15 10:56:46 +01:00
Gunnar Skjold
1cee48eab4 Some design changes 2023-01-14 09:34:28 +01:00
Gunnar Skjold
c28752a00a Fixed firmware upgrade 2023-01-12 21:24:36 +01:00
Gunnar Skjold
365061df29 Some changes after testing 2023-01-12 20:17:37 +01:00
Gunnar Skjold
870617f780 More changes for v2.2 2023-01-11 20:41:27 +01:00
Gunnar Skjold
4972b980ba Changes for v2.2 2023-01-06 14:34:07 +01:00
Gunnar Skjold
51c70abda3 Merge branch 'master' into dev-v2.2 2023-01-04 18:55:38 +01:00
Gunnar Skjold
8d448533c7 Fixed config export for esp32 2022-12-19 18:36:04 +01:00
Gunnar Skjold
43cb9a0000 Fixed DSMR CRC check 2022-12-19 18:14:12 +01:00
Gunnar Skjold
d49b7eac09 Minor change 2022-12-19 16:49:40 +01:00
Gunnar Skjold
8f057e687c Merge pull request #387 from lassebm/lg-e360-fixes
Landis+Gyr E360 fixes
2022-12-19 16:45:52 +01:00
Lasse Bang Mikkelsen
5b4f680114 Align Landis+Gyr naming 2022-12-17 12:38:18 +01:00
Lasse Bang Mikkelsen
fabdfbadf4 Add support for Landis+Gyr meters using "LGF" manufacturer ID 2022-12-16 20:20:40 +01:00
Lasse Bang Mikkelsen
afa47ea633 Use list type 4 when phase active import/export power is available 2022-12-16 20:19:15 +01:00
Gunnar Skjold
461d76e651 Merge branch 'master' into dev-v2.2 2022-12-15 18:32:36 +01:00
Gunnar Skjold
c40e20c8e9 Extract active import/export per phase from DSMR 2022-12-15 18:25:13 +01:00
Gunnar Skjold
4e97554514 Some changes during testing 2022-12-14 19:37:32 +01:00
Gunnar Skjold
28a9d6746b Fixed discover when setting different hostname in setup 2022-12-14 18:15:46 +01:00
Gunnar Skjold
7ea4fe881c HAN serial autodetect 2022-12-13 21:05:42 +01:00
Gunnar Skjold
c4eaf8184b Check if authenticated to show upgrade links and config up/download 2022-12-10 09:27:18 +01:00
Gunnar Skjold
312972f77d Some improvements to v2.2 2022-12-09 20:26:48 +01:00
Gunnar Skjold
27b9058af5 Some improvements to v2.2 2022-12-09 20:26:31 +01:00
Gunnar Skjold
bc4d61098c Some error handling 2022-12-09 19:10:24 +01:00
Gunnar Skjold
b2de6472cf Some experiments with captive portal 2022-12-09 18:24:43 +01:00
Gunnar Skjold
6c3ddc57b5 Removed "auto multiplier" for Kamstrup 2022-12-08 13:44:21 +01:00
Gunnar Skjold
2ff7044c85 Fixed incorrect value for peaks in MQTT 2022-12-07 20:13:30 +01:00
Gunnar Skjold
0145be851e Merge branch 'master' into dev-v2.2 2022-12-07 20:11:14 +01:00
Gunnar Skjold
6b0d540f39 Added 300 baud 2022-12-07 11:51:45 +01:00
Gunnar Skjold
2218ac4e8a Fixed graph clearing problem 2022-12-07 11:32:55 +01:00
Gunnar Skjold
33bd3da310 Various fix for realtime values 2022-12-06 15:45:43 +01:00
Gunnar Skjold
a239e1a63d Fix on special case for Kaifa MA304T3 2022-12-06 08:28:30 +01:00
Gunnar Skjold
8a809ec128 Bugfix from testing 2022-12-04 14:32:47 +01:00
Gunnar Skjold
b48a0f13fe More v2.2 2022-12-04 14:12:35 +01:00
Gunnar Skjold
b07ed075f4 Even more v2.2 2022-12-02 20:38:56 +01:00
Gunnar Skjold
0927cab8e2 Some changes for fetching prices from amshub 2022-12-02 19:24:53 +01:00
Gunnar Skjold
eed35b7bbc Merge branch 'hub-prices' into dev-v2.2 2022-12-02 19:08:29 +01:00
Gunnar Skjold
e34da5fd83 Merge branch 'master' into hub-prices 2022-12-02 19:04:14 +01:00
Gunnar Skjold
148fb14c93 More v2.2 2022-12-02 19:03:16 +01:00
Gunnar Skjold
000cfd8697 Merge branch 'master' into dev-v2.2 2022-12-01 18:12:32 +01:00
Gunnar Skjold
1ef5703971 Fixed changing mdns settings 2022-12-01 18:06:19 +01:00
Gunnar Skjold
cda3b80b35 Some changes 2022-11-29 21:24:58 +01:00
Gunnar Skjold
c7b8090634 Some minor changes 2022-11-29 21:02:23 +01:00
Gunnar Skjold
63a8d79b95 Making it work on 8266 2022-11-29 20:50:36 +01:00
Gunnar Skjold
02ae3fc7f5 More v2.2 2022-11-29 20:20:56 +01:00
Gunnar Skjold
2dcc874592 More v2.2 2022-11-25 19:18:32 +01:00
Gunnar Skjold
d4d9d2224f More v2.2 2022-11-23 20:52:37 +01:00
Gunnar Skjold
902e43979b Even more v2.2 2022-11-21 18:48:04 +01:00
Gunnar Skjold
8e54f23367 More v2.2 2022-11-20 20:47:29 +01:00
Gunnar Skjold
ab7128c53a Further work on v2.2 2022-11-19 15:44:23 +01:00
Gunnar Skjold
6563700df4 Merge branch 'master' into dev-v2.2 2022-11-15 18:07:02 +01:00
Gunnar Skjold
538de5ea99 Increased range of multipliers 2022-11-08 18:41:31 +01:00
Gunnar Skjold
042e2bcc85 Fixed power saving 2022-11-07 14:57:53 +01:00
Gunnar Skjold
b6f630b134 Some more Svelte implementation 2022-11-01 20:59:38 +01:00
Gunnar Skjold
65a09dcecf Merge branch 'master' into dev-v2.2 2022-11-01 19:07:00 +01:00
Gunnar Skjold
775e5a0881 Support different size numbers in L&G payload 2022-11-01 18:41:43 +01:00
Gunnar Skjold
6c0d5fcc09 Some adjustments to wifi power saving option 2022-10-31 10:09:07 +01:00
Gunnar Skjold
69d8fa9254 Made wifi sleep mode configurable 2022-10-27 19:17:50 +02:00
root
71d261bf34 Merge branch 'hub-prices' of github.com:gskjold/AmsReader into hub-prices 2022-10-23 19:11:51 +02:00
root
9f4f5b4620 Merge branch 'master' into hub-prices 2022-10-23 19:09:15 +02:00
Gunnar Skjold
c38c305bab Fixed threshold update via config file 2022-10-21 19:21:55 +02:00
Gunnar Skjold
bfee2a1d64 Fixed month average calculation 2022-10-21 18:43:30 +02:00
Gunnar Skjold
ec7edae9a1 Updated issue templates 2022-10-21 17:43:33 +02:00
Gunnar Skjold
d8e265b7ac Fixed JSON errors 2022-10-20 20:12:33 +02:00
Gunnar Skjold
1987cddab7 Some more cleanup 2022-10-20 20:11:13 +02:00
Gunnar Skjold
e18be5f97c Better memory optimization 2022-10-19 20:58:57 +02:00
Gunnar Skjold
537597d6d1 Expanding size of daily and monthly data points 2022-10-19 20:30:29 +02:00
Gunnar Skjold
a89013cec3 Fixed config file upload 2022-10-19 20:24:21 +02:00
Gunnar Skjold
9c7a0cb7ff Fixed graph error 2022-10-19 19:59:41 +02:00
Gunnar Skjold
ade12199b9 Reclaiming some heap space 2022-10-19 19:54:26 +02:00
Gunnar Skjold
92692c6eaf More implementation of Svelte GUI 2022-10-18 20:52:42 +02:00
Gunnar Skjold
e4114c3e74 Merge branch 'master' into dev-v2.2 2022-10-18 18:30:37 +02:00
Gunnar Skjold
39b68aca51 Removed invalid code 2022-10-18 18:30:26 +02:00
root
feffbb53a3 Merge branch 'master' of github.com:gskjold/AmsToMqttBridge into hub-prices 2022-10-17 15:42:23 +02:00
Gunnar Skjold
8ac1e034b1 Reverted WiFi_connect method 2022-10-17 13:09:28 +02:00
Gunnar Skjold
f446dff865 Fixed LED blink delay 2022-10-17 13:09:04 +02:00
root
e1b2554af2 Merge branch 'master' of github.com:gskjold/AmsToMqttBridge into hub-prices 2022-10-16 18:46:47 +02:00
Gunnar Skjold
627a50ab50 Fixed priceapi dropdowns 2022-10-16 18:46:31 +02:00
Gunnar Skjold
a20d007b45 Merge branch 'hub-prices' of github.com:gskjold/AmsReader into hub-prices 2022-10-16 18:32:25 +02:00
Gunnar Skjold
64840e13f0 Merge branch 'master' into hub-prices 2022-10-16 18:31:26 +02:00
Gunnar Skjold
34ebe9601a Changed blink behaviour 2022-10-16 18:31:11 +02:00
root
72f1d59338 Merge branch 'hub-prices' of github.com:gskjold/AmsReader into hub-prices 2022-10-16 17:36:48 +02:00
Gunnar Skjold
07ed425320 Merge branch 'master' into hub-prices 2022-10-16 17:34:19 +02:00
Gunnar Skjold
2e75e1c4dc Moved HA files 2022-10-13 20:58:08 +02:00
Gunnar Skjold
e6df68481f Merge branch 'master' into dev-v2.2 2022-10-13 20:47:48 +02:00
Gunnar Skjold
fa299198fc Fixed all build warnings 2022-10-13 20:33:59 +02:00
Gunnar Skjold
6d81b0a856 Fixed all build warnings 2022-10-13 20:24:56 +02:00
Gunnar Skjold
dd095da97b Reduced footprint of GPIO config page 2022-10-13 19:27:25 +02:00
Gunnar Skjold
1b6ce203b7 Better debug output whem receiving unknown data 2022-10-13 17:40:44 +02:00
Gunnar Skjold
2850be4e48 Merge pull request #322 from tbarnekov/extend_power_info
Populate Active Import and Export per phase and export to HA
2022-10-12 20:34:09 +02:00
Gunnar Skjold
7eca31de84 Fixed build errors 2022-10-12 20:01:55 +02:00
Gunnar Skjold
a64f960cc7 Added Pow-U+ profile 2022-10-12 19:30:00 +02:00
Gunnar Skjold
ce3a47a7e6 Fixed error blinks and improved WiFi reconnect 2022-10-12 19:25:48 +02:00
Thomas Barnekov
8395e1dc77 Merge remote-tracking branch 'origin/master' into extend_power_info 2022-10-10 17:06:09 +02:00
Thomas Barnekov
39a4761415 Add DOCTYPE to html to fix quirks mode 2022-10-10 16:59:43 +02:00
root
dc25c147b9 Merge remote-tracking branch 'public/master' into hub-prices 2022-10-07 17:36:49 +00:00
Gunnar Skjold
867ab9d6c2 Build variable for upgrade URL 2022-10-07 19:35:30 +02:00
Gunnar Skjold
6a76144566 Merge branch 'master' into hub-prices 2022-10-07 17:49:47 +02:00
Thomas Barnekov
5e03e3d3c2 Move per-phase power consumption and power factor values to new ha4.json 2022-10-07 13:40:29 +02:00
Thomas Barnekov
adb5050621 Merge branch 'gskjold:master' into extend_power_info 2022-10-06 17:31:43 +02:00
Gunnar Skjold
026cd25c8c Setting modem sleep to max 2022-10-06 17:30:37 +02:00
Gunnar Skjold
06ec97b42a Make sure we dont use more than 5 peaks if defined higher 2022-10-06 17:20:13 +02:00
Gunnar Skjold
b420a0e6f4 Limit peak count if over 5 2022-10-06 17:12:12 +02:00
Thomas Barnekov
7cd52d5689 Add individual power reading for all phases (include in HA) 2022-10-06 00:35:30 +02:00
Thomas Barnekov
ad78ff3082 Fix implicit cast and non-const char pointer warnings 2022-10-05 22:42:43 +02:00
Gunnar Skjold
fe7be81f1e Rudimentary detection for L&G data. Probably change this in the future 2022-10-01 12:11:46 +02:00
Gunnar Skjold
7674fc2ad0 Merge branch 'lng-parser' 2022-10-01 11:56:08 +02:00
Gunnar Skjold
d50181c347 Upgrade with custom URL 2022-10-01 11:43:42 +02:00
Gunnar Skjold
8ca771fa5a Changed "BUS" to "M-BUS" 2022-10-01 09:25:56 +02:00
Gunnar Skjold
7d557b2679 Only show new version when actually new in firmware page. Also be more specific on USB power in upgrade warning 2022-10-01 09:24:07 +02:00
Gunnar Skjold
2f0c912388 Exclude decimals in BUILD_EPOCH flag 2022-10-01 09:22:09 +02:00
Gunnar Skjold
57e6d0fbe3 Modifications to make price sensors work in HA 2022-09-30 19:18:23 +02:00
Gunnar Skjold
e7c25fafda Prices and realtime HA sensors 2022-09-28 20:13:03 +02:00
Gunnar Skjold
2a4772fe25 Fixed memory leak when reconnecting to MQTT/SSL 2022-09-27 19:59:37 +02:00
Gunnar Skjold
f1f7408208 Added price zones 2022-09-25 11:46:04 +02:00
Gunnar Skjold
feb8e5007b Fixed MQTT buffer size 2022-09-25 10:50:34 +02:00
Gunnar Skjold
c4af1ee74f Fixed saving tariff thresholds 2022-09-25 10:50:09 +02:00
Gunnar Skjold
992e1b6121 Net value for active power (L&G) 2022-09-12 08:03:32 +02:00
Gunnar Skjold
95d3008a66 Updates to Svelte UI 2022-09-12 07:29:25 +02:00
Gunnar Skjold
04cd6ac387 Some changes to make Svelte UI work 2022-09-03 14:37:31 +02:00
Gunnar Skjold
584c7b1154 More restructuring 2022-09-03 14:03:15 +02:00
Gunnar Skjold
493c4b4337 Moved Svelte UI to lib folder 2022-09-03 13:41:11 +02:00
Gunnar Skjold
488c969858 Some serious restructuring to be able to swap between implementations 2022-09-03 13:12:29 +02:00
Gunnar Skjold
8b0d4185d3 Created new UI with Svelte 2022-09-03 12:19:56 +02:00
Gunnar Skjold
9cc7529934 Fixed L&G scaling 2022-08-31 08:19:09 +02:00
Gunnar Skjold
ef8715be6d Merge branch 'master' into lng-parser 2022-08-31 08:04:24 +02:00
Gunnar Skjold
e232b875fa Fixed Aidon timestamp 2022-08-23 21:07:06 +02:00
Gunnar Skjold
f18171fecc Fixed MQTT byte mode 2022-08-23 20:39:47 +02:00
Gunnar Skjold
a3c7a09211 Adding peaks to data.json and MQTT RAW 2022-08-23 20:21:19 +02:00
Gunnar Skjold
a6f3bc3f71 Added version to system json 2022-08-23 19:49:19 +02:00
Gunnar Skjold
44bcd386d1 aggregate obis 1.8.x and 2.8.x for L&G 2022-08-23 09:04:45 +02:00
Gunnar Skjold
01547f9a52 Adjustments to make L&G parser work 2022-08-19 13:26:29 +02:00
Gunnar Skjold
608470c5f7 Merge branch 'master' into hub-prices 2022-08-18 19:56:31 +02:00
Gunnar Skjold
940d38af5c Merge branch 'master' into lng-parser 2022-08-17 17:33:24 +02:00
Gunnar Skjold
4e451c51e1 HW serial fix for S2 2022-08-17 17:33:06 +02:00
Gunnar Skjold
43def1c311 Fixed Pow-P1 profile 2022-08-17 17:32:53 +02:00
Gunnar Skjold
2978116207 Fixed formatting error in HA payload 2022-08-16 11:55:07 +02:00
Gunnar Skjold
a055465ce0 Untestet L&G data parser 2022-08-16 08:22:43 +02:00
Gunnar Skjold
68b3415a6c Cleanup for price API 2022-08-13 16:32:55 +02:00
Gunnar Skjold
d8f3ae8b07 Merge branch 'master' into hub-prices 2022-08-13 16:00:25 +02:00
Gunnar Skjold
998b986604 Adjusted modem sleep to a safer value 2022-08-12 20:48:34 +02:00
Gunnar Skjold
7799431405 Remove unused files 2022-08-12 20:42:13 +02:00
Gunnar Skjold
1a92cd1978 Fetch prices from hub 2022-08-12 20:41:53 +02:00
Gunnar Skjold
508c14a671 Fixed modem sleep on S2 and fixed default wifi power 2022-08-12 09:02:04 +02:00
Gunnar Skjold
4a7ef87269 Fixed realtime day calculation 2022-08-10 17:43:51 +02:00
Gunnar Skjold
3a4fc707b0 Show real time production 2022-08-10 10:53:16 +02:00
Gunnar Skjold
5d2e320b07 Fixed export labels on graph 2022-08-10 10:28:31 +02:00
Gunnar Skjold
d12613b91a Trying to fix HA accumulated 2022-08-10 10:01:24 +02:00
Gunnar Skjold
e6a02f34ab USB power warning when upgrading 2022-08-10 09:31:50 +02:00
Gunnar Skjold
0b2ffbfd77 Revert to arduino 2.0.3 because of #298 2022-08-10 09:30:31 +02:00
Gunnar Skjold
cab6c54ed9 Correct Vcc GPIO for Pow-K+ 2022-08-01 12:48:31 +02:00
Gunnar Skjold
313024f273 Support OTA upgrade from ESP8266 2022-08-01 08:17:24 +02:00
Gunnar Skjold
91fc078c5e Higher precision on prices 2022-07-31 20:26:38 +02:00
Gunnar Skjold
5b9d44a3e9 Fixed loading of energy accounting 2022-07-31 20:19:01 +02:00
238 changed files with 15650 additions and 7129 deletions

2
.github/FUNDING.yml vendored
View File

@@ -1 +1 @@
custom: ["https://paypal.me/gskjold"]
custom: ["https://amsleser.no"]

View File

@@ -33,6 +33,7 @@ If applicable, add screenshots to help explain your problem.
**Relevant firmware information:**
- Version: [e.g. 2.1.0]
- MQTT: [yes/no]
- MQTT payload type: [e.g. JSON]
- HAN GPIO: [e.g. GPIO5]
- HAN baud and parity: [e.g. 2400 8E1]
- Temperature sensors [e.g. 3xDS18B20]

View File

@@ -1,8 +1,8 @@
blank_issues_enabled: false
contact_links:
- name: Meter configuration
url: https://github.com/gskjold/AmsToMqttBridge/wiki/Known-hardware-configurations
url: https://github.com/UtilitechAS/amsreader-firmware/wiki/Known-hardware-configurations
about: Please check your meter configuration here first.
- name: Frequently asked questions
url: https://github.com/gskjold/AmsToMqttBridge/wiki/FAQ
url: https://github.com/UtilitechAS/amsreader-firmware/wiki/FAQ
about: Please check frequently asked questions first.

View File

@@ -20,6 +20,7 @@ A clear and concise description of what the problem is.
**Relevant firmware information:**
- Version: [e.g. 2.1.0]
- MQTT: [yes/no]
- MQTT payload type: [e.g. JSON]
- HAN GPIO: [e.g. GPIO5]
- HAN baud and parity: [e.g. 2400 8E1]
- Temperature sensors [e.g. 3xDS18B20]

View File

@@ -8,6 +8,7 @@ on:
- scripts/**
- web/**
- platformio.ini
- .github/workflows/**
branches:
- '*'
tags:
@@ -22,6 +23,12 @@ jobs:
steps:
- name: Check out code from repo
uses: actions/checkout@v1
- name: Inject secrets into ini file
run: |
sed -i 's/NO_AMS2MQTT_PRICE_KEY/AMS2MQTT_PRICE_KEY="${{secrets.AMS2MQTT_PRICE_KEY}}"/g' platformio.ini
sed -i 's/NO_AMS2MQTT_PRICE_AUTHENTICATION/AMS2MQTT_PRICE_AUTHENTICATION="${{secrets.AMS2MQTT_PRICE_AUTHENTICATION}}"/g' platformio.ini
- name: Cache Python dependencies
uses: actions/cache@v1
with:
@@ -40,6 +47,18 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -U platformio css_html_js_minify
- name: Set up node
uses: actions/setup-node@v1
with:
node-version: '16.x'
- name: Build with node
run: |
cd lib/SvelteUi/app
npm ci
npm run build
cd -
env:
CI: true
- name: PlatformIO lib install
run: pio lib install
- name: PlatformIO run

View File

@@ -23,6 +23,12 @@ jobs:
env:
GITHUB_REF: ${{ github.ref }}
run: echo "GITHUB_TAG=$(echo ${GITHUB_REF##*/})" >> $GITHUB_ENV
- name: Inject secrets into ini file
run: |
sed -i 's/NO_AMS2MQTT_PRICE_KEY/AMS2MQTT_PRICE_KEY="${{secrets.AMS2MQTT_PRICE_KEY}}"/g' platformio.ini
sed -i 's/NO_AMS2MQTT_PRICE_AUTHENTICATION/AMS2MQTT_PRICE_AUTHENTICATION="${{secrets.AMS2MQTT_PRICE_AUTHENTICATION}}"/g' platformio.ini
- name: Cache Python dependencies
uses: actions/cache@v1
with:
@@ -41,12 +47,23 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -U platformio css_html_js_minify
- name: Set up node
uses: actions/setup-node@v1
with:
node-version: '16.x'
- name: Build with node
run: |
cd lib/SvelteUi/app
npm ci
npm run build
cd -
env:
CI: false
- name: PlatformIO lib install
run: pio lib install
- name: PlatformIO run
run: pio run
- name: Create zip files
run: /bin/sh scripts/mkzip.sh
- name: Create Release
id: create_release
uses: actions/create-release@v1.0.0
@@ -58,6 +75,19 @@ jobs:
draft: false
prerelease: false
- name: Build esp8266 firmware
run: pio run -e esp8266
- name: Create esp8266 zip file
run: /bin/sh scripts/esp8266/mkzip.sh
- name: Upload esp8266 binary to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: .pio/build/esp8266/firmware.bin
asset_name: ams2mqtt-esp8266-${{ steps.release_tag.outputs.tag }}.bin
asset_content_type: application/octet-stream
- name: Upload esp8266 zip to release
uses: actions/upload-release-asset@v1
env:
@@ -67,6 +97,20 @@ jobs:
asset_path: esp8266.zip
asset_name: ams2mqtt-esp8266-${{ steps.release_tag.outputs.tag }}.zip
asset_content_type: application/zip
- name: Build esp32 firmware
run: pio run -e esp32
- name: Create esp32 zip file
run: /bin/sh scripts/esp32/mkzip.sh
- name: Upload esp32 binary to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: .pio/build/esp32/firmware.bin
asset_name: ams2mqtt-esp32-${{ steps.release_tag.outputs.tag }}.bin
asset_content_type: application/octet-stream
- name: Upload esp32 zip to release
uses: actions/upload-release-asset@v1
env:
@@ -76,6 +120,20 @@ jobs:
asset_path: esp32.zip
asset_name: ams2mqtt-esp32-${{ steps.release_tag.outputs.tag }}.zip
asset_content_type: application/zip
- name: Build esp32s2 firmware
run: pio run -e esp32s2
- name: Create esp32s2 zip file
run: /bin/sh scripts/esp32s2/mkzip.sh
- name: Upload esp32s2 binary to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: .pio/build/esp32s2/firmware.bin
asset_name: ams2mqtt-esp32s2-${{ steps.release_tag.outputs.tag }}.bin
asset_content_type: application/octet-stream
- name: Upload esp32s2 zip to release
uses: actions/upload-release-asset@v1
env:
@@ -86,24 +144,10 @@ jobs:
asset_name: ams2mqtt-esp32s2-${{ steps.release_tag.outputs.tag }}.zip
asset_content_type: application/zip
- name: Upload esp8266 binary to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: .pio/build/esp8266/firmware.bin
asset_name: ams2mqtt-esp8266-${{ steps.release_tag.outputs.tag }}.bin
asset_content_type: application/octet-stream
- name: Upload esp32 binary to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: .pio/build/esp32/firmware.bin
asset_name: ams2mqtt-esp32-${{ steps.release_tag.outputs.tag }}.bin
asset_content_type: application/octet-stream
- name: Build esp32solo firmware
run: pio run -e esp32solo
- name: Create esp32solo zip file
run: /bin/sh scripts/esp32solo/mkzip.sh
- name: Upload esp32solo binary to release
uses: actions/upload-release-asset@v1
env:
@@ -113,12 +157,35 @@ jobs:
asset_path: .pio/build/esp32solo/firmware.bin
asset_name: ams2mqtt-esp32solo-${{ steps.release_tag.outputs.tag }}.bin
asset_content_type: application/octet-stream
- name: Upload esp32s2 binary to release
- name: Upload esp32solo zip to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: .pio/build/esp32s2/firmware.bin
asset_name: ams2mqtt-esp32s2-${{ steps.release_tag.outputs.tag }}.bin
asset_path: esp32solo.zip
asset_name: ams2mqtt-esp32solo-${{ steps.release_tag.outputs.tag }}.zip
asset_content_type: application/zip
- name: Build esp32c3 firmware
run: pio run -e esp32c3
- name: Create esp32c3 zip file
run: /bin/sh scripts/esp32c3/mkzip.sh
- name: Upload esp32c3 binary to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: .pio/build/esp32c3/firmware.bin
asset_name: ams2mqtt-esp32c3-${{ steps.release_tag.outputs.tag }}.bin
asset_content_type: application/octet-stream
- name: Upload esp32c3 zip to release
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: esp32c3.zip
asset_name: ams2mqtt-esp32c3-${{ steps.release_tag.outputs.tag }}.zip
asset_content_type: application/zip

5
.gitignore vendored
View File

@@ -7,7 +7,7 @@
.vscode
.pio
platformio-user.ini
/src/version.h
/lib/AmsConfiguration/include/version.h
/src/web/root
/src/AmsToMqttBridge.ino.cpp
/test
@@ -15,3 +15,6 @@ platformio-user.ini
/sdkconfig
/.tmp
/*.zip
node_modules
/gui/dist
/scripts/*dev

View File

@@ -1,12 +1,12 @@
# AMS MQTT Bridge
# AMS Reader
This code is designed to decode data from electric smart meters installed in many countries in Europe these days. The data is presented in a graphical web interface and can also send the data to a MQTT broker which makes it suitable for home automation project. Originally it was only designed to work with Norwegian meters, but has since been adapter to read any IEC-62056-7-5 or IEC-62056-21 compliant meters.
Later development have added Energy usage graph for both day and month, as well as future energy price (Prices only available for ESP32). The code can run on any ESP8266 or ESP32 hardware which you can read more about in the [WiKi](https://github.com/gskjold/AmsToMqttBridge/wiki). If you don't have the knowledge to set up a ESP device yourself, have a look at the shop at [amsleser.no](https://amsleser.no/).
Later development have added Energy usage graph for both day and month, as well as future energy price. The code can run on any ESP8266 or ESP32 hardware which you can read more about in the [WiKi](https://github.com/UtilitechAS/amsreader-firmware/wiki). If you don't have the knowledge to set up a ESP device yourself, have a look at the shop at [amsleser.no](https://amsleser.no/).
<img src="webui.png">
<img src="images/dashboard.png">
Go to the [WiKi](https://github.com/gskjold/AmsToMqttBridge/wiki) for information on how to get your own device! And find the latest prebuilt firmware file at the [release section](https://github.com/gskjold/AmsToMqttBridge/releases).
Go to the [WiKi](https://github.com/UtilitechAS/amsreader-firmware/wiki) for information on how to get your own device! And find the latest prebuilt firmware file at the [release section](https://github.com/UtilitechAS/amsreader-firmware/releases).
## Building this project with PlatformIO
To build this project, you need [PlatformIO](https://platformio.org/) installed.

View File

@@ -42,4 +42,4 @@ DB
09 06 01 00 47 07 00 FF 12 00 54 02 02 0F FE 16 21
09 06 01 00 0D 07 00 FF 10 03 CF 02 02 0F FD 16 FF // Power factor
09 0C 31 37 38 32 31 30 30 31 35 31 36 35 // Meter ID
01 67
01 67

View File

@@ -43,3 +43,37 @@ FF // Last byte of OBIS in previous block
0600000000 // Accumulated export
8BA4
7E
7E A1 23 CE FF 03 13 21 55 E6 E7 00
0F 00 00 08 E2
0C 07 E5 07 13 01 0C 1A 0A FF 80 00 00
02 0B // 11
01 0B // 11
02 04 12 00 28 09 06 00 08 19 09 00 FF 0F 02 12 00 00
02 04 12 00 28 09 06 00 08 19 09 00 FF 0F 01 12 00 00
02 04 12 00 01 09 06 00 00 60 01 00 FF 0F 02 12 00 00
02 04 12 00 03 09 06 01 00 01 07 00 FF 0F 02 12 00 00
02 04 12 00 03 09 06 01 00 02 07 00 FF 0F 02 12 00 00
02 04 12 00 03 09 06 01 01 01 08 00 FF 0F 02 12 00 00
02 04 12 00 03 09 06 01 01 02 08 00 FF 0F 02 12 00 00
02 04 12 00 03 09 06 01 01 05 08 00 FF 0F 02 12 00 00
02 04 12 00 03 09 06 01 01 06 08 00 FF 0F 02 12 00 00
02 04 12 00 03 09 06 01 01 07 08 00 FF 0F 02 12 00 00
02 04 12 00 03 09 06 01 01 08 08 00 FF 0F 02 12 00 00
09 06 00 08 19 09 00 FF
09 08 34 33 30 39 34 33 35 31
06 00 00 00 0B
06 00 00 00 00
06 00 00 00 10
06 00 00 00 04
06 00 00 00 00
06 00 00 00 08
06 00 00 00 00
06 00 00 00 01
7C 8B
7E

33
frames/lng2.raw Normal file
View File

@@ -0,0 +1,33 @@
7E
A0 76 CE FF 03 13 3C 02 E6 E7 00
DB
08 4C 47 5A 67 72 A9 A1 11
5E 30 00 21 80 F7 FE B8 07 C6
72 B1 90 AE AC 15 D0 AD 95 7B AC 13 7E 67 D8 A2
F0 43 51 3C 63 B6 A1 89 10 AE 9A 7E 55 4A 12 49
B9 6D EB A5 7B 57 03 69 9A BF 16 5E AD 2A 54 41
65 5E 79 C6 95 71 92 46 A2 3F 5B 63 0D 53 96 7D
42 52 1F A3 80 1C 00 E8 E3
A4 B3 9B 86 CB E5 2D 2C CA B0 E2 B7
AE 4D
7E
0f00057e41 // UI Frame header
0c07e60c0c010c232dff800000 // Date & time
020e // Structure with 14 items
1200ec // U1 = 236 V
1200ec // U2 = 236 V
1200ec // U3 = 236 V
120000 // I1 = 0.00 A
12002e // I2 = 0.46 A
12001a // I3 = 0.26 A
060000007d // Active import = 125 W
0600000000 // Active export = 0 W
0601a96ebd // Accumulated import = 27881.149 kWh
0600001dc3 // Accumulated export = 7.619 kWh
120190 // 400 ?
120003 // 3 ?
120120 // 288 ?
09083330313337313831 // Meter ID = 30137181

View File

@@ -1 +1 @@
[See Hardware page in Wiki](https://github.com/gskjold/AmsToMqttBridge/wiki)
[See Hardware page in Wiki](https://github.com/UtilitechAS/amsreader-firmware/wiki)

View File

@@ -0,0 +1,76 @@
{
"board": {
"active_layer": 0,
"active_layer_preset": "",
"auto_track_width": true,
"hidden_nets": [],
"high_contrast_mode": 0,
"net_color_mode": 1,
"opacity": {
"pads": 1.0,
"tracks": 1.0,
"vias": 1.0,
"zones": 0.6
},
"ratsnest_display_mode": 0,
"selection_filter": {
"dimensions": true,
"footprints": true,
"graphics": true,
"keepouts": true,
"lockedItems": true,
"otherItems": true,
"pads": true,
"text": true,
"tracks": true,
"vias": true,
"zones": true
},
"visible_items": [
0,
1,
2,
3,
4,
5,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
32,
33,
34,
35,
36
],
"visible_layers": "fffffff_ffffffff",
"zone_display_mode": 0
},
"meta": {
"filename": "HAN_ESP_TSS721.kicad_prl",
"version": 3
},
"project": {
"files": []
}
}

View File

@@ -0,0 +1,440 @@
{
"board": {
"design_settings": {
"defaults": {
"board_outline_line_width": 0.15,
"copper_line_width": 0.19999999999999998,
"copper_text_italic": false,
"copper_text_size_h": 1.5,
"copper_text_size_v": 1.5,
"copper_text_thickness": 0.3,
"copper_text_upright": false,
"courtyard_line_width": 0.049999999999999996,
"dimension_precision": 4,
"dimension_units": 3,
"dimensions": {
"arrow_length": 1270000,
"extension_offset": 500000,
"keep_text_aligned": true,
"suppress_zeroes": false,
"text_position": 0,
"units_format": 1
},
"fab_line_width": 0.09999999999999999,
"fab_text_italic": false,
"fab_text_size_h": 1.0,
"fab_text_size_v": 1.0,
"fab_text_thickness": 0.15,
"fab_text_upright": false,
"other_line_width": 0.09999999999999999,
"other_text_italic": false,
"other_text_size_h": 1.0,
"other_text_size_v": 1.0,
"other_text_thickness": 0.15,
"other_text_upright": false,
"pads": {
"drill": 0.762,
"height": 1.524,
"width": 1.524
},
"silk_line_width": 0.15,
"silk_text_italic": false,
"silk_text_size_h": 1.0,
"silk_text_size_v": 1.0,
"silk_text_thickness": 0.15,
"silk_text_upright": false,
"zones": {
"45_degree_only": true,
"min_clearance": 0.508
}
},
"diff_pair_dimensions": [],
"drc_exclusions": [],
"meta": {
"filename": "board_design_settings.json",
"version": 2
},
"rule_severities": {
"annular_width": "error",
"clearance": "error",
"copper_edge_clearance": "error",
"courtyards_overlap": "error",
"diff_pair_gap_out_of_range": "error",
"diff_pair_uncoupled_length_too_long": "error",
"drill_out_of_range": "error",
"duplicate_footprints": "warning",
"extra_footprint": "warning",
"footprint_type_mismatch": "error",
"hole_clearance": "error",
"hole_near_hole": "error",
"invalid_outline": "error",
"item_on_disabled_layer": "error",
"items_not_allowed": "error",
"length_out_of_range": "error",
"malformed_courtyard": "error",
"microvia_drill_out_of_range": "error",
"missing_courtyard": "ignore",
"missing_footprint": "warning",
"net_conflict": "warning",
"npth_inside_courtyard": "ignore",
"padstack": "error",
"pth_inside_courtyard": "ignore",
"shorting_items": "error",
"silk_over_copper": "warning",
"silk_overlap": "warning",
"skew_out_of_range": "error",
"through_hole_pad_without_hole": "error",
"too_many_vias": "error",
"track_dangling": "warning",
"track_width": "error",
"tracks_crossing": "error",
"unconnected_items": "error",
"unresolved_variable": "error",
"via_dangling": "warning",
"zone_has_empty_net": "error",
"zones_intersect": "error"
},
"rules": {
"allow_blind_buried_vias": false,
"allow_microvias": false,
"max_error": 0.005,
"min_clearance": 0.0,
"min_copper_edge_clearance": 0.075,
"min_hole_clearance": 0.25,
"min_hole_to_hole": 0.25,
"min_microvia_diameter": 0.19999999999999998,
"min_microvia_drill": 0.09999999999999999,
"min_silk_clearance": 0.0,
"min_through_hole_diameter": 0.3,
"min_track_width": 0.19999999999999998,
"min_via_annular_width": 0.049999999999999996,
"min_via_diameter": 0.39999999999999997,
"use_height_for_length_calcs": true
},
"track_widths": [
0.0,
0.2,
0.4,
0.6,
1.0
],
"via_dimensions": [],
"zones_allow_external_fillets": false,
"zones_use_no_outline": true
},
"layer_presets": []
},
"boards": [],
"cvpcb": {
"equivalence_files": []
},
"erc": {
"erc_exclusions": [],
"meta": {
"version": 0
},
"pin_map": [
[
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
2
],
[
0,
2,
0,
1,
0,
0,
1,
0,
2,
2,
2,
2
],
[
0,
0,
0,
0,
0,
0,
1,
0,
1,
0,
1,
2
],
[
0,
1,
0,
0,
0,
0,
1,
1,
2,
1,
1,
2
],
[
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
2
],
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2
],
[
1,
1,
1,
1,
1,
0,
1,
1,
1,
1,
1,
2
],
[
0,
0,
0,
1,
0,
0,
1,
0,
0,
0,
0,
2
],
[
0,
2,
1,
2,
0,
0,
1,
0,
2,
2,
2,
2
],
[
0,
2,
0,
1,
0,
0,
1,
0,
2,
0,
0,
2
],
[
0,
2,
1,
1,
0,
0,
1,
0,
2,
0,
0,
2
],
[
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2
]
],
"rule_severities": {
"bus_definition_conflict": "error",
"bus_entry_needed": "error",
"bus_label_syntax": "error",
"bus_to_bus_conflict": "error",
"bus_to_net_conflict": "error",
"different_unit_footprint": "error",
"different_unit_net": "error",
"duplicate_reference": "error",
"duplicate_sheet_names": "error",
"extra_units": "error",
"global_label_dangling": "warning",
"hier_label_mismatch": "error",
"label_dangling": "error",
"lib_symbol_issues": "warning",
"multiple_net_names": "warning",
"net_not_bus_member": "warning",
"no_connect_connected": "warning",
"no_connect_dangling": "warning",
"pin_not_connected": "error",
"pin_not_driven": "error",
"pin_to_pin": "warning",
"power_pin_not_driven": "error",
"similar_labels": "warning",
"unannotated": "error",
"unit_value_mismatch": "error",
"unresolved_variable": "error",
"wire_dangling": "error"
}
},
"libraries": {
"pinned_footprint_libs": [],
"pinned_symbol_libs": []
},
"meta": {
"filename": "HAN_ESP_TSS721.kicad_pro",
"version": 1
},
"net_settings": {
"classes": [
{
"bus_width": 12.0,
"clearance": 0.2,
"diff_pair_gap": 0.25,
"diff_pair_via_gap": 0.25,
"diff_pair_width": 0.2,
"line_style": 0,
"microvia_diameter": 0.3,
"microvia_drill": 0.1,
"name": "Default",
"pcb_color": "rgba(0, 0, 0, 0.000)",
"schematic_color": "rgba(0, 0, 0, 0.000)",
"track_width": 0.25,
"via_diameter": 0.6,
"via_drill": 0.4,
"wire_width": 6.0
},
{
"bus_width": 12.0,
"clearance": 0.5,
"diff_pair_gap": 0.25,
"diff_pair_via_gap": 0.25,
"diff_pair_width": 0.2,
"line_style": 0,
"microvia_diameter": 0.5,
"microvia_drill": 0.2,
"name": "PWR",
"nets": [
"+3V3"
],
"pcb_color": "rgba(0, 0, 0, 0.000)",
"schematic_color": "rgba(0, 0, 0, 0.000)",
"track_width": 0.5,
"via_diameter": 0.8,
"via_drill": 0.6,
"wire_width": 6.0
}
],
"meta": {
"version": 2
},
"net_colors": null
},
"pcbnew": {
"last_paths": {
"gencad": "",
"idf": "",
"netlist": "",
"specctra_dsn": "",
"step": "",
"vrml": ""
},
"page_layout_descr_file": ""
},
"schematic": {
"annotate_start_num": 0,
"drawing": {
"default_line_thickness": 6.0,
"default_text_size": 50.0,
"field_names": [],
"intersheets_ref_own_page": false,
"intersheets_ref_prefix": "",
"intersheets_ref_short": false,
"intersheets_ref_show": false,
"intersheets_ref_suffix": "",
"junction_size_choice": 3,
"label_size_ratio": 0.25,
"pin_symbol_size": 0.0,
"text_offset_ratio": 0.08
},
"legacy_lib_dir": "",
"legacy_lib_list": [],
"meta": {
"version": 1
},
"net_format_name": "",
"ngspice": {
"fix_include_paths": true,
"fix_passive_vals": false,
"meta": {
"version": 0
},
"model_mode": 0,
"workbook_filename": ""
},
"page_layout_descr_file": "",
"plot_directory": "",
"spice_adjust_passive_values": false,
"spice_external_command": "spice \"%I\"",
"subpart_first_id": 65,
"subpart_id_separator": 0
},
"sheets": [],
"text_variables": {}
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,3 @@
EESchema-DOCLIB Version 2.0
#
#End Doc Library

View File

@@ -1,6 +1,21 @@
EESchema-LIBRARY Version 2.4
#encoding utf-8
#
# +3.3V-power
#
DEF +3.3V-power #PWR 0 0 Y Y 1 F P
F0 "#PWR" 0 -150 50 H I C CNN
F1 "+3.3V-power" 0 140 50 H V C CNN
F2 "" 0 0 50 H I C CNN
F3 "" 0 0 50 H I C CNN
DRAW
P 2 0 1 0 -30 50 0 100 N
P 2 0 1 0 0 0 0 100 N
P 2 0 1 0 0 100 30 50 N
X +3V3 1 0 0 0 U 50 50 1 1 W N
ENDDRAW
ENDDEF
#
# CONN_01X08
#
DEF CONN_01X08 P 0 40 Y N 1 F N
@@ -35,4 +50,23 @@ X P8 8 -200 -350 150 R 50 50 1 1 P
ENDDRAW
ENDDEF
#
# Jumper-Device
#
DEF Jumper-Device JP 0 30 Y N 1 F N
F0 "JP" 0 150 50 H V C CNN
F1 "Jumper-Device" 0 -80 50 H V C CNN
F2 "" 0 0 50 H I C CNN
F3 "" 0 0 50 H I C CNN
$FPLIST
SolderJumper*
$ENDFPLIST
DRAW
C -100 0 35 0 1 0 N
A 0 -26 125 375 1422 0 1 0 N 99 50 -98 50
C 100 0 35 0 1 0 N
X 1 1 -300 0 165 R 50 50 0 1 P
X 2 2 300 0 165 L 50 50 0 1 P
ENDDRAW
ENDDEF
#
#End Library

View File

@@ -0,0 +1,75 @@
{
"board": {
"active_layer": 0,
"active_layer_preset": "",
"auto_track_width": true,
"hidden_nets": [],
"high_contrast_mode": 0,
"net_color_mode": 1,
"opacity": {
"pads": 1.0,
"tracks": 1.0,
"vias": 1.0,
"zones": 0.6
},
"ratsnest_display_mode": 0,
"selection_filter": {
"dimensions": true,
"footprints": true,
"graphics": true,
"keepouts": true,
"lockedItems": true,
"otherItems": true,
"pads": true,
"text": true,
"tracks": true,
"vias": true,
"zones": true
},
"visible_items": [
0,
1,
2,
3,
4,
5,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
32,
33,
34,
35,
36
],
"visible_layers": "fffffff_ffffffff",
"zone_display_mode": 0
},
"meta": {
"filename": "d1_mini_shield.kicad_prl",
"version": 3
},
"project": {
"files": []
}
}

View File

@@ -0,0 +1,356 @@
{
"board": {
"design_settings": {
"defaults": {
"board_outline_line_width": 0.15,
"copper_line_width": 0.2,
"copper_text_italic": false,
"copper_text_size_h": 1.5,
"copper_text_size_v": 1.5,
"copper_text_thickness": 0.3,
"copper_text_upright": true,
"courtyard_line_width": 0.05,
"other_line_width": 0.15,
"other_text_italic": false,
"other_text_size_h": 1.0,
"other_text_size_v": 1.0,
"other_text_thickness": 0.15,
"other_text_upright": true,
"silk_line_width": 0.15,
"silk_text_italic": false,
"silk_text_size_h": 1.0,
"silk_text_size_v": 1.0,
"silk_text_thickness": 0.15,
"silk_text_upright": true
},
"diff_pair_dimensions": [
{
"gap": 0.25,
"via_gap": 0.25,
"width": 0.2
}
],
"drc_exclusions": [],
"rule_severitieslegacy_courtyards_overlap": true,
"rule_severitieslegacy_no_courtyard_defined": false,
"rules": {
"allow_blind_buried_vias": false,
"allow_microvias": false,
"min_hole_to_hole": 0.25,
"min_microvia_diameter": 0.2,
"min_microvia_drill": 0.09999999999999999,
"min_through_hole_diameter": 0.3,
"min_track_width": 0.2,
"min_via_diameter": 0.4,
"solder_mask_clearance": 0.2,
"solder_mask_min_width": 0.0,
"solder_paste_clearance": 0.0,
"solder_paste_margin_ratio": -0.0
},
"track_widths": [
0.25,
0.5
],
"via_dimensions": [
{
"diameter": 0.6,
"drill": 0.4
}
]
},
"layer_presets": []
},
"boards": [],
"cvpcb": {
"equivalence_files": []
},
"erc": {
"erc_exclusions": [],
"meta": {
"version": 0
},
"pin_map": [
[
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
2
],
[
0,
2,
0,
1,
0,
0,
1,
0,
2,
2,
2,
2
],
[
0,
0,
0,
0,
0,
0,
1,
0,
1,
0,
1,
2
],
[
0,
1,
0,
0,
0,
0,
1,
1,
2,
1,
1,
2
],
[
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
2
],
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2
],
[
1,
1,
1,
1,
1,
0,
1,
1,
1,
1,
1,
2
],
[
0,
0,
0,
1,
0,
0,
1,
0,
0,
0,
0,
2
],
[
0,
2,
1,
2,
0,
0,
1,
0,
2,
2,
2,
2
],
[
0,
2,
0,
1,
0,
0,
1,
0,
2,
0,
0,
2
],
[
0,
2,
1,
1,
0,
0,
1,
0,
2,
0,
0,
2
],
[
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2
]
],
"rule_severities": {
"bus_definition_conflict": "error",
"bus_entry_needed": "error",
"bus_label_syntax": "error",
"bus_to_bus_conflict": "error",
"bus_to_net_conflict": "error",
"different_unit_footprint": "error",
"different_unit_net": "error",
"duplicate_reference": "error",
"duplicate_sheet_names": "error",
"extra_units": "error",
"global_label_dangling": "warning",
"hier_label_mismatch": "error",
"label_dangling": "error",
"lib_symbol_issues": "warning",
"multiple_net_names": "warning",
"net_not_bus_member": "warning",
"no_connect_connected": "warning",
"no_connect_dangling": "warning",
"pin_not_connected": "error",
"pin_not_driven": "error",
"pin_to_pin": "warning",
"power_pin_not_driven": "error",
"similar_labels": "warning",
"unannotated": "error",
"unit_value_mismatch": "error",
"unresolved_variable": "error",
"wire_dangling": "error"
}
},
"libraries": {
"pinned_footprint_libs": [],
"pinned_symbol_libs": []
},
"meta": {
"filename": "d1_mini_shield.kicad_pro",
"version": 1
},
"net_settings": {
"classes": [
{
"bus_width": 12.0,
"clearance": 0.2,
"diff_pair_gap": 0.25,
"diff_pair_via_gap": 0.25,
"diff_pair_width": 0.2,
"line_style": 0,
"microvia_diameter": 0.3,
"microvia_drill": 0.1,
"name": "Default",
"pcb_color": "rgba(0, 0, 0, 0.000)",
"schematic_color": "rgba(0, 0, 0, 0.000)",
"track_width": 0.25,
"via_diameter": 0.8,
"via_drill": 0.4,
"wire_width": 6.0
}
],
"meta": {
"version": 2
},
"net_colors": null
},
"pcbnew": {
"last_paths": {
"gencad": "",
"idf": "",
"netlist": "d1_mini_shield.net",
"specctra_dsn": "",
"step": "",
"vrml": ""
},
"page_layout_descr_file": ""
},
"schematic": {
"annotate_start_num": 0,
"drawing": {
"default_line_thickness": 6.0,
"default_text_size": 50.0,
"field_names": [],
"intersheets_ref_own_page": false,
"intersheets_ref_prefix": "",
"intersheets_ref_short": false,
"intersheets_ref_show": false,
"intersheets_ref_suffix": "",
"junction_size_choice": 3,
"label_size_ratio": 0.25,
"pin_symbol_size": 0.0,
"text_offset_ratio": 0.08
},
"legacy_lib_dir": "",
"legacy_lib_list": [],
"meta": {
"version": 1
},
"net_format_name": "Pcbnew",
"ngspice": {
"fix_include_paths": true,
"fix_passive_vals": false,
"meta": {
"version": 0
},
"model_mode": 0,
"workbook_filename": ""
},
"page_layout_descr_file": "",
"plot_directory": "",
"spice_adjust_passive_values": false,
"spice_external_command": "spice \"%I\"",
"subpart_first_id": 65,
"subpart_id_separator": 0
},
"sheets": [],
"text_variables": {}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 136 KiB

After

Width:  |  Height:  |  Size: 173 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View File

@@ -4,12 +4,14 @@
#include "Arduino.h"
#define EEPROM_SIZE 1024*3
#define EEPROM_CHECK_SUM 95 // Used to check if config is stored. Change if structure changes
#define EEPROM_CHECK_SUM 103 // Used to check if config is stored. Change if structure changes
#define EEPROM_CLEARED_INDICATOR 0xFC
#define EEPROM_CONFIG_ADDRESS 0
#define EEPROM_TEMP_CONFIG_ADDRESS 2048
#define CONFIG_SYSTEM_START 8
#define CONFIG_METER_START 32
#define CONFIG_UI_START 248
#define CONFIG_GPIO_START 266
#define CONFIG_ENTSOE_START 290
#define CONFIG_WIFI_START 360
@@ -19,29 +21,18 @@
#define CONFIG_DOMOTICZ_START 856
#define CONFIG_NTP_START 872
#define CONFIG_MQTT_START 1004
#define CONFIG_HA_START 1680
#define CONFIG_MQTT_START_86 224
#define CONFIG_METER_START_87 784
#define CONFIG_ENTSOE_START_90 286
#define CONFIG_WIFI_START_91 16
#define CONFIG_METER_START_93 224
struct SystemConfig {
uint8_t boardType;
}; // 1
struct WiFiConfig91 {
char ssid[32];
char psk[64];
char ip[15];
char gateway[15];
char subnet[15];
char dns1[15];
char dns2[15];
char hostname[32];
bool mdns;
}; // 204
bool vendorConfigured;
bool userConfigured;
uint8_t dataCollectionConsent; // 0 = unknown, 1 = accepted, 2 = declined
char country[3];
}; // 7
struct WiFiConfig {
char ssid[32];
@@ -54,19 +45,10 @@ struct WiFiConfig {
char hostname[32];
bool mdns;
uint8_t power;
}; // 210
struct MqttConfig86 {
char host[128];
uint16_t port;
char clientId[32];
char publishTopic[64];
char subscribeTopic[64];
char username[64];
char password[64];
uint8_t payloadFormat;
bool ssl;
}; // 420
uint8_t sleep;
uint8_t mode;
bool autoreboot;
}; // 213
struct MqttConfig {
char host[128];
@@ -87,6 +69,40 @@ struct WebConfig {
}; // 129
struct MeterConfig {
uint32_t baud;
uint8_t parity;
bool invert;
uint8_t distributionSystem;
uint16_t mainFuse;
uint16_t productionCapacity;
uint8_t encryptionKey[16];
uint8_t authenticationKey[16];
uint32_t wattageMultiplier;
uint32_t voltageMultiplier;
uint32_t amperageMultiplier;
uint32_t accumulatedMultiplier;
uint8_t source;
uint8_t parser;
}; // 61
struct MeterConfig100 {
uint32_t baud;
uint8_t parity;
bool invert;
uint8_t distributionSystem;
uint8_t mainFuse;
uint8_t productionCapacity;
uint8_t encryptionKey[16];
uint8_t authenticationKey[16];
uint32_t wattageMultiplier;
uint32_t voltageMultiplier;
uint32_t amperageMultiplier;
uint32_t accumulatedMultiplier;
uint8_t source;
uint8_t parser;
}; // 59
struct MeterConfig95 {
uint32_t baud;
uint8_t parity;
bool invert;
@@ -103,16 +119,6 @@ struct MeterConfig {
uint8_t parser;
}; // 50
struct MeterConfig87 {
uint8_t type;
uint8_t distributionSystem;
uint8_t mainFuse;
uint8_t productionCapacity;
uint8_t encryptionKey[16];
uint8_t authenticationKey[16];
bool substituteMissing;
}; // 37
struct DebugConfig {
bool telnet;
bool serial;
@@ -136,7 +142,8 @@ struct GpioConfig {
uint8_t vccBootLimit;
uint16_t vccResistorGnd;
uint16_t vccResistorVcc;
}; // 20
bool hanPinPullup;
}; // 21
struct DomoticzConfig {
uint16_t elidx;
@@ -146,7 +153,20 @@ struct DomoticzConfig {
uint16_t cl1idx;
}; // 10
struct HomeAssistantConfig {
char discoveryPrefix[64];
char discoveryHostname[64];
char discoveryNameTag[16];
}; // 145
struct NtpConfig {
bool enable;
bool dhcp;
char server[64];
char timezone[32];
}; // 98
struct NtpConfig96 {
bool enable;
bool dhcp;
int16_t offset;
@@ -159,13 +179,34 @@ struct EntsoeConfig {
char area[17];
char currency[4];
uint32_t multiplier;
}; // 62
bool enabled;
uint16_t fixedPrice;
}; // 64
struct EnergyAccountingConfig {
uint16_t thresholds[10];
uint8_t hours;
}; // 21
struct EnergyAccountingConfig101 {
uint8_t thresholds[10];
uint8_t hours;
}; // 11
struct UiConfig {
uint8_t showImport;
uint8_t showExport;
uint8_t showVoltage;
uint8_t showAmperage;
uint8_t showReactive;
uint8_t showRealtime;
uint8_t showPeaks;
uint8_t showPricePlot;
uint8_t showDayPlot;
uint8_t showMonthPlot;
uint8_t showTemperaturePlot;
}; // 11
struct TempSensorConfig {
uint8_t address[8];
char name[16];
@@ -221,8 +262,10 @@ public:
bool getDomoticzConfig(DomoticzConfig&);
bool setDomoticzConfig(DomoticzConfig&);
void clearDomo(DomoticzConfig&);
bool isDomoChanged();
void ackDomoChange();
bool getHomeAssistantConfig(HomeAssistantConfig&);
bool setHomeAssistantConfig(HomeAssistantConfig&);
void clearHomeAssistantConfig(HomeAssistantConfig&);
bool getNtpConfig(NtpConfig&);
bool setNtpConfig(NtpConfig&);
@@ -242,6 +285,10 @@ public:
bool isEnergyAccountingChanged();
void ackEnergyAccountingChange();
bool getUiConfig(UiConfig&);
bool setUiConfig(UiConfig&);
void clearUiConfig(UiConfig&);
void loadTempSensors();
void saveTempSensors();
uint8_t getTempSensorCount();
@@ -257,21 +304,22 @@ protected:
private:
uint8_t configVersion = 0;
bool wifiChanged, mqttChanged, meterChanged = true, domoChanged, ntpChanged = true, entsoeChanged = false, energyAccountingChanged = true;
bool wifiChanged, mqttChanged, meterChanged = true, ntpChanged = true, entsoeChanged = false, energyAccountingChanged = true;
uint8_t tempSensorCount = 0;
TempSensorConfig** tempSensors = NULL;
bool relocateConfig86(); // 1.5.0
bool relocateConfig87(); // 1.5.4
bool relocateConfig90(); // 2.0.0
bool relocateConfig91(); // 2.0.2
bool relocateConfig92(); // 2.0.3
bool relocateConfig93(); // 2.1.0
bool relocateConfig94(); // 2.1.4
bool relocateConfig94(); // 2.1.0
bool relocateConfig95(); // 2.1.4
bool relocateConfig96(); // 2.1.14
bool relocateConfig100(); // 2.2-dev
bool relocateConfig101(); // 2.2.0 through 2.2.8
bool relocateConfig102(); // 2.2.9 through 2.2.11
void saveToFs();
bool loadFromFs(uint8_t version);
void deleteFromFs(uint8_t version);
};
#endif

View File

@@ -0,0 +1,86 @@
#include <Timezone.h>
#define JULY1970 15634800
TimeChangeRule TC_GMT = {"GMT", Last, Sun, Jan, 0, 0};
TimeChangeRule TC_WET = {"WET", Last, Sun, Oct, 2, 0};
TimeChangeRule TC_WEST = {"WEST", Last, Sun, Mar, 1, 60};
TimeChangeRule TC_CET = {"CET", Last, Sun, Oct, 3, 60};
TimeChangeRule TC_CEST = {"CEST", Last, Sun, Mar, 2, 120};
TimeChangeRule TC_EET = {"EET", Last, Sun, Oct, 4, 120};
TimeChangeRule TC_EEST = {"EEST", Last, Sun, Mar, 3, 180};
Timezone GMT = Timezone(TC_GMT);
Timezone WesterEuropean = Timezone(TC_WET, TC_WEST);
Timezone CentralEuropean = Timezone(TC_CET, TC_CEST);
Timezone EasternEuropean = Timezone(TC_EET, TC_EEST);
Timezone* resolveTimezone(char* name) {
if(strncmp_P(name, PSTR("Europe/"), 7) == 0) {
if(strncmp_P(name+7, PSTR("Amsterdam"), 9) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Athens"), 6) == 0)
return &EasternEuropean;
if(strncmp_P(name+7, PSTR("Belfast"), 7) == 0)
return &WesterEuropean;
if(strncmp_P(name+7, PSTR("Berlin"), 6) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Bratislava"), 10) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Brussels"), 8) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Bucharest"), 9) == 0)
return &EasternEuropean;
if(strncmp_P(name+7, PSTR("Budapest"), 8) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Copenhagen"), 10) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Dublin"), 6) == 0)
return &WesterEuropean;
if(strncmp_P(name+7, PSTR("Helsinki"), 8) == 0)
return &EasternEuropean;
if(strncmp_P(name+7, PSTR("Lisbon"), 6) == 0)
return &WesterEuropean;
if(strncmp_P(name+7, PSTR("Ljubljana"), 9) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("London"), 6) == 0)
return &WesterEuropean;
if(strncmp_P(name+7, PSTR("Luxembourg"), 10) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Madrid"), 6) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Malta"), 5) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Nicosia"), 7) == 0)
return &EasternEuropean;
if(strncmp_P(name+7, PSTR("Oslo"), 4) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Paris"), 5) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Podgorica"), 9) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Prague"), 6) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Riga"), 4) == 0)
return &EasternEuropean;
if(strncmp_P(name+7, PSTR("Rome"), 4) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Sofia"), 5) == 0)
return &EasternEuropean;
if(strncmp_P(name+7, PSTR("Stockholm"), 9) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Tallinn"), 7) == 0)
return &EasternEuropean;
if(strncmp_P(name+7, PSTR("Vienna"), 6) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Vilnius"), 7) == 0)
return &EasternEuropean;
if(strncmp_P(name+7, PSTR("Warsaw"), 6) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Zagreb"), 6) == 0)
return &CentralEuropean;
if(strncmp_P(name+7, PSTR("Zurich"), 6) == 0)
return &CentralEuropean;
}
return &GMT;
}

View File

@@ -7,5 +7,6 @@
String toHex(uint8_t* in);
String toHex(uint8_t* in, uint16_t size);
void fromHex(uint8_t *out, String in, uint16_t size);
void stripNonAscii(uint8_t* in, uint16_t size, bool extended = false);
#endif

View File

@@ -1,18 +1,26 @@
#include "AmsConfiguration.h"
#include "hexutils.h"
bool AmsConfiguration::getSystemConfig(SystemConfig& config) {
if(hasConfig()) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.begin(EEPROM_SIZE);
uint8_t configVersion = EEPROM.read(EEPROM_CONFIG_ADDRESS);
if(configVersion == EEPROM_CHECK_SUM || configVersion == EEPROM_CLEARED_INDICATOR) {
EEPROM.get(CONFIG_SYSTEM_START, config);
EEPROM.end();
return true;
} else {
config.boardType = 0xFF;
config.vendorConfigured = false;
config.userConfigured = false;
config.dataCollectionConsent = 0;
strcpy(config.country, "");
return false;
}
}
bool AmsConfiguration::setSystemConfig(SystemConfig& config) {
EEPROM.begin(EEPROM_SIZE);
stripNonAscii((uint8_t*) config.country, 2);
EEPROM.put(CONFIG_SYSTEM_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
@@ -24,6 +32,7 @@ bool AmsConfiguration::getWiFiConfig(WiFiConfig& config) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_WIFI_START, config);
EEPROM.end();
if(config.sleep > 2) config.sleep = 1;
return true;
} else {
clearWifi(config);
@@ -33,6 +42,7 @@ bool AmsConfiguration::getWiFiConfig(WiFiConfig& config) {
bool AmsConfiguration::setWiFiConfig(WiFiConfig& config) {
WiFiConfig existing;
if(config.sleep > 2) config.sleep = 1;
if(getWiFiConfig(existing)) {
wifiChanged |= strcmp(config.ssid, existing.ssid) != 0;
wifiChanged |= strcmp(config.psk, existing.psk) != 0;
@@ -45,9 +55,22 @@ bool AmsConfiguration::setWiFiConfig(WiFiConfig& config) {
}
wifiChanged |= strcmp(config.hostname, existing.hostname) != 0;
wifiChanged |= config.power != existing.power;
wifiChanged |= config.sleep != existing.sleep;
wifiChanged |= config.mode != existing.mode;
wifiChanged |= config.autoreboot != existing.autoreboot;
} else {
wifiChanged = true;
}
stripNonAscii((uint8_t*) config.ssid, 32, true);
stripNonAscii((uint8_t*) config.psk, 64);
stripNonAscii((uint8_t*) config.ip, 16);
stripNonAscii((uint8_t*) config.gateway, 16);
stripNonAscii((uint8_t*) config.subnet, 16);
stripNonAscii((uint8_t*) config.dns1, 16);
stripNonAscii((uint8_t*) config.dns2, 16);
stripNonAscii((uint8_t*) config.hostname, 32);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_WIFI_START, config);
bool ret = EEPROM.commit();
@@ -63,11 +86,14 @@ void AmsConfiguration::clearWifi(WiFiConfig& config) {
uint16_t chipId;
#if defined(ESP32)
chipId = ESP.getEfuseMac();
config.power = 195;
#else
chipId = ESP.getChipId();
config.power = 205;
#endif
strcpy(config.hostname, (String("ams-") + String(chipId, HEX)).c_str());
config.mdns = true;
config.sleep = 0xFF;
}
void AmsConfiguration::clearWifiIp(WiFiConfig& config) {
@@ -113,6 +139,14 @@ bool AmsConfiguration::setMqttConfig(MqttConfig& config) {
} else {
mqttChanged = true;
}
stripNonAscii((uint8_t*) config.host, 128);
stripNonAscii((uint8_t*) config.clientId, 32);
stripNonAscii((uint8_t*) config.publishTopic, 64);
stripNonAscii((uint8_t*) config.subscribeTopic, 64);
stripNonAscii((uint8_t*) config.username, 128);
stripNonAscii((uint8_t*) config.password, 256);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_MQTT_START, config);
bool ret = EEPROM.commit();
@@ -157,6 +191,10 @@ bool AmsConfiguration::getWebConfig(WebConfig& config) {
}
bool AmsConfiguration::setWebConfig(WebConfig& config) {
stripNonAscii((uint8_t*) config.username, 64);
stripNonAscii((uint8_t*) config.password, 64);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_WEB_START, config);
bool ret = EEPROM.commit();
@@ -204,11 +242,11 @@ bool AmsConfiguration::setMeterConfig(MeterConfig& config) {
}
void AmsConfiguration::clearMeter(MeterConfig& config) {
config.baud = 2400;
config.parity = 11; // 8E1
config.baud = 0;
config.parity = 0;
config.invert = false;
config.distributionSystem = 0;
config.mainFuse = 0;
config.distributionSystem = 2;
config.mainFuse = 40;
config.productionCapacity = 0;
memset(config.encryptionKey, 0, 16);
memset(config.authenticationKey, 0, 16);
@@ -216,6 +254,8 @@ void AmsConfiguration::clearMeter(MeterConfig& config) {
config.voltageMultiplier = 0;
config.amperageMultiplier = 0;
config.accumulatedMultiplier = 0;
config.source = 1; // Serial
config.parser = 0; // Auto
}
bool AmsConfiguration::isMeterChanged() {
@@ -269,15 +309,14 @@ bool AmsConfiguration::getDomoticzConfig(DomoticzConfig& config) {
bool AmsConfiguration::setDomoticzConfig(DomoticzConfig& config) {
DomoticzConfig existing;
if(getDomoticzConfig(existing)) {
domoChanged |= config.elidx != existing.elidx;
domoChanged |= config.vl1idx != existing.vl1idx;
domoChanged |= config.vl2idx != existing.vl2idx;
domoChanged |= config.vl3idx != existing.vl3idx;
domoChanged |= config.cl1idx != existing.cl1idx;
mqttChanged |= config.elidx != existing.elidx;
mqttChanged |= config.vl1idx != existing.vl1idx;
mqttChanged |= config.vl2idx != existing.vl2idx;
mqttChanged |= config.vl3idx != existing.vl3idx;
mqttChanged |= config.cl1idx != existing.cl1idx;
} else {
domoChanged = true;
mqttChanged = true;
}
mqttChanged = domoChanged;
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_DOMOTICZ_START, config);
bool ret = EEPROM.commit();
@@ -293,12 +332,43 @@ void AmsConfiguration::clearDomo(DomoticzConfig& config) {
config.cl1idx = 0;
}
bool AmsConfiguration::isDomoChanged() {
return domoChanged;
bool AmsConfiguration::getHomeAssistantConfig(HomeAssistantConfig& config) {
if(hasConfig()) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_HA_START, config);
EEPROM.end();
return true;
} else {
clearHomeAssistantConfig(config);
return false;
}
}
void AmsConfiguration::ackDomoChange() {
domoChanged = false;
bool AmsConfiguration::setHomeAssistantConfig(HomeAssistantConfig& config) {
HomeAssistantConfig existing;
if(getHomeAssistantConfig(existing)) {
mqttChanged |= strcmp(config.discoveryPrefix, existing.discoveryPrefix) != 0;
mqttChanged |= strcmp(config.discoveryHostname, existing.discoveryHostname) != 0;
mqttChanged |= strcmp(config.discoveryNameTag, existing.discoveryNameTag) != 0;
} else {
mqttChanged = true;
}
stripNonAscii((uint8_t*) config.discoveryPrefix, 64);
stripNonAscii((uint8_t*) config.discoveryHostname, 64);
stripNonAscii((uint8_t*) config.discoveryNameTag, 16);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_HA_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
void AmsConfiguration::clearHomeAssistantConfig(HomeAssistantConfig& config) {
strcpy(config.discoveryPrefix, "");
strcpy(config.discoveryHostname, "");
strcpy(config.discoveryNameTag, "");
}
bool AmsConfiguration::pinUsed(uint8_t pin, GpioConfig& config) {
@@ -318,8 +388,9 @@ bool AmsConfiguration::pinUsed(uint8_t pin, GpioConfig& config) {
}
bool AmsConfiguration::getGpioConfig(GpioConfig& config) {
if(hasConfig()) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.begin(EEPROM_SIZE);
uint8_t configVersion = EEPROM.read(EEPROM_CONFIG_ADDRESS);
if(configVersion == EEPROM_CHECK_SUM || configVersion == EEPROM_CLEARED_INDICATOR) {
EEPROM.get(CONFIG_GPIO_START, config);
EEPROM.end();
return true;
@@ -333,6 +404,7 @@ bool AmsConfiguration::setGpioConfig(GpioConfig& config) {
GpioConfig existing;
if(getGpioConfig(existing)) {
meterChanged |= config.hanPin != existing.hanPin;
meterChanged |= config.hanPinPullup != existing.hanPinPullup;
}
/* This currently does not work, as it checks its own pin
if(pinUsed(config.hanPin, config)) {
@@ -384,6 +456,7 @@ bool AmsConfiguration::setGpioConfig(GpioConfig& config) {
void AmsConfiguration::clearGpio(GpioConfig& config) {
config.hanPin = 3;
config.hanPinPullup = true;
config.apPin = 0xFF;
config.ledPin = 0xFF;
config.ledInverted = true;
@@ -424,12 +497,15 @@ bool AmsConfiguration::setNtpConfig(NtpConfig& config) {
}
}
ntpChanged |= config.dhcp != existing.dhcp;
ntpChanged |= config.offset != existing.offset;
ntpChanged |= config.summerOffset != existing.summerOffset;
ntpChanged |= strcmp(config.server, existing.server) != 0;
ntpChanged |= strcmp(config.timezone, existing.timezone) != 0;
} else {
ntpChanged = true;
}
stripNonAscii((uint8_t*) config.server, 64);
stripNonAscii((uint8_t*) config.timezone, 32);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_NTP_START, config);
bool ret = EEPROM.commit();
@@ -448,9 +524,8 @@ void AmsConfiguration::ackNtpChange() {
void AmsConfiguration::clearNtp(NtpConfig& config) {
config.enable = true;
config.dhcp = true;
config.offset = 360;
config.summerOffset = 360;
strcpy(config.server, "pool.ntp.org");
strcpy(config.timezone, "Europe/Oslo");
}
bool AmsConfiguration::getEntsoeConfig(EntsoeConfig& config) {
@@ -463,6 +538,7 @@ bool AmsConfiguration::getEntsoeConfig(EntsoeConfig& config) {
}
return true;
} else {
clearEntsoe(config);
return false;
}
}
@@ -474,9 +550,16 @@ bool AmsConfiguration::setEntsoeConfig(EntsoeConfig& config) {
entsoeChanged |= strcmp(config.area, existing.area) != 0;
entsoeChanged |= strcmp(config.currency, existing.currency) != 0;
entsoeChanged |= config.multiplier != existing.multiplier;
entsoeChanged |= config.enabled != existing.enabled;
entsoeChanged |= config.fixedPrice != existing.fixedPrice;
} else {
entsoeChanged = true;
}
stripNonAscii((uint8_t*) config.token, 37);
stripNonAscii((uint8_t*) config.area, 17);
stripNonAscii((uint8_t*) config.currency, 4);
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_ENTSOE_START, config);
bool ret = EEPROM.commit();
@@ -489,6 +572,7 @@ void AmsConfiguration::clearEntsoe(EntsoeConfig& config) {
strcpy(config.area, "");
strcpy(config.currency, "");
config.multiplier = 1000;
config.enabled = false;
}
bool AmsConfiguration::isEntsoeChanged() {
@@ -505,16 +589,19 @@ bool AmsConfiguration::getEnergyAccountingConfig(EnergyAccountingConfig& config)
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_ENERGYACCOUNTING_START, config);
EEPROM.end();
if(config.thresholds[9] != 255) {
if(config.thresholds[9] != 0xFFFF) {
clearEnergyAccountingConfig(config);
}
if(config.hours > 5) config.hours = 5;
return true;
} else {
clearEnergyAccountingConfig(config);
return false;
}
}
bool AmsConfiguration::setEnergyAccountingConfig(EnergyAccountingConfig& config) {
if(config.hours > 5) config.hours = 5;
EnergyAccountingConfig existing;
if(getEnergyAccountingConfig(existing)) {
for(int i = 0; i < 9; i++) {
@@ -522,7 +609,8 @@ bool AmsConfiguration::setEnergyAccountingConfig(EnergyAccountingConfig& config)
energyAccountingChanged = true;
}
}
config.thresholds[9] = 255;
config.thresholds[9] = 0xFFFF;
energyAccountingChanged |= config.hours != existing.hours;
} else {
energyAccountingChanged = true;
}
@@ -531,7 +619,6 @@ bool AmsConfiguration::setEnergyAccountingConfig(EnergyAccountingConfig& config)
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
void AmsConfiguration::clearEnergyAccountingConfig(EnergyAccountingConfig& config) {
@@ -544,7 +631,8 @@ void AmsConfiguration::clearEnergyAccountingConfig(EnergyAccountingConfig& confi
config.thresholds[6] = 75;
config.thresholds[7] = 100;
config.thresholds[8] = 150;
config.thresholds[9] = 255;
config.thresholds[9] = 0xFFFF;
config.hours = 3;
}
bool AmsConfiguration::isEnergyAccountingChanged() {
@@ -555,9 +643,53 @@ void AmsConfiguration::ackEnergyAccountingChange() {
energyAccountingChanged = false;
}
bool AmsConfiguration::getUiConfig(UiConfig& config) {
if(hasConfig()) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_UI_START, config);
if(config.showImport > 2) clearUiConfig(config); // Must be wrong
EEPROM.end();
return true;
} else {
clearUiConfig(config);
return false;
}
}
bool AmsConfiguration::setUiConfig(UiConfig& config) {
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(CONFIG_UI_START, config);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
void AmsConfiguration::clearUiConfig(UiConfig& config) {
// 1 = Always, 2 = If value present, 0 = Hidden
config.showImport = 1;
config.showExport = 2;
config.showVoltage = 2;
config.showAmperage = 2;
config.showReactive = 0;
config.showRealtime = 1;
config.showPeaks = 2;
config.showPricePlot = 2;
config.showDayPlot = 1;
config.showMonthPlot = 1;
config.showTemperaturePlot = 2;
}
void AmsConfiguration::clear() {
EEPROM.begin(EEPROM_SIZE);
SystemConfig sys;
EEPROM.get(CONFIG_SYSTEM_START, sys);
sys.userConfigured = false;
sys.dataCollectionConsent = 0;
strcpy(sys.country, "");
EEPROM.put(CONFIG_SYSTEM_START, sys);
MeterConfig meter;
clearMeter(meter);
EEPROM.put(CONFIG_METER_START, meter);
@@ -590,7 +722,15 @@ void AmsConfiguration::clear() {
clearEnergyAccountingConfig(eac);
EEPROM.put(CONFIG_ENERGYACCOUNTING_START, eac);
EEPROM.put(EEPROM_CONFIG_ADDRESS, -1);
DebugConfig debug;
clearDebug(debug);
EEPROM.put(CONFIG_DEBUG_START, debug);
UiConfig ui;
clearUiConfig(ui);
EEPROM.put(CONFIG_UI_START, ui);
EEPROM.put(EEPROM_CONFIG_ADDRESS, EEPROM_CLEARED_INDICATOR);
EEPROM.commit();
EEPROM.end();
}
@@ -609,46 +749,6 @@ bool AmsConfiguration::hasConfig() {
}
} else {
switch(configVersion) {
case 86:
configVersion = -1; // Prevent loop
if(relocateConfig86()) {
configVersion = 87;
} else {
configVersion = 0;
return false;
}
case 87:
configVersion = -1; // Prevent loop
if(relocateConfig87()) {
configVersion = 88;
} else {
configVersion = 0;
return false;
}
case 90:
configVersion = -1; // Prevent loop
if(relocateConfig90()) {
configVersion = 91;
} else {
configVersion = 0;
return false;
}
case 91:
configVersion = -1; // Prevent loop
if(relocateConfig91()) {
configVersion = 92;
} else {
configVersion = 0;
return false;
}
case 92:
configVersion = -1; // Prevent loop
if(relocateConfig92()) {
configVersion = 93;
} else {
configVersion = 0;
return false;
}
case 93:
configVersion = -1; // Prevent loop
if(relocateConfig93()) {
@@ -665,6 +765,46 @@ bool AmsConfiguration::hasConfig() {
configVersion = 0;
return false;
}
case 95:
configVersion = -1; // Prevent loop
if(relocateConfig95()) {
configVersion = 96;
} else {
configVersion = 0;
return false;
}
case 96:
configVersion = -1; // Prevent loop
if(relocateConfig96()) {
configVersion = 100;
} else {
configVersion = 0;
return false;
}
case 100:
configVersion = -1; // Prevent loop
if(relocateConfig100()) {
configVersion = 101;
} else {
configVersion = 0;
return false;
}
case 101:
configVersion = -1; // Prevent loop
if(relocateConfig101()) {
configVersion = 102;
} else {
configVersion = 0;
return false;
}
case 102:
configVersion = -1; // Prevent loop
if(relocateConfig102()) {
configVersion = 103;
} else {
configVersion = 0;
return false;
}
case EEPROM_CHECK_SUM:
return true;
default:
@@ -717,106 +857,8 @@ void AmsConfiguration::saveTempSensors() {
}
}
bool AmsConfiguration::relocateConfig86() {
MqttConfig86 mqtt86;
MqttConfig mqtt;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_MQTT_START_86, mqtt86);
strcpy(mqtt.host, mqtt86.host);
mqtt.port = mqtt86.port;
strcpy(mqtt.clientId, mqtt86.clientId);
strcpy(mqtt.publishTopic, mqtt86.publishTopic);
strcpy(mqtt.subscribeTopic, mqtt86.subscribeTopic);
strcpy(mqtt.username, mqtt86.username);
strcpy(mqtt.password, mqtt86.password);
mqtt.payloadFormat = mqtt86.payloadFormat;
mqtt.ssl = mqtt86.ssl;
EEPROM.put(CONFIG_MQTT_START, mqtt);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 87);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig87() {
MeterConfig87 meter87;
MeterConfig meter;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_METER_START_87, meter87);
if(meter87.type < 5) {
meter.baud = 2400;
meter.parity = meter87.type == 3 || meter87.type == 4 ? 3 : 11;
meter.invert = false;
} else {
meter.baud = 115200;
meter.parity = 3;
meter.invert = meter87.type == 6;
}
meter.distributionSystem = meter87.distributionSystem;
meter.mainFuse = meter87.mainFuse;
meter.productionCapacity = meter87.productionCapacity;
EEPROM.put(CONFIG_METER_START, meter);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 88);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig90() {
EntsoeConfig entsoe;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_ENTSOE_START_90, entsoe);
EEPROM.put(CONFIG_ENTSOE_START, entsoe);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 91);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig91() {
WiFiConfig91 wifi91;
WiFiConfig wifi;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_WIFI_START_91, wifi91);
strcpy(wifi.ssid, wifi91.ssid);
strcpy(wifi.psk, wifi91.psk);
strcpy(wifi.ip, wifi91.ip);
strcpy(wifi.gateway, wifi91.gateway);
strcpy(wifi.subnet, wifi91.subnet);
strcpy(wifi.dns1, wifi91.dns1);
strcpy(wifi.dns2, wifi91.dns2);
strcpy(wifi.hostname, wifi91.hostname);
wifi.mdns = wifi91.mdns;
EEPROM.put(CONFIG_WIFI_START, wifi);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 92);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig92() {
WiFiConfig wifi;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_WIFI_START, wifi);
#if defined(ESP32)
wifi.power = 195;
#elif defined(ESP8266)
wifi.power = 205;
#endif
EEPROM.put(CONFIG_WIFI_START, wifi);
EnergyAccountingConfig eac;
clearEnergyAccountingConfig(eac);
EEPROM.put(CONFIG_ENERGYACCOUNTING_START, eac);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 93);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig93() {
MeterConfig meter;
MeterConfig95 meter;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_METER_START_93, meter);
meter.wattageMultiplier = 0;
@@ -842,6 +884,171 @@ bool AmsConfiguration::relocateConfig94() {
return ret;
}
bool AmsConfiguration::relocateConfig95() {
MeterConfig95 meter;
MeterConfig95 meter95;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_METER_START, meter);
EEPROM.get(CONFIG_METER_START, meter95);
meter.wattageMultiplier = meter95.wattageMultiplier;
meter.voltageMultiplier = meter95.voltageMultiplier;
meter.amperageMultiplier = meter95.amperageMultiplier;
meter.accumulatedMultiplier = meter95.accumulatedMultiplier;
EEPROM.put(CONFIG_METER_START, meter);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 96);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig96() {
EEPROM.begin(EEPROM_SIZE);
SystemConfig sys;
EEPROM.get(CONFIG_SYSTEM_START, sys);
MeterConfig100 meter;
EEPROM.get(CONFIG_METER_START, meter);
meter.source = 1; // Serial
meter.parser = 0; // Auto
EEPROM.put(CONFIG_METER_START, meter);
#if defined(ESP8266)
GpioConfig gpio;
EEPROM.get(CONFIG_GPIO_START, gpio);
switch(sys.boardType) {
case 3: // Pow UART0 -- Now Pow-K UART0
case 4: // Pow GPIO12 -- Now Pow-U UART0
case 5: // Pow-K+ -- Now also Pow-K GPIO12
case 7: // Pow-U+ -- Now also Pow-U GPIO12
if(meter.baud == 2400 && meter.parity == 3) { // 3 == 8N1, assuming Pow-K
if(gpio.hanPin == 3) { // UART0
sys.boardType = 3;
} else if(gpio.hanPin == 12) {
sys.boardType = 5;
}
} else { // Assuming Pow-U
if(gpio.hanPin == 3) { // UART0
sys.boardType = 4;
} else if(gpio.hanPin == 12) {
sys.boardType = 7;
}
}
break;
}
#endif
sys.vendorConfigured = true;
sys.userConfigured = true;
sys.dataCollectionConsent = 0;
strcpy(sys.country, "");
EEPROM.put(CONFIG_SYSTEM_START, sys);
WiFiConfig wifi;
EEPROM.get(CONFIG_WIFI_START, wifi);
wifi.mode = 1; // WIFI_STA
wifi.autoreboot = true;
EEPROM.put(CONFIG_WIFI_START, wifi);
NtpConfig ntp;
NtpConfig96 ntp96;
EEPROM.get(CONFIG_NTP_START, ntp96);
ntp.enable = ntp96.enable;
ntp.dhcp = ntp96.dhcp;
if(ntp96.offset == 360 && ntp96.summerOffset == 360) {
strcpy(ntp.timezone, "Europe/Oslo");
} else {
strcpy(ntp.timezone, "GMT");
}
strcpy(ntp.server, ntp96.server);
EEPROM.put(CONFIG_NTP_START, ntp);
EntsoeConfig entsoe;
EEPROM.get(CONFIG_ENTSOE_START, entsoe);
entsoe.enabled = strlen(entsoe.token) > 0;
EEPROM.put(CONFIG_ENTSOE_START, entsoe);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 100);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig100() {
EEPROM.begin(EEPROM_SIZE);
MeterConfig100 meter100;
EEPROM.get(CONFIG_METER_START, meter100);
MeterConfig meter;
meter.baud = meter100.baud;
meter.parity = meter100.parity;
meter.invert = meter100.invert;
meter.distributionSystem = meter100.distributionSystem;
meter.mainFuse = meter100.mainFuse;
meter.productionCapacity = meter100.productionCapacity;
memcpy(meter.encryptionKey, meter100.encryptionKey, 16);
memcpy(meter.authenticationKey, meter100.authenticationKey, 16);
meter.wattageMultiplier = meter100.wattageMultiplier;
meter.voltageMultiplier = meter100.voltageMultiplier;
meter.amperageMultiplier = meter100.amperageMultiplier;
meter.accumulatedMultiplier = meter100.accumulatedMultiplier;
meter.source = meter100.source;
meter.parser = meter100.parser;
EEPROM.put(CONFIG_METER_START, meter);
UiConfig ui;
clearUiConfig(ui);
EEPROM.put(CONFIG_UI_START, ui);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 101);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig101() {
EEPROM.begin(EEPROM_SIZE);
EnergyAccountingConfig config;
EnergyAccountingConfig101 config101;
EEPROM.get(CONFIG_ENERGYACCOUNTING_START, config101);
for(uint8_t i = 0; i < 9; i++) {
config.thresholds[i] = config101.thresholds[i];
}
config.thresholds[9] = 0xFFFF;
config.hours = config101.hours;
EEPROM.put(CONFIG_ENERGYACCOUNTING_START, config);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 102);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::relocateConfig102() {
EEPROM.begin(EEPROM_SIZE);
GpioConfig gpioConfig;
EEPROM.get(CONFIG_GPIO_START, gpioConfig);
gpioConfig.hanPinPullup = true;
EEPROM.put(CONFIG_GPIO_START, gpioConfig);
HomeAssistantConfig haconf;
clearHomeAssistantConfig(haconf);
EEPROM.put(CONFIG_HA_START, haconf);
EntsoeConfig entsoe;
EEPROM.get(CONFIG_ENTSOE_START, entsoe);
entsoe.fixedPrice = 0;
EEPROM.put(CONFIG_ENTSOE_START, entsoe);
EEPROM.put(EEPROM_CONFIG_ADDRESS, 103);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}
bool AmsConfiguration::save() {
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(EEPROM_CONFIG_ADDRESS, EEPROM_CHECK_SUM);
@@ -996,6 +1203,7 @@ void AmsConfiguration::print(Print* debugger)
if(getGpioConfig(gpio)) {
debugger->println("--GPIO configuration--");
debugger->printf("HAN pin: %i\r\n", gpio.hanPin);
debugger->printf("HAN pin pullup %s\r\n", gpio.hanPinPullup ? "Yes" : "No");
debugger->printf("LED pin: %i\r\n", gpio.ledPin);
debugger->printf("LED inverted: %s\r\n", gpio.ledInverted ? "Yes" : "No");
debugger->printf("LED red pin: %i\r\n", gpio.ledPinRed);
@@ -1045,8 +1253,7 @@ void AmsConfiguration::print(Print* debugger)
debugger->println("--NTP configuration--");
debugger->printf("Enabled: %s\r\n", ntp.enable ? "Yes" : "No");
if(ntp.enable) {
debugger->printf("Offset: %i\r\n", ntp.offset);
debugger->printf("Summer offset: %i\r\n", ntp.summerOffset);
debugger->printf("Timezone: %s\r\n", ntp.timezone);
debugger->printf("Server: %s\r\n", ntp.server);
debugger->printf("DHCP: %s\r\n", ntp.dhcp ? "Yes" : "No");
}
@@ -1057,12 +1264,12 @@ void AmsConfiguration::print(Print* debugger)
EntsoeConfig entsoe;
if(getEntsoeConfig(entsoe)) {
debugger->println("--ENTSO-E configuration--");
debugger->printf("Token: %s\r\n", entsoe.token);
if(strlen(entsoe.token) > 0) {
if(strlen(entsoe.area) > 0) {
debugger->println("--ENTSO-E configuration--");
debugger->printf("Area: %s\r\n", entsoe.area);
debugger->printf("Currency: %s\r\n", entsoe.currency);
debugger->printf("Multiplier: %f\r\n", entsoe.multiplier / 1000.0);
debugger->printf("Token: %s\r\n", entsoe.token);
}
debugger->println("");
delay(10);

View File

@@ -0,0 +1,38 @@
#include "hexutils.h"
String toHex(uint8_t* in) {
return toHex(in, sizeof(in)*2);
}
String toHex(uint8_t* in, uint16_t size) {
String hex;
for(int i = 0; i < size; i++) {
if(in[i] < 0x10) {
hex += '0';
}
hex += String(in[i], HEX);
}
hex.toUpperCase();
return hex;
}
void fromHex(uint8_t *out, String in, uint16_t size) {
for(int i = 0; i < size*2; i += 2) {
out[i/2] = strtol(in.substring(i, i+2).c_str(), 0, 16);
}
}
void stripNonAscii(uint8_t* in, uint16_t size, bool extended) {
for(uint16_t i = 0; i < size; i++) {
if(in[i] == 0) { // Clear the rest with null-terminator
memset(in+i, 0, size-i);
break;
}
if(extended && (in[i] < 32 || in[i] == 127 || in[i] == 129 || in[i] == 141 || in[i] == 143 || in[i] == 144 || in[i] == 157)) {
memset(in+i, ' ', 1);
} else if(in[i] < 32 || in[i] > 126) {
memset(in+i, ' ', 1);
}
}
memset(in+size-1, 0, 1); // Make sure the last character is null-terminator
}

View File

@@ -10,7 +10,7 @@ enum AmsType {
AmsTypeKaifa = 0x02,
AmsTypeKamstrup = 0x03,
AmsTypeIskra = 0x08,
AmsTypeLandis = 0x09,
AmsTypeLandisGyr = 0x09,
AmsTypeSagemcom = 0x0A,
AmsTypeCustom = 0x88,
AmsTypeUnknown = 0xFF
@@ -35,10 +35,10 @@ public:
time_t getMeterTimestamp();
uint16_t getActiveImportPower();
uint16_t getReactiveImportPower();
uint16_t getActiveExportPower();
uint16_t getReactiveExportPower();
uint32_t getActiveImportPower();
uint32_t getReactiveImportPower();
uint32_t getActiveExportPower();
uint32_t getReactiveExportPower();
float getL1Voltage();
float getL2Voltage();
@@ -53,6 +53,14 @@ public:
float getL2PowerFactor();
float getL3PowerFactor();
float getL1ActiveImportPower();
float getL2ActiveImportPower();
float getL3ActiveImportPower();
float getL1ActiveExportPower();
float getL2ActiveExportPower();
float getL3ActiveExportPower();
double getActiveImportCounter();
double getReactiveImportCounter();
double getActiveExportCounter();
@@ -61,18 +69,26 @@ public:
bool isThreePhase();
bool isTwoPhase();
int8_t getLastError();
void setLastError(int8_t);
protected:
unsigned long lastUpdateMillis = 0;
unsigned long lastList2 = 0;
uint8_t listType = 0, meterType = AmsTypeUnknown;
time_t packageTimestamp = 0;
String listId, meterId, meterModel;
String listId = "", meterId = "", meterModel = "";
time_t meterTimestamp = 0;
uint16_t activeImportPower = 0, reactiveImportPower = 0, activeExportPower = 0, reactiveExportPower = 0;
uint32_t activeImportPower = 0, reactiveImportPower = 0, activeExportPower = 0, reactiveExportPower = 0;
float l1voltage = 0, l2voltage = 0, l3voltage = 0, l1current = 0, l2current = 0, l3current = 0;
float l1activeImportPower = 0, l2activeImportPower = 0, l3activeImportPower = 0;
float l1activeExportPower = 0, l2activeExportPower = 0, l3activeExportPower = 0;
float powerFactor = 0, l1PowerFactor = 0, l2PowerFactor = 0, l3PowerFactor = 0;
double activeImportCounter = 0, reactiveImportCounter = 0, activeExportCounter = 0, reactiveExportCounter = 0;
bool threePhase = false, twoPhase = false, counterEstimated = false;
int8_t lastError = 0x00;
uint8_t lastErrorCount = 0;
};
#endif

View File

@@ -7,7 +7,7 @@
#include "AmsConfiguration.h"
#include "EnergyAccounting.h"
#include "HwTools.h"
#include "entsoe/EntsoeApi.h"
#include "EntsoeApi.h"
class AmsMqttHandler {
public:
@@ -15,11 +15,12 @@ public:
this->mqtt = mqtt;
this->json = buf;
};
virtual ~AmsMqttHandler() {};
virtual bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea);
virtual bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, EntsoeApi* eapi);
virtual bool publishTemperatures(AmsConfiguration*, HwTools*);
virtual bool publishPrices(EntsoeApi* eapi);
virtual bool publishSystem(HwTools*);
virtual bool publishSystem(HwTools*, EntsoeApi*, EnergyAccounting*);
protected:
MQTTClient* mqtt;

View File

@@ -45,6 +45,12 @@ void AmsData::apply(AmsData& other) {
this->l1PowerFactor = other.getL1PowerFactor();
this->l2PowerFactor = other.getL2PowerFactor();
this->l3PowerFactor = other.getL3PowerFactor();
this->l1activeImportPower = other.getL1ActiveImportPower();
this->l2activeImportPower = other.getL2ActiveImportPower();
this->l3activeImportPower = other.getL3ActiveImportPower();
this->l1activeExportPower = other.getL1ActiveExportPower();
this->l2activeExportPower = other.getL2ActiveExportPower();
this->l3activeExportPower = other.getL3ActiveExportPower();
case 3:
this->meterTimestamp = other.getMeterTimestamp();
this->activeImportCounter = other.getActiveImportCounter();
@@ -58,7 +64,6 @@ void AmsData::apply(AmsData& other) {
this->meterType = other.getMeterType();
this->meterModel = other.getMeterModel();
this->reactiveImportPower = other.getReactiveImportPower();
this->activeExportPower = other.getActiveExportPower();
this->reactiveExportPower = other.getReactiveExportPower();
this->l1current = other.getL1Current();
this->l2current = other.getL2Current();
@@ -68,9 +73,13 @@ void AmsData::apply(AmsData& other) {
this->l3voltage = other.getL3Voltage();
this->threePhase = other.isThreePhase();
this->twoPhase = other.isTwoPhase();
case 1:
this->activeImportPower = other.getActiveImportPower();
}
// Moved outside switch to handle meters alternating between sending active and accumulated values
if(other.getListType() == 1 || (other.getActiveImportPower() > 0 || other.getActiveExportPower() > 0))
this->activeImportPower = other.getActiveImportPower();
if(other.getListType() == 2 || (other.getActiveImportPower() > 0 || other.getActiveExportPower() > 0))
this->activeExportPower = other.getActiveExportPower();
}
unsigned long AmsData::getLastUpdateMillis() {
@@ -105,19 +114,19 @@ time_t AmsData::getMeterTimestamp() {
return this->meterTimestamp;
}
uint16_t AmsData::getActiveImportPower() {
uint32_t AmsData::getActiveImportPower() {
return this->activeImportPower;
}
uint16_t AmsData::getReactiveImportPower() {
uint32_t AmsData::getReactiveImportPower() {
return this->reactiveImportPower;
}
uint16_t AmsData::getActiveExportPower() {
uint32_t AmsData::getActiveExportPower() {
return this->activeExportPower;
}
uint16_t AmsData::getReactiveExportPower() {
uint32_t AmsData::getReactiveExportPower() {
return this->reactiveExportPower;
}
@@ -161,6 +170,30 @@ float AmsData::getL3PowerFactor() {
return this->l3PowerFactor;
}
float AmsData::getL1ActiveImportPower() {
return this->l1activeImportPower;
}
float AmsData::getL2ActiveImportPower() {
return this->l2activeImportPower;
}
float AmsData::getL3ActiveImportPower() {
return this->l3activeImportPower;
}
float AmsData::getL1ActiveExportPower() {
return this->l1activeExportPower;
}
float AmsData::getL2ActiveExportPower() {
return this->l2activeExportPower;
}
float AmsData::getL3ActiveExportPower() {
return this->l3activeExportPower;
}
double AmsData::getActiveImportCounter() {
return this->activeImportCounter;
}
@@ -184,3 +217,16 @@ bool AmsData::isThreePhase() {
bool AmsData::isTwoPhase() {
return this->twoPhase;
}
int8_t AmsData::getLastError() {
return lastErrorCount > 3 ? lastError : 0;
}
void AmsData::setLastError(int8_t lastError) {
this->lastError = lastError;
if(lastError == 0) {
lastErrorCount = 0;
} else {
lastErrorCount++;
}
}

View File

@@ -7,31 +7,33 @@
struct DayDataPoints {
uint8_t version;
int16_t hImport[24];
uint16_t hImport[24];
time_t lastMeterReadTime;
uint32_t activeImport;
uint32_t activeExport;
int16_t hExport[24];
}; // 112 bytes
uint16_t hExport[24];
uint8_t accuracy;
}; // 113 bytes
struct MonthDataPoints {
uint8_t version;
int16_t dImport[31];
uint16_t dImport[31];
time_t lastMeterReadTime;
uint32_t activeImport;
uint32_t activeExport;
int16_t dExport[31];
}; // 141 bytes
uint16_t dExport[31];
uint8_t accuracy;
}; // 142 bytes
class AmsDataStorage {
public:
AmsDataStorage(RemoteDebug*);
void setTimezone(Timezone*);
bool update(AmsData*);
int32_t getHourImport(uint8_t);
int32_t getHourExport(uint8_t);
int32_t getDayImport(uint8_t);
int32_t getDayExport(uint8_t);
uint32_t getHourImport(uint8_t);
uint32_t getHourExport(uint8_t);
uint32_t getDayImport(uint8_t);
uint32_t getDayExport(uint8_t);
bool load();
bool save();
@@ -40,6 +42,11 @@ public:
MonthDataPoints getMonthData();
bool setMonthData(MonthDataPoints&);
uint8_t getDayAccuracy();
void setDayAccuracy(uint8_t);
uint8_t getMonthAccuracy();
void setMonthAccuracy(uint8_t);
bool isHappy();
bool isDayHappy();
bool isMonthHappy();
@@ -50,19 +57,21 @@ private:
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10
};
MonthDataPoints month = {
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10
};
RemoteDebug* debugger;
void setHourImport(uint8_t, int32_t);
void setHourExport(uint8_t, int32_t);
void setDayImport(uint8_t, int32_t);
void setDayExport(uint8_t, int32_t);
void setHourImport(uint8_t, uint32_t);
void setHourExport(uint8_t, uint32_t);
void setDayImport(uint8_t, uint32_t);
void setDayExport(uint8_t, uint32_t);
};
#endif

View File

@@ -5,8 +5,10 @@
#include "version.h"
AmsDataStorage::AmsDataStorage(RemoteDebug* debugger) {
day.version = 4;
month.version = 5;
day.version = 5;
day.accuracy = 1;
month.version = 6;
month.accuracy = 1;
this->debugger = debugger;
}
@@ -21,7 +23,7 @@ bool AmsDataStorage::update(AmsData* data) {
}
time_t now = time(nullptr);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Time is: %lld\n", (int64_t) now);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Time is: %lu\n", (int32_t) now);
if(tz == NULL) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Timezone is missing\n");
return false;
@@ -30,18 +32,18 @@ bool AmsDataStorage::update(AmsData* data) {
if(data->getMeterTimestamp() > BUILD_EPOCH) {
now = data->getMeterTimestamp();
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf("(AmsDataStorage) Using meter timestamp, which is: %lld\n", (int64_t) now);
debugger->printf("(AmsDataStorage) Using meter timestamp, which is: %lu\n", (int32_t) now);
}
} else if(data->getPackageTimestamp() > BUILD_EPOCH) {
now = data->getPackageTimestamp();
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf("(AmsDataStorage) Using package timestamp, which is: %lld\n", (int64_t) now);
debugger->printf("(AmsDataStorage) Using package timestamp, which is: %lu\n", (int32_t) now);
}
}
}
if(now < BUILD_EPOCH) {
if(debugger->isActive(RemoteDebug::VERBOSE)) {
debugger->printf("(AmsDataStorage) Invalid time: %lld\n", (int64_t) now);
debugger->printf("(AmsDataStorage) Invalid time: %lu\n", (int32_t) now);
}
return false;
}
@@ -52,18 +54,21 @@ bool AmsDataStorage::update(AmsData* data) {
breakTime(now-3600, utcYesterday);
breakTime(tz->toLocal(now-3600), ltzYesterDay);
uint64_t importCounter = data->getActiveImportCounter() * 1000;
uint64_t exportCounter = data->getActiveExportCounter() * 1000;
// Clear hours between last update and now
if(day.lastMeterReadTime > now) {
if(debugger->isActive(RemoteDebug::WARNING)) {
debugger->printf("(AmsDataStorage) Invalid future timestamp for day plot, resetting\n");
}
day.activeImport = data->getActiveImportCounter() * 1000;
day.activeExport = data->getActiveExportCounter() * 1000;
day.activeImport = importCounter;
day.activeExport = exportCounter;
day.lastMeterReadTime = now;
return true;
} else {
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf("(AmsDataStorage) Last day update: %lld\n", (int64_t) day.lastMeterReadTime);
debugger->printf("(AmsDataStorage) Last day update: %lu\n", (int32_t) day.lastMeterReadTime);
}
tmElements_t last;
breakTime(day.lastMeterReadTime, last);
@@ -81,12 +86,12 @@ bool AmsDataStorage::update(AmsData* data) {
if(debugger->isActive(RemoteDebug::WARNING)) {
debugger->printf("(AmsDataStorage) Invalid future timestamp for month plot, resetting\n");
}
month.activeImport = data->getActiveImportCounter() * 1000;
month.activeExport = data->getActiveExportCounter() * 1000;
month.activeImport = importCounter;
month.activeExport = exportCounter;
month.lastMeterReadTime = now;
} else {
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf("(AmsDataStorage) Last month update: %lld\n", (int64_t) month.lastMeterReadTime);
debugger->printf("(AmsDataStorage) Last month update: %lu\n", (int32_t) month.lastMeterReadTime);
}
tmElements_t last;
breakTime(tz->toLocal(month.lastMeterReadTime), last);
@@ -108,9 +113,15 @@ bool AmsDataStorage::update(AmsData* data) {
// Update day plot
if(!isDayHappy()) {
if(day.activeImport == 0 || now - day.lastMeterReadTime > 86400) {
day.activeImport = data->getActiveImportCounter() * 1000;
day.activeExport = data->getActiveExportCounter() * 1000;
if(day.activeImport > importCounter || day.activeExport > exportCounter) {
day.activeImport = importCounter;
day.activeExport = exportCounter;
day.lastMeterReadTime = now;
setHourImport(utcYesterday.Hour, 0);
setHourExport(utcYesterday.Hour, 0);
} else if(day.activeImport == 0 || now - day.lastMeterReadTime > 86400) {
day.activeImport = importCounter;
day.activeExport = exportCounter;
day.lastMeterReadTime = now;
if(debugger->isActive(RemoteDebug::WARNING)) {
debugger->printf("(AmsDataStorage) Too long since last day update, clearing data\n");
@@ -120,19 +131,19 @@ bool AmsDataStorage::update(AmsData* data) {
setHourExport(i, 0);
}
} else if(now - day.lastMeterReadTime < 4000) {
uint32_t imp = (data->getActiveImportCounter() * 1000) - day.activeImport;
uint32_t exp = (data->getActiveExportCounter() * 1000) - day.activeExport;
uint32_t imp = importCounter - day.activeImport;
uint32_t exp = exportCounter - day.activeExport;
setHourImport(utcYesterday.Hour, imp);
setHourExport(utcYesterday.Hour, exp);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(AmsDataStorage) Usage for hour %d: %d - %d\n", ltzYesterDay.Hour, imp, exp);
day.activeImport = data->getActiveImportCounter() * 1000;
day.activeExport = data->getActiveExportCounter() * 1000;
day.activeImport = importCounter;
day.activeExport = exportCounter;
day.lastMeterReadTime = now;
} else {
float mins = (now - day.lastMeterReadTime) / 60.0;
uint32_t im = (data->getActiveImportCounter() * 1000) - day.activeImport;
uint32_t ex = (data->getActiveExportCounter() * 1000) - day.activeExport;
uint32_t im = importCounter - day.activeImport;
uint32_t ex = exportCounter - day.activeExport;
float ipm = im / mins;
float epm = ex / mins;
@@ -156,7 +167,7 @@ bool AmsDataStorage::update(AmsData* data) {
setHourExport(last.Hour, exp);
if(debugger->isActive(RemoteDebug::INFO)) {
debugger->printf("(AmsDataStorage) Estimated usage for hour %u: %.1f - %.1f (%lld)\n", last.Hour, imp, exp, (int64_t) cur);
debugger->printf("(AmsDataStorage) Estimated usage for hour %u: %.1f - %.1f (%lu)\n", last.Hour, imp, exp, (int32_t) cur);
}
day.activeImport += imp;
@@ -169,9 +180,15 @@ bool AmsDataStorage::update(AmsData* data) {
// Update month plot
if(ltz.Hour == 0 && !isMonthHappy()) {
if(month.activeImport == 0 || now - month.lastMeterReadTime > 2678400) {
month.activeImport = data->getActiveImportCounter() * 1000;
month.activeExport = data->getActiveExportCounter() * 1000;
if(month.activeImport > importCounter || month.activeExport > exportCounter) {
month.activeImport = importCounter;
month.activeExport = exportCounter;
month.lastMeterReadTime = now;
setDayImport(ltzYesterDay.Day, 0);
setDayExport(ltzYesterDay.Day, 0);
} else if(month.activeImport == 0 || now - month.lastMeterReadTime > 2678400) {
month.activeImport = importCounter;
month.activeExport = exportCounter;
month.lastMeterReadTime = now;
if(debugger->isActive(RemoteDebug::WARNING)) {
debugger->printf("(AmsDataStorage) Too long since last month update, clearing data\n");
@@ -180,9 +197,9 @@ bool AmsDataStorage::update(AmsData* data) {
setDayImport(i, 0);
setDayExport(i, 0);
}
} else if(now - month.lastMeterReadTime < 86500 && now - month.lastMeterReadTime > 86300) {
int32_t imp = (data->getActiveImportCounter() * 1000) - month.activeImport;
int32_t exp = (data->getActiveExportCounter() * 1000) - month.activeExport;
} else if(now - month.lastMeterReadTime < 90100 && now - month.lastMeterReadTime > 82700) { // DST days are 23h (82800s) and 25h (90000)
int32_t imp = importCounter - month.activeImport;
int32_t exp = exportCounter - month.activeExport;
if(debugger->isActive(RemoteDebug::INFO)) {
debugger->printf("(AmsDataStorage) Usage for day %d: %d - %d\n", ltzYesterDay.Day, imp, exp);
@@ -190,8 +207,8 @@ bool AmsDataStorage::update(AmsData* data) {
setDayImport(ltzYesterDay.Day, imp);
setDayExport(ltzYesterDay.Day, exp);
month.activeImport = data->getActiveImportCounter() * 1000;
month.activeExport = data->getActiveExportCounter() * 1000;
month.activeImport = importCounter;
month.activeExport = exportCounter;
month.lastMeterReadTime = now;
} else {
// Make sure last month read is at midnight
@@ -199,12 +216,12 @@ bool AmsDataStorage::update(AmsData* data) {
breakTime(tz->toLocal(month.lastMeterReadTime), last);
month.lastMeterReadTime = month.lastMeterReadTime - (last.Hour * 3600) - (last.Minute * 60) - last.Second;
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf("(AmsDataStorage) Last month read after resetting to midnight: %lld\n", (int64_t) month.lastMeterReadTime);
debugger->printf("(AmsDataStorage) Last month read after resetting to midnight: %lu\n", (int32_t) month.lastMeterReadTime);
}
float hrs = (now - month.lastMeterReadTime) / 3600.0;
uint32_t im = (data->getActiveImportCounter() * 1000) - month.activeImport;
uint32_t ex = (data->getActiveExportCounter() * 1000) - month.activeExport;
uint32_t im = importCounter - month.activeImport;
uint32_t ex = exportCounter - month.activeExport;
float iph = im / hrs;
float eph = ex / hrs;
@@ -224,7 +241,7 @@ bool AmsDataStorage::update(AmsData* data) {
setDayExport(last.Day, exp);
if(debugger->isActive(RemoteDebug::INFO)) {
debugger->printf("(AmsDataStorage) Estimated usage for day %u: %.1f - %.1f (%lld)\n", last.Day, imp, exp, (int64_t) cur);
debugger->printf("(AmsDataStorage) Estimated usage for day %u: %.1f - %.1f (%lu)\n", last.Day, imp, exp, (int32_t) cur);
}
month.activeImport += imp;
@@ -237,44 +254,160 @@ bool AmsDataStorage::update(AmsData* data) {
return ret;
}
void AmsDataStorage::setHourImport(uint8_t hour, int32_t val) {
void AmsDataStorage::setHourImport(uint8_t hour, uint32_t val) {
if(hour < 0 || hour > 24) return;
day.hImport[hour] = val / 10;
uint8_t accuracy = day.accuracy;
uint32_t update = val / pow(10, accuracy);
while(update > UINT16_MAX) {
accuracy++;
update = val / pow(10, accuracy);
}
if(accuracy != day.accuracy) {
setDayAccuracy(accuracy);
}
day.hImport[hour] = update;
uint32_t max = 0;
for(uint8_t i = 0; i < 24; i++) {
if(day.hImport[i] > max)
max = day.hImport[i];
if(day.hExport[i] > max)
max = day.hExport[i];
}
while(max < UINT16_MAX/10 && accuracy > 0) {
accuracy--;
max = max*10;
}
if(accuracy != day.accuracy) {
setDayAccuracy(accuracy);
}
}
int32_t AmsDataStorage::getHourImport(uint8_t hour) {
uint32_t AmsDataStorage::getHourImport(uint8_t hour) {
if(hour < 0 || hour > 24) return 0;
return day.hImport[hour] * 10;
return day.hImport[hour] * pow(10, day.accuracy);
}
void AmsDataStorage::setHourExport(uint8_t hour, int32_t val) {
void AmsDataStorage::setHourExport(uint8_t hour, uint32_t val) {
if(hour < 0 || hour > 24) return;
day.hExport[hour] = val / 10;
uint8_t accuracy = day.accuracy;
uint32_t update = val / pow(10, accuracy);
while(update > UINT16_MAX) {
accuracy++;
update = val / pow(10, accuracy);
}
if(accuracy != day.accuracy) {
setDayAccuracy(accuracy);
}
day.hExport[hour] = update;
uint32_t max = 0;
for(uint8_t i = 0; i < 24; i++) {
if(day.hImport[i] > max)
max = day.hImport[i];
if(day.hExport[i] > max)
max = day.hExport[i];
}
while(max < UINT16_MAX/10 && accuracy > 0) {
accuracy--;
max = max*10;
}
if(accuracy != day.accuracy) {
setDayAccuracy(accuracy);
}
}
int32_t AmsDataStorage::getHourExport(uint8_t hour) {
uint32_t AmsDataStorage::getHourExport(uint8_t hour) {
if(hour < 0 || hour > 24) return 0;
return day.hExport[hour] * 10;
return day.hExport[hour] * pow(10, day.accuracy);
}
void AmsDataStorage::setDayImport(uint8_t day, int32_t val) {
void AmsDataStorage::setDayImport(uint8_t day, uint32_t val) {
if(day < 1 || day > 31) return;
month.dImport[day-1] = val / 10;
uint8_t accuracy = month.accuracy;
uint32_t update = val / pow(10, accuracy);
while(update > UINT16_MAX) {
accuracy++;
update = val / pow(10, accuracy);
}
if(accuracy != month.accuracy) {
setMonthAccuracy(accuracy);
}
month.dImport[day-1] = update;
uint32_t max = 0;
for(uint8_t i = 0; i < 31; i++) {
if(month.dImport[i] > max)
max = month.dImport[i];
if(month.dExport[i] > max)
max = month.dExport[i];
}
while(max < UINT16_MAX/10 && accuracy > 0) {
accuracy--;
max = max*10;
}
if(accuracy != month.accuracy) {
setMonthAccuracy(accuracy);
}
}
int32_t AmsDataStorage::getDayImport(uint8_t day) {
uint32_t AmsDataStorage::getDayImport(uint8_t day) {
if(day < 1 || day > 31) return 0;
return (month.dImport[day-1] * 10);
return (month.dImport[day-1] * pow(10, month.accuracy));
}
void AmsDataStorage::setDayExport(uint8_t day, int32_t val) {
void AmsDataStorage::setDayExport(uint8_t day, uint32_t val) {
if(day < 1 || day > 31) return;
month.dExport[day-1] = val / 10;
uint8_t accuracy = month.accuracy;
uint32_t update = val / pow(10, accuracy);
while(update > UINT16_MAX) {
accuracy++;
update = val / pow(10, accuracy);
}
if(accuracy != month.accuracy) {
setMonthAccuracy(accuracy);
}
month.dExport[day-1] = update;
uint32_t max = 0;
for(uint8_t i = 0; i < 31; i++) {
if(month.dImport[i] > max)
max = month.dImport[i];
if(month.dExport[i] > max)
max = month.dExport[i];
}
while(max < UINT16_MAX/10 && accuracy > 0) {
accuracy--;
max = max*10;
}
if(accuracy != month.accuracy) {
setMonthAccuracy(accuracy);
}
}
int32_t AmsDataStorage::getDayExport(uint8_t day) {
uint32_t AmsDataStorage::getDayExport(uint8_t day) {
if(day < 1 || day > 31) return 0;
return (month.dExport[day-1] * 10);
return (month.dExport[day-1] * pow(10, month.accuracy));
}
bool AmsDataStorage::load() {
@@ -348,31 +481,74 @@ MonthDataPoints AmsDataStorage::getMonthData() {
}
bool AmsDataStorage::setDayData(DayDataPoints& day) {
if(day.version == 4) {
if(day.version == 5) {
this->day = day;
return true;
} else if(day.version == 4) {
this->day = day;
this->day.accuracy = 1;
this->day.version = 5;
return true;
} else if(day.version == 3) {
this->day = day;
for(uint8_t i = 0; i < 24; i++) this->day.hExport[i] = 0;
this->day.version = 4;
this->day.accuracy = 1;
this->day.version = 5;
return true;
}
return false;
}
bool AmsDataStorage::setMonthData(MonthDataPoints& month) {
if(month.version == 5) {
if(month.version == 6) {
this->month = month;
return true;
} else if(month.version == 5) {
this->month = month;
this->month.accuracy = 1;
this->month.version = 6;
return true;
} else if(month.version == 4) {
this->month = month;
for(uint8_t i = 0; i < 31; i++) this->month.dExport[i] = 0;
this->month.version = 5;
this->month.accuracy = 1;
this->month.version = 6;
return true;
}
return false;
}
uint8_t AmsDataStorage::getDayAccuracy() {
return day.accuracy;
}
void AmsDataStorage::setDayAccuracy(uint8_t accuracy) {
if(day.accuracy != accuracy) {
uint16_t multiplier = pow(10, day.accuracy)/pow(10, accuracy);
for(uint8_t i = 0; i < 24; i++) {
day.hImport[i] = day.hImport[i] * multiplier;
day.hExport[i] = day.hExport[i] * multiplier;
}
day.accuracy = accuracy;
}
}
uint8_t AmsDataStorage::getMonthAccuracy() {
return month.accuracy;
}
void AmsDataStorage::setMonthAccuracy(uint8_t accuracy) {
if(month.accuracy != accuracy) {
uint16_t multiplier = pow(10, month.accuracy)/pow(10, accuracy);
for(uint8_t i = 0; i < 31; i++) {
month.dImport[i] = month.dImport[i] * multiplier;
month.dExport[i] = month.dExport[i] * multiplier;
}
month.accuracy = accuracy;
}
month.accuracy = accuracy;
}
bool AmsDataStorage::isHappy() {
return isDayHappy() && isMonthHappy();
}
@@ -383,17 +559,17 @@ bool AmsDataStorage::isDayHappy() {
tmElements_t tm, last;
if(now < day.lastMeterReadTime) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Day %lld < %lld\n", (int64_t) now, (int64_t) day.lastMeterReadTime);
return false;
}
if(now-day.lastMeterReadTime > 3600) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Day %lld - %lld > 3600\n", (int64_t) now, (int64_t) day.lastMeterReadTime);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Day data timestamp %lu < %lu\n", (int32_t) now, (int32_t) day.lastMeterReadTime);
return false;
}
breakTime(tz->toLocal(now), tm);
breakTime(tz->toLocal(day.lastMeterReadTime), last);
if(now-day.lastMeterReadTime > 3600) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Day data timestamp age %lu - %lu > 3600\n", (int32_t) now, (int32_t) day.lastMeterReadTime);
return false;
}
if(tm.Hour > last.Hour) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Day %d > %d\n", tm.Hour, last.Hour);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Day data hour of last timestamp %d > %d\n", tm.Hour, last.Hour);
return false;
}
@@ -411,17 +587,14 @@ bool AmsDataStorage::isMonthHappy() {
tmElements_t tm, last;
if(now < month.lastMeterReadTime) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Month %lld < %lld\n", (int64_t) now, (int64_t) month.lastMeterReadTime);
return false;
}
if(now-month.lastMeterReadTime > 86400) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Month %lld - %lld > 3600\n", (int64_t) now, (int64_t) month.lastMeterReadTime);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Month data timestamp %lu < %lu\n", (int32_t) now, (int32_t) month.lastMeterReadTime);
return false;
}
breakTime(tz->toLocal(now), tm);
breakTime(tz->toLocal(month.lastMeterReadTime), last);
if(tm.Day > last.Day) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Month %d > %d\n", tm.Day, last.Day);
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(AmsDataStorage) Month data day of last timestamp %d > %d\n", tm.Day, last.Day);
return false;
}

View File

@@ -4,6 +4,7 @@
#include "Arduino.h"
#include <stdint.h>
uint16_t crc16(const uint8_t* p, int len);
uint16_t crc16_x25(const uint8_t* p, int len);
#endif

View File

@@ -1,10 +1,12 @@
#include "DsmrParser.h"
#include "crc.h"
#include "hexutils.h"
#include "lwip/def.h"
int8_t DSMRParser::parse(uint8_t *buf, DataParserContext &ctx, bool verified) {
uint16_t crcPos = 0;
bool reachedEnd = verified;
uint8_t lastByte = 0x00;
int c = 0;
for(int pos = 0; pos < ctx.length; pos++) {
uint8_t b = *(buf+pos);
if(pos == 0 && b != '/') return DATA_PARSE_BOUNDRY_FLAG_MISSING;
@@ -15,8 +17,13 @@ int8_t DSMRParser::parse(uint8_t *buf, DataParserContext &ctx, bool verified) {
if(!reachedEnd) return DATA_PARSE_INCOMPLETE;
buf[ctx.length+1] = '\0';
if(crcPos > 0) {
// TODO: CRC
Serial.printf("CRC: %s\n", buf+crcPos);
uint16_t crc_calc = crc16(buf, crcPos);
uint16_t crc = 0x0000;
fromHex((uint8_t*) &crc, String((char*) buf+crcPos), 2);
crc = ntohs(crc);
if(crc != crc_calc)
return DATA_PARSE_FOOTER_CHECKSUM_ERROR;
}
return DATA_PARSE_OK;
}

View File

@@ -27,7 +27,7 @@ int8_t GCMParser::parse(uint8_t *d, DataParserContext &ctx) {
memcpy(ctx.system_title, ptr, systemTitleLength);
memcpy(initialization_vector, ctx.system_title, systemTitleLength);
int len;
int len = 0;
int headersize = 2 + systemTitleLength;
ptr += systemTitleLength;
if(((*ptr) & 0xFF) == 0x81) {
@@ -44,6 +44,10 @@ int8_t GCMParser::parse(uint8_t *d, DataParserContext &ctx) {
ptr += 3;
headersize += 3;
} else {
len = *ptr;
ptr++;
headersize++;
}
if(len + headersize > ctx.length)
return DATA_PARSE_INCOMPLETE;

View File

@@ -5,8 +5,6 @@
int8_t HDLCParser::parse(uint8_t *d, DataParserContext &ctx) {
int len;
uint8_t flag = *d;
uint8_t* ptr;
if(ctx.length < 3)
return DATA_PARSE_INCOMPLETE;

View File

@@ -1,7 +1,6 @@
#include "LlcParser.h"
int8_t LLCParser::parse(uint8_t *buf, DataParserContext &ctx) {
LLCHeader* llc = (LLCHeader*) buf;
ctx.length -= 3;
return 3;
}

View File

@@ -5,8 +5,6 @@ int8_t MBUSParser::parse(uint8_t *d, DataParserContext &ctx) {
int headersize = 3;
int footersize = 1;
uint8_t flag = *d;
uint8_t* ptr;
// https://m-bus.com/documentation-wired/06-application-layer

View File

@@ -10,3 +10,20 @@ uint16_t crc16_x25(const uint8_t* p, int len)
return (~crc << 8) | (~crc >> 8 & 0xff);
}
uint16_t crc16 (const uint8_t *p, int len) {
uint16_t crc = 0;
while (len--) {
int i;
crc ^= *p++;
for (i = 0 ; i < 8 ; ++i) {
if (crc & 1)
crc = (crc >> 1) ^ 0xa001;
else
crc = (crc >> 1);
}
}
return crc;
}

View File

@@ -0,0 +1 @@
json/*.h

View File

@@ -9,14 +9,14 @@ public:
DomoticzMqttHandler(MQTTClient* mqtt, char* buf, DomoticzConfig config) : AmsMqttHandler(mqtt, buf) {
this->config = config;
};
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea);
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, EntsoeApi* eapi);
bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(EntsoeApi*);
bool publishSystem(HwTools*);
bool publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea);
private:
DomoticzConfig config;
int energy = 0.0;
double energy = 0.0;
};
#endif

View File

@@ -4,7 +4,7 @@ import shutil
import subprocess
try:
from css_html_js_minify import html_minify, js_minify, css_minify
from css_html_js_minify import js_minify
except:
from SCons.Script import (
ARGUMENTS,
@@ -20,13 +20,13 @@ except:
)
)
try:
from css_html_js_minify import html_minify, js_minify, css_minify
from css_html_js_minify import js_minify
except:
print("WARN: Unable to load minifier")
webroot = "web"
srcroot = "src/web/root"
webroot = "lib/DomoticzMqttHandler/json"
srcroot = "lib/DomoticzMqttHandler/include/json"
version = os.environ.get('GITHUB_TAG')
if version == None:
@@ -57,11 +57,7 @@ for filename in os.listdir(webroot):
content = f.read().replace("${version}", version)
try:
if filename.endswith(".html"):
content = html_minify(content)
elif filename.endswith(".css"):
content = css_minify(content)
elif (filename.endswith(".js") and filename != 'gaugemeter.js') or filename.endswith(".json"):
if (filename.endswith(".js") and filename != 'gaugemeter.js') or filename.endswith(".json"):
content = js_minify(content)
except:
print("WARN: Unable to minify")

View File

@@ -1,7 +1,7 @@
#include "DomoticzMqttHandler.h"
#include "web/root/domoticz_json.h"
#include "json/domoticz_json.h"
bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea) {
bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, EntsoeApi* eapi) {
bool ret = false;
if (config.elidx > 0) {
if(data->getActiveImportCounter() > 1.0) {
@@ -71,6 +71,6 @@ bool DomoticzMqttHandler::publishPrices(EntsoeApi* eapi) {
return false;
}
bool DomoticzMqttHandler::publishSystem(HwTools* hw) {
bool DomoticzMqttHandler::publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea) {
return false;
}

View File

@@ -4,7 +4,7 @@
#include "Arduino.h"
#include "AmsData.h"
#include "AmsDataStorage.h"
#include "entsoe/EntsoeApi.h"
#include "EntsoeApi.h"
struct EnergyAccountingPeak {
uint8_t day;
@@ -12,6 +12,18 @@ struct EnergyAccountingPeak {
};
struct EnergyAccountingData {
uint8_t version;
uint8_t month;
uint16_t costYesterday;
uint16_t costThisMonth;
uint16_t costLastMonth;
uint16_t incomeYesterday;
uint16_t incomeThisMonth;
uint16_t incomeLastMonth;
EnergyAccountingPeak peaks[5];
};
struct EnergyAccountingData4 {
uint8_t version;
uint8_t month;
uint16_t costYesterday;
@@ -40,22 +52,38 @@ public:
bool update(AmsData* amsData);
bool load();
bool save();
bool isInitialized();
double getUseThisHour();
double getCostThisHour();
double getUseToday();
double getUseThisMonth();
double getProducedThisHour();
double getProducedToday();
double getProducedThisMonth();
double getCostThisHour();
double getCostToday();
double getCostYesterday();
double getUseThisMonth();
double getCostThisMonth();
uint16_t getCostLastMonth();
double getIncomeThisHour();
double getIncomeToday();
double getIncomeYesterday();
double getIncomeThisMonth();
uint16_t getIncomeLastMonth();
float getMonthMax();
uint8_t getCurrentThreshold();
EnergyAccountingPeak getPeak(uint8_t);
EnergyAccountingData getData();
void setData(EnergyAccountingData&);
void setFixedPrice(double price);
double getPriceForHour(uint8_t h);
private:
RemoteDebug* debugger = NULL;
unsigned long lastUpdateMillis = 0;
@@ -65,8 +93,10 @@ private:
EnergyAccountingConfig *config = NULL;
Timezone *tz = NULL;
uint8_t currentHour = 0, currentDay = 0, currentThresholdIdx = 0;
double use, costHour, costDay;
double use = 0, costHour = 0, costDay = 0;
double produce = 0, incomeHour = 0, incomeDay = 0;
EnergyAccountingData data = { 0, 0, 0, 0, 0, 0 };
double fixedPrice = 0;
void calcDayCost();
bool updateMax(uint16_t val, uint8_t day);

View File

@@ -26,6 +26,10 @@ void EnergyAccounting::setTimezone(Timezone* tz) {
this->tz = tz;
}
bool EnergyAccounting::isInitialized() {
return this->init;
}
bool EnergyAccounting::update(AmsData* amsData) {
if(config == NULL) return false;
time_t now = time(nullptr);
@@ -42,11 +46,12 @@ bool EnergyAccounting::update(AmsData* amsData) {
if(!init) {
currentHour = local.Hour;
currentDay = local.Day;
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EnergyAccounting) Initializing data at %lld\n", (int64_t) now);
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EnergyAccounting) Initializing data at %lu\n", (int32_t) now);
if(!load()) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EnergyAccounting) Unable to load existing data\n");
data = { 4, local.Month,
0, 0, 0,
data = { 5, local.Month,
0, 0, 0, // Cost
0, 0, 0, // Income
0, 0, // Peak 1
0, 0, // Peak 2
0, 0, // Peak 3
@@ -57,37 +62,47 @@ bool EnergyAccounting::update(AmsData* amsData) {
for(uint8_t i = 0; i < 5; i++) {
debugger->printf("(EnergyAccounting) Peak hour from day %d: %d\n", data.peaks[i].day, data.peaks[i].value*10);
}
debugger->printf("(EnergyAccounting) Loaded cost yesterday: %d, this month: %d, last month: %d\n", data.costYesterday / 10.0, data.costThisMonth, data.costLastMonth);
debugger->printf("(EnergyAccounting) Loaded cost yesterday: %.2f, this month: %d, last month: %d\n", data.costYesterday / 10.0, data.costThisMonth, data.costLastMonth);
debugger->printf("(EnergyAccounting) Loaded income yesterday: %.2f, this month: %d, last month: %d\n", data.incomeYesterday / 10.0, data.incomeThisMonth, data.incomeLastMonth);
}
init = true;
}
if(!initPrice && eapi != NULL && eapi->getValueForHour(0) != ENTSOE_NO_VALUE) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EnergyAccounting) Initializing prices at %lld\n", (int64_t) now);
if(!initPrice && eapi != NULL && getPriceForHour(0) != ENTSOE_NO_VALUE) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EnergyAccounting) Initializing prices at %lu\n", (int32_t) now);
calcDayCost();
}
if(local.Hour != currentHour && (amsData->getListType() >= 3 || local.Minute == 1)) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EnergyAccounting) New local hour %d\n", local.Hour);
tmElements_t oneHrAgo;
tmElements_t oneHrAgo, oneHrAgoLocal;
breakTime(now-3600, oneHrAgo);
uint16_t val = ds->getHourImport(oneHrAgo.Hour) / 10;
ret |= updateMax(val, local.Day);
breakTime(tz->toLocal(now-3600), oneHrAgoLocal);
ret |= updateMax(val, oneHrAgoLocal.Day);
currentHour = local.Hour; // Need to be defined here so that day cost is correctly calculated
if(local.Hour > 0) {
calcDayCost();
}
use = 0;
produce = 0;
costHour = 0;
currentHour = local.Hour;
incomeHour = 0;
if(local.Day != currentDay) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EnergyAccounting) New day %d\n", local.Day);
data.costYesterday = costDay * 10;
data.costThisMonth += costDay;
costDay = 0;
data.incomeYesterday = incomeDay * 10;
data.incomeThisMonth += incomeDay;
incomeDay = 0;
currentDay = local.Day;
ret = true;
}
@@ -96,6 +111,8 @@ bool EnergyAccounting::update(AmsData* amsData) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EnergyAccounting) New month %d\n", local.Month);
data.costLastMonth = data.costThisMonth;
data.costThisMonth = 0;
data.incomeLastMonth = data.incomeThisMonth;
data.incomeThisMonth = 0;
for(uint8_t i = 0; i < 5; i++) {
data.peaks[i] = { 0, 0 };
}
@@ -106,19 +123,31 @@ bool EnergyAccounting::update(AmsData* amsData) {
}
unsigned long ms = this->lastUpdateMillis > amsData->getLastUpdateMillis() ? 0 : amsData->getLastUpdateMillis() - this->lastUpdateMillis;
float kwh = (amsData->getActiveImportPower() * (((float) ms) / 3600000.0)) / 1000.0;
float kwhi = (amsData->getActiveImportPower() * (((float) ms) / 3600000.0)) / 1000.0;
float kwhe = (amsData->getActiveExportPower() * (((float) ms) / 3600000.0)) / 1000.0;
lastUpdateMillis = amsData->getLastUpdateMillis();
if(kwh > 0) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) Adding %.4f kWh\n", kwh);
use += kwh;
if(eapi != NULL && eapi->getValueForHour(0) != ENTSOE_NO_VALUE) {
float price = eapi->getValueForHour(0);
float cost = price * kwh;
if(kwhi > 0) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) Adding %.4f kWh import\n", kwhi);
use += kwhi;
if(getPriceForHour(0) != ENTSOE_NO_VALUE) {
float price = getPriceForHour(0);
float cost = price * kwhi;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) and %.4f %s\n", cost / 100.0, eapi->getCurrency());
costHour += cost;
costDay += cost;
}
}
if(kwhe > 0) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) Adding %.4f kWh export\n", kwhe);
produce += kwhe;
if(getPriceForHour(0) != ENTSOE_NO_VALUE) {
float price = getPriceForHour(0);
float income = price * kwhe;
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) and %.4f %s\n", income / 100.0, eapi->getCurrency());
incomeHour += income;
incomeDay += income;
}
}
if(config != NULL) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf("(EnergyAccounting) calculating threshold, currently at %d\n", currentThresholdIdx);
@@ -134,14 +163,20 @@ void EnergyAccounting::calcDayCost() {
tmElements_t local, utc;
breakTime(tz->toLocal(now), local);
if(eapi != NULL && eapi->getValueForHour(0) != ENTSOE_NO_VALUE) {
if(initPrice) costDay = 0;
for(int i = 0; i < local.Hour; i++) {
float price = eapi->getValueForHour(i - local.Hour);
if(eapi != NULL && getPriceForHour(0) != ENTSOE_NO_VALUE) {
if(initPrice) {
costDay = 0;
incomeDay = 0;
}
for(int i = 0; i < currentHour; i++) {
float price = getPriceForHour(i - local.Hour);
if(price == ENTSOE_NO_VALUE) break;
breakTime(now - ((local.Hour - i) * 3600), utc);
int16_t wh = ds->getHourImport(utc.Hour);
costDay += price * (wh / 1000.0);
wh = ds->getHourExport(utc.Hour);
incomeDay += price * (wh / 1000.0);
}
initPrice = true;
}
@@ -151,23 +186,62 @@ double EnergyAccounting::getUseThisHour() {
return use;
}
double EnergyAccounting::getCostThisHour() {
return costHour;
}
double EnergyAccounting::getUseToday() {
float ret = 0.0;
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0;
tmElements_t local, utc;
if(now < BUILD_EPOCH) return 0.0;
if(tz == NULL) return 0.0;
tmElements_t utc, local;
breakTime(tz->toLocal(now), local);
for(int i = 0; i < local.Hour; i++) {
for(int i = 0; i < currentHour; i++) {
breakTime(now - ((local.Hour - i) * 3600), utc);
ret += ds->getHourImport(utc.Hour) / 1000.0;
}
return ret + getUseThisHour();
}
double EnergyAccounting::getUseThisMonth() {
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0.0;
float ret = 0;
for(int i = 0; i < currentDay; i++) {
ret += ds->getDayImport(i) / 1000.0;
}
return ret + getUseToday();
}
double EnergyAccounting::getProducedThisHour() {
return produce;
}
double EnergyAccounting::getProducedToday() {
float ret = 0.0;
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0.0;
tmElements_t utc, local;
breakTime(tz->toLocal(now), local);
for(int i = 0; i < currentHour; i++) {
breakTime(now - ((local.Hour - i) * 3600), utc);
ret += ds->getHourExport(utc.Hour) / 1000.0;
}
return ret + getProducedThisHour();
}
double EnergyAccounting::getProducedThisMonth() {
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0.0;
float ret = 0;
for(int i = 0; i < currentDay; i++) {
ret += ds->getDayExport(i) / 1000.0;
}
return ret + getProducedToday();
}
double EnergyAccounting::getCostThisHour() {
return costHour;
}
double EnergyAccounting::getCostToday() {
return costDay;
}
@@ -176,21 +250,6 @@ double EnergyAccounting::getCostYesterday() {
return data.costYesterday / 10.0;
}
double EnergyAccounting::getUseThisMonth() {
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0;
tmElements_t tm;
if(tz != NULL)
breakTime(tz->toLocal(now), tm);
else
breakTime(now, tm);
float ret = 0;
for(int i = 0; i < tm.Day; i++) {
ret += ds->getDayImport(i) / 1000.0;
}
return ret + getUseToday();
}
double EnergyAccounting::getCostThisMonth() {
return data.costThisMonth + getCostToday();
}
@@ -199,6 +258,26 @@ uint16_t EnergyAccounting::getCostLastMonth() {
return data.costLastMonth;
}
double EnergyAccounting::getIncomeThisHour() {
return incomeHour;
}
double EnergyAccounting::getIncomeToday() {
return incomeDay;
}
double EnergyAccounting::getIncomeYesterday() {
return data.incomeYesterday / 10.0;
}
double EnergyAccounting::getIncomeThisMonth() {
return data.incomeThisMonth + getIncomeToday();
}
uint16_t EnergyAccounting::getIncomeLastMonth() {
return data.incomeLastMonth;
}
uint8_t EnergyAccounting::getCurrentThreshold() {
if(config == NULL)
return 0;
@@ -206,11 +285,45 @@ uint8_t EnergyAccounting::getCurrentThreshold() {
}
float EnergyAccounting::getMonthMax() {
if(config == NULL)
return 0.0;
uint8_t count = 0;
uint32_t maxHour = 0.0;
bool included[5] = { false, false, false, false, false };
while(count < config->hours) {
for(uint8_t x = 0;x < min((uint8_t) 5, config->hours); x++) {
uint8_t maxIdx = 0;
uint16_t maxVal = 0;
for(uint8_t i = 0; i < 5; i++) {
if(included[i]) continue;
if(data.peaks[i].day == 0) continue;
if(data.peaks[i].value > maxVal) {
maxVal = data.peaks[i].value;
maxIdx = i;
}
}
if(maxVal > 0) {
included[maxIdx] = true;
count++;
}
}
for(uint8_t i = 0; i < 5; i++) {
if(!included[i]) continue;
maxHour += data.peaks[i].value;
}
return maxHour > 0 ? maxHour / count / 100.0 : 0.0;
}
EnergyAccountingPeak EnergyAccounting::getPeak(uint8_t num) {
if(config == NULL)
return EnergyAccountingPeak({0,0});
if(num < 1 || num > 5) return EnergyAccountingPeak({0,0});
uint8_t count = 0;
bool included[5] = { false, false, false, false, false };
for(uint8_t x = 0;x < min((uint8_t) 5, config->hours); x++) {
uint8_t maxIdx = 0;
uint16_t maxVal = 0;
for(uint8_t i = 0; i < 5; i++) {
@@ -220,17 +333,21 @@ float EnergyAccounting::getMonthMax() {
maxIdx = i;
}
}
included[maxIdx] = true;
count++;
}
for(uint8_t i = 0; i < 5; i++) {
if(!included[i]) continue;
if(data.peaks[i].day > 0) {
maxHour += data.peaks[i].value;
if(maxVal > 0) {
included[maxIdx] = true;
count++;
}
}
return maxHour > 0 ? maxHour / count / 100.0 : 0.0;
uint8_t pos = 0;
for(uint8_t i = 0; i < 5; i++) {
if(!included[i]) continue;
pos++;
if(pos == num) {
return data.peaks[i];
}
}
return EnergyAccountingPeak({0,0});
}
bool EnergyAccounting::load() {
@@ -248,13 +365,29 @@ bool EnergyAccounting::load() {
file.readBytes(buf, file.size());
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EnergyAccounting) Data version %d\n", buf[0]);
if(buf[0] == 4) {
if(buf[0] == 5) {
EnergyAccountingData* data = (EnergyAccountingData*) buf;
memcpy(&this->data, data, sizeof(this->data));
ret = true;
} else if(buf[0] == 4) {
EnergyAccountingData4* data = (EnergyAccountingData4*) buf;
this->data = { 5, data->month,
data->costYesterday,
data->costThisMonth,
data->costLastMonth,
0,0,0, // Income from production
data->peaks[0].day, data->peaks[0].value,
data->peaks[1].day, data->peaks[1].value,
data->peaks[2].day, data->peaks[2].value,
data->peaks[3].day, data->peaks[3].value,
data->peaks[4].day, data->peaks[4].value
};
ret = true;
} else if(buf[0] == 3) {
EnergyAccountingData* data = (EnergyAccountingData*) buf;
this->data = { 4, data->month,
this->data = { 5, data->month,
(uint16_t) (data->costYesterday / 10), (uint16_t) (data->costThisMonth / 100), (uint16_t) (data->costLastMonth / 100),
0,0,0, // Income from production
data->peaks[0].day, data->peaks[0].value,
data->peaks[1].day, data->peaks[1].value,
data->peaks[2].day, data->peaks[2].value,
@@ -263,8 +396,9 @@ bool EnergyAccounting::load() {
};
ret = true;
} else {
data = { 4, 0,
0, 0, 0,
data = { 5, 0,
0, 0, 0, // Cost
0,0,0, // Income from production
0, 0, // Peak 1
0, 0, // Peak 2
0, 0, // Peak 3
@@ -282,7 +416,7 @@ bool EnergyAccounting::load() {
this->data.peaks[b].day = b;
memcpy(&this->data.peaks[b].value, buf+i, 2);
b++;
if(b >= config->hours) break;
if(b >= config->hours || b >= 5) break;
}
ret = true;
} else if(buf[0] == 1) {
@@ -368,4 +502,14 @@ bool EnergyAccounting::updateMax(uint16_t val, uint8_t day) {
return true;
}
return false;
}
void EnergyAccounting::setFixedPrice(double price) {
this->fixedPrice = price;
}
double EnergyAccounting::getPriceForHour(uint8_t h) {
if(fixedPrice > 0.0) return fixedPrice;
if(eapi == NULL) return ENTSOE_NO_VALUE;
return eapi->getValueForHour(h);
}

View File

@@ -2,6 +2,7 @@
#define _ENTSOEA44PARSER_H
#include "Stream.h"
#include "PricesContainer.h"
#define DOCPOS_SEEK 0
#define DOCPOS_CURRENCY 1
@@ -14,6 +15,7 @@
class EntsoeA44Parser: public Stream {
public:
EntsoeA44Parser();
virtual ~EntsoeA44Parser();
char* getCurrency();
char* getMeasurementUnit();
@@ -25,11 +27,12 @@ public:
void flush();
size_t write(const uint8_t *buffer, size_t size);
size_t write(uint8_t);
void get(PricesContainer*);
private:
char currency[4];
char measurementUnit[4];
float points[24];
float points[25];
char buf[64];
uint8_t pos = 0;

View File

@@ -4,8 +4,8 @@
#include "TimeLib.h"
#include "Timezone.h"
#include "RemoteDebug.h"
#include "EntsoeA44Parser.h"
#include "AmsConfiguration.h"
#include "EntsoeA44Parser.h"
#if defined(ESP8266)
#include <ESP8266HTTPClient.h>
@@ -25,33 +25,44 @@ public:
char* getToken();
char* getCurrency();
char* getArea();
float getValueForHour(int8_t);
float getValueForHour(time_t, int8_t);
int16_t getLastError();
private:
RemoteDebug* debugger;
EntsoeConfig* config = NULL;
HTTPClient http;
uint8_t currentDay = 0, currentHour = 0;
uint32_t tomorrowFetchMillis = 36000000; // Number of ms before midnight. Default fetch 10hrs before midnight (14:00 CE(S)T)
uint64_t midnightMillis = 0;
uint8_t tomorrowFetchMinute = 15; // How many minutes over 13:00 should it fetch prices
uint64_t lastTodayFetch = 0;
uint64_t lastTomorrowFetch = 0;
uint64_t lastCurrencyFetch = 0;
EntsoeA44Parser* today = NULL;
EntsoeA44Parser* tomorrow = NULL;
PricesContainer* today = NULL;
PricesContainer* tomorrow = NULL;
Timezone* tz = NULL;
static const uint16_t BufferSize = 256;
char* buf;
bool hub = false;
uint8_t* key = NULL;
uint8_t* auth = NULL;
float currencyMultiplier = 0;
int16_t lastError = 0;
PricesContainer* fetchPrices(time_t);
bool retrieve(const char* url, Stream* doc);
float getCurrencyMultiplier(const char* from, const char* to);
float getCurrencyMultiplier(const char* from, const char* to, time_t t);
void printD(String fmt, ...);
void printE(String fmt, ...);
void debugPrint(byte *buffer, int start, int length);
};
#endif

View File

@@ -0,0 +1,8 @@
#ifndef _PRICESCONTAINER_H
#define _PRICESCONTAINER_H
struct PricesContainer {
char currency[4];
char measurementUnit[4];
int32_t points[25];
};
#endif

View File

@@ -22,7 +22,7 @@ void DnbCurrParser::flush() {
}
size_t DnbCurrParser::write(const uint8_t *buffer, size_t size) {
for(int i = 0; i < size; i++) {
for(size_t i = 0; i < size; i++) {
write(buffer[i]);
}
return size;

View File

@@ -2,7 +2,11 @@
#include "HardwareSerial.h"
EntsoeA44Parser::EntsoeA44Parser() {
for(int i = 0; i < 24; i++) points[i] = ENTSOE_NO_VALUE;
for(int i = 0; i < 25; i++) points[i] = ENTSOE_NO_VALUE;
}
EntsoeA44Parser::~EntsoeA44Parser() {
}
char* EntsoeA44Parser::getCurrency() {
@@ -14,7 +18,7 @@ char* EntsoeA44Parser::getMeasurementUnit() {
}
float EntsoeA44Parser::getPoint(uint8_t position) {
if(position >= 24) return ENTSOE_NO_VALUE;
if(position >= 25) return ENTSOE_NO_VALUE;
return points[position];
}
@@ -35,7 +39,7 @@ void EntsoeA44Parser::flush() {
}
size_t EntsoeA44Parser::write(const uint8_t *buffer, size_t size) {
for(int i = 0; i < size; i++) {
for(size_t i = 0; i < size; i++) {
write(buffer[i]);
}
return size;
@@ -102,3 +106,12 @@ size_t EntsoeA44Parser::write(uint8_t byte) {
}
return 1;
}
void EntsoeA44Parser::get(PricesContainer* container) {
strcpy(container->currency, currency);
strcpy(container->measurementUnit, measurementUnit);
for(uint8_t i = 0; i < 25; i++) {
container->points[i] = points[i] == ENTSOE_NO_VALUE ? ENTSOE_NO_VALUE : points[i] * 10000;
}
}

View File

@@ -0,0 +1,426 @@
#include "EntsoeApi.h"
#include <EEPROM.h>
#include "Uptime.h"
#include "TimeLib.h"
#include "DnbCurrParser.h"
#include "version.h"
#include "GcmParser.h"
#if defined(ESP32)
#include <esp_task_wdt.h>
#endif
EntsoeApi::EntsoeApi(RemoteDebug* Debug) {
this->buf = (char*) malloc(BufferSize);
debugger = Debug;
// Entso-E uses CET/CEST
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120};
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60};
tz = new Timezone(CEST, CET);
tomorrowFetchMinute = 15 + random(45); // Random between 13:15 and 14:00
}
void EntsoeApi::setup(EntsoeConfig& config) {
if(this->config == NULL) {
this->config = new EntsoeConfig();
}
memcpy(this->config, &config, sizeof(config));
lastTodayFetch = lastTomorrowFetch = lastCurrencyFetch = 0;
if(today != NULL) delete today;
if(tomorrow != NULL) delete tomorrow;
today = tomorrow = NULL;
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http.setReuse(false);
http.setTimeout(60000);
http.setUserAgent("ams2mqtt/" + String(VERSION));
http.useHTTP10(true);
#if defined(AMS2MQTT_PRICE_KEY)
key = new uint8_t[16] AMS2MQTT_PRICE_KEY;
hub = true;
#else
hub = false;
#endif
#if defined(AMS2MQTT_PRICE_AUTHENTICATION)
auth = new uint8_t[16] AMS2MQTT_PRICE_AUTHENTICATION;
hub = hub && true;
#else
hub = false;
#endif
}
char* EntsoeApi::getToken() {
return this->config->token;
}
char* EntsoeApi::getCurrency() {
return this->config->currency;
}
char* EntsoeApi::getArea() {
return this->config->area;
}
float EntsoeApi::getValueForHour(int8_t hour) {
time_t cur = time(nullptr);
return getValueForHour(cur, hour);
}
float EntsoeApi::getValueForHour(time_t ts, int8_t hour) {
tmElements_t tm;
int8_t pos = hour;
breakTime(tz->toLocal(ts), tm);
while(tm.Hour > 0) {
ts -= 3600;
breakTime(tz->toLocal(ts), tm);
pos++;
}
uint8_t hoursToday = 0;
uint8_t todayDate = tm.Day;
while(tm.Day == todayDate) {
ts += 3600;
breakTime(tz->toLocal(ts), tm);
hoursToday++;
}
if(pos >= 48)
return ENTSOE_NO_VALUE;
double value = ENTSOE_NO_VALUE;
double multiplier = config->multiplier / 1000.0;
if(pos >= hoursToday) {
if(tomorrow == NULL)
return ENTSOE_NO_VALUE;
if(tomorrow->points[pos-hoursToday] == ENTSOE_NO_VALUE)
return ENTSOE_NO_VALUE;
value = tomorrow->points[pos-hoursToday] / 10000.0;
if(strcmp(tomorrow->measurementUnit, "KWH") == 0) {
// Multiplier is 1
} else if(strcmp(tomorrow->measurementUnit, "MWH") == 0) {
multiplier *= 0.001;
} else {
return ENTSOE_NO_VALUE;
}
float mult = getCurrencyMultiplier(tomorrow->currency, config->currency, time(nullptr));
if(mult == 0) return ENTSOE_NO_VALUE;
multiplier *= mult;
} else if(pos >= 0) {
if(today == NULL)
return ENTSOE_NO_VALUE;
if(today->points[pos] == ENTSOE_NO_VALUE)
return ENTSOE_NO_VALUE;
value = today->points[pos] / 10000.0;
if(strcmp(today->measurementUnit, "KWH") == 0) {
// Multiplier is 1
} else if(strcmp(today->measurementUnit, "MWH") == 0) {
multiplier *= 0.001;
} else {
return ENTSOE_NO_VALUE;
}
float mult = getCurrencyMultiplier(today->currency, config->currency, time(nullptr));
if(mult == 0) return ENTSOE_NO_VALUE;
multiplier *= mult;
}
return value * multiplier;
}
bool EntsoeApi::loop() {
uint64_t now = millis64();
if(now < 10000) return false; // Grace period
time_t t = time(nullptr);
if(t < BUILD_EPOCH) return false;
#ifndef AMS2MQTT_PRICE_KEY
if(strlen(getToken()) == 0) {
return false;
}
#endif
if(!config->enabled)
return false;
if(strlen(config->area) == 0)
return false;
if(strlen(config->currency) == 0)
return false;
tmElements_t tm;
breakTime(tz->toLocal(t), tm);
if(currentDay == 0) {
currentDay = tm.Day;
currentHour = tm.Hour;
}
if(currentDay != tm.Day) {
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Rotating price objects at %lu\n", t);
if(today != NULL) delete today;
if(tomorrow != NULL) {
today = tomorrow;
tomorrow = NULL;
}
currentDay = tm.Day;
currentHour = tm.Hour;
return today != NULL; // Only trigger MQTT publish if we have todays prices.
} else if(currentHour != tm.Hour) {
currentHour = tm.Hour;
return today != NULL; // Only trigger MQTT publish if we have todays prices.
}
bool readyToFetchForTomorrow = tomorrow == NULL && (tm.Hour > 13 || (tm.Hour == 13 && tm.Minute >= tomorrowFetchMinute)) && (lastTomorrowFetch == 0 || now - lastTomorrowFetch > 900000);
if(today == NULL && (lastTodayFetch == 0 || now - lastTodayFetch > 60000)) {
try {
lastTodayFetch = now;
today = fetchPrices(t);
} catch(const std::exception& e) {
if(lastError == 0) lastError = 900;
today = NULL;
}
return today != NULL && !readyToFetchForTomorrow; // Only trigger MQTT publish if we have todays prices and we are not immediately ready to fetch price for tomorrow.
}
// Prices for next day are published at 13:00 CE(S)T, but to avoid heavy server traffic at that time, we will
// fetch with one hour (with some random delay) and retry every 15 minutes
if(readyToFetchForTomorrow) {
try {
lastTomorrowFetch = now;
tomorrow = fetchPrices(t+SECS_PER_DAY);
} catch(const std::exception& e) {
if(lastError == 0) lastError = 900;
tomorrow = NULL;
}
return tomorrow != NULL;
}
return false;
}
bool EntsoeApi::retrieve(const char* url, Stream* doc) {
#if defined(ESP32)
if(http.begin(url)) {
printD("Connection established");
#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif
int status = http.GET();
#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif
if(status == HTTP_CODE_OK) {
printD("Receiving data");
http.writeToStream(doc);
http.end();
lastError = 0;
return true;
} else {
lastError = status;
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf("(EntsoeApi) Communication error, returned status: %d\n", status);
printE(http.errorToString(status));
printD(http.getString());
http.end();
return false;
}
} else {
return false;
}
#endif
return false;
}
float EntsoeApi::getCurrencyMultiplier(const char* from, const char* to, time_t t) {
if(strcmp(from, to) == 0)
return 1.00;
uint64_t now = millis64();
if(now > lastCurrencyFetch && (lastCurrencyFetch == 0 || (now - lastCurrencyFetch) > 60000)) {
lastCurrencyFetch = now;
DnbCurrParser p;
#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif
snprintf(buf, BufferSize, "https://data.norges-bank.no/api/data/EXR/M.%s.NOK.SP?lastNObservations=1", from);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Retrieving %s to NOK conversion\n", from);
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) url: %s\n", buf);
if(retrieve(buf, &p)) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) got exchange rate %.4f\n", p.getValue());
currencyMultiplier = p.getValue();
if(strncmp(to, "NOK", 3) != 0) {
snprintf(buf, BufferSize, "https://data.norges-bank.no/api/data/EXR/M.%s.NOK.SP?lastNObservations=1", to);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Retrieving %s to NOK conversion\n", to);
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) url: %s\n", buf);
if(retrieve(buf, &p)) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) got exchange rate %.4f\n", p.getValue());
currencyMultiplier /= p.getValue();
} else {
return 0;
}
}
} else {
return 0;
}
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) Resulting currency multiplier: %.4f\n", currencyMultiplier);
tmElements_t tm;
breakTime(t, tm);
lastCurrencyFetch = now + (SECS_PER_DAY * 1000) - (((((tm.Hour * 60) + tm.Minute) * 60) + tm.Second) * 1000);
}
return currencyMultiplier;
}
PricesContainer* EntsoeApi::fetchPrices(time_t t) {
tmElements_t tm;
breakTime(t, tm);
if(strlen(getToken()) > 0) {
time_t e1 = t - (tm.Hour * 3600) - (tm.Minute * 60) - tm.Second; // UTC midnight
time_t e2 = e1 + SECS_PER_DAY;
tmElements_t d1, d2;
breakTime(tz->toUTC(e1), d1); // To get day and hour for CET/CEST at UTC midnight
breakTime(tz->toUTC(e2), d2);
snprintf(buf, BufferSize, "%s?securityToken=%s&documentType=A44&periodStart=%04d%02d%02d%02d%02d&periodEnd=%04d%02d%02d%02d%02d&in_Domain=%s&out_Domain=%s",
"https://web-api.tp.entsoe.eu/api", getToken(),
d1.Year+1970, d1.Month, d1.Day, d1.Hour, 00,
d2.Year+1970, d2.Month, d2.Day, d2.Hour, 00,
config->area, config->area);
#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Fetching prices for %d.%d.%d\n", tm.Day, tm.Month, tm.Year+1970);
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) url: %s\n", buf);
EntsoeA44Parser a44;
if(retrieve(buf, &a44) && a44.getPoint(0) != ENTSOE_NO_VALUE) {
PricesContainer* ret = new PricesContainer();
a44.get(ret);
return ret;
} else {
return NULL;
}
} else if(hub) {
String data;
snprintf(buf, BufferSize, "%s/%s/%d/%d/%d?currency=%s",
"http://hub.amsleser.no/hub/price",
config->area,
tm.Year+1970,
tm.Month,
tm.Day,
config->currency
);
#if defined(ESP8266)
WiFiClient client;
client.setTimeout(5000);
if(http.begin(client, buf)) {
#elif defined(ESP32)
if(http.begin(buf)) {
#endif
int status = http.GET();
#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif
if(status == HTTP_CODE_OK) {
printD("Receiving data");
data = http.getString();
http.end();
uint8_t* content = (uint8_t*) (data.c_str());
if(debugger->isActive(RemoteDebug::DEBUG)) {
printD("Received content for prices:");
debugPrint(content, 0, data.length());
}
DataParserContext ctx;
ctx.length = data.length();
GCMParser gcm(key, auth);
int8_t gcmRet = gcm.parse(content, ctx);
if(debugger->isActive(RemoteDebug::DEBUG)) {
printD("Decrypted content for prices:");
debugPrint(content, 0, data.length());
}
if(gcmRet > 0) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) Price data starting at: %d\n", gcmRet);
PricesContainer* ret = new PricesContainer();
for(uint8_t i = 0; i < 25; i++) {
ret->points[i] = ENTSOE_NO_VALUE;
}
memcpy(ret, content+gcmRet, sizeof(*ret));
for(uint8_t i = 0; i < 25; i++) {
ret->points[i] = ntohl(ret->points[i]);
}
lastError = 0;
return ret;
} else {
lastError = gcmRet;
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf("(EntsoeApi) Error code while decrypting prices: %d\n", gcmRet);
}
} else {
lastError = status;
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf("(EntsoeApi) Communication error, returned status: %d\n", status);
printE(http.errorToString(status));
printD(http.getString());
http.end();
}
}
}
return NULL;
}
void EntsoeApi::printD(String fmt, ...) {
va_list args;
va_start(args, fmt);
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(String("(EntsoeApi)" + fmt + "\n").c_str(), args);
va_end(args);
}
void EntsoeApi::printE(String fmt, ...) {
va_list args;
va_start(args, fmt);
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(String("(EntsoeApi)" + fmt + "\n").c_str(), args);
va_end(args);
}
void EntsoeApi::debugPrint(byte *buffer, int start, int length) {
for (int i = start; i < start + length; i++) {
if (buffer[i] < 0x10)
debugger->print("0");
debugger->print(buffer[i], HEX);
debugger->print(" ");
if ((i - start + 1) % 16 == 0)
debugger->println("");
else if ((i - start + 1) % 4 == 0)
debugger->print(" ");
yield(); // Let other get some resources too
}
debugger->println("");
}
int16_t EntsoeApi::getLastError() {
return lastError;
}

View File

@@ -0,0 +1 @@
json/*.h

View File

@@ -0,0 +1,163 @@
#ifndef _HOMEASSISTANTMQTTHANDLER_H
#define _HOMEASSISTANTMQTTHANDLER_H
#include "AmsMqttHandler.h"
#include "HomeAssistantStatic.h"
#include "AmsConfiguration.h"
class HomeAssistantMqttHandler : public AmsMqttHandler {
public:
HomeAssistantMqttHandler(MQTTClient* mqtt, char* buf, const char* clientId, const char* topic, uint8_t boardType, HomeAssistantConfig config, HwTools* hw) : AmsMqttHandler(mqtt, buf) {
this->clientId = clientId;
this->topic = String(topic);
this->hw = hw;
l1Init = l2Init = l2eInit = l3Init = l3eInit = l4Init = l4eInit = rtInit = rteInit = pInit = sInit = false;
if(strlen(config.discoveryNameTag) > 0) {
snprintf_P(buf, 128, PSTR("AMS reader (%s)"), config.discoveryNameTag);
deviceName = String(buf);
snprintf_P(buf, 128, PSTR("[%s] "), config.discoveryNameTag);
sensorNamePrefix = String(buf);
} else {
deviceName = F("AMS reader");
sensorNamePrefix = "";
}
deviceModel = boardTypeToString(boardType);
manufacturer = boardManufacturerToString(boardType);
#if defined(ESP8266)
String hostname = WiFi.hostname();
#elif defined(ESP32)
String hostname = WiFi.getHostname();
#endif
deviceUid = hostname; // Maybe configurable in the future?
if(strlen(config.discoveryHostname) > 0) {
if(strncmp_P(config.discoveryHostname, PSTR("http"), 4) == 0) {
deviceUrl = String(config.discoveryHostname);
} else {
snprintf_P(buf, 128, PSTR("http://%s/"), config.discoveryHostname);
deviceUrl = String(buf);
}
} else {
snprintf_P(buf, 128, PSTR("http://%s.local/"), hostname);
deviceUrl = String(buf);
}
if(strlen(config.discoveryPrefix) > 0) {
snprintf_P(buf, 128, PSTR("%s/sensor/"), config.discoveryPrefix);
discoveryTopic = String(buf);
} else {
discoveryTopic = F("homeassistant/sensor/");
}
};
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, EntsoeApi* eapi);
bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(EntsoeApi*);
bool publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea);
void publishSensor(const HomeAssistantSensor& sensor);
void publishList1Sensors();
void publishList1ExportSensors();
void publishList2Sensors();
void publishList2ExportSensors();
void publishList3Sensors();
void publishList3ExportSensors();
void publishList4Sensors();
void publishList4ExportSensors();
void publishRealtimeSensors(EnergyAccounting* ea, EntsoeApi* eapi);
void publishRealtimeExportSensors(EnergyAccounting* ea, EntsoeApi* eapi);
void publishTemperatureSensor(uint8_t index, String id);
void publishPriceSensors(EntsoeApi* eapi);
void publishSystemSensors();
private:
String deviceName;
String deviceModel;
String deviceUid;
String manufacturer;
String deviceUrl;
String discoveryTopic;
String sensorNamePrefix;
bool l1Init, l2Init, l2eInit, l3Init, l3eInit, l4Init, l4eInit, rtInit, rteInit, pInit, sInit;
bool tInit[32] = {false};
bool prInit[38] = {false};
String clientId;
String topic;
HwTools* hw;
String boardTypeToString(uint8_t b) {
switch(b) {
case 5:
#if defined(ESP8266)
return F("Pow-K");
#elif defined(ESP32)
return F("Pow-K+");
#endif
case 7:
#if defined(ESP8266)
return F("Pow-U");
#elif defined(ESP32)
return F("Pow-U+");
#endif
case 6:
return F("Pow-P1");
case 51:
return F("S2 mini");
case 50:
return F("ESP32-S2");
case 201:
return F("LOLIN D32");
case 202:
return F("HUZZAH32");
case 203:
return F("DevKitC");
case 200:
return F("ESP32");
case 2:
return F("HAN Reader 2.0 by Max Spencer");
case 0:
return F("Custom hardware by Roar Fredriksen");
case 1:
return F("Kamstrup module by Egil Opsahl");
case 3:
return F("Pow-K");
case 4:
return F("Pow-U");
case 101:
return F("D1 mini");
case 100:
return F("ESP8266");
case 70:
return F("ESP32-C3");
case 71:
return F("ESP32-C3-DevKitM-1");
}
#if defined(ESP8266)
return F("ESP8266");
#elif defined(ESP32)
return F("ESP32");
#endif
};
String boardManufacturerToString(uint8_t b) {
if(b >= 3 && b <= 7)
return F("amsleser.no");
if(b < 50)
return F("Custom");
switch(b) {
case 51:
case 101:
case 201:
return F("Wemos");
case 202:
return F("Adafruit");
}
return F("Espressif");
};
};
#endif

View File

@@ -0,0 +1,112 @@
#ifndef _HOMEASSISTANTSTATIC_H
#define _HOMEASSISTANTSTATIC_H
#include "Arduino.h"
typedef struct HomeAssistantSensor {
const char* name;
const char* topic;
const char* path;
const char* uom;
const char* devcl;
const char* stacl;
};
const uint8_t List1SensorCount PROGMEM = 1;
const HomeAssistantSensor List1Sensors[List1SensorCount] PROGMEM = {
{"Active import", "/power", "P", "W", "power", "\"measurement\""}
};
const uint8_t List2SensorCount PROGMEM = 8;
const HomeAssistantSensor List2Sensors[List2SensorCount] PROGMEM = {
{"Reactive import", "/power", "Q", "var", "reactive_power", "\"measurement\""},
{"Reactive export", "/power", "QO", "var", "reactive_power", "\"measurement\""},
{"L1 current", "/power", "I1", "A", "current", "\"measurement\""},
{"L2 current", "/power", "I2", "A", "current", "\"measurement\""},
{"L3 current", "/power", "I3", "A", "current", "\"measurement\""},
{"L1 voltage", "/power", "U1", "V", "voltage", "\"measurement\""},
{"L2 voltage", "/power", "U2", "V", "voltage", "\"measurement\""},
{"L3 voltage", "/power", "U3", "V", "voltage", "\"measurement\""}
};
const uint8_t List2ExportSensorCount PROGMEM = 1;
const HomeAssistantSensor List2ExportSensors[List2ExportSensorCount] PROGMEM = {
{"Active export", "/power", "PO", "W", "power", "\"measurement\""}
};
const uint8_t List3SensorCount PROGMEM = 3;
const HomeAssistantSensor List3Sensors[List3SensorCount] PROGMEM = {
{"Accumulated active import", "/energy", "tPI", "kWh", "energy", "\"total_increasing\""},
{"Accumulated reactive import","/energy", "tQI", "", "energy", "\"total_increasing\""},
{"Accumulated reactive export","/energy", "tQO", "", "energy", "\"total_increasing\""}
};
const uint8_t List3ExportSensorCount PROGMEM = 1;
const HomeAssistantSensor List3ExportSensors[List3ExportSensorCount] PROGMEM = {
{"Accumulated active export", "/energy", "tPO", "kWh", "energy", "\"total_increasing\""}
};
const uint8_t List4SensorCount PROGMEM = 7;
const HomeAssistantSensor List4Sensors[List4SensorCount] PROGMEM = {
{"Power factor", "/power", "PF", "%", "power_factor", "\"measurement\""},
{"L1 power factor", "/power", "PF1", "%", "power_factor", "\"measurement\""},
{"L2 power factor", "/power", "PF2", "%", "power_factor", "\"measurement\""},
{"L3 power factor", "/power", "PF3", "%", "power_factor", "\"measurement\""},
{"L1 active import", "/power", "P1", "W", "power", "\"measurement\""},
{"L2 active import", "/power", "P2", "W", "power", "\"measurement\""},
{"L3 active import", "/power", "P3", "W", "power", "\"measurement\""}
};
const uint8_t List4ExportSensorCount PROGMEM = 3;
const HomeAssistantSensor List4ExportSensors[List4ExportSensorCount] PROGMEM = {
{"L1 active export", "/power", "PO1", "W", "power", "\"measurement\""},
{"L2 active export", "/power", "PO2", "W", "power", "\"measurement\""},
{"L3 active export", "/power", "PO3", "W", "power", "\"measurement\""}
};
const uint8_t RealtimeSensorCount PROGMEM = 8;
const HomeAssistantSensor RealtimeSensors[RealtimeSensorCount] PROGMEM = {
{"Month max", "/realtime","max", "kWh", "energy", "\"total_increasing\""},
{"Tariff threshold", "/realtime","threshold", "kWh", "energy", "\"total_increasing\""},
{"Current hour used", "/realtime","hour.use", "kWh", "energy", "\"total_increasing\""},
{"Current hour cost", "/realtime","hour.cost", "", "monetary", ""},
{"Current day used", "/realtime","day.use", "kWh", "energy", "\"total_increasing\""},
{"Current day cost", "/realtime","day.cost", "", "monetary", ""},
{"Current month used", "/realtime","month.use", "kWh", "energy", "\"total_increasing\""},
{"Current month cost", "/realtime","month.cost", "", "monetary", ""}
};
const uint8_t RealtimeExportSensorCount PROGMEM = 6;
const HomeAssistantSensor RealtimeExportSensors[RealtimeExportSensorCount] PROGMEM = {
{"Current hour produced", "/realtime","hour.produced", "kWh", "energy", "\"total_increasing\""},
{"Current hour income", "/realtime","hour.income", "", "monetary", ""},
{"Current day produced", "/realtime","day.produced", "kWh", "energy", "\"total_increasing\""},
{"Current day income", "/realtime","day.income", "", "monetary", ""},
{"Current month produced", "/realtime","month.produced", "kWh", "energy", "\"total_increasing\""},
{"Current month income", "/realtime","month.income", "", "monetary", ""}
};
const HomeAssistantSensor RealtimePeakSensor PROGMEM = {"Current month peak %d", "/realtime", "peaks[%d]", "kWh", "energy", ""};
const uint8_t PriceSensorCount PROGMEM = 5;
const HomeAssistantSensor PriceSensors[PriceSensorCount] PROGMEM = {
{"Minimum price ahead", "/prices", "prices.min", "", "monetary", ""},
{"Maximum price ahead", "/prices", "prices.max", "", "monetary", ""},
{"Cheapest 1hr period ahead", "/prices", "prices.cheapest1hr","", "timestamp", ""},
{"Cheapest 3hr period ahead", "/prices", "prices.cheapest3hr","", "timestamp", ""},
{"Cheapest 6hr period ahead", "/prices", "prices.cheapest6hr","", "timestamp", ""}
};
const HomeAssistantSensor PriceSensor PROGMEM = {"Price in %02d %s", "/prices", "prices['%d']", "", "monetary", ""};
const uint8_t SystemSensorCount PROGMEM = 2;
const HomeAssistantSensor SystemSensors[SystemSensorCount] PROGMEM = {
{"Status", "/state", "rssi", "dBm", "signal_strength", "\"measurement\""},
{"Supply volt", "/state", "vcc", "V", "voltage", "\"measurement\""}
};
const HomeAssistantSensor TemperatureSensor PROGMEM = {"Temperature sensor %s", "/temperatures", "temperatures['%s']", "°C", "temperature", "\"measurement\""};
#endif

View File

@@ -3,5 +3,5 @@
"tPO" : %.2f,
"tQI" : %.2f,
"tQO" : %.2f,
"rtc" : %llu
"rtc" : %lu
}

View File

@@ -0,0 +1,15 @@
{
"lv" : "%s",
"id" : "%s",
"type" : "%s",
"P" : %d,
"Q" : %d,
"PO" : %d,
"QO" : %d,
"I1" : %.2f,
"I2" : %.2f,
"I3" : %.2f,
"U1" : %.2f,
"U2" : %.2f,
"U3" : %.2f
}

View File

@@ -3,8 +3,14 @@
"id" : "%s",
"type" : "%s",
"P" : %d,
"P1" : %.2f,
"P2" : %.2f,
"P3" : %.2f,
"Q" : %d,
"PO" : %d,
"PO1" : %.2f,
"PO2" : %.2f,
"PO3" : %.2f,
"QO" : %d,
"I1" : %.2f,
"I2" : %.2f,

View File

@@ -1,5 +1,5 @@
{
"name" : "%s",
"name" : "%s%s",
"stat_t" : "%s%s",
"uniq_id" : "%s_%s",
"obj_id" : "%s_%s",

View File

@@ -0,0 +1,23 @@
{
"max" : %.1f,
"peaks" : [ %s ],
"threshold" : %d,
"hour" : {
"use" : %.2f,
"cost" : %.2f,
"produced" : %.2f,
"income" : %.2f
},
"day" : {
"use" : %.2f,
"cost" : %.2f,
"produced" : %.2f,
"income" : %.2f
},
"month" : {
"use" : %.2f,
"cost" : %.2f,
"produced" : %.2f,
"income" : %.2f
}
}

View File

@@ -0,0 +1,76 @@
import os
import re
import shutil
import subprocess
try:
from css_html_js_minify import js_minify
except:
from SCons.Script import (
ARGUMENTS,
COMMAND_LINE_TARGETS,
DefaultEnvironment,
)
env = DefaultEnvironment()
env.Execute(
env.VerboseAction(
'$PYTHONEXE -m pip install "css_html_js_minify" ',
"Installing Python dependencies",
)
)
try:
from css_html_js_minify import js_minify
except:
print("WARN: Unable to load minifier")
webroot = "lib/HomeAssistantMqttHandler/json"
srcroot = "lib/HomeAssistantMqttHandler/include/json"
version = os.environ.get('GITHUB_TAG')
if version == None:
try:
result = subprocess.run(['git','rev-parse','--short','HEAD'], capture_output=True, check=False)
if result.returncode == 0:
version = result.stdout.decode('utf-8').strip()
else:
version = "SNAPSHOT"
except:
version = "SNAPSHOT"
if os.path.exists(srcroot):
shutil.rmtree(srcroot)
os.mkdir(srcroot)
else:
os.mkdir(srcroot)
for filename in os.listdir(webroot):
basename = re.sub("[^0-9a-zA-Z]+", "_", filename)
srcfile = webroot + "/" + filename
dstfile = srcroot + "/" + basename + ".h"
varname = basename.upper()
with open(srcfile, encoding="utf-8") as f:
content = f.read().replace("${version}", version)
try:
if (filename.endswith(".js") and filename != 'gaugemeter.js') or filename.endswith(".json"):
content = js_minify(content)
except:
print("WARN: Unable to minify")
with open(dstfile, "w") as dst:
dst.write("static const char ")
dst.write(varname)
dst.write("[] PROGMEM = R\"==\"==(")
dst.write(content)
dst.write(")==\"==\";\n")
dst.write("const int ");
dst.write(varname)
dst.write("_LEN PROGMEM = ");
dst.write(str(len(content)))
dst.write(";");

View File

@@ -0,0 +1,512 @@
#include "HomeAssistantMqttHandler.h"
#include "hexutils.h"
#include "Uptime.h"
#include "version.h"
#include "json/ha1_json.h"
#include "json/ha2_json.h"
#include "json/ha3_json.h"
#include "json/ha4_json.h"
#include "json/jsonsys_json.h"
#include "json/jsonprices_json.h"
#include "json/hadiscover_json.h"
#include "json/realtime_json.h"
#if defined(ESP32)
#include <esp_task_wdt.h>
#endif
bool HomeAssistantMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, EntsoeApi* eapi) {
if(topic.isEmpty() || !mqtt->connected())
return false;
if(data->getListType() >= 3) { // publish energy counts
publishList3Sensors();
if(data->getActiveExportCounter() > 0.0) publishList3ExportSensors();
snprintf_P(json, BufferSize, HA2_JSON,
data->getActiveImportCounter(),
data->getActiveExportCounter(),
data->getReactiveImportCounter(),
data->getReactiveExportCounter(),
data->getMeterTimestamp()
);
mqtt->publish(topic + "/energy", json);
mqtt->loop();
delay(10);
}
String meterModel = data->getMeterModel();
meterModel.replace("\\", "\\\\");
if(data->getListType() == 1) { // publish power counts
publishList1Sensors();
snprintf_P(json, BufferSize, HA1_JSON,
data->getActiveImportPower()
);
mqtt->publish(topic + "/power", json);
} else if(data->getListType() <= 3) { // publish power counts and volts/amps
publishList2Sensors();
if(data->getActiveExportPower() > 0) publishList2ExportSensors();
snprintf_P(json, BufferSize, HA3_JSON,
data->getListId().c_str(),
data->getMeterId().c_str(),
meterModel.c_str(),
data->getActiveImportPower(),
data->getReactiveImportPower(),
data->getActiveExportPower(),
data->getReactiveExportPower(),
data->getL1Current(),
data->getL2Current(),
data->getL3Current(),
data->getL1Voltage(),
data->getL2Voltage(),
data->getL3Voltage()
);
mqtt->publish(topic + "/power", json);
} else if(data->getListType() == 4) { // publish power counts and volts/amps/phase power and PF
publishList4Sensors();
if(data->getL1ActiveExportPower() > 0 || data->getL2ActiveExportPower() > 0 || data->getL3ActiveExportPower() > 0) publishList4ExportSensors();
snprintf_P(json, BufferSize, HA4_JSON,
data->getListId().c_str(),
data->getMeterId().c_str(),
meterModel.c_str(),
data->getActiveImportPower(),
data->getL1ActiveImportPower(),
data->getL2ActiveImportPower(),
data->getL3ActiveImportPower(),
data->getReactiveImportPower(),
data->getActiveExportPower(),
data->getL1ActiveExportPower(),
data->getL2ActiveExportPower(),
data->getL3ActiveExportPower(),
data->getReactiveExportPower(),
data->getL1Current(),
data->getL2Current(),
data->getL3Current(),
data->getL1Voltage(),
data->getL2Voltage(),
data->getL3Voltage(),
data->getPowerFactor() == 0 ? 1 : data->getPowerFactor(),
data->getPowerFactor() == 0 ? 1 : data->getL1PowerFactor(),
data->getPowerFactor() == 0 ? 1 : data->getL2PowerFactor(),
data->getPowerFactor() == 0 ? 1 : data->getL3PowerFactor()
);
mqtt->publish(topic + "/power", json);
}
if(ea->isInitialized()) {
publishRealtimeSensors(ea, eapi);
if(ea->getProducedThisHour() > 0.0 || ea->getProducedToday() > 0.0 || ea->getProducedThisMonth() > 0.0) publishRealtimeExportSensors(ea, eapi);
String peaks = "";
uint8_t peakCount = ea->getConfig()->hours;
if(peakCount > 5) peakCount = 5;
for(uint8_t i = 1; i <= peakCount; i++) {
if(!peaks.isEmpty()) peaks += ",";
peaks += String(ea->getPeak(i).value / 100.0, 2);
}
snprintf_P(json, BufferSize, REALTIME_JSON,
ea->getMonthMax(),
peaks.c_str(),
ea->getCurrentThreshold(),
ea->getUseThisHour(),
ea->getCostThisHour(),
ea->getProducedThisHour(),
ea->getIncomeThisHour(),
ea->getUseToday(),
ea->getCostToday(),
ea->getProducedToday(),
ea->getIncomeToday(),
ea->getUseThisMonth(),
ea->getCostThisMonth(),
ea->getProducedThisMonth(),
ea->getIncomeThisMonth()
);
mqtt->publish(topic + "/realtime", json);
}
return true;
}
bool HomeAssistantMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw) {
int count = hw->getTempSensorCount();
if(count < 2) return false;
int size = 32 + (count * 26);
char buf[size];
snprintf_P(buf, 24, PSTR("{\"temperatures\":{"));
for(int i = 0; i < count; i++) {
TempSensorData* data = hw->getTempSensorData(i);
if(data != NULL) {
char* pos = buf+strlen(buf);
String id = toHex(data->address, 8);
snprintf(pos, 26, "\"%s\":%.2f,",
id.c_str(),
data->lastRead
);
data->changed = false;
publishTemperatureSensor(i+1, id);
delay(1);
}
}
char* pos = buf+strlen(buf);
snprintf_P(count == 0 ? pos : pos-1, 8, PSTR("}}"));
return mqtt->publish(topic + "/temperatures", buf);
}
bool HomeAssistantMqttHandler::publishPrices(EntsoeApi* eapi) {
if(topic.isEmpty() || !mqtt->connected())
return false;
if(eapi->getValueForHour(0) == ENTSOE_NO_VALUE)
return false;
publishPriceSensors(eapi);
time_t now = time(nullptr);
float min1hr = 0.0, min3hr = 0.0, min6hr = 0.0;
int8_t min1hrIdx = -1, min3hrIdx = -1, min6hrIdx = -1;
float min = INT16_MAX, max = INT16_MIN;
float values[38];
for(int i = 0;i < 38; i++) values[i] = ENTSOE_NO_VALUE;
for(uint8_t i = 0; i < 38; i++) {
float val = eapi->getValueForHour(now, i);
values[i] = val;
if(val == ENTSOE_NO_VALUE) break;
if(val < min) min = val;
if(val > max) max = val;
if(min1hrIdx == -1 || min1hr > val) {
min1hr = val;
min1hrIdx = i;
}
if(i >= 2) {
i -= 2;
float val1 = values[i++];
float val2 = values[i++];
float val3 = val;
if(val1 == ENTSOE_NO_VALUE || val2 == ENTSOE_NO_VALUE || val3 == ENTSOE_NO_VALUE) continue;
float val3hr = val1+val2+val3;
if(min3hrIdx == -1 || min3hr > val3hr) {
min3hr = val3hr;
min3hrIdx = i-2;
}
}
if(i >= 5) {
i -= 5;
float val1 = values[i++];
float val2 = values[i++];
float val3 = values[i++];
float val4 = values[i++];
float val5 = values[i++];
float val6 = val;
if(val1 == ENTSOE_NO_VALUE || val2 == ENTSOE_NO_VALUE || val3 == ENTSOE_NO_VALUE || val4 == ENTSOE_NO_VALUE || val5 == ENTSOE_NO_VALUE || val6 == ENTSOE_NO_VALUE) continue;
float val6hr = val1+val2+val3+val4+val5+val6;
if(min6hrIdx == -1 || min6hr > val6hr) {
min6hr = val6hr;
min6hrIdx = i-5;
}
}
}
char ts1hr[24];
memset(ts1hr, 0, 24);
if(min1hrIdx > -1) {
time_t ts = now + (SECS_PER_HOUR * min1hrIdx);
//Serial.printf("1hr: %d %lu\n", min1hrIdx, ts);
tmElements_t tm;
breakTime(ts, tm);
sprintf_P(ts1hr, PSTR("%04d-%02d-%02dT%02d:00:00Z"), tm.Year+1970, tm.Month, tm.Day, tm.Hour);
}
char ts3hr[24];
memset(ts3hr, 0, 24);
if(min3hrIdx > -1) {
time_t ts = now + (SECS_PER_HOUR * min3hrIdx);
//Serial.printf("3hr: %d %lu\n", min3hrIdx, ts);
tmElements_t tm;
breakTime(ts, tm);
sprintf_P(ts3hr, PSTR("%04d-%02d-%02dT%02d:00:00Z"), tm.Year+1970, tm.Month, tm.Day, tm.Hour);
}
char ts6hr[24];
memset(ts6hr, 0, 24);
if(min6hrIdx > -1) {
time_t ts = now + (SECS_PER_HOUR * min6hrIdx);
//Serial.printf("6hr: %d %lu\n", min6hrIdx, ts);
tmElements_t tm;
breakTime(ts, tm);
sprintf_P(ts6hr, PSTR("%04d-%02d-%02dT%02d:00:00Z"), tm.Year+1970, tm.Month, tm.Day, tm.Hour);
}
snprintf_P(json, BufferSize, JSONPRICES_JSON,
WiFi.macAddress().c_str(),
values[0],
values[1],
values[2],
values[3],
values[4],
values[5],
values[6],
values[7],
values[8],
values[9],
values[10],
values[11],
values[12],
values[13],
values[14],
values[15],
values[16],
values[17],
values[18],
values[19],
values[20],
values[21],
values[22],
values[23],
values[24],
values[25],
values[26],
values[27],
values[28],
values[29],
values[30],
values[31],
values[32],
values[33],
values[34],
values[35],
values[36],
values[37],
min == INT16_MAX ? 0.0 : min,
max == INT16_MIN ? 0.0 : max,
ts1hr,
ts3hr,
ts6hr
);
return mqtt->publish(topic + "/prices", json, true, 0);
}
bool HomeAssistantMqttHandler::publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea) {
if(topic.isEmpty() || !mqtt->connected())
return false;
publishSystemSensors();
if(hw->getTemperature() > -50) publishTemperatureSensor(0, "");
snprintf_P(json, BufferSize, JSONSYS_JSON,
WiFi.macAddress().c_str(),
clientId.c_str(),
(uint32_t) (millis64()/1000),
hw->getVcc(),
hw->getWifiRssi(),
hw->getTemperature(),
VERSION
);
return mqtt->publish(topic + "/state", json);
}
void HomeAssistantMqttHandler::publishSensor(const HomeAssistantSensor& sensor) {
String uid = String(sensor.path);
uid.replace(".", "");
uid.replace("[", "");
uid.replace("]", "");
uid.replace("'", "");
snprintf_P(json, BufferSize, HADISCOVER_JSON,
sensorNamePrefix.c_str(),
sensor.name,
topic.c_str(), sensor.topic,
deviceUid.c_str(), uid.c_str(),
deviceUid.c_str(), uid.c_str(),
sensor.uom,
sensor.path,
sensor.devcl,
deviceUid.c_str(),
deviceName.c_str(),
deviceModel.c_str(),
VERSION,
manufacturer.c_str(),
deviceUrl.c_str(),
strlen_P(sensor.stacl) > 0 ? ", \"stat_cla\" :" : "",
strlen_P(sensor.stacl) > 0 ? (char *) FPSTR(sensor.stacl) : ""
);
mqtt->publish(discoveryTopic + deviceUid + "_" + uid.c_str() + "/config", json, true, 0);
mqtt->loop();
delay(10);
#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif
}
void HomeAssistantMqttHandler::publishList1Sensors() {
if(l1Init) return;
for(uint8_t i = 0; i < List1SensorCount; i++) {
publishSensor(List1Sensors[i]);
}
l1Init = true;
}
void HomeAssistantMqttHandler::publishList2Sensors() {
if(l2Init) return;
for(uint8_t i = 0; i < List2SensorCount; i++) {
publishSensor(List2Sensors[i]);
}
l2Init = true;
}
void HomeAssistantMqttHandler::publishList2ExportSensors() {
if(l2eInit) return;
for(uint8_t i = 0; i < List2ExportSensorCount; i++) {
publishSensor(List2ExportSensors[i]);
}
l2eInit = true;
}
void HomeAssistantMqttHandler::publishList3Sensors() {
if(l3Init) return;
for(uint8_t i = 0; i < List3SensorCount; i++) {
publishSensor(List3Sensors[i]);
}
l3Init = true;
}
void HomeAssistantMqttHandler::publishList3ExportSensors() {
if(l3eInit) return;
for(uint8_t i = 0; i < List3ExportSensorCount; i++) {
publishSensor(List3ExportSensors[i]);
}
l3eInit = true;
}
void HomeAssistantMqttHandler::publishList4Sensors() {
if(l4Init) return;
for(uint8_t i = 0; i < List4SensorCount; i++) {
publishSensor(List4Sensors[i]);
}
l4Init = true;
}
void HomeAssistantMqttHandler::publishList4ExportSensors() {
if(l4eInit) return;
for(uint8_t i = 0; i < List4ExportSensorCount; i++) {
publishSensor(List4ExportSensors[i]);
}
l4eInit = true;
}
void HomeAssistantMqttHandler::publishRealtimeSensors(EnergyAccounting* ea, EntsoeApi* eapi) {
if(rtInit) return;
for(uint8_t i = 0; i < RealtimeSensorCount; i++) {
HomeAssistantSensor sensor = RealtimeSensors[i];
if(strncmp_P(sensor.devcl, PSTR("monetary"), 8) == 0) {
if(eapi == NULL) continue;
sensor.uom = eapi->getCurrency();
}
publishSensor(sensor);
}
uint8_t peakCount = ea->getConfig()->hours;
if(peakCount > 5) peakCount = 5;
for(uint8_t i = 0; i < peakCount; i++) {
char name[strlen(RealtimePeakSensor.name)];
snprintf(name, strlen(RealtimePeakSensor.name), RealtimePeakSensor.name, i+1);
char path[strlen(RealtimePeakSensor.path)];
snprintf(path, strlen(RealtimePeakSensor.path), RealtimePeakSensor.path, i+1);
HomeAssistantSensor sensor = {
name,
RealtimePeakSensor.topic,
path,
RealtimePeakSensor.uom,
RealtimePeakSensor.devcl,
RealtimePeakSensor.stacl
};
publishSensor(sensor);
}
rtInit = true;
}
void HomeAssistantMqttHandler::publishRealtimeExportSensors(EnergyAccounting* ea, EntsoeApi* eapi) {
if(rteInit) return;
for(uint8_t i = 0; i < RealtimeExportSensorCount; i++) {
HomeAssistantSensor sensor = RealtimeExportSensors[i];
if(strncmp_P(sensor.devcl, PSTR("monetary"), 8) == 0) {
if(eapi == NULL) continue;
sensor.uom = eapi->getCurrency();
}
publishSensor(sensor);
}
rteInit = true;
}
void HomeAssistantMqttHandler::publishTemperatureSensor(uint8_t index, String id) {
if(index > 32) return;
if(tInit[index]) return;
char name[strlen(TemperatureSensor.name)+id.length()];
snprintf(name, strlen(TemperatureSensor.name)+id.length(), TemperatureSensor.name, id.c_str());
char path[strlen(TemperatureSensor.path)+id.length()];
if(index == 0) {
memcpy_P(path, PSTR("temp\0"), 5);
} else {
snprintf(path, strlen(TemperatureSensor.path)+id.length(), TemperatureSensor.path, id.c_str());
}
HomeAssistantSensor sensor = {
name,
index == 0 ? SystemSensors[0].topic : TemperatureSensor.topic,
path,
TemperatureSensor.uom,
TemperatureSensor.devcl,
TemperatureSensor.stacl
};
publishSensor(sensor);
tInit[index] = true;
}
void HomeAssistantMqttHandler::publishPriceSensors(EntsoeApi* eapi) {
if(eapi == NULL) return;
String uom = String(eapi->getCurrency()) + "/kWh";
if(!pInit) {
for(uint8_t i = 0; i < PriceSensorCount; i++) {
HomeAssistantSensor sensor = PriceSensors[i];
if(strncmp_P(sensor.devcl, PSTR("monetary"), 8) == 0) {
sensor.uom = uom.c_str();
}
publishSensor(sensor);
}
pInit = true;
}
for(uint8_t i = 0; i < 38; i++) {
if(prInit[i]) continue;
float val = eapi->getValueForHour(i);
if(val == ENTSOE_NO_VALUE) continue;
char name[strlen(PriceSensor.name)+2];
snprintf(name, strlen(PriceSensor.name)+2, PriceSensor.name, i, i == 1 ? "hour" : "hours");
char path[strlen(PriceSensor.path)+1];
snprintf(path, strlen(PriceSensor.path)+1, PriceSensor.path, i);
HomeAssistantSensor sensor = {
i == 0 ? "Price current hour" : name,
PriceSensor.topic,
path,
uom.c_str(),
PriceSensor.devcl,
PriceSensor.stacl
};
publishSensor(sensor);
prInit[i] = true;
}
}
void HomeAssistantMqttHandler::publishSystemSensors() {
if(sInit) return;
for(uint8_t i = 0; i < SystemSensorCount; i++) {
publishSensor(SystemSensors[i]);
}
sInit = true;
}

View File

@@ -119,68 +119,96 @@ void HwTools::getAdcChannel(uint8_t pin, AdcConfig& config) {
config.unit = ADC_UNIT_1;
config.channel = ADC1_CHANNEL_4;
break;
#if defined(ADC1_CHANNEL_5_GPIO_NUM)
case ADC1_CHANNEL_5_GPIO_NUM:
config.unit = ADC_UNIT_1;
config.channel = ADC1_CHANNEL_5;
break;
#endif
#if defined(ADC1_CHANNEL_6_GPIO_NUM)
case ADC1_CHANNEL_6_GPIO_NUM:
config.unit = ADC_UNIT_1;
config.channel = ADC1_CHANNEL_6;
break;
#endif
#if defined(ADC1_CHANNEL_7_GPIO_NUM)
case ADC1_CHANNEL_7_GPIO_NUM:
config.unit = ADC_UNIT_1;
config.channel = ADC1_CHANNEL_7;
break;
#if defined(CONFIG_IDF_TARGET_ESP32S2)
#endif
#if defined(ADC1_CHANNEL_8_GPIO_NUM)
case ADC1_CHANNEL_8_GPIO_NUM:
config.unit = ADC_UNIT_1;
config.channel = ADC1_CHANNEL_8;
break;
#endif
#if defined(ADC1_CHANNEL_9_GPIO_NUM)
case ADC1_CHANNEL_9_GPIO_NUM:
config.unit = ADC_UNIT_1;
config.channel = ADC1_CHANNEL_9;
break;
#endif
#if defined(ADC2_CHANNEL_0_GPIO_NUM)
case ADC2_CHANNEL_0_GPIO_NUM:
config.unit = ADC_UNIT_2;
config.channel = ADC2_CHANNEL_0;
break;
#endif
#if defined(ADC2_CHANNEL_1_GPIO_NUM)
case ADC2_CHANNEL_1_GPIO_NUM:
config.unit = ADC_UNIT_2;
config.channel = ADC2_CHANNEL_1;
break;
#endif
#if defined(ADC2_CHANNEL_2_GPIO_NUM)
case ADC2_CHANNEL_2_GPIO_NUM:
config.unit = ADC_UNIT_2;
config.channel = ADC2_CHANNEL_2;
break;
#endif
#if defined(ADC2_CHANNEL_3_GPIO_NUM)
case ADC2_CHANNEL_3_GPIO_NUM:
config.unit = ADC_UNIT_2;
config.channel = ADC2_CHANNEL_3;
break;
#endif
#if defined(ADC2_CHANNEL_4_GPIO_NUM)
case ADC2_CHANNEL_4_GPIO_NUM:
config.unit = ADC_UNIT_2;
config.channel = ADC2_CHANNEL_4;
break;
#endif
#if defined(ADC2_CHANNEL_5_GPIO_NUM)
case ADC2_CHANNEL_5_GPIO_NUM:
config.unit = ADC_UNIT_2;
config.channel = ADC2_CHANNEL_5;
break;
#endif
#if defined(ADC2_CHANNEL_6_GPIO_NUM)
case ADC2_CHANNEL_6_GPIO_NUM:
config.unit = ADC_UNIT_2;
config.channel = ADC2_CHANNEL_6;
break;
#endif
#if defined(ADC2_CHANNEL_7_GPIO_NUM)
case ADC2_CHANNEL_7_GPIO_NUM:
config.unit = ADC_UNIT_2;
config.channel = ADC2_CHANNEL_7;
break;
#endif
#if defined(ADC2_CHANNEL_8_GPIO_NUM)
case ADC2_CHANNEL_8_GPIO_NUM:
config.unit = ADC_UNIT_2;
config.channel = ADC2_CHANNEL_8;
break;
#endif
#if defined(ADC2_CHANNEL_9_GPIO_NUM)
case ADC2_CHANNEL_9_GPIO_NUM:
config.unit = ADC_UNIT_2;
config.channel = ADC2_CHANNEL_9;
break;
#endif
}
#endif
}
@@ -371,10 +399,9 @@ bool HwTools::ledOff(uint8_t color) {
bool HwTools::ledBlink(uint8_t color, uint8_t blink) {
for(int i = 0; i < blink; i++) {
if(!ledOn(color)) return false;
delay(50);
delay(75);
ledOff(color);
if(i != blink)
delay(50);
if(i != blink) delay(75);
}
return true;
}

View File

@@ -0,0 +1 @@
json/*.h

View File

@@ -10,15 +10,14 @@ public:
this->topic = String(topic);
this->hw = hw;
};
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea);
bool publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, EntsoeApi* eapi);
bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(EntsoeApi*);
bool publishSystem(HwTools*);
bool publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea);
private:
String clientId;
String topic;
HwTools* hw;
bool init = false;
};
#endif

View File

@@ -1,7 +1,7 @@
{
"id" : "%s",
"name" : "%s",
"up" : %lu,
"up" : %u,
"t" : %lu,
"vcc" : %.3f,
"rssi": %d,
@@ -13,6 +13,8 @@
"h" : %.2f,
"d" : %.1f,
"t" : %d,
"x" : %.2f
"x" : %.2f,
"he" : %.2f,
"de" : %.1f
}
}

View File

@@ -1,7 +1,7 @@
{
"id" : "%s",
"name" : "%s",
"up" : %lu,
"up" : %u,
"t" : %lu,
"vcc" : %.3f,
"rssi": %d,
@@ -25,6 +25,8 @@
"h" : %.2f,
"d" : %.1f,
"t" : %d,
"x" : %.2f
"x" : %.2f,
"he" : %.2f,
"de" : %.1f
}
}

View File

@@ -1,7 +1,7 @@
{
"id" : "%s",
"name" : "%s",
"up" : %lu,
"up" : %u,
"t" : %lu,
"vcc" : %.3f,
"rssi": %d,
@@ -30,6 +30,8 @@
"h" : %.2f,
"d" : %.1f,
"t" : %d,
"x" : %.2f
"x" : %.2f,
"he" : %.2f,
"de" : %.1f
}
}

View File

@@ -1,7 +1,7 @@
{
"id" : "%s",
"name" : "%s",
"up" : %lu,
"up" : %u,
"t" : %lu,
"vcc" : %.3f,
"rssi": %d,
@@ -11,8 +11,14 @@
"id" : "%s",
"type" : "%s",
"P" : %d,
"P1" : %.2f,
"P2" : %.2f,
"P3" : %.2f,
"Q" : %d,
"PO" : %d,
"PO1" : %.2f,
"PO2" : %.2f,
"PO3" : %.2f,
"QO" : %d,
"I1" : %.2f,
"I2" : %.2f,
@@ -34,6 +40,8 @@
"h" : %.2f,
"d" : %.1f,
"t" : %d,
"x" : %.2f
"x" : %.2f,
"he" : %.2f,
"de" : %.1f
}
}

View File

@@ -0,0 +1,48 @@
{
"id" : "%s",
"prices" : {
"0" : %.4f,
"1" : %.4f,
"2" : %.4f,
"3" : %.4f,
"4" : %.4f,
"5" : %.4f,
"6" : %.4f,
"7" : %.4f,
"8" : %.4f,
"9" : %.4f,
"10" : %.4f,
"11" : %.4f,
"12" : %.4f,
"13" : %.4f,
"14" : %.4f,
"15" : %.4f,
"16" : %.4f,
"17" : %.4f,
"18" : %.4f,
"19" : %.4f,
"20" : %.4f,
"21" : %.4f,
"22" : %.4f,
"23" : %.4f,
"24" : %.4f,
"25" : %.4f,
"26" : %.4f,
"27" : %.4f,
"28" : %.4f,
"29" : %.4f,
"30" : %.4f,
"31" : %.4f,
"32" : %.4f,
"33" : %.4f,
"34" : %.4f,
"35" : %.4f,
"36" : %.4f,
"37" : %.4f,
"min" : %.4f,
"max" : %.4f,
"cheapest1hr" : "%s",
"cheapest3hr" : "%s",
"cheapest6hr" : "%s"
}
}

View File

@@ -4,5 +4,6 @@
"up" : %d,
"vcc" : %.3f,
"rssi": %d,
"temp": %.2f
"temp": %.2f,
"version": "%s"
}

View File

@@ -0,0 +1,76 @@
import os
import re
import shutil
import subprocess
try:
from css_html_js_minify import js_minify
except:
from SCons.Script import (
ARGUMENTS,
COMMAND_LINE_TARGETS,
DefaultEnvironment,
)
env = DefaultEnvironment()
env.Execute(
env.VerboseAction(
'$PYTHONEXE -m pip install "css_html_js_minify" ',
"Installing Python dependencies",
)
)
try:
from css_html_js_minify import js_minify
except:
print("WARN: Unable to load minifier")
webroot = "lib/JsonMqttHandler/json"
srcroot = "lib/JsonMqttHandler/include/json"
version = os.environ.get('GITHUB_TAG')
if version == None:
try:
result = subprocess.run(['git','rev-parse','--short','HEAD'], capture_output=True, check=False)
if result.returncode == 0:
version = result.stdout.decode('utf-8').strip()
else:
version = "SNAPSHOT"
except:
version = "SNAPSHOT"
if os.path.exists(srcroot):
shutil.rmtree(srcroot)
os.mkdir(srcroot)
else:
os.mkdir(srcroot)
for filename in os.listdir(webroot):
basename = re.sub("[^0-9a-zA-Z]+", "_", filename)
srcfile = webroot + "/" + filename
dstfile = srcroot + "/" + basename + ".h"
varname = basename.upper()
with open(srcfile, encoding="utf-8") as f:
content = f.read().replace("${version}", version)
try:
if (filename.endswith(".js") and filename != 'gaugemeter.js') or filename.endswith(".json"):
content = js_minify(content)
except:
print("WARN: Unable to minify")
with open(dstfile, "w") as dst:
dst.write("static const char ")
dst.write(varname)
dst.write("[] PROGMEM = R\"==\"==(")
dst.write(content)
dst.write(")==\"==\";\n")
dst.write("const int ");
dst.write(varname)
dst.write("_LEN PROGMEM = ");
dst.write(str(len(content)))
dst.write(";");

View File

@@ -1,14 +1,15 @@
#include "JsonMqttHandler.h"
#include "version.h"
#include "hexutils.h"
#include "Uptime.h"
#include "web/root/json1_json.h"
#include "web/root/json2_json.h"
#include "web/root/json3_json.h"
#include "web/root/json4_json.h"
#include "web/root/jsonsys_json.h"
#include "web/root/jsonprices_json.h"
#include "json/json1_json.h"
#include "json/json2_json.h"
#include "json/json3_json.h"
#include "json/json4_json.h"
#include "json/jsonsys_json.h"
#include "json/jsonprices_json.h"
bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea) {
bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccounting* ea, EntsoeApi* eapi) {
if(topic.isEmpty() || !mqtt->connected())
return false;
@@ -27,7 +28,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold(),
ea->getMonthMax()
ea->getMonthMax(),
ea->getProducedThisHour(),
ea->getProducedToday()
);
return mqtt->publish(topic, json);
} else if(data->getListType() == 2) {
@@ -55,7 +58,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold(),
ea->getMonthMax()
ea->getMonthMax(),
ea->getProducedThisHour(),
ea->getProducedToday()
);
return mqtt->publish(topic, json);
} else if(data->getListType() == 3) {
@@ -88,7 +93,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold(),
ea->getMonthMax()
ea->getMonthMax(),
ea->getProducedThisHour(),
ea->getProducedToday()
);
return mqtt->publish(topic, json);
} else if(data->getListType() == 4) {
@@ -104,8 +111,14 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
data->getMeterId().c_str(),
meterModel.c_str(),
data->getActiveImportPower(),
data->getL1ActiveImportPower(),
data->getL2ActiveImportPower(),
data->getL3ActiveImportPower(),
data->getReactiveImportPower(),
data->getActiveExportPower(),
data->getL1ActiveExportPower(),
data->getL2ActiveExportPower(),
data->getL3ActiveExportPower(),
data->getReactiveExportPower(),
data->getL1Current(),
data->getL2Current(),
@@ -125,7 +138,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
ea->getUseThisHour(),
ea->getUseToday(),
ea->getCurrentThreshold(),
ea->getMonthMax()
ea->getMonthMax(),
ea->getProducedThisHour(),
ea->getProducedToday()
);
return mqtt->publish(topic, json);
}
@@ -134,8 +149,9 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState, EnergyAccou
bool JsonMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw) {
int count = hw->getTempSensorCount();
if(count < 2)
if(count < 2) {
return false;
}
snprintf(json, 24, "{\"temperatures\":{");
@@ -159,17 +175,17 @@ bool JsonMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw)
bool JsonMqttHandler::publishPrices(EntsoeApi* eapi) {
if(topic.isEmpty() || !mqtt->connected())
return false;
if(strlen(eapi->getToken()) == 0)
if(eapi->getValueForHour(0) == ENTSOE_NO_VALUE)
return false;
time_t now = time(nullptr);
float min1hr, min3hr, min6hr;
float min1hr = 0.0, min3hr = 0.0, min6hr = 0.0;
int8_t min1hrIdx = -1, min3hrIdx = -1, min6hrIdx = -1;
float min = INT16_MAX, max = INT16_MIN;
float values[24];
for(int i = 0;i < 24; i++) values[i] = ENTSOE_NO_VALUE;
for(uint8_t i = 0; i < 24; i++) {
float values[38];
for(int i = 0;i < 38; i++) values[i] = ENTSOE_NO_VALUE;
for(uint8_t i = 0; i < 38; i++) {
float val = eapi->getValueForHour(now, i);
values[i] = val;
@@ -214,7 +230,8 @@ bool JsonMqttHandler::publishPrices(EntsoeApi* eapi) {
}
char ts1hr[21];
char ts1hr[24];
memset(ts1hr, 0, 24);
if(min1hrIdx > -1) {
time_t ts = now + (SECS_PER_HOUR * min1hrIdx);
//Serial.printf("1hr: %d %lu\n", min1hrIdx, ts);
@@ -222,7 +239,8 @@ bool JsonMqttHandler::publishPrices(EntsoeApi* eapi) {
breakTime(ts, tm);
sprintf(ts1hr, "%04d-%02d-%02dT%02d:00:00Z", tm.Year+1970, tm.Month, tm.Day, tm.Hour);
}
char ts3hr[21];
char ts3hr[24];
memset(ts3hr, 0, 24);
if(min3hrIdx > -1) {
time_t ts = now + (SECS_PER_HOUR * min3hrIdx);
//Serial.printf("3hr: %d %lu\n", min3hrIdx, ts);
@@ -230,7 +248,8 @@ bool JsonMqttHandler::publishPrices(EntsoeApi* eapi) {
breakTime(ts, tm);
sprintf(ts3hr, "%04d-%02d-%02dT%02d:00:00Z", tm.Year+1970, tm.Month, tm.Day, tm.Hour);
}
char ts6hr[21];
char ts6hr[24];
memset(ts6hr, 0, 24);
if(min6hrIdx > -1) {
time_t ts = now + (SECS_PER_HOUR * min6hrIdx);
//Serial.printf("6hr: %d %lu\n", min6hrIdx, ts);
@@ -253,6 +272,32 @@ bool JsonMqttHandler::publishPrices(EntsoeApi* eapi) {
values[9],
values[10],
values[11],
values[12],
values[13],
values[14],
values[15],
values[16],
values[17],
values[18],
values[19],
values[20],
values[21],
values[22],
values[23],
values[24],
values[25],
values[26],
values[27],
values[28],
values[29],
values[30],
values[31],
values[32],
values[33],
values[34],
values[35],
values[36],
values[37],
min == INT16_MAX ? 0.0 : min,
max == INT16_MIN ? 0.0 : max,
ts1hr,
@@ -262,8 +307,8 @@ bool JsonMqttHandler::publishPrices(EntsoeApi* eapi) {
return mqtt->publish(topic, json);
}
bool JsonMqttHandler::publishSystem(HwTools* hw) {
if(init || topic.isEmpty() || !mqtt->connected())
bool JsonMqttHandler::publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounting* ea) {
if(topic.isEmpty() || !mqtt->connected())
return false;
snprintf_P(json, BufferSize, JSONSYS_JSON,
@@ -272,8 +317,8 @@ bool JsonMqttHandler::publishSystem(HwTools* hw) {
(uint32_t) (millis64()/1000),
hw->getVcc(),
hw->getWifiRssi(),
hw->getTemperature()
hw->getTemperature(),
VERSION
);
init = mqtt->publish(topic, json);
return init;
return mqtt->publish(topic, json);
}

Some files were not shown because too many files have changed in this diff Show More