Last active
August 21, 2022 21:11
-
-
Save thanksmister/3f94d647926fdc84202913b255a52a0a to your computer and use it in GitHub Desktop.
Raspberry Pi, eSpeak, and MQTT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
For espeak parameters use this:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was for a Pimoroni Speaker pHAT Raspberry Pi Zero