Piannouncer script (announce notifications sent using LANnouncer)

Quite a simple script, I put this together so I could have several pi’s dotted around the house with speakers attached, which will receive notifications from Home assistant using the LANouncer notify component. Had this running for months on PI 2 and PI 3, seems to run ok. Only limit is the packet size is limited to 4096 characters, which I think is safe for an announcement.
It uses picoTTS and talkey which will need to be installed.

sudo apt-get install libttspico-utils

sudo pip install talkey

import socket
import sys
# import subprocess
#from subprocess import call
import talkey

# piannouncer.py
# threw together by Lee Goodman
# Simple announcer for raspberry pi
# Recieves and announces messages sent from Home assistant
# Using the LANnouncer notify component
# https://home-assistant.io/components/notify.lannouncer/
# 
# To make this work  it needs PicoTTS 
# sudo apt-get install libttspico-utils
#
# and Talkey
# sudo pip install talkey
#
# Feel free to modify it
# I would be interested to see any improvements

tts = talkey.Talkey()
tts.say ('Piannouncer is online')

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('', 1035)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()
    message = ""
    try:
        print >>sys.stderr, 'connection from', client_address
       
        # Receive the data and retransmit it
        data = connection.recv(4096)         
        if data:
            connection.sendall(data)
            message = message + data
        # Send ok message to reciever
        connection.sendall('LANnouncer: OK')
        print >>sys.stderr, 'no more data from', client_address  


    finally: 
        # close the connection
        connection.close()
        # clean up the message, so it makes sense
        msg2=message.replace("+"," ")
        msg2=msg2.replace("%3A",":")
        msg2=msg2.replace("&@DONE@","")
        msg2=msg2.replace("speak=","")
        msg2=msg2.replace("%2C",",")
        msg2=msg2.replace("%C2%B0"," degrees ")
        #print and speak the message
        print msg2
        tts.say (msg2)
4 Likes

Hey,

This looks great, I’m looking for something to replace the current Airplay / Shairplay setup I have which is a little unreliable.

One key thing is that I need to get the Pi’s / Speakers to play some audio files as well, I noticed that the official Lannoucer app supports playing back MP3’s as well as a few built in sounds - looking at the code above, I’m guessing this isn’t supported?