Skip to content

Instantly share code, notes, and snippets.

@matt-desmarais
Last active October 14, 2022 21:24
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 matt-desmarais/20e785dca34181b88d8d4f8b32ad5827 to your computer and use it in GitHub Desktop.
Save matt-desmarais/20e785dca34181b88d8d4f8b32ad5827 to your computer and use it in GitHub Desktop.
Greeter Mac with custom messages
# Write your code here :-)
import ssl
import socketpool
import wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
import neopixel
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixels.fill((0, 255, 0))
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Set your Adafruit IO Username and Key in secrets.py
# (visit io.adafruit.com if you need to create an account,
# or if you need your Adafruit IO key.)
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
### Topic Setup ###
# MQTT Topic
# Use this topic if you'd like to connect to a standard MQTT broker
mqtt_topic = "greetermac/actions"
# Adafruit IO-style Topic
# Use this topic if you'd like to connect to io.adafruit.com
# mqtt_topic = secrets["aio_username"] + '/feeds/temperature'
### Code ###
# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connect(mqtt_client, userdata, flags, rc):
# This function will be called when the mqtt_client is connected
# successfully to the broker.
print("Connected to MQTT Broker!")
print("Flags: {0}\n RC: {1}".format(flags, rc))
def disconnect(mqtt_client, userdata, rc):
# This method is called when the mqtt_client disconnects
# from the broker.
print("Disconnected from MQTT Broker!")
def subscribe(mqtt_client, userdata, topic, granted_qos):
# This method is called when the mqtt_client subscribes to a new feed.
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
def unsubscribe(mqtt_client, userdata, topic, pid):
# This method is called when the mqtt_client unsubscribes from a feed.
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
def publish(mqtt_client, userdata, topic, pid):
# This method is called when the mqtt_client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
def message(client, topic, message):
# Method called when a client's subscribed feed has a new value.
print("New message on topic {0}: {1}".format(topic, message))
keyboard.press(Keycode.COMMAND)
keyboard.press(Keycode.N)
time.sleep(.25)
keyboard.release_all()
keyboard_layout.write(message)
time.sleep(.5)
keyboard.press(Keycode.COMMAND)
keyboard.press(Keycode.T)
time.sleep(.25)
keyboard.release_all()
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=secrets["broker"],
port=secrets["port"],
username=secrets["user"],
password=secrets["pass"],
socket_pool=pool,
ssl_context=ssl.create_default_context(),
)
# Connect callback handlers to mqtt_client
mqtt_client.on_connect = connect
mqtt_client.on_disconnect = disconnect
mqtt_client.on_subscribe = subscribe
mqtt_client.on_unsubscribe = unsubscribe
mqtt_client.on_publish = publish
mqtt_client.on_message = message
print("Attempting to connect to %s" % mqtt_client.broker)
mqtt_client.connect()
print("Publishing to %s" % mqtt_topic)
mqtt_client.publish(mqtt_topic, "Performa Power Board Online!")
print("Subscribing to %s" % mqtt_topic)
mqtt_client.subscribe(mqtt_topic, qos=0
)
#print("Unsubscribing from %s" % mqtt_topic)
#mqtt_client.unsubscribe(mqtt_topic)
#print("Disconnecting from %s" % mqtt_client.broker)
#mqtt_client.disconnect()
while True:
try:
mqtt_client.loop()
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
mqtt_client.reconnect()
continue
time.sleep(1)
alias: Greeter Mac TTS Goodbye
sequence:
- service: mqtt.publish
data:
topic: greetermac/actions
payload_template: >-
Good bye "{{ states('input_text.greeter_tts_message') }}" I hope you
enjoyed the tour
alias: Greeter Mac TTS
sequence:
- service: mqtt.publish
data:
topic: greetermac/actions
payload_template: "{{ states('input_text.greeter_tts_message') }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment