From ac45ff71084609460df03d60a4b82c52ba3db587 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Fri, 4 Nov 2022 21:19:51 +0100 Subject: [PATCH] RPI: don't try to reach inverter after sunset, will not work for polar day or night but should be easy to add if really needed --- tools/rpi/README.md | 2 +- tools/rpi/ahoy.yml.example | 8 ++++++-- tools/rpi/hoymiles/__main__.py | 35 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/tools/rpi/README.md b/tools/rpi/README.md index 1027ccb0..bc8b23c0 100644 --- a/tools/rpi/README.md +++ b/tools/rpi/README.md @@ -86,7 +86,7 @@ Required python modules Some modules are not installed by default on a RaspberryPi, therefore add them manually: ``` -pip install crcmod pyyaml paho-mqtt +pip install crcmod pyyaml paho-mqtt SunTimes ``` Configuration diff --git a/tools/rpi/ahoy.yml.example b/tools/rpi/ahoy.yml.example index 644508e5..58866816 100644 --- a/tools/rpi/ahoy.yml.example +++ b/tools/rpi/ahoy.yml.example @@ -1,8 +1,12 @@ --- ahoy: - interval: 0 - sunset: true + interval: 5 + sunset: + disabled: false + latitude: 51.799118 + longitude: 10.615523 + altitude: 1142 # List of available NRF24 transceivers nrf: diff --git a/tools/rpi/hoymiles/__main__.py b/tools/rpi/hoymiles/__main__.py index 2b3ee5d6..181dfd0b 100644 --- a/tools/rpi/hoymiles/__main__.py +++ b/tools/rpi/hoymiles/__main__.py @@ -11,6 +11,8 @@ from enum import IntEnum import re import time from datetime import datetime +from datetime import timedelta +from suntimes import SunTimes import argparse import yaml from yaml.loader import SafeLoader @@ -37,16 +39,49 @@ class InfoCommands(IntEnum): GetSelfCheckState = 30 # 0x1e InitDataState = 0xff +class SunsetHandler: + def __init__(self, sunset_config): + self.sunset = None + if sunset_config and sunset_config.get('disabled', True) == False: + latitude = sunset_config.get('latitude') + longitude = sunset_config.get('longitude') + altitude = sunset_config.get('altitude') + self.suntimes = SunTimes(longitude=longitude, latitude=latitude, altitude=altitude) + now = datetime.now() + self.nextSunset = self.suntimes.setutc(now) + print (f'Todays sunset is at {self.nextSunset}') + + def checkWaitForSunrise(self): + if not self.suntimes: + return + # if the sunset already happened for today + now = datetime.now() + if self.nextSunset < now: + # wait until the sun rises tomorrow + nextSunrise = self.suntimes.riseutc(now + timedelta(days=1)) + self.nextSunset = self.suntimes.setutc(now + timedelta(days=1)) + time_to_sleep = (nextSunrise - datetime.now()).total_seconds() + print (f'Waiting for sunrise at {nextSunrise} ({time_to_sleep} seconds)') + if time_to_sleep > 0: + time.sleep(time_to_sleep) + now = datetime.now() + print (f'Woke up... next sunset is at {self.nextSunset}') + return + def main_loop(ahoy_config): """Main loop""" inverters = [ inverter for inverter in ahoy_config.get('inverters', []) if not inverter.get('disabled', False)] + sunset = SunsetHandler(ahoy_config.get('sunset')) + loop_interval = ahoy_config.get('interval', 1) try: do_init = True while True: + sunset.checkWaitForSunrise() + t_loop_start = time.time() for inverter in inverters: