<commit_before>#!/usr/bin/env python

########################################################################################
########################################################################################
########################################################################################
import SimpleSerial, os, time, datetime, json, sys

def trace(msg):
    print "%s: %s"%(datetime.datetime.now(), msg)

def startup(ss):
    trace("Ping Arduino")
    ss.write0(SimpleSerial.SSC_PING)
    while ss.available() == False:
        time.sleep(0.1)
    (command, data) = ss.read()
    trace("Arduino Reply: %s"%(SimpleSerial.str(command)))
    
    trace("X Test Arduino")
    ss.write0('X')
    while ss.available() == False:
        time.sleep(0.1)
    (command, data) = ss.read()
    trace("Arduino Reply: %s"%(SimpleSerial.str(command)))
    
def loop(ss, target, expire, trigger, overflow):
    trace("======================================")
    trace("Mesure Teperature and Humidity")
    ss.write0(SimpleSerial.SSC_QUERY_TH)
    (command, temperature, humidity) = ss.read2()
    if command != SimpleSerial.SSC_RESP_TH:
        raise Exception("Invalid Response.")
    trace("Arduino Reply: %s %d*C %d%%"%(SimpleSerial.str(command), temperature, humidity))
    
    if temperature < target - trigger:
        trace("Too cold, open the heater util %d*C"%(target))
        ss.write2(SimpleSerial.SSC_OPEN_HEATER, target, expire)
        command = ss.read0()
        if command != SimpleSerial.SSC_HEATER_OPENED:
            raise Exception("Invalid Response.")
        trace("Arduino Reply: %s"%(SimpleSerial.str(command)))
        
    overflow = False
    if temperature >= target:
        overflow = True
        
    return (trigger, overflow)

if __name__ == "__main__":
    print "Greenhouse use RaspberryPi and Arduino\n" \
          "Copyright (c) 2015 winlin(<EMAIL>)"
          
    # start heater when <=(target-trigger)
    # stop heater when overflow
    (target, expire, trigger, overflow) = (21, 30, 2, False)
    
    ss = SimpleSerial.open("/dev/ttyUSB0", 115200)
    ok = False
    while True:
        try:
            if ok == False:
                startup(ss)
            ok = True
        
            (trigger, overflow) = loop(ss, target, expire, trigger, overflow)
            if overflow == True:
                trigger = 2
        except Exception, ex:
            trace("Ignore error: %s"%(ex))
        time.sleep(1)
    sys.exit(0)
<commit_msg>refine code<commit_after>#!/usr/bin/env python

# @see https://github.com/winlinvip/SimpleSerial/blob/master/examples/CommandDefault/CommandDefault.ino
# @see https://github.com/winlinvip/SimpleSerial/blob/master/SimpleSerial.py
import SimpleSerial, os, time, datetime, json, sys

def trace(msg):
    print "%s: %s"%(datetime.datetime.now(), msg)

def startup(ss):
    trace("Ping Arduino")
    ss.write0(SimpleSerial.SSC_PING)
    while ss.available() == False:
        time.sleep(0.1)
    (command, data) = ss.read()
    trace("Arduino Reply: %s"%(SimpleSerial.str(command)))
    
    trace("X Test Arduino")
    ss.write0('X')
    while ss.available() == False:
        time.sleep(0.1)
    (command, data) = ss.read()
    trace("Arduino Reply: %s"%(SimpleSerial.str(command)))
    
def loop(ss, target, expire, trigger, overflow):
    trace("======================================")
    trace("Mesure Teperature and Humidity")
    ss.write0(SimpleSerial.SSC_QUERY_TH)
    (command, temperature, humidity) = ss.read2()
    if command != SimpleSerial.SSC_RESP_TH:
        raise Exception("Invalid Response.")
    trace("Arduino Reply: %s %d*C %d%%"%(SimpleSerial.str(command), temperature, humidity))
    
    if temperature < target - trigger:
        trace("Too cold, open the heater util %d*C"%(target))
        ss.write2(SimpleSerial.SSC_OPEN_HEATER, target, expire)
        command = ss.read0()
        if command != SimpleSerial.SSC_HEATER_OPENED:
            raise Exception("Invalid Response.")
        trace("Arduino Reply: %s"%(SimpleSerial.str(command)))
        
    overflow = False
    if temperature >= target:
        overflow = True
        
    return (trigger, overflow)

if __name__ == "__main__":
    print "Greenhouse use RaspberryPi and Arduino\n" \
          "Copyright (c) 2015 winlin(<EMAIL>)"
          
    # start heater when <=(target-trigger)
    # stop heater when overflow
    (target, expire, trigger, overflow) = (21, 30, 2, False)
    
    ss = SimpleSerial.open("/dev/ttyUSB0", 115200)
    ok = False
    while True:
        try:
            if ok == False:
                startup(ss)
            ok = True
        
            (trigger, overflow) = loop(ss, target, expire, trigger, overflow)
            if overflow == True:
                trigger = 2
        except Exception, ex:
            trace("Ignore error: %s"%(ex))
            time.sleep(1)
            continue
        time.sleep(3)
    sys.exit(0)

{'fraction_non_alphanumeric': 0.19514661274014156, 'fraction_numerical': 0.01193124368048534, 'mean_word_length': 2.792944785276074, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1144630', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1437, 'n_words': 349}