-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton_listener.py
More file actions
74 lines (64 loc) · 2.09 KB
/
button_listener.py
File metadata and controls
74 lines (64 loc) · 2.09 KB
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
from aiy.board import Board, Led
import subprocess
def main():
print('Press the button to run speak_gpt.py (Ctrl-C to exit).')
script_path = '/home/pi/Desktop/RoboticStudyCompanion-main/CodeStation/speak_gpt.py' # Full path to speak_gpt.py
with Board() as board:
while True:
board.button.wait_for_press()
print('Button pressed, running speak_gpt.py')
subprocess.run(['sudo','python3', script_path])
board.led.state = Led.ON
board.button.wait_for_release()
print('Button released')
board.led.state = Led.OFF
if __name__ == '__main__':
main()
# written by Isaac & Ishamael Jackson, May2024
'''
### Button_listener.py file configuration:
Ensure Your Script is Ready:
Make sure your button_listener.py script is executable and located in /home/pi/.
Create a systemd Service File:
Open a terminal on your Raspberry Pi and create a new service file:
bash
Copy code
sudo nano /etc/systemd/system/button_listener.service
Add the Following Configuration to the File:
ini
Copy code
[Unit]
Description=Button Listener Python Script
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/button_listener.py
WorkingDirectory=/home/pi/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
Group=pi
[Install]
WantedBy=multi-user.target
ExecStart specifies the command to run your Python script. Adjust the path if your script is located elsewhere.
WorkingDirectory is set to /home/pi/ where your script is located.
StandardOutput and StandardError are set to inherit for logging.
Restart=always ensures the service restarts if the script fails.
Reload systemd to Recognize the New Service:
bash
Copy code
sudo systemctl daemon-reload
Enable the Service to Start on Boot:
bash
Copy code
sudo systemctl enable button_listener.service
Start the Service Immediately (Optional):
bash
Copy code
sudo systemctl start button_listener.service
Check the Status of the Service:
bash
Copy code
sudo systemctl status button_listener.service
This will show you the status and any output or errors from your script.
'''