Skip to content

Instantly share code, notes, and snippets.

@thanksmister
Last active August 21, 2022 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thanksmister/3f94d647926fdc84202913b255a52a0a to your computer and use it in GitHub Desktop.
Save thanksmister/3f94d647926fdc84202913b255a52a0a to your computer and use it in GitHub Desktop.
Raspberry Pi, eSpeak, and MQTT
# Setup Raspberry Pi with WiFi
# Install Paho and eSpeak libraries for Raspberry Pi
# Run using "python3 MQTTSpeak.py"
# Import MQTT client.
import os
import time
import paho.mqtt.client as paho
# MQTT credentials
mqtt_username = 'username' # MQTT client username (if needed)
mqtt_password = '1111' # MQTT client password (if needed)
client_id = '1142' # unique client_id
mqtt_broker = "192.168.1.1" # broker address, usually your HASS IP address
mqtt_port = 1883 # MQTT port
# Subscribe topics
door_topic = 'home/notification/door'
motion_topic = 'home/notification/motion'
# connection event
def on_connect(client, data, flags, rc):
print('Connected: ' + str(rc))
client.subscribe('home/notification/#')
def on_message(client, data, msg):
p = msg.payload.decode()
print('Message: ' + p)
def on_message_door(client, data, msg):
p = msg.payload.decode()
print('Door: ' + p)
os.system('espeak -ven-us+f4 -a50 -s210 "' + p + '"')
def on_message_motion(client, data, msg):
p = msg.payload.decode()
print('Motion: ' + p)
os.system('espeak -ven-us+f4 -a50 -s210 "' + p + '"')
# create the MQTT client
client = paho.Client(client_id=client_id, protocol=paho.MQTTv31)
# client connection
client.username_pw_set(mqtt_username, mqtt_password) # MQTT server credentials
client.connect(mqtt_broker, mqtt_port, 60) # MQTT server address
client.on_connect = on_connect
# subscribe to multiple topics with callback
client.message_callback_add(motion_topic, on_message_motion)
client.message_callback_add(door_topic, on_message_door)
client.on_message = on_message
client.loop_forever()
@thanksmister
Copy link
Author

This was for a Pimoroni Speaker pHAT Raspberry Pi Zero

@thanksmister
Copy link
Author

For espeak parameters use this:

http://espeak.sourceforge.net/commands.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment