Last Updated or created 2022-05-11
When running scripts which take a long time, i don’t want to wait for things to finish before i can start the next one.
For example, using my dedup script or compiling stuff. I wanna know when it is finished.
So i made some scripts
I’ve put a function in .bashrc, so i can use a command like
notify “Compiling is ready”
A command like this i can put at the end of a command or in a script file at the end.
make && make install && notify “compile ready”
What does it do when executed?
- Send a mqtt message to the broker
- Node-red will read this message and:
- Send a message to my display on my desk – Sound and message notification. (See another post how i made this )
- Send a message to a script on my Domoticz instance downstairs.
- This will use a script to get a speech file from google, and play this on some small speakers in my livingroom
- Send a pushover message to my phone
- Display a message on my TV ( not in code below )
How?
At the end of your .bashrc
function notify() {
if [ -z "$1" ]; then
echo "Usage: $0 \"message\"";
exit 1;
fi
mosquitto_pub -h 10.1.0.17 -t notify/bashscript -m "$1"
}
Scripts on my Domoticz instance
Python script
#!/usr/bin/python
import paho.mqtt.client as mqttClient
import time
import os
import subprocess
import shlex
Connected = False
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected
Connected =True
else:
print("Connection failed")
def on_message(client, userdata, message):
print "Message received: " + message.topic + " : " + message.payload
fileName = "/home/pi/domoticz/scripts/speech" + " \"" + message.payload + "\""
print fileName
args = shlex.split(fileName)
time.sleep(1)
p = subprocess.Popen(args)
broker_address = "10.1.0.17"
port = 1883
#user = "user"
#password = "password"
client = mqttClient.Client("speechcmd")
#client.username_pw_set(user, password=password)
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker_address, port=port)
client.loop_start()
while Connected != True:
time.sleep(0.1)
client.subscribe('speech/cmd')
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print "exiting"
client.disconnect()
client.loop_stop()
Caching speech script
This script will look for a cached audiofile with requested text, and uses that. Else it wil request a audio file from google, caches it and plays it though the speakers.
#!/bin/bash
INPUT=$*
input2=$(echo $INPUT | base64)
echo "$input2 = $INPUT" >> /home/pi/cache/files-text-relation
if [ -f /home/pi/cache/$input2.mp3 ] ; then
mpg123 -q /home/pi/cache/$input2.mp3 1>/dev/null 2>/dev/null
else
echo not cached
STRINGNUM=0
ary=($INPUT)
for key in "${!ary[@]}"
do
SHORTTMP[$STRINGNUM]="${SHORTTMP[$STRINGNUM]} ${ary[$key]}"
LENGTH=$(echo ${#SHORTTMP[$STRINGNUM]})
#echo "word:$key, ${ary[$key]}"
#echo "adding to: $STRINGNUM"
if [[ "$LENGTH" -lt "100" ]]; then
#echo starting new line
SHORT[$STRINGNUM]=${SHORTTMP[$STRINGNUM]}
else
STRINGNUM=$(($STRINGNUM+1))
SHORTTMP[$STRINGNUM]="${ary[$key]}"
SHORT[$STRINGNUM]="${ary[$key]}"
fi
done
for key in "${!SHORT[@]}"
do
echo "Playing line: $(($key+1)) of $(($STRINGNUM+1))"
NEXTURL=$(echo ${SHORT[$key]} | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g')
echo $NEXTURL
mpg123 -w $input2 -q "http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=$NEXTURL&tl=En-us"
ffmpeg -i $input2 -codec:a libmp3lame -qscale:a 2 /home/pi/cache/$input2.mp3
mpg123 /home/pi/cache/$input2.mp3
done
fi
Node-red flow
function notify() {
if [ -z "$1" ]; then
echo "Usage: $0 \"message\"";
exit 1;
fi
mosquitto_pub -h 10.1.0.17 -t notify/bashscript -m [
{
"id": "1442fca698589679",
"type": "mqtt in",
"z": "cb6f001b.721c3",
"name": "",
"topic": "notify/bashscript",
"qos": "2",
"datatype": "auto",
"broker": "8c74c5f6.9a7a48",
"nl": false,
"rap": false,
"inputs": 0,
"x": 180,
"y": 580,
"wires": [
[
"ddf5744bb5b73d4d",
"faa5c794652d7a57",
"b4e0107399248fea",
"443f960b5d1cf40e"
]
]
},
{
"id": "ddf5744bb5b73d4d",
"type": "mqtt out",
"z": "cb6f001b.721c3",
"name": "",
"topic": "speech/cmd",
"qos": "",
"retain": "",
"broker": "8c74c5f6.9a7a48",
"x": 590,
"y": 560,
"wires": []
},
{
"id": "e95e828451d83158",
"type": "comment",
"z": "cb6f001b.721c3",
"name": "bash notify",
"info": "",
"x": 170,
"y": 540,
"wires": []
},
{
"id": "faa5c794652d7a57",
"type": "mqtt out",
"z": "cb6f001b.721c3",
"name": "",
"topic": "mqttlcd/message",
"qos": "",
"retain": "",
"broker": "8c74c5f6.9a7a48",
"x": 570,
"y": 720,
"wires": []
},
{
"id": "b4e0107399248fea",
"type": "delay",
"z": "cb6f001b.721c3",
"name": "",
"pauseType": "delay",
"timeout": "5",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"outputs": 1,
"x": 480,
"y": 640,
"wires": [
[
"ac4faf30b8adbe3f"
]
]
},
{
"id": "ac4faf30b8adbe3f",
"type": "function",
"z": "cb6f001b.721c3",
"name": "Empty payload",
"func": "msg.payload = \"\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 640,
"y": 640,
"wires": [
[
"faa5c794652d7a57"
]
]
},
{
"id": "dfbe26c12fc5e742",
"type": "pushover",
"z": "cb6f001b.721c3",
"name": "Alleen Henri",
"device": "rmx1931",
"title": "Node-Red-Pushover",
"priority": "1",
"sound": "pushover",
"url": "",
"url_title": "",
"html": false,
"x": 850,
"y": 500,
"wires": []
},
{
"id": "443f960b5d1cf40e",
"type": "function",
"z": "cb6f001b.721c3",
"name": "Set pushover payload",
"func": "\nmsg.topic = \"Bash Notify\";\nmsg.priority = 1;\nmsg.sound = \"cosmic\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 660,
"y": 500,
"wires": [
[
"dfbe26c12fc5e742"
]
]
},
{
"id": "8c74c5f6.9a7a48",
"type": "mqtt-broker",
"name": "10.1.0.17",
"broker": "10.1.0.17",
"port": "1883",
"clientid": "",
"usetls": false,
"compatmode": true,
"keepalive": "15",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"closeTopic": "",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willPayload": ""
}
]