from time import sleep
import serial
port = '/dev/tty.usbmodem14201'
ser = serial.Serial(port)
while True:
ser.write(b'~G')
ser_data = ser.readline()
if ser_data:
data = ser_data.decode("utf-8")
if data.startswith('~G'):
try:
print('The door is closed. Temperature is {0}'.format(int(data[2:])/10))
except ValueError:
print("The door is opened.")
sleep(1)