From cda078de5993622d5ed414bcbd46ec2763f5a133 Mon Sep 17 00:00:00 2001 From: Phill Price Date: Tue, 28 Jun 2022 09:25:10 +0100 Subject: [PATCH 1/4] mqtt from server --- scripts/server.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/scripts/server.py b/scripts/server.py index 9d9e242..be860fd 100755 --- a/scripts/server.py +++ b/scripts/server.py @@ -12,6 +12,7 @@ import operator import socket import threading import os +import paho.mqtt.client as mqtt from utils.notifications import sendAppriseNotifications from utils.parse_settings import config_to_settings @@ -32,6 +33,15 @@ ADDR = (SERVER, PORT) FORMAT = 'utf-8' DISCONNECT_MESSAGE = "!DISCONNECT" +MQTT_SERVER = "192.168.0.1" +MQTT_PORT = 1883 +MQTT_CLIENT = "birdpi" +MQTT_USER = "birdpi" +MQTT_PASSWORD = "password" +MQTT_TOPIC = "birdpi/found" +MQTT_TOPIC_ATTRIBUTES = "birdpi/found/attributes" + + userDir = os.path.expanduser('~') DB_PATH = userDir + '/BirdNET-Pi/scripts/birds.db' @@ -443,6 +453,15 @@ def handle_client(conn, addr): File_Name + '\n') + # post to mqtt + try: + mclient = mqtt.Client(client_id=MQTT_CLIENT, clean_session=True, userdata=None, protocol=mqtt.MQTTv311, transport="> + mclient.username_pw_set(username=MQTT_USER, password=MQTT_PASSWORD) + mclient.connect(MQTT_SERVER, MQTT_PORT) + mclient.publish(MQTT_TOPIC,Com_Name) + mclient.disconnect() + except: + print('MQTT Failed') if birdweather_id != "99999": try: @@ -485,6 +504,13 @@ def handle_client(conn, addr): post_soundscape_end_time + post_commonName + post_scientificName + post_algorithm + post_confidence + post_end print(post_json) response = requests.post(detection_url, json=json.loads(post_json)) + + mclient = mqtt.Client(client_id=MQTT_CLIENT, clean_session=True, userdata=None, protocol=mqtt.MQTTv311, trans> + mclient.username_pw_set(username=MQTT_USER, password=MQTT_PASSWORD) + mclient.connect(MQTT_SERVER, MQTT_PORT) + mclient.publish(MQTT_TOPIC_ATTRIBUTES,str(post_json)) + mclient.disconnect() + print("Detection POST Response Status - ", response.status_code) except BaseException: print("Cannot POST right now") From ea5d9d70eb8e8f40c8bc6afc29bf12f300ce6146 Mon Sep 17 00:00:00 2001 From: Phill Price Date: Thu, 29 Sep 2022 17:57:40 +0100 Subject: [PATCH 2/4] Add Heartbeat ping on every analysis to see if service is working --- birdnet.conf-defaults | 40 +++++++++++++++++++++---------------- scripts/birdnet_analysis.sh | 5 +++++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/birdnet.conf-defaults b/birdnet.conf-defaults index 239d4d8..9a027c7 100644 --- a/birdnet.conf-defaults +++ b/birdnet.conf-defaults @@ -16,8 +16,8 @@ LONGITUDE= BIRDWEATHER_ID= #----------------------- Web Interface User Password ------------------------# -#____________________The variable below sets the 'birdnet'_____________________# -#___________________user password for the Live Audio Stream,___________________# +#____________________The variable below sets the 'birdnet'_____________________# +#___________________user password for the Live Audio Stream,___________________# #_________________Tools, System Links, and the Processed files ________________# ## CADDY_PWD is the plaintext password (that will be hashed) and used to access @@ -26,7 +26,7 @@ BIRDWEATHER_ID= CADDY_PWD= #------------------------- Live Audio Stream --------------------------------# -#_____________The variable below configures/enables the live___________________# +#_____________The variable below configures/enables the live___________________# #_____________________________audio stream.____________________________________# @@ -43,7 +43,7 @@ ICE_PWD=birdnetpi ## BIRDNETPI_URL is the URL where the extractions, data-set, and live-stream -## will be web-hosted. If you do not own a domain, or would just prefer to keep +## will be web-hosted. If you do not own a domain, or would just prefer to keep ## the BirdNET-Pi on your local network, keep this EMPTY. BIRDNETPI_URL= @@ -65,8 +65,8 @@ APPRISE_NOTIFY_NEW_SPECIES=0 APPRISE_WEEKLY_REPORT=1 #---------------------- Flickr Images API Configuration -----------------------# -## If FLICKR_API_KEY is set, the web interface will try and display bird images -## for each detection. If FLICKR_FILTER_EMAIL is set, the images will only be +## If FLICKR_API_KEY is set, the web interface will try and display bird images +## for each detection. If FLICKR_FILTER_EMAIL is set, the images will only be ## displayed from a particular Flickr user (e.g. yourself). FLICKR_API_KEY= @@ -78,18 +78,18 @@ FLICKR_FILTER_EMAIL= ## RECS_DIR is the location birdnet_analysis.service will look for the data-set ## it needs to analyze. Be sure this directory is readable and writable for -## the BIRDNET_USER. +## the BIRDNET_USER. RECS_DIR=$HOME/BirdSongs -## REC_CARD is the sound card you would want the birdnet_recording.service to +## REC_CARD is the sound card you would want the birdnet_recording.service to ## use. Leave this as "default" to use PulseAudio (recommended), or use ## the output from "aplay -L" to specify an ALSA device. REC_CARD=default -## PROCESSED is the directory where the formerly 'Analyzed' files are moved -## after extractions have been made from them. This includes both WAVE and +## PROCESSED is the directory where the formerly 'Analyzed' files are moved +## after extractions have been made from them. This includes both WAVE and ## BirdNET.selection.txt files. PROCESSED=${RECS_DIR}/Processed @@ -103,7 +103,7 @@ EXTRACTED=${RECS_DIR}/Extracted OVERLAP=0.0 -## CONFIDENCE is the minimum confidence level from 0.0-1.0 BirdNET's analysis +## CONFIDENCE is the minimum confidence level from 0.0-1.0 BirdNET's analysis ## should reach before creating an entry in the BirdNET.selection.txt file. ## Don't set this to 1.0 or you won't have any results. @@ -126,14 +126,14 @@ FULL_DISK=purge ## PRIVACY_THRESHOLD can be set to enable sensitivity to Human sounds. This ## setting is an effort to introduce privacy into the data collection. -## The PRIVACY_THRESHOLD value represents a percentage of the entire species +## The PRIVACY_THRESHOLD value represents a percentage of the entire species ## list used during analysis. If a human sound is predicted anywhere within ## the precentile set below, no data is collected for that audio chunk. ## Valid range: 0-3 PRIVACY_THRESHOLD=0 -## RECORDING_LENGTH sets the length of the recording that BirdNET-Lite will +## RECORDING_LENGTH sets the length of the recording that BirdNET-Lite will ## analyze. RECORDING_LENGTH=15 @@ -145,11 +145,11 @@ RECORDING_LENGTH=15 EXTRACTION_LENGTH= ## AUDIOFMT set the audio format that sox should use for the extractions. -## The default is mp3. Available formats are: 8svx aif aifc aiff aiffc al amb +## The default is mp3. Available formats are: 8svx aif aifc aiff aiffc al amb ## amr-nb amr-wb anb au avr awb caf cdda cdr cvs cvsd cvu dat dvms f32 f4 f64 f8 -## fap flac fssd gsm gsrt hcom htk ima ircam la lpc lpc10 lu mat mat4 mat5 maud -## mp2 mp3 nist ogg paf prc pvf raw s1 s16 s2 s24 s3 s32 s4 s8 sb sd2 sds sf sl -## sln smp snd sndfile sndr sndt sou sox sph sw txw u1 u16 u2 u24 u3 u32 u4 u8 +## fap flac fssd gsm gsrt hcom htk ima ircam la lpc lpc10 lu mat mat4 mat5 maud +## mp2 mp3 nist ogg paf prc pvf raw s1 s16 s2 s24 s3 s32 s4 s8 sb sd2 sds sf sl +## sln smp snd sndfile sndr sndt sou sox sph sw txw u1 u16 u2 u24 u3 u32 u4 u8 ## ub ul uw vms voc vorbis vox w64 wav wavpcm wv wve xa xi ## Note: Most have not been tested. @@ -163,6 +163,12 @@ DATABASE_LANG=en ## your installation is behind by, relative to the Github repo. This number ## appears next to "Tools" when you're 50 or more commits behind. +## HEARTBEAT_URL is a location to ping every time some analysis is done +## no information is sent to the the URL, its a heart beat to show that the +## analysis is continuing + +HEARBEAT_URL= + SILENCE_UPDATE_INDICATOR=0 ## These are just for debugging diff --git a/scripts/birdnet_analysis.sh b/scripts/birdnet_analysis.sh index 9a77c01..edf8100 100755 --- a/scripts/birdnet_analysis.sh +++ b/scripts/birdnet_analysis.sh @@ -152,6 +152,11 @@ ${BIRDWEATHER_ID_LOG} ${INCLUDEPARAM} \ ${EXCLUDEPARAM} \ ${BIRDWEATHER_ID_PARAM} + if [ ! -z $HEARTBEAT_URL ]; then + echo "Performing Heartbeat" + IP=`curl -s ${HEARTBEAT_URL}` + echo "Heartbeat: $IP" + fi done } From 9a237bec444bbd67a373eee9bb5bf8c929b57680 Mon Sep 17 00:00:00 2001 From: Phill Price Date: Sat, 8 Oct 2022 16:08:31 +0100 Subject: [PATCH 3/4] Revert "mqtt from server" This reverts commit cda078de5993622d5ed414bcbd46ec2763f5a133. --- scripts/server.py | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/scripts/server.py b/scripts/server.py index be860fd..9d9e242 100755 --- a/scripts/server.py +++ b/scripts/server.py @@ -12,7 +12,6 @@ import operator import socket import threading import os -import paho.mqtt.client as mqtt from utils.notifications import sendAppriseNotifications from utils.parse_settings import config_to_settings @@ -33,15 +32,6 @@ ADDR = (SERVER, PORT) FORMAT = 'utf-8' DISCONNECT_MESSAGE = "!DISCONNECT" -MQTT_SERVER = "192.168.0.1" -MQTT_PORT = 1883 -MQTT_CLIENT = "birdpi" -MQTT_USER = "birdpi" -MQTT_PASSWORD = "password" -MQTT_TOPIC = "birdpi/found" -MQTT_TOPIC_ATTRIBUTES = "birdpi/found/attributes" - - userDir = os.path.expanduser('~') DB_PATH = userDir + '/BirdNET-Pi/scripts/birds.db' @@ -453,15 +443,6 @@ def handle_client(conn, addr): File_Name + '\n') - # post to mqtt - try: - mclient = mqtt.Client(client_id=MQTT_CLIENT, clean_session=True, userdata=None, protocol=mqtt.MQTTv311, transport="> - mclient.username_pw_set(username=MQTT_USER, password=MQTT_PASSWORD) - mclient.connect(MQTT_SERVER, MQTT_PORT) - mclient.publish(MQTT_TOPIC,Com_Name) - mclient.disconnect() - except: - print('MQTT Failed') if birdweather_id != "99999": try: @@ -504,13 +485,6 @@ def handle_client(conn, addr): post_soundscape_end_time + post_commonName + post_scientificName + post_algorithm + post_confidence + post_end print(post_json) response = requests.post(detection_url, json=json.loads(post_json)) - - mclient = mqtt.Client(client_id=MQTT_CLIENT, clean_session=True, userdata=None, protocol=mqtt.MQTTv311, trans> - mclient.username_pw_set(username=MQTT_USER, password=MQTT_PASSWORD) - mclient.connect(MQTT_SERVER, MQTT_PORT) - mclient.publish(MQTT_TOPIC_ATTRIBUTES,str(post_json)) - mclient.disconnect() - print("Detection POST Response Status - ", response.status_code) except BaseException: print("Cannot POST right now") From de85ac32291e18497d873ff2b0b0c49c6bc57013 Mon Sep 17 00:00:00 2001 From: Phill Price Date: Sat, 8 Oct 2022 16:15:33 +0100 Subject: [PATCH 4/4] Correct HEARTBEAT_URL name --- birdnet.conf-defaults | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/birdnet.conf-defaults b/birdnet.conf-defaults index 9a027c7..112dc18 100644 --- a/birdnet.conf-defaults +++ b/birdnet.conf-defaults @@ -167,7 +167,7 @@ DATABASE_LANG=en ## no information is sent to the the URL, its a heart beat to show that the ## analysis is continuing -HEARBEAT_URL= +HEARTBEAT_URL= SILENCE_UPDATE_INDICATOR=0