This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_shutdown.py
executable file
·78 lines (66 loc) · 2.03 KB
/
arduino_shutdown.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
#########################################################
##### If button on Arduino pin 6 is pressed, reboot #####
#########################################################
############################
##### Import Libraries #####
############################
from signal import signal, SIGTERM
from os import getuid, system
from serial import Serial
from time import sleep
###############################################
##### Exit cleanly if SIGTERM is received #####
###############################################
def sigterm_handler(signal, frame):
raise SystemExit
#####################
##### Main Loop #####
#####################
def main():
######################################
#### Check if we are run as root #####
######################################
if getuid() != 0:
raise Exception("Please run script as root")
##############################
##### Connect to Arduino #####
##############################
board = Serial("/dev/ttyACM0") # Connect to Arduino
board.timeout = 2
sleep(5)
board.write("Q")
while True:
sleep(10) # Sleep for ten seconds
board.write("h")
sleep(1) # Wait a second
rx_bytes = board.readline()
if "True" in rx_bytes:
board.write("h")
sleep(1) # Wait a second
rx_bytes = board.readline()
if "True" in rx_bytes:
board.write("q") # Flash Leds
sleep(1)
board.write("Q")
sleep(1)
board.write("q")
sleep(1)
board.write("Q")
sleep(1)
board.write("q")
system("shutdown -r 1")
raise SystemExit
try:
if __name__ == "__main__":
signal(SIGTERM, sigterm_handler)
main()
except KeyboardInterrupt:
print ""
print "You pressed CTRL+C"
except SystemExit:
print "Rebooting!"
except Exception as e:
print "An error occurred: " + str(e)
finally:
board.close()