aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerg <uinarf@autistici.org>2023-03-06 12:54:22 +0100
committererg <uinarf@autistici.org>2023-03-06 12:54:22 +0100
commit62b5ea8e762e8f563b768b517a0e193cc5375613 (patch)
tree6581cd9606df8f2235f8280bf46be5ba3501fa1c
parent93ef2d3614455198d0117e99a5922e47e0461fd0 (diff)
downloadPi_Temp_PID_Control-62b5ea8e762e8f563b768b517a0e193cc5375613.tar.gz
Pi_Temp_PID_Control-62b5ea8e762e8f563b768b517a0e193cc5375613.tar.bz2
Pi_Temp_PID_Control-62b5ea8e762e8f563b768b517a0e193cc5375613.zip
Fixes
-rw-r--r--pi_temp_pid.py35
1 files changed, 21 insertions, 14 deletions
diff --git a/pi_temp_pid.py b/pi_temp_pid.py
index 24b0428..edb6c29 100644
--- a/pi_temp_pid.py
+++ b/pi_temp_pid.py
@@ -30,8 +30,9 @@ import signal
import logging
import io
import getpass
+from csv import writer
-from pydantic import BaseSettings
+from pydantic import BaseSettings, BaseModel, Field
try:
from PID import PID
import pigpio
@@ -173,11 +174,17 @@ class ShroomboxSettings(BaseSettings):
case_sensitive = True
-try:
- settings = ShroomboxSettings()
-except Exception as exception:
- print(f"Failed to read settings: {exception}")
- sys.exit()
+settings = read_settings()
+
+
+def read_settings():
+ global settings
+ try:
+ settings = ShroomboxSettings()
+ except Exception as exception:
+ print(f"Failed to read settings: {exception}")
+ sys.exit()
+ return settings
class ShroomboxManager:
@@ -210,7 +217,7 @@ class ShroomboxManager:
val = self.temp_control(temp)
# Set MOSFET to PID value
self.mosfet_set(val)
- time.sleep(settings.read_fqcy)
+ time.sleep(settings.read_frequency_sec)
def read(
self,
@@ -254,7 +261,7 @@ class ShroomboxManager:
result = True
if not os.path.exists(DATA_FILE):
try:
- os.makedirs(DATA_FILE)
+ os.makedirs(DATA_FOLDER)
with open(DATA_FILE, 'w', encoding='UTF-8') as _file:
header = "timestamp,temperature"
_file.write(header)
@@ -264,9 +271,9 @@ class ShroomboxManager:
result = True
try:
with open(DATA_FILE, 'a', encoding='UTF-8') as _file:
+ wrtr = writer(_file)
timestamp = time.strftime(TIMEFORMAT)
- data_point = f'{timestamp},{datum}'
- _file.write(data_point)
+ wrtr.writerow((timestamp, datum))
result = False
except FileNotFoundError as exc:
result = True
@@ -298,7 +305,7 @@ class ShroomboxManager:
@staticmethod
def temp_control(
current_temperature: float,
- ) -> int:
+ ) -> PidValue:
"""
Calculate value to pass to mosfet.
:param current_temperature: float
@@ -312,7 +319,7 @@ class ShroomboxManager:
)
pid.output_limits = (0, 255)
pid.sample_time = settings.sample_time
- return pid(current_temperature)
+ return int(pid(current_temperature)) # TODO: check if it can be done nicer
def mosfet_set(
self,
@@ -325,7 +332,7 @@ class ShroomboxManager:
"""
logger.debug(f'Changing pin {settings.mosfet_pin} to {value}')
try:
- self.gpio.write(settings.mosfet_pin, value)
+ self.gpio.set_PWM_dutycycle(settings.mosfet_pin, value)
result = False
except Exception as exc:
logger.warning('Failed to set mosfet value: %exc.')
@@ -340,7 +347,7 @@ def time_now():
if __name__ == '__main__':
controller = ShroomboxManager()
- signal.signal(signal.SIGHUP, settings.ShroomboxSettings)
+ signal.signal(signal.SIGHUP, read_settings)
while True:
controller.callback()
time.sleep(settings.read_frequency_sec)