Skip to content

Commit

Permalink
Add sound detection for cams that support it
Browse files Browse the repository at this point in the history
  • Loading branch information
roleoroleo committed Oct 25, 2023
1 parent f2fe2f1 commit 011f9d6
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/static/static/home/yi-hack/etc/camera.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ SWITCH_ON=yes
SAVE_VIDEO_ON_MOTION=yes
SENSITIVITY=low
BABY_CRYING_DETECT=no
SOUND_DETECTION=no
SOUND_SENSITIVITY=80
LED=no
ROTATE=no
IR=yes
Expand Down
7 changes: 6 additions & 1 deletion src/static/static/home/yi-hack/script/check_conf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ SWITCH_ON=yes
SAVE_VIDEO_ON_MOTION=yes
SENSITIVITY=low
BABY_CRYING_DETECT=no
SOUND_DETECTION=no
SOUND_SENSITIVITY=80
LED=no
ROTATE=no
IR=yes
Expand All @@ -72,17 +74,20 @@ TOPIC_MOTION=motion_detection
TOPIC_MOTION_IMAGE=motion_detection_image
MOTION_IMAGE_DELAY=0.5
TOPIC_MOTION_FILES=motion_files
TOPIC_SOUND_DETECTION=sound_detection
BIRTH_MSG=online
WILL_MSG=offline
MOTION_START_MSG=motion_start
MOTION_STOP_MSG=motion_stop
BABY_CRYING_MSG=crying
SOUND_DETECTION_MSG=sound_detected
MQTT_KEEPALIVE=120
MQTT_QOS=1
MQTT_RETAIN_BIRTH_WILL=1
MQTT_RETAIN_MOTION=0
MQTT_RETAIN_MOTION_IMAGE=0
MQTT_RETAIN_MOTION_FILES=0"
MQTT_RETAIN_MOTION_FILES=0
MQTT_RETAIN_SOUND_DETECTION=0"

if [ ! -f $SYSTEM_CONF_FILE ]; then
touch $SYSTEM_CONF_FILE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ MOTION_STOP_MSG=$(get_config MOTION_STOP_MSG)

BABY_CRYING_MSG=$(get_config BABY_CRYING_MSG)

TOPIC_SOUND_DETECTION=$(get_config TOPIC_SOUND_DETECTION)
SOUND_DETECTION_MSG=$(get_config SOUND_DETECTION_MSG)

TOPIC_MOTION_IMAGE=$(get_config TOPIC_MOTION_IMAGE)

HOST=$MQTT_IP
Expand Down Expand Up @@ -258,6 +261,17 @@ MQTT_RETAIN_MOTION=$(get_config MQTT_RETAIN_MOTION)
# fi
CONTENT='{"availability_topic":"'$MQTT_PREFIX'/'$TOPIC_BIRTH_WILL'","payload_available":"'$BIRTH_MSG'","payload_not_available":"'$WILL_MSG'","device":'$DEVICE_DETAILS', "qos": "'$MQTT_QOS'", '$RETAIN' "device_class":"sound","state_topic":"'$MQTT_PREFIX'/'$TOPIC_MOTION'","name":"'$UNIQUE_NAME'","unique_id":"'$UNIQUE_ID'","payload_on":"'$BABY_CRYING_MSG'","off_delay":60, "platform": "mqtt"}'
mqtt_publish
# Sound Detection
hass_topic "binary_sensor" "sound_detection" "Sound Detection"
MQTT_RETAIN_SOUND_DETECTION=$(get_config MQTT_RETAIN_SOUND_DETECTION)
#Don't know why... ..Home Assistant don't allow retain for Sensor and Binary Sensor
# if [ "$MQTT_RETAIN_SOUND_DETECTION" == "1" ]; then
# RETAIN='"retain":true, '
# else
RETAIN=""
# fi
CONTENT='{"availability_topic":"'$MQTT_PREFIX'/'$TOPIC_BIRTH_WILL'","payload_available":"'$BIRTH_MSG'","payload_not_available":"'$WILL_MSG'","device":'$DEVICE_DETAILS', "qos": "'$MQTT_QOS'", '$RETAIN' "device_class":"sound","state_topic":"'$MQTT_PREFIX'/'$TOPIC_SOUND_DETECTION'","name":"'$UNIQUE_NAME'","unique_id":"'$UNIQUE_ID'","payload_on":"'$SOUND_DETECTION_MSG'","off_delay":60, "platform": "mqtt"}'
mqtt_publish
# Motion Detection Image
hass_topic "camera" "motion_detection_image" "Motion Detection Image"
MQTT_RETAIN_MOTION_IMAGE=$(get_config MQTT_RETAIN_MOTION_IMAGE)
Expand Down Expand Up @@ -292,6 +306,12 @@ if [ "$MQTT_ADV_CAMERA_SETTING_ENABLE" == "yes" ]; then
# Baby Crying Detection
hass_setup_switch "BABY_CRYING_DETECT" "Baby crying detect" "music-note" $MQTT_ADV_CAMERA_SETTING_TOPIC "config"
mqtt_publish
# Sound Detection
hass_setup_switch "SOUND_DETECTION" "Sound Detection" "music-note" $MQTT_ADV_CAMERA_SETTING_TOPIC "config"
mqtt_publish
# Sound detection sensitivity
hass_setup_number "SOUND_SENSITIVITY" "Sound Detection sensitivity" "account-voice" $MQTT_ADV_CAMERA_SETTING_TOPIC 30 90 5 "slider" "config"
mqtt_publish
# Led
hass_setup_switch "LED" "Status Led" "led-on" $MQTT_ADV_CAMERA_SETTING_TOPIC "config"
mqtt_publish
Expand All @@ -302,7 +322,7 @@ if [ "$MQTT_ADV_CAMERA_SETTING_ENABLE" == "yes" ]; then
hass_setup_switch "ROTATE" "Rotate" "rotate-right" $MQTT_ADV_CAMERA_SETTING_TOPIC "config"
mqtt_publish
else
for ITEM in SWITCH_ON BABY_CRYING_DETECT LED IR ROTATE; do
for ITEM in SWITCH_ON BABY_CRYING_DETECT SOUND_DETECTION LED IR ROTATE; do
hass_topic "switch" "$ITEM"
$YI_HACK_PREFIX/bin/mosquitto_pub $HA_QOS $HA_RETAIN -h $HOST -t $TOPIC -n
done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ $YI_HACK_PREFIX/bin/mosquitto_sub -v -h $HOST -t $TOPIC | while read -r SUBSCRIB
baby_crying_detect)
IPC_OPT="-b"
;;
sound_detection)
IPC_OPT="-b"
;;
led)
IPC_OPT="-l"
;;
Expand Down
12 changes: 11 additions & 1 deletion src/www/httpd/cgi-bin/camera_settings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fi

CONF_LAST="CONF_LAST"

for I in 1 2 3 4 5 6 7 8
for I in 1 2 3 4 5 6 7 8 9 10
do
CONF="$(echo $QUERY_STRING | cut -d'&' -f$I | cut -d'=' -f1)"
VAL="$(echo $QUERY_STRING | cut -d'&' -f$I | cut -d'=' -f2)"
Expand Down Expand Up @@ -64,6 +64,16 @@ do
else
ipc_cmd -B on
fi
elif [ "$CONF" == "sound_detection" ] ; then
if [ "$VAL" == "no" ] ; then
ipc_cmd -b off
else
ipc_cmd -b on
fi
elif [ "$CONF" == "sound_sensitivity" ] ; then
if [ "$VAL" == "30" ] || [ "$VAL" == "35" ] || [ "$VAL" == "40" ] || [ "$VAL" == "45" ] || [ "$VAL" == "50" ] || [ "$VAL" == "60" ] || [ "$VAL" == "70" ] || [ "$VAL" == "80" ] || [ "$VAL" == "90" ] ; then
ipc_cmd -n $VAL
fi
elif [ "$CONF" == "led" ] ; then
if [ "$VAL" == "no" ] ; then
ipc_cmd -l off
Expand Down
5 changes: 4 additions & 1 deletion src/www/httpd/htdocs/js/modules/camera_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ APP.camera_settings = (function($) {
loadingStatusElem.fadeOut(500);

$.each(response, function(key, state) {
if (key == "SENSITIVITY" || key=="CRUISE")
if(key=="SENSITIVITY" || key=="SOUND_SENSITIVITY" || key=="CRUISE")
$('select[data-key="' + key + '"]').prop('value', state);
else
$('input[type="checkbox"][data-key="' + key + '"]').prop('checked', state === 'yes');
Expand All @@ -51,6 +51,7 @@ APP.camera_settings = (function($) {
});

configs["SENSITIVITY"] = $('select[data-key="SENSITIVITY"]').prop('value');
configs["SOUND_SENSITIVITY"] = $('select[data-key="SOUND_SENSITIVITY"]').prop('value');
configs["CRUISE"] = $('select[data-key="CRUISE"]').prop('value');

$.ajax({
Expand All @@ -59,6 +60,8 @@ APP.camera_settings = (function($) {
'save_video_on_motion=' + configs["SAVE_VIDEO_ON_MOTION"] +
'&sensitivity=' + configs["SENSITIVITY"] +
'&baby_crying_detect=' + configs["BABY_CRYING_DETECT"] +
'&sound_detection=' + configs["SOUND_DETECTION"] +
'&sound_sensitivity=' + configs["SOUND_SENSITIVITY"] +
'&led=' + configs["LED"] +
'&ir=' + configs["IR"] +
'&rotate=' + configs["ROTATE"] +
Expand Down
31 changes: 31 additions & 0 deletions src/www/httpd/htdocs/pages/camera_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,37 @@ <h4 class="no-margin">General</h4>
</span>
</td>
</tr>
<tr class="row">
<td>Sound Detection</td>
<td>
<label class="switch small">
<input type="checkbox" data-key="SOUND_DETECTION"/>
<span class="slider round"></span>
<span class="switch-text"></span>
</label>
<span class="switch-description">
Enable/disable sound detection.
</span>
</td>
</tr>
<tr class="row">
<td>Sound detection sensitivity</td>
<td>
<div class="standard-select">
<select data-key="SOUND_SENSITIVITY" id="SOUND_SENSITIVITY">
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="60">60</option>
<option value="70">70</option>
<option value="80">80</option>
<option value="90">90</option>
</select>
</div>
</td>
</tr>
<tr class="row">
<td>Status led</td>
<td>
Expand Down
18 changes: 18 additions & 0 deletions src/www/httpd/htdocs/pages/mqtt.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ <h4 class="no-margin">Topics</h4>
<input class="u-full-width" type="text" placeholder="" id="topic_motion_files" data-key="TOPIC_MOTION_FILES">
</td>
</tr>
<tr class="row">
<td>Sound Detection</td>
<td>
<label for="topic_sound_detection">Topic Suffix</label>
<input class="u-full-width" type="text" placeholder="" id="topic_sound_detection" data-key="TOPIC_SOUND_DETECTION"/>
<label for="sound_detection_msg">Sound Detection Message</label>
<input class="u-full-width" type="text" placeholder="" id="sound_detection_msg" data-key="SOUND_DETECTION_MSG"/>
</td>
</tr>
<!-- <tr class="row">
<td>Topics Prefix for remote configuration</td>
<td>
Expand Down Expand Up @@ -219,6 +228,15 @@ <h4 class="no-margin">Advanced</h4>
</span>
</td>
</tr>
<tr class="row">
<td>Retain for Sound Detection messages</td>
<td>
<input class="u-full-width" type="text" placeholder="1" data-key="MQTT_RETAIN_SOUND_DETECTION"/>
<span class="switch-description">
A retained message is a normal MQTT message with the retained flag set to true. The broker stores the last retained message and the corresponding QoS for that topic. (1 = enabled, 0 = disabled)
</span>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down

0 comments on commit 011f9d6

Please sign in to comment.