diff options
-rw-r--r-- | .env | 4 | ||||
-rw-r--r-- | pi_temp_pid.py | 52 |
2 files changed, 29 insertions, 27 deletions
@@ -4,7 +4,7 @@ kd=0 target_temperature=24 sample_time=15 # todo: check both of below!: -relay_pin=22 -mosfet_pin=5 +relay_pin=5 +mosfet_pin=12 read_retry=3 read_frequency_sec=15 diff --git a/pi_temp_pid.py b/pi_temp_pid.py index edb6c29..8be1fd4 100644 --- a/pi_temp_pid.py +++ b/pi_temp_pid.py @@ -174,9 +174,6 @@ class ShroomboxSettings(BaseSettings): case_sensitive = True -settings = read_settings() - - def read_settings(): global settings try: @@ -187,6 +184,9 @@ def read_settings(): return settings +settings = read_settings() + + class ShroomboxManager: """ Shroombox class. @@ -196,12 +196,12 @@ class ShroomboxManager: self.current_temperature = float('NaN') self.gpio = pigpio.pi() # Is below a good idea? - self.relay_switch(False) + self.relay_switch(on=True) # gpio_status: on is 0, off is 1 self.gpio_status = self.gpio.read(settings.relay_pin) def __del__(self): - self.relay_switch(True) + self.relay_switch(on=False) # FIXME: this doesn't work as class instance is already gone with gpio instance def callback(self): """ @@ -211,10 +211,10 @@ class ShroomboxManager: while True: # Get current temperature temp = self.read() - # Try to write to CSV file - self.write(temp) # Get PID value for MOSFET val = self.temp_control(temp) + # Try to write to CSV file + self.write(temp, val) # Set MOSFET to PID value self.mosfet_set(val) time.sleep(settings.read_frequency_sec) @@ -251,33 +251,35 @@ class ShroomboxManager: @staticmethod def write( datum: float, + kp: float, ) -> bool: """ Method writing data point to csv file. CSV file header: timestamp,temperature :param datum: float + :param kp: float :return: bool """ result = True if not os.path.exists(DATA_FILE): try: - os.makedirs(DATA_FOLDER) with open(DATA_FILE, 'w', encoding='UTF-8') as _file: - header = "timestamp,temperature" - _file.write(header) + header = "timestamp,temperature,kp" + wrtr = writer(_file) + wrtr.writerow(header) result = False except OSError as exc: - logger.error("Couldn't create path %DATA_FILE %exc") + logger.error("Couldn't create path %s, %s", DATA_FILE, exc) result = True try: with open(DATA_FILE, 'a', encoding='UTF-8') as _file: wrtr = writer(_file) timestamp = time.strftime(TIMEFORMAT) - wrtr.writerow((timestamp, datum)) + wrtr.writerow((timestamp, datum, kp)) result = False except FileNotFoundError as exc: result = True - logger.error('Data file not found: %exc') + logger.error('Data file not found: %s', exc) return result def relay_switch( @@ -285,22 +287,22 @@ class ShroomboxManager: on: bool = False, ) -> None: """ - Turn relay on or off. + Turn relay on or off :param on: bool :return: None """ if on: - logger.info('Turning on relay %time_now()') + logger.info('Turning on relay %s', time_now()) try: self.gpio.write(settings.relay_pin, 0) except Exception as exc: - logger.warning('Failed to turn on relay: %exc') + logger.warning('Failed to turn on relay: %s', exc) else: - logger.info('Turning off relay %time_now()') + logger.info('Turning off relay %s', time_now()) try: self.gpio.write(settings.relay_pin, 1) except Exception as exc: - logger.warning('Failed to turn off relay: %exc') + logger.warning('Failed to turn off relay: %s', exc) @staticmethod def temp_control( @@ -312,13 +314,13 @@ class ShroomboxManager: :return: int """ pid = PID( - settings.kp, - settings.ki, - settings.kd, + Kp=settings.kp, + Ki=settings.ki, + Kd=settings.kd, setpoint=settings.target_temperature, + sample_time=settings.sample_time, + output_limits=(0, 255), ) - pid.output_limits = (0, 255) - pid.sample_time = settings.sample_time return int(pid(current_temperature)) # TODO: check if it can be done nicer def mosfet_set( @@ -330,12 +332,12 @@ class ShroomboxManager: :param value: int :return: bool """ - logger.debug(f'Changing pin {settings.mosfet_pin} to {value}') + logger.debug('Changing pin %s to %s', settings.mosfet_pin, value) try: self.gpio.set_PWM_dutycycle(settings.mosfet_pin, value) result = False except Exception as exc: - logger.warning('Failed to set mosfet value: %exc.') + logger.warning('Failed to set mosfet value: %s', exc) result = True return result |