diff --git a/install b/install index 4957dc4e..9dd12e3d 100755 --- a/install +++ b/install @@ -255,6 +255,15 @@ if [[ $(grep 'bookworm' /etc/os-release) ]]; then sudo sh -c 'echo "\n" >> /boot/firmware/config.txt' + sudo sed -i 's/managed=false/managed=true/g' /etc/NetworkManager/NetworkManager.conf # allow nmcli to manage WiFi + + if [ ! -d "/var/log/journal" ]; then # persistant journal logs across boots + sudo mkdir -p /var/log/journal + sudo systemd-tmpfiles --create --prefix /var/log/journal + sudo systemctl restart systemd-journal-flush.service + sudo sed -i 's/Storage=volatile/Storage=persistent/g' /etc/systemd/journald.conf + fi + elif [[ $(grep 'trixie' /etc/os-release) ]]; then echo "Trixie detected, installation continuing." @@ -469,38 +478,6 @@ else echo "b" > .mode fi -if [ ! -d "/home/pi/pi-power-button" ]; then - - cd - git clone https://github.com/alanbjohnston/pi-power-button.git - cd pi-power-button - git checkout master - ./script/install - sudo apt-get install -y libraspberrypi-dev - -fi - -cd /home/pi/pi-power-button - -if [ ! $(git branch --show-current | grep master-b) ]; then - - echo "updating branch of pi-power-button to master-b." - git pull - git checkout master-b - script/install - FLAG=1 -fi - -git pull --no-rebase > .updated_p -#git checkout master -grep 'changed' /home/pi/pi-power-button/.updated_p -if [[ $(grep 'changed' /home/pi/pi-power-button/.updated_p) ]]; then - - echo "updating pi-power-button." - script/install - FLAG=1 -fi - if [ ! -d "/home/pi/g0kla_common" ]; then cd git clone https://github.com/alanbjohnston/g0kla_common.git @@ -741,6 +718,28 @@ else FLAG=1 fi +FILE=/etc/init.d/listen-for-shutdown.sh +if [ -f $FILE ]; then + echo "Uninstalling pi-power-button" + sudo rm $FILE +fi + +FILE=/etc/systemd/system/pushbutton.service +if [ -f "$FILE" ]; then + if [[ $(diff /home/pi/CubeSatSim/systemd/pushbutton.service /etc/systemd/system/pushbutton.service) ]]; then + echo "changed pushbutton.service." + sudo cp /home/pi/CubeSatSim/systemd/pushbutton.service /etc/systemd/system/pushbutton.service + sudo systemctl enable pushbutton + FLAG=1 + else + echo "no change to pushbutton.service." + fi +else + echo "creating pushbutton.service." + sudo cp /home/pi/CubeSatSim/systemd/pushbutton.service /etc/systemd/system/pushbutton.service + sudo systemctl enable pushbutton + FLAG=1 +fi FILE=/etc/asound.conf if [ -f "$FILE" ]; then diff --git a/log b/log index c4017153..8963b64f 100755 --- a/log +++ b/log @@ -10,6 +10,7 @@ if [ "$1" = "-h" ] ; then echo " -p pacsat" echo " -r frequency control" echo " -f frequency display" + echo " -b pushbutton" echo "Default is the log is dumpted to the screen and written to a file." echo "If an additional r is included, the log is realtime, and exits with a Control-C" echo @@ -28,19 +29,19 @@ if [ "$REALTIME" = "1" ] ; then sleep 2 if [ "$1" = "-t" ] ; then - sudo journalctl -af -u transmit # > /home/pi/CubeSatSim/logt.txt + sudo journalctl -b all -af -u transmit # > /home/pi/CubeSatSim/logt.txt # cat /home/pi/CubeSatSim/logt.txt # echo -e "\nTransmit Log file also saved as /home/pi/CubeSatSim/logt.txt" elif [ "$1" = "-c" ]; then - sudo journalctl -af -u command # > /home/pi/CubeSatSim/logc.txt + sudo journaclt -b all -af -u command # > /home/pi/CubeSatSim/logc.txt cat /home/pi/CubeSatSim/logc.txt # echo -e "\nCommand and Control Log file also saved as /home/pi/CubeSatSim/logc.txt" elif [ "$1" = "-p" ]; then - sudo journalctl -af -u pacsatsim # > /home/pi/CubeSatSim/logp.txt + sudo journalctl -b all -af -u pacsatsim # > /home/pi/CubeSatSim/logp.txt cat /home/pi/CubeSatSim/logp.txt # echo -e "\nCommand and Control Log file also saved as /home/pi/CubeSatSim/logp.txt" elif [ "$1" = "-r" ]; then - sudo journalctl -af -u frequency # > /home/pi/CubeSatSim/logr.txt + sudo journalctl -b all -af -u frequency # > /home/pi/CubeSatSim/logr.txt elif [ "$1" = "-f" ] ; then while true; do cat /home/pi/CubeSatSim/frequency.txt | tr -d '\n' @@ -48,8 +49,11 @@ if [ "$REALTIME" = "1" ] ; then sleep 1 printf "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" done + elif [ "$1" = "-b" ]; then + sudo journalctl -b all -af -u pushbutton # > /home/pi/CubeSatSim/logb.txt + cat /home/pi/CubeSatSim/logb.txt else - sudo journalctl -af -u cubesatsim # > /home/pi/CubeSatSim/log.txt + sudo journalctl -b all -af -u cubesatsim # > /home/pi/CubeSatSim/log.txt # cat /home/pi/CubeSatSim/log.txt # echo -e "\nLog file also saved as /home/pi/CubeSatSim/log.txt" fi @@ -57,26 +61,31 @@ if [ "$REALTIME" = "1" ] ; then else if [ "$1" = "-t" ] ; then - sudo journalctl -a -u transmit > /home/pi/CubeSatSim/logt.txt + sudo journalctl -b all -a -u transmit > /home/pi/CubeSatSim/logt.txt cat /home/pi/CubeSatSim/logt.txt echo -e "\nTransmit Log file also saved as /home/pi/CubeSatSim/logt.txt" elif [ "$1" = "-c" ]; then - sudo journalctl -a -u command > /home/pi/CubeSatSim/logc.txt + sudo journalctl -b all -a -u command > /home/pi/CubeSatSim/logc.txt cat /home/pi/CubeSatSim/logc.txt echo -e "\nCommand and Control Log file also saved as /home/pi/CubeSatSim/logc.txt" elif [ "$1" = "-p" ]; then - sudo journalctl -a -u pacsatsim > /home/pi/CubeSatSim/logp.txt + sudo journalctl -b all -a -u pacsatsim > /home/pi/CubeSatSim/logp.txt cat /home/pi/CubeSatSim/logp.txt - echo -e "\nCommand and Control Log file also saved as /home/pi/CubeSatSim/logp.txt" + echo -e "\nPacSat Log file also saved as /home/pi/CubeSatSim/logp.txt" elif [ "$1" = "-r" ]; then - sudo journalctl -a -u frequency > /home/pi/CubeSatSim/logr.txt + sudo journalctl -b all -a -u frequency > /home/pi/CubeSatSim/logr.txt cat /home/pi/CubeSatSim/logr.txt echo -e "\nFrequency Log file also saved as /home/pi/CubeSatSim/logf.txt" elif [ "$1" = "-f" ] ; then cat /home/pi/CubeSatSim/frequency.txt echo + elif [ "$1" = "-b" ]; then + sudo journalctl -b all -a -u pushbutton > /home/pi/CubeSatSim/logb.txt + cat /home/pi/CubeSatSim/logb.txt + echo -e "\nPushbutton Log file also saved as /home/pi/CubeSatSim/logb.txt" + else - sudo journalctl -a -u cubesatsim > /home/pi/CubeSatSim/log.txt + sudo journalctl -b all -a -u cubesatsim > /home/pi/CubeSatSim/log.txt cat /home/pi/CubeSatSim/log.txt echo -e "\nLog file also saved as /home/pi/CubeSatSim/log.txt" fi diff --git a/pushbutton.py b/pushbutton.py new file mode 100644 index 00000000..dcf58e4e --- /dev/null +++ b/pushbutton.py @@ -0,0 +1,159 @@ +#!/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 + +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) + +if not GPIO.input(push_button): # if pushbutton is held down during boot + 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', 'CubeSat', 'password', 'amsatao7'], capture_output=True, text=True) + print(result, flush=True) #.stdout) + count = count + 1 + success = result.returncode + sleep(1) + result = subprocess.run(['nmcli', 'connection', 'show'], capture_output=True, text=True) + print(result, flush=True) + sleep(2) +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) diff --git a/systemd/pushbutton.service b/systemd/pushbutton.service new file mode 100644 index 00000000..da0fbdef --- /dev/null +++ b/systemd/pushbutton.service @@ -0,0 +1,14 @@ +[Unit] +Description=pushbutton + +[Service] +TimeoutStopSec=5 +ExecStart=python3 -u /home/pi/CubeSatSim/pushbutton.py +WorkingDirectory=/home/pi/CubeSatSim +StandardOutput=inherit +StandardError=inherit +Restart=always +User=root + +[Install] +WantedBy=default.target