Improve logging on setup and startup, make setup scripts idempotent

This commit is contained in:
Satu Koskinen 2023-02-18 22:31:04 +02:00
parent 559e449ed1
commit 296e8c245b
10 changed files with 71 additions and 41 deletions

View File

@ -7,7 +7,6 @@ import busio
import adafruit_ads1x15.ads1015 as ADS
from adafruit_extended_bus import ExtendedI2C as I2C
from adafruit_ads1x15.analog_in import AnalogIn
# from rpi_lcd import LCD
parser = argparse.ArgumentParser(description='GPS Logger')
parser.add_argument('-o', '--output', help='Output directory', required=True)
@ -16,11 +15,6 @@ parser.add_argument('-b', '--bus', help='Custom i2c bus to use', required=False)
args = parser.parse_args()
# try:
# lcd = LCD(bus=2)
# except OSError:
# lcd = None
# Create the I2C bus
if args.bus:
i2c_bus = args.bus
@ -65,10 +59,6 @@ with open(filename, "w", 1) as f:
print((str(voltage) + " V ") + (str(depthM) + " m ") + (str(roundvolts) + " V ") + (str(rounddepth) + " m"), flush=True)
# if lcd:
# lcd.clear()
# lcd.text((str(roundvolts) + " V ") + (str(rounddepth) + " m"), 1)
f.write(time.strftime("%Y-%m-%dT%H:%M:%S") + "," + str(roundvolts) + "," + str(rounddepth) + "\n")
time.sleep(interval)

View File

@ -2,21 +2,26 @@
SAMPLE_RATE=44100
CHANNELS=2
BITRATE="192k"
BITRATE=192k
# in seconds
BATCH_RECORD_LENGTH=60
# GPS
# in seconds
GPS_INTERVAL=5
# Depth
# in seconds
DEPTH_INTERVAL=5
# Output location
TRY_MOUNT_SSD=true
# TODO
#TRY_MOUNT_SSD=true
OUTPUT_PATH=/home/pi/data
HOME_PATH=/home/pi
DEFAULT_OUTPUT_PATH=/home/pi/recordings
OUTPUT_PATH=/home/pi/data

View File

@ -13,18 +13,21 @@ card_number=$(aplay -l | grep -i usb | grep -i audio | cut -d ' ' -f 2 | cut -d
# Change default audio device
sudo touch /etc/asound.conf
sudo cat << EOF | sudo tee /etc/asound.conf
pcm.!default {
config="pcm.!default {
type plug
slave {
pcm "hw:$card_number,0"
pcm \"hw:$card_number,0\"
}
}
ctl.!default {
type hw
card $card_number
}
EOF
}"
if ! grep -q "$config" /etc/asound.conf; then
echo "$config" | sudo tee -a /etc/asound.conf
fi
cd $HOME/hydrophonitor/audio-logger && cargo build --release

View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
set -ex
@ -17,4 +17,8 @@ sudo gpsd ${device} -F /var/run/gpsd.sock
sudo sed -i "s|DEVICES=\"\"|DEVICES=\"${device}\"|g" /etc/default/gpsd
sudo grep -qxF "START_DAEMON=\"true\"" /etc/default/gpsd || echo "START_DAEMON=\"true\"" | sudo tee -a /etc/default/gpsd
config="START_DAEMON=\"true\""
if ! grep -q "$config" /etc/default/gpsd; then
echo "$config" | sudo tee -a /etc/default/gpsd
fi

View File

@ -1,16 +1,22 @@
#!/bin/sh
#!/bin/bash
set -ex
SDA_GPIO_PIN=10
SCL_GPIO_PIN=11
I2C_BUS=3
echo "Setting up depth recording"
# Enable i2c
sudo raspi-config nonint do_i2c 0
# Enable i2c on bus 3 and GPIO pins sda=23 and scl=24
sudo cat << EOF | sudo tee -a /boot/config.txt
dtoverlay=i2c-gpio,bus=3,i2c_gpio_sda=23,i2c_gpio_scl=24
EOF
config="dtoverlay=i2c-gpio,bus=$I2C_BUS,i2c_gpio_sda=$SDA_GPIO_PIN,i2c_gpio_scl=$SCL_GPIO_PIN"
if ! grep -q "$config" /boot/config.txt; then
echo "$config" | sudo tee -a /boot/config.txt
fi
# Install packages
sudo apt-get update && sudo apt-get install -y i2c-tools python3-pip

View File

@ -2,9 +2,13 @@
set -ex
DIR_PATH=$HOME
BOOT_DIR_PATH=/boot/hydrophonitor
# Output from commands within the curly braces is written
# to a log file in $BOOT_DIR_PATH
{
DIR_PATH=$HOME
echo
echo "### Update file paths in config and start script files"
echo
@ -25,6 +29,7 @@ echo
mkdir -p "$DIR_PATH"
cd "$DIR_PATH"
rm -rf hydrophonitor
cp -R $BOOT_DIR_PATH/ .
# Install some development tools
@ -80,9 +85,12 @@ CRON_FILE=/etc/crontab
CRON_COMMAND="@reboot root $DIR_PATH/hydrophonitor/scripts/start-all.sh 2>&1 > $BOOT_DIR_PATH/$(date +"%Y-%m-%dT%H-%M-%S")-log.txt"
# Append command to cron file only if it's not there yet
sudo grep -qxF "$CRON_COMMAND" $CRON_FILE || echo "$CRON_COMMAND" | sudo tee -a $CRON_FILE
if ! grep -q "$CRON_COMMAND" "$CRON_FILE"; then
echo "$CRON_COMMAND" | sudo tee -a "$CRON_FILE"
fi
# Reboot
echo
echo "### Setup ready, run 'sudo reboot' to apply all changes"
echo
} 2>&1 | sudo tee $BOOT_DIR_PATH/$(date +"%Y-%m-%dT%H-%M-%S")-setup-log.txt

View File

@ -8,9 +8,11 @@ echo "Setting up the real time clock module, part 1"
sudo raspi-config nonint do_i2c 0
# Enable i2c rtc on the default i2c pins
sudo cat << EOF | sudo tee -a /boot/config.txt
dtoverlay=i2c-rtc,ds3231
EOF
config="dtoverlay=i2c-rtc,ds3231"
if ! grep -q "$config" /boot/config.txt; then
echo "$config" | sudo tee -a /boot/config.txt
fi
# Disable fake-hwclock
sudo apt-get remove -y fake-hwclock
@ -18,11 +20,13 @@ sudo update-rc.d -f fake-hwclock remove
sudo systemctl disable fake-hwclock
# Load needed modules at boot
sudo cat << EOF | sudo tee -a /etc/modules
i2c-bcm2708
config="i2c-bcm2708
i2c-dev
rtc-ds1307
EOF
rtc-ds1307"
if ! grep -q "$config" /etc/modules; then
echo "$config" | sudo tee -a /etc/modules
fi
# Remove some lines from /lib/udev/hwclock-set
sudo sed -i '/^if \[ \-e \/run\/systemd\/system \] ; then$/,/^fi$/d' /lib/udev/hwclock-set

View File

@ -4,15 +4,19 @@ set -x
echo "Setting up the real time clock module, part 2"
echo ds1307 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
I2C_BUS=1
echo ds1307 0x68 | sudo tee /sys/class/i2c-adapter/i2c-$I2C_BUS/new_device
# Load RTC clock at boot
sudo sed -i "s/^exit 0$//" /etc/rc.local
sudo cat << EOF | sudo tee -a /etc/rc.local
echo ds1307 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
config="echo ds1307 0x68 | sudo tee /sys/class/i2c-adapter/i2c-$I2C_BUS/new_device
sudo hwclock -s
exit 0
EOF
exit 0"
if ! grep -q "$config" /etc/rc.local; then
sudo sed -i "s/^exit 0$//" /etc/rc.local
echo "$config" | sudo tee -a /etc/rc.local
fi
# Set system time to Internet time
echo "Restarting systmd-timesyncd to update system time"

View File

@ -3,6 +3,9 @@
# Print all commands to standard output
set -x
BOOT_DIR_PATH=/boot/hydrophonitor
{
SCRIPT_PATH=/home/pi/hydrophonitor/scripts
# Export the configuration values
@ -16,4 +19,5 @@ mkdir -p "$OUTPUT_DIR"/audio
# Sleep for a little to wait for GPS and sound card to be ready
sleep 10
(export OUTPUT_DIR=$OUTPUT_DIR; $SCRIPT_PATH/start-audio.sh & $SCRIPT_PATH/start-gps.sh & $SCRIPT_PATH/start-pressure-depth.sh) >> "$OUTPUT_DIR"/log.txt 2>&1
(export OUTPUT_DIR=$OUTPUT_DIR; $SCRIPT_PATH/start-audio.sh & $SCRIPT_PATH/start-gps.sh & $SCRIPT_PATH/start-pressure-depth.sh) 2>&1 | tee "$OUTPUT_DIR"/log.txt
} 2>&1 | tee $BOOT_DIR_PATH/$(date +"%Y-%m-%dT%H-%M-%S")-startup-log.txt

View File

@ -2,10 +2,12 @@
set -x
I2C_BUS=3
# Export the configuration values
SCRIPT_PATH=/home/pi/hydrophonitor/scripts
. $SCRIPT_PATH/export-config-values.sh
OPTIONS="--output $OUTPUT_DIR --interval $DEPTH_INTERVAL --bus 3"
OPTIONS="--output $OUTPUT_DIR --interval $DEPTH_INTERVAL --bus $I2C_BUS"
cd $HOME_PATH/hydrophonitor/depth-logger && python record-depth.py $OPTIONS