Getting Started
Adafruit Playground is a wonderful and safe place to share your interests with Adafruit's vibrant community of makers and doers. Have a cool project you are working on? Have a bit of code that you think others will find useful? Want to show off your electronics workbench? You have come to the right place.
The goal of Adafruit Playground is to make it as simple as possible to share your work. On the Adafruit Playground users can create Notes. A note is a single-page space where you can document your topic using Adafruit's easy-to-use editor. Notes are like Guides on the Adafruit Learning System but guides are high-fidelity content curated and maintained by Adafuit. Notes are whatever you want them to be. Have fun and be kind.
Click here to learn more about Adafruit Playground and how to get started.
-
Home Assistant WiFi Button with CircuitPython
Have you ever wanted to connect a simple button or sensor to Home Assistant using Circuit Python? This script demonstrates registering a device with Home Assistant so that it can be automatically connected to an Automation. The script triggers its event on power-on. The example uses the "trigger" type, which is better than "button" for a simple remote control. Other MQTT integrations could put a sensor reading on the dashboard, or turn your CircuitPython board into a WiFi light or fan.
Ingredients
- ESP32-C3 board running CircuitPython 9.1.4
- Home Assistant with the MQTT integration and broker, accessible to the local network
This script also demonstrates setting a BSSID, a 12-digit hexadecimal string, to connect to a specific access point.
I was not able to connect to my local
mosquittoMQTT broker over ssl; something about how the the MQTT client and broker interact prevents CircuitPython from validating the certificate. The comments include an example of loading a custom root certificate for the connection.How It Works
Home Assistant's MQTT integration listens on the
homeassistant/device_automation/#topic for discovery messages. When it sees a properly formatted message, it adds or updates that device underSettings > Devices and Services > MQTT > devices. It also listens ontopic, herecircuitpy/{unique_id}/action, for commands from the device. For a two-way device, you can also set a topic for Home Assistant to send commands back to CircuitPython.Now, we can add our device to an automation in Home Assistant without any additional configuration.
Going Further
Can we send our configuration messages with
mqtt_client.publish(topic, msg, retain=True)so Home Assistant will remember us, even if it restarts before we do?Can we configure our CircuitPython board as a Home Assistant temperature sensor?
Can we put the device in low power mode, wake on a pin change, and make a real battery-operated remote control?
Use nginx to proxy SSL instead of mosqitto's idiosyncratic support.
See also