# Can enable debug output by uncommenting: #import logging #logging.basicConfig(level=logging.DEBUG) import time import Adafruit_GPIO.SPI as SPI import MAX6675.MAX6675 as MAX6675 from simple_pid import PID kP =150 kI = 5 kD = 1 setpoint = 580 pid = PID(kP, kI, kD, setpoint) loop_size = int(3000) pid.output_limits = (0, loop_size) # output value will be between 0 and 5000 # Raspberry Pi software SPI configuration. CLK = 3 CS = 14 DO = 4 sensor = MAX6675.MAX6675(CLK, CS, DO) relayPin = 2 import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library GPIO.setwarnings(False) # Ignore warning for now GPIO.setmode(GPIO.BCM) # Use physical pin numbering GPIO.setup(relayPin, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial value to low (off) # Define a function to convert celsius to fahrenheit. def c_to_f(c): return c * 9.0 / 5.0 + 32.0 print('Press Ctrl-C to quit.') try: while True: temp = c_to_f(sensor.readTempC()) start_milli_time = int(round(time.time() * 1000)) current_milli_time = int(round(time.time() * 1000)) current_loop_end = start_milli_time + loop_size while current_milli_time < current_loop_end: temp = c_to_f(sensor.readTempC()) output = pid(temp) if current_milli_time < start_milli_time + output: GPIO.output(relayPin, GPIO.HIGH) else: GPIO.output(relayPin, GPIO.LOW) current_milli_time = int(round(time.time() * 1000)) time.sleep(0.3) print("Temp: {0:.2f}, PWR: {1:.2f}, {2}".format(temp, output, GPIO.input(relayPin))) except: GPIO.output(relayPin, GPIO.LOW) print("Exiting properly")