diff --git a/README.md b/README.md index 3786efa..dce9609 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ Linux box with a loudspeaker and an emergency light to be used for announcements and notifications. ## Hardware +### Overview * SBC with GPIOs and sound output :: OrangePi Zero with an expansion board * PA system * speaker :: 30W, 4 Ohms horn loudspeaker @@ -11,12 +12,30 @@ Linux box with a loudspeaker and an emergency light to be used for announcements * Warning Light system * warning light :: 12V 10W rotary orange warning light * relay module :: 12V low trigger relay - + * System Activity Light system + * indicator LED + * Human Activity Sensor system + * PIR module + +### Building +TBD + ## Software +### Overview * Operating System - Linux :: Armbian Bullseye for the OrangePi Zero * PA System * Voice Synthesizer :: Festival / ESpeak +### Installation +Run as root: +```bash +cp toolkit/* /usr/local/bin/ + +cp etc/systemd/system /etc/systemd/system +systemctl enable --now system-activity-light.service +systemctl enable --now human-activity-sensor.service +``` + ## Implementation Notes ### Armbian + OrangePi Zero Installation @@ -44,21 +63,6 @@ cd wiringOP ./build ``` -#### Initialize GPIOs -```bash -# System Activity Indicator -PIN_SALIGHT=11 -gpio mode $PIN_SALIGHT output - -# Emergency Light system -PIN_ELIGHT=7 -gpio mode $PIN_ELIGHT output - -# PA system -PIN_PASOUND=5 -gpio mode $PIN_PASOUND output -``` - ### PA System #### Enabling mixed sound output ```bash @@ -90,78 +94,10 @@ espeak -f ssml.txt -m sox infile.wav outfile.wav downsample echo 0.5 1 1 1 ``` -#### Playing a sound file -```bash -aplay file.wav -mplayer file.mp3 -``` - -### Emergency Light -#### Control Script -```bash -cat /usr/local/bin/emergency-light -#!/bin/bash - -PIN_ELIGHT=7 - -# init -gpio mode $PIN_ELIGHT output - -# command -if [[ "$1" == "on" ]]; then - gpio write $PIN_ELIGHT 1 -elif [[ "$1" == "off" ]]; then - gpio write $PIN_ELIGHT 0 -elif [[ "$1" == "read" ]]; then - gpio read $PIN_ELIGHT -else - gpio toggle $PIN_ELIGHT -fi -``` - -### System Activity Light -#### System Usage -```bash -uptime | head -n 1 | cut -f3 -d, | cut -f2 -d: -``` - -#### Daemon -```bash -cat /etc/systemd/system/system-activity-light.service -[Unit] -Description=System Activity Light Daemon - -[Service] -ExecStart=/usr/sbin/system-activity-light-daemon - -[Install] -WantedBy=multi-user.target -``` - -```bash -cat /usr/sbin/system-activity-light-daemon -#!/bin/bash - -DELAY=10000 - -while [[ true ]]; do - ACTIVITY=2000 - TOP=`system-usage` - if (( $(echo "$TOP > 1.0" | bc -l) )); then - ACTIVITY=50 - elif (( $(echo "$TOP > 0.75" | bc -l) )); then - ACTIVITY=100 - elif (( $(echo "$TOP > 0.5" | bc -l) )); then - ACTIVITY=500 - elif (( $(echo "$TOP > 0.2" | bc -l) )); then - ACTIVITY=1000 - fi - echo $TOP - system-activity-light blink $ACTIVITY $DELAY -done -``` +### Human Activity Sensor +#### Reading File Timestamp ```bash -sudo systemctl enable --now system-activity-light.service +stat /var/tmp/human_last_seen --format=%Y ``` diff --git a/bin/emergency-freeform b/bin/emergency-freeform index 5411f74..407c752 100755 --- a/bin/emergency-freeform +++ b/bin/emergency-freeform @@ -4,20 +4,18 @@ source pa-lib pa-preheat -pa-volume-loud -pa-announcement-quickest +pa-volume-loud-if-inactive emergency-light on +pa-alarm-start -sleep 1 +sleep 5 pa-say "$@" -sleep 1 +sleep 5 emergency-light off -pa-announcement-quick - pa-shutdown diff --git a/bin/pa-lib b/bin/pa-lib index 6d43d8e..9f36201 100644 --- a/bin/pa-lib +++ b/bin/pa-lib @@ -29,6 +29,17 @@ function pa-volume-loud() { pa-volume 25 } +function pa-volume-loud-if-inactive() { + LAST_ACTIVE=`human-activity-ago` + if [[ $LAST_ACTIVE > 200 ]]; then + info "Human is inactive. Last activity: $LAST_ACTIVE seconds ago." + pa-volume-loud + else + info "Human is active. Setting only normal volume." + pa-volume-normal + fi +} + function pa-volume-quiet() { info "Volume set to low" pa-volume 8 @@ -47,6 +58,7 @@ function pa-shutdown() { info "PA Shutdown" pa-power off pa-volume-normal + killall $PLAYER } function pa-announcement() { diff --git a/etc/systemd/system/human-activity-sensor.service b/etc/systemd/system/human-activity-sensor.service new file mode 100644 index 0000000..5594252 --- /dev/null +++ b/etc/systemd/system/human-activity-sensor.service @@ -0,0 +1,9 @@ +[Unit] +Description=Human Activity Sensor Daemon + +[Service] +ExecStart=/usr/local/bin/human-activity-daemon + +[Install] +WantedBy=multi-user.target + diff --git a/etc/systemd/system/system-activity-light.service b/etc/systemd/system/system-activity-light.service new file mode 100644 index 0000000..b3a93e5 --- /dev/null +++ b/etc/systemd/system/system-activity-light.service @@ -0,0 +1,9 @@ +[Unit] +Description=System Activity Light Daemon + +[Service] +ExecStart=/usr/sbin/system-activity-light-daemon + +[Install] +WantedBy=multi-user.target + diff --git a/toolkit/human-activity-ago b/toolkit/human-activity-ago new file mode 100755 index 0000000..d31f47c --- /dev/null +++ b/toolkit/human-activity-ago @@ -0,0 +1,11 @@ +#!/bin/bash + +LAST_SEEN_FILE=/var/tmp/human_last_seen +LAST_CHECKED_FILE=/var/tmp/human_last_checked + +LAST_SEEN=`stat /var/tmp/human_last_seen --format=%Y` +TIME_NOW=`date +%s` + +AGO=$(( $TIME_NOW - $LAST_SEEN )) +echo $AGO + diff --git a/toolkit/human-activity-daemon b/toolkit/human-activity-daemon new file mode 100755 index 0000000..7a5962b --- /dev/null +++ b/toolkit/human-activity-daemon @@ -0,0 +1,27 @@ +#!/bin/bash + +READER=human-activity-sensor +LAST_SEEN_FILE=/var/tmp/human_last_seen +LAST_CHECKED_FILE=/var/tmp/human_last_checked + +echo "Human Activity Daemon" +echo "" +echo "Last seen file: $LAST_SEEN_FILE" +echo "Last checked file: $LAST_CHECKED_FILE" +echo "" +echo "Values stream:" + +LAST_VAL=-1 +while [[ true ]]; do + VAL=`$READER` + touch $LAST_CHECKED_FILE + if [[ $VAL == "1" ]]; then + touch $LAST_SEEN_FILE + fi + if [[ $LAST_VAL != $VAL ]]; then + echo "[`date "+%F %T"`] $VAL" + LAST_VAL=$VAL + fi + sleep 1 +done + diff --git a/toolkit/human-activity-sensor b/toolkit/human-activity-sensor new file mode 100755 index 0000000..d56684d --- /dev/null +++ b/toolkit/human-activity-sensor @@ -0,0 +1,10 @@ +#!/bin/bash + +PIN_PIR=3 + +# init +gpio mode $PIN_PIR input + +# read +gpio read $PIN_PIR +