Repository navigation
Expand file tree
/
Copy pathpushbutton.py
More file actions
202 lines (190 loc) · 6.25 KB
/
Copy pathpushbutton.py
File metadata and controls
202 lines (190 loc) · 6.25 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
# originally based on listen-for-shutdown.py from pi-power-button
import RPi.GPIO as GPIO
import subprocess
import time
import os
from time import sleep
import configparser
def read_config_ini():
global hotspot_ssid
global hotspot_password
global hotspot_default
global web_term_hotspot
global web_term_wifi
# global dashboard_port
try:
# Initialize the parser
config = configparser.ConfigParser()
# Read the file
config.read('/home/pi/CubeSatSim/hotspot.ini')
# Access the values like a dictionary
hotspot_ssid = config['Hotspot']['hotspot_ssid']
hotspot_password = config['Hotspot']['hotspot_password']
hotspot_default = config['Hotspot']['hotspot_default']
web_term_hotspot = config['Hotspot']['web_term_hotspot']
web_term_wifi = config['Hotspot']['web_term_wifi']
# dashboard_port = config.getint('Hotspot', 'dashboard_port')
print(f"SSID: {hotspot_ssid} Password: {hotspot_password} Hotspot Default: {hotspot_default} Term Hotspot: {web_term_hotspot} Term WiFi {web_term_wifi}") # : {dashboard_}")
except Exception as e:
print(f"An error occurred: {e}")
print(" failed, setting defaults")
hotspot_ssid = "CubeSat"
hotspot_password = "amsatcubesat"
hotspot_default = "no"
web_term_hotspot = "yes"
web_term_wifi = "no"
# dashboard_port = 80
def blink(times):
blink_time = 0.1
powerPin = 16
for i in range(times): # blink times
GPIO.output(powerPin, 0)
sleep(blink_time)
GPIO.output(powerPin, 1)
sleep(blink_time)
sleep(0.65)
def change_mode():
push_button = 26
powerPin = 16
txPin = 27
sleep(0.75)
if GPIO.input(push_button):
print("sudo reboot -h now", flush=True)
os.system("echo 'reboot due to push button!' | wall")
GPIO.setwarnings(False)
GPIO.setup(powerPin, GPIO.OUT)
GPIO.output(powerPin, 0);
os.system("sudo reboot now")
# subprocess.call(['reboot', '-h', 'now'], shell=False)
return
blink(1)
if GPIO.input(push_button):
print("switch to AFSK", flush=True)
os.system("echo 'switch to AFSK due to push button!' | wall")
os.system("/home/pi/CubeSatSim/config -a")
return
blink(2)
if GPIO.input(push_button):
print("switch to FSK", flush=True)
os.system("echo 'switch to FSK due to push button!' | wall")
os.system("/home/pi/CubeSatSim/config -f")
return
blink(3)
if GPIO.input(push_button):
print("switch to BPSK", flush=True)
os.system("echo 'switch to BPSK due to push button!' | wall")
os.system("/home/pi/CubeSatSim/config -b")
return
blink(4)
if GPIO.input(push_button):
print("switch to SSTV", flush=True)
os.system("echo 'switch to SSTV due to push button!' | wall")
os.system("/home/pi/CubeSatSim/config -s")
return
blink(5)
if GPIO.input(push_button):
print("switch to CW", flush=True)
os.system("echo 'switch to CW due to push button!' | wall")
os.system("/home/pi/CubeSatSim/config -m")
return
blink(6)
if GPIO.input(push_button):
print("switch to Repeater", flush=True)
os.system("echo 'switch to Repeater due to push button!' | wall")
os.system("/home/pi/CubeSatSim/config -e")
return
blink(7)
if GPIO.input(push_button):
print("switch to FunCube", flush=True)
os.system("echo 'switch to FunCube due to push button!' | wall")
os.system("/home/pi/CubeSatSim/config -j")
return
blink(8)
if GPIO.input(push_button):
print("switch to PacSat", flush=True)
os.system("echo 'switch to PacSat due to push button!' | wall")
os.system("/home/pi/CubeSatSim/config -G")
return
for i in range(3): # blink 3 times slowly
GPIO.output(powerPin, 0)
sleep(0.35)
GPIO.output(powerPin, 1)
sleep(0.35)
sleep(0.65)
if GPIO.input(push_button):
print("sudo shutdown -h now", flush=True)
os.system("echo 'shutdown due to push button!' | wall")
GPIO.setwarnings(False)
GPIO.setup(powerPin, GPIO.OUT)
subprocess.call(['shutdown', '-h', 'now'], shell=False)
return
for i in range(3): # blink two times even more slowly
GPIO.output(powerPin, 0)
sleep(0.7)
GPIO.output(powerPin, 1)
sleep(0.7)
sleep(0.7)
print("toggle command and control mode", flush=True)
try:
f = open("/home/pi/CubeSatSim/command_control", "r")
f.close()
print("command and control will be deactivated", flush=True)
os.system('sudo rm /home/pi/CubeSatSim/command_control')
os.system("echo 'command and control deactivated by push button!' | wall")
os.system('sudo systemctl restart command')
except:
print("command and control will be activated", flush=True)
os.system('touch /home/pi/CubeSatSim/command_control')
os.system("echo 'command and control activated by push button!' | wall")
os.system('sudo systemctl restart command')
sleep(1)
powerPin = 16
txPin = 27
push_button = 26
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(txPin, GPIO.OUT)
GPIO.output(txPin, 0)
GPIO.setup(powerPin, GPIO.OUT)
GPIO.output(powerPin, 0)
GPIO.setup(push_button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
read_config_ini()
if not GPIO.input(push_button) or hotspot_default == "yes": # if pushbutton is held down during boot or configured as default
print("Activating WiFi access point!", flush=True)
GPIO.output(powerPin, 1)
sleep(0.1)
GPIO.output(powerPin, 0)
sleep(0.1)
GPIO.output(powerPin, 1)
sleep(0.3)
GPIO.output(powerPin, 0)
sleep(0.1)
GPIO.output(powerPin, 1)
sleep(0.3)
GPIO.output(powerPin, 0)
success = 1
count = 0
while (success != 0) and (count < 5):
result = subprocess.run(['nmcli', 'device', 'wifi', 'hotspot', 'con-name', 'Hotspot', 'ifname', 'wlan0', 'ssid', hotspot_ssid, 'password', hotspot_password], capture_output=True, text=True)
print(result, flush=True) #.stdout)
count = count + 1
success = result.returncode
sleep(1)
print(f"Starting Wi-Fi Hotspot with SSID: {hotspot_ssid} and password: {hotspot_password}")
result = subprocess.run(['nmcli', 'connection', 'show'], capture_output=True, text=True)
print(result, flush=True)
sleep(2)
if web_term_hotspot == "yes":
subprocess.Popen(['sudo', '-u', 'pi', 'ttyd', '-W', '-p', '8081', 'bash'])
print("Starting web terminal on port 8081")
elif web_term_wifi == "yes":
subprocess.Popen(['sudo', '-u', 'pi', 'ttyd', '-W', '-p', '8081', 'bash'])
print("Starting web terminal on port 8081")
GPIO.setup(powerPin, GPIO.OUT)
GPIO.output(powerPin, 1)
while (True):
sleep(1)
GPIO.wait_for_edge(push_button, GPIO.FALLING)
change_mode()
sleep(5)