1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
int pSwitch = 2; void setup() { Serial.begin(9600); Serial.println("WatchDog"); } int InitCount = 60; // s int count = InitCount; void loop() { if (Serial.available()) { int c = Serial.read(); switch(c) { default: Serial.print("Invalid input: "); Serial.println(c); break; case '\r': case '\n': ; break; case '0': Serial.println("Manual Reset"); count = 0; break; case '1': Serial.println("Alive"); count = 1000; break; } } Serial.println(count); if (count) { count--; digitalWrite(pSwitch, HIGH); } else { Serial.println("Power Off"); digitalWrite(pSwitch, LOW); delay(3000); Serial.println("Power On"); digitalWrite(pSwitch, HIGH); count = InitCount; delay(60000); } delay(1000); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#need pyserial: # $> pip install pyserial import sys import serial import time import datetime args = sys.argv if len(args) < 2: print("Format: BarkDog port cmd") quit() port = args[1] seri = serial.Serial(port, 9600, timeout=1000) print("Open [{}]".format(seri)) one = bytes("1",'utf-8') try: line = seri.readline().strip() print(line) while True: seri.write(one); time.sleep(1); print(datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")) except Exception as x: print("Error Stop!! " + x.args) finally: seri.close()