diff --git a/Makefile b/Makefile index 719540d..89482d9 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,4 @@ build-audio: cd audio-logger && cargo build --release start-all: - cd scripts && start-all.sh \ No newline at end of file + cd scripts && start-all.sh diff --git a/audio-logger/.gitignore b/audio-logger/.gitignore index 0cd831f..5b3eb2d 100644 --- a/audio-logger/.gitignore +++ b/audio-logger/.gitignore @@ -1,3 +1,3 @@ -taget +target *.wav recordings \ No newline at end of file diff --git a/audio-logger/run_test.sh b/audio-logger/run_test.sh index ebd0a94..a4fdc59 100644 --- a/audio-logger/run_test.sh +++ b/audio-logger/run_test.sh @@ -7,4 +7,4 @@ target/release/audio rec \ --sample-rate 44100 \ --channels 2 \ --buffer-size 1024 \ - alsa \ \ No newline at end of file + alsa \ 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/docs/pi4wiring.png b/docs/pi4wiring.png new file mode 100644 index 0000000..b150884 Binary files /dev/null and b/docs/pi4wiring.png differ 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/lcd-display/test_lcd.py b/lcd-display/test_lcd.py deleted file mode 100644 index 4f17595..0000000 --- a/lcd-display/test_lcd.py +++ /dev/null @@ -1,13 +0,0 @@ -from rpi_lcd import LCD -from time import sleep - -lcd = LCD(bus=2) - -lcd.text('Hello World!', 1) -lcd.text('Raspberry Pi', 2) -lcd.text('is really', 3, 'center') -lcd.text('awesome', 4, 'right') - -sleep(5) -lcd.clear() - diff --git a/scripts/setup-audio.sh b/scripts/setup-audio.sh index 48fa92a..f0c5e5b 100755 --- a/scripts/setup-audio.sh +++ b/scripts/setup-audio.sh @@ -13,18 +13,20 @@ 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 d889d26..b96dd71 100644 --- a/scripts/setup-raspberry-pi.sh +++ b/scripts/setup-raspberry-pi.sh @@ -2,8 +2,17 @@ set -ex -DIR_PATH=$HOME BOOT_DIR_PATH=/boot/hydrophonitor +LOG_FILE=$BOOT_DIR_PATH/$(date +"%Y-%m-%dT%H-%M-%S")-setup-log.txt + +# Output from commands within the curly braces is written +# to $LOG_FILE +{ +DIR_PATH=$HOME + +echo +echo "Starting setup, writing logs to $LOG_FILE" +echo echo echo "### Update file paths in config and start script files" @@ -25,6 +34,7 @@ echo mkdir -p "$DIR_PATH" cd "$DIR_PATH" +rm -rf hydrophonitor cp -R $BOOT_DIR_PATH/ . # Install some development tools @@ -63,19 +73,29 @@ echo cd "$DIR_PATH" && ./hydrophonitor/scripts/setup-pressure-depth.sh +# Setup shutdown button +echo +echo "### Setup shutdown button" +echo + +cd "$DIR_PATH" && ./hydrophonitor/scripts/setup-shutdown-button.sh + # Set up cron job to start the recordings at boot echo echo "### Set up a cron job to start the recordings at boot" echo -# USER=$(whoami) 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" +CRON_LOG_FILE="$BOOT_DIR_PATH/\$(date +\"%Y-%m-%dT%H-%M-%S\")-cron-log.txt" +CRON_COMMAND="@reboot root $DIR_PATH/hydrophonitor/scripts/start-all.sh 2>&1 > $CRON_LOG_FILE" # 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 $LOG_FILE \ 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/setup-shutdown-button.sh b/scripts/setup-shutdown-button.sh new file mode 100644 index 0000000..acdc2f0 --- /dev/null +++ b/scripts/setup-shutdown-button.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -ex + +echo "Setting up shutdown button" + +config="dtoverlay=gpio-shutdown,gpio_pin=21,gpio_pull=up,active_low=1" + +if ! grep -q "$config" /boot/config.txt; then + echo "$config" | sudo tee -a /boot/config.txt +fi diff --git a/scripts/start-all.sh b/scripts/start-all.sh index 28a4ccd..a1d395e 100755 --- a/scripts/start-all.sh +++ b/scripts/start-all.sh @@ -3,6 +3,10 @@ # Print all commands to standard output set -x +BOOT_DIR_PATH=/boot/hydrophonitor +LOG_FILE=$BOOT_DIR_PATH/$(date +"%Y-%m-%dT%H-%M-%S")-startup-log.txt + +{ SCRIPT_PATH=/home/pi/hydrophonitor/scripts # Export the configuration values @@ -16,4 +20,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 $LOG_FILE 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