-
Notifications
You must be signed in to change notification settings - Fork 0
/
power.ino
127 lines (94 loc) · 3.13 KB
/
power.ino
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* power.ino
*
* arduinoCLI sketch to make your Mac, Linux, or Windows host
* go to sleep, shutdown, or reboot when the Arduino tells it to.
*
* IMPORTANT NOTE: For Mac and Linux hosts in order to use these functions
* the Python Agent must be started with the "sudu" command:
*
* $ sudo python arduino_exec.py
*/
// ==========================================================================================
// Uncomment ONE of the following lines to use this sketch for a Windows, Mac or Linux host:
//#define USE_WINDOWS
#define USE_MAC
//#define USE_LINUX
// ==========================================================================================
#include <SoftwareSerial.h>
#define RX_PIN 7
#define TX_PIN 8
// Software Serial object to send the
// commands to the Python Agent
SoftwareSerial command_serial(RX_PIN, TX_PIN); // RX, TX
// ==========================================================================================
#ifdef USE_WINDOWS
/*
* sleep, reboot and shutdown commands for Windows hosts
*/
void sleep() {
String command = "!shutdown /h"; // on some Windows systems this tells the host to hybernate
// on others it puts the host to sleep
command_serial.println(command);
}
void reboot(int const delay_seconds = 300) {
char command[64] = "";
sprintf(command, "!shutdown /r /t %d", delay_seconds);
command_serial.println(command);
}
void shutdown(int const delay_seconds = 300) {
char command[64] = "";
sprintf(command, "!shutdown /s /t %d", delay_seconds);
command_serial.println(command);
}
#endif
// ==========================================================================================
#ifdef USE_MAC
/*
* sleep, reboot and shutdown commands for Mac hosts
*/
void sleep() {
String command = "!pmset sleepnow";
command_serial.println(command);
}
void reboot(int const delay_minutes = 5) {
char command[64] = "";
sprintf(command, "!shutdown -r +%d", delay_minutes);
command_serial.println(command);
}
void shutdown(int const delay_minutes = 5) {
char command[64] = "";
sprintf(command, "!shutdown -h +%d", delay_minutes);
command_serial.println(command);
}
#endif
// ==========================================================================================
#ifdef USE_LINUX
/*
* sleep, reboot and shutdown commands for Linux hosts
*/
void sleep() {
String command = "!systemctl suspend";
command_serial.println(command);
}
void reboot(int const delay_minutes = 5) {
char command[64] = "";
sprintf(command, "!shutdown -r +%d", delay_minutes);
command_serial.println(command);
}
void shutdown(int const delay_minutes = 5) {
char command[64] = "";
sprintf(command, "!shutdown -h +%d", delay_minutes);
command_serial.println(command);
}
#endif
// ==========================================================================================
void setup() {
command_serial.begin(9600);
sleep(); // tell the host to go to sleep
// or
//shutdown(0); // tell the host to shut down
// or
//reboot(0); // tell the host to reboot
}
void loop() { }