Last active
August 29, 2015 14:16
-
-
Save naotaco/f8b0a05b673d0e431da1 to your computer and use it in GitHub Desktop.
Cooker-prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
from max31855 import MAX31855, MAX31855Error | |
import time | |
import atexit | |
import RPi.GPIO as GPIO | |
from datetime import datetime | |
atexit.register(GPIO.cleanup) # 終了時にピンを落とす | |
pin = 35 # as board | |
GPIO.setmode(GPIO.BOARD) # GPIO**じゃなくて、物理ピン番号で指定 | |
GPIO.setup(pin, GPIO.OUT) | |
# GPIO.output(pin, True) | |
cs_pins = [24] # MAX31855のCSピン。複数に対応。 | |
clock_pin = 23 # clock. 複数あっても全部同じところにつなぐ。 | |
data_pin = 21 # DO(MISO). 同上. | |
units = "c" # 摂氏 | |
thermocouples = [] | |
now = datetime.now() | |
filename = "temperature_log_" + now.isoformat() + ".txt" | |
logfile = open(filename, "w") | |
target_temperature = 70.0 | |
if (len(sys.argv) > 0): | |
target_temperature = float(sys.argv[1]) | |
print ("Target temperature is " + str(target_temperature)) | |
for cs_pin in cs_pins: | |
thermocouples.append(MAX31855(cs_pin, clock_pin, data_pin, units, GPIO.BOARD)) | |
running = True | |
while(running): | |
now = datetime.now() | |
print (now.isoformat()) | |
try: | |
for thermocouple in thermocouples: | |
rj = thermocouple.get_rj() | |
command = "none" | |
try: | |
tc = thermocouple.get() | |
if(tc < target_temperature): | |
print ("turn on") | |
GPIO.output(pin, True) | |
command = "100" | |
else: | |
print ("turn off") | |
GPIO.output(pin, False) | |
command = "0" | |
except MAX31855Error as e: | |
tc = "Error: "+ e.value | |
# running = False | |
print("tc: {} and rj: {}".format(tc, rj)) | |
timestamp = now.isoformat().replace("T", "\t") | |
logfile.write(timestamp + "\t" + str(tc) + "\t" + str(rj) + "\t" + command + "\t" + str(target_temperature) + "\n\ | |
") | |
time.sleep(10) | |
except KeyboardInterrupt: | |
running = False | |
for thermocouple in thermocouples: | |
thermocouple.cleanup() | |
logfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment