From 296e8c245b63d35bea93e213ff7be5ca487e5ceb Mon Sep 17 00:00:00 2001 From: Satu Koskinen Date: Sat, 18 Feb 2023 22:31:04 +0200 Subject: [PATCH] Improve logging on setup and startup, make setup scripts idempotent --- depth-logger/record-depth.py | 10 ---------- hydrophonitor-config.txt | 11 ++++++++--- scripts/setup-audio.sh | 13 ++++++++----- scripts/setup-gps.sh | 8 ++++++-- scripts/setup-pressure-depth.sh | 14 ++++++++++---- scripts/setup-raspberry-pi.sh | 12 ++++++++++-- scripts/setup-rtc-1.sh | 18 +++++++++++------- scripts/setup-rtc-2.sh | 16 ++++++++++------ scripts/start-all.sh | 6 +++++- scripts/start-pressure-depth.sh | 4 +++- 10 files changed, 71 insertions(+), 41 deletions(-) diff --git a/depth-logger/record-depth.py b/depth-logger/record-depth.py index fb15d0f..284277d 100644 --- a/depth-logger/record-depth.py +++ b/depth-logger/record-depth.py @@ -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 @@ -64,10 +58,6 @@ with open(filename, "w", 1) as f: # roundtemp = round(temperatureC, 2) 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") diff --git a/hydrophonitor-config.txt b/hydrophonitor-config.txt index d0d4741..efff13c 100644 --- a/hydrophonitor-config.txt +++ b/hydrophonitor-config.txt @@ -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 diff --git a/scripts/setup-audio.sh b/scripts/setup-audio.sh index 48fa92a..3b37d54 100755 --- a/scripts/setup-audio.sh +++ b/scripts/setup-audio.sh @@ -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 diff --git a/scripts/setup-gps.sh b/scripts/setup-gps.sh index f544018..3749a2e 100755 --- a/scripts/setup-gps.sh +++ b/scripts/setup-gps.sh @@ -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 diff --git a/scripts/setup-pressure-depth.sh b/scripts/setup-pressure-depth.sh index a99db4d..6f5f9d8 100644 --- a/scripts/setup-pressure-depth.sh +++ b/scripts/setup-pressure-depth.sh @@ -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 diff --git a/scripts/setup-raspberry-pi.sh b/scripts/setup-raspberry-pi.sh index 841ae6e..65faafe 100644 --- a/scripts/setup-raspberry-pi.sh +++ b/scripts/setup-raspberry-pi.sh @@ -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 \ No newline at end of file diff --git a/scripts/setup-rtc-1.sh b/scripts/setup-rtc-1.sh index a3c9350..0f32df1 100755 --- a/scripts/setup-rtc-1.sh +++ b/scripts/setup-rtc-1.sh @@ -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 diff --git a/scripts/setup-rtc-2.sh b/scripts/setup-rtc-2.sh index 79d87a6..44cc833 100755 --- a/scripts/setup-rtc-2.sh +++ b/scripts/setup-rtc-2.sh @@ -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" diff --git a/scripts/start-all.sh b/scripts/start-all.sh index 28a4ccd..e2d8307 100755 --- a/scripts/start-all.sh +++ b/scripts/start-all.sh @@ -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 diff --git a/scripts/start-pressure-depth.sh b/scripts/start-pressure-depth.sh index f24b4ac..a4b5506 100644 --- a/scripts/start-pressure-depth.sh +++ b/scripts/start-pressure-depth.sh @@ -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