Post

Learning C and Assembly – The UniFi Doorbell MQTT Project

Building a C service that connects Home Assistant and UniFi Protect using MQTT and SFTP to control doorbell sounds and animations.

Learning C and Assembly – The UniFi Doorbell MQTT Project

After a busy summer with family and kids, I finally sat down to start my first real C project. I’ve been writing a lot about the UniFi G4 Doorbell Pro lately, and somewhere in the middle of documenting its webhooks and Home Assistant automations, an idea hit me.

Most people use the UniFi Protect app to change their doorbell sounds and animations. Others rely on SFTP and shell scripts to push new files. I figured why not build a small C program to manage the uploads and learn a ton in the process.

This project ties together a few of my favorite things:

  • C programming
  • Home Assistant automations
  • MQTT communication
  • UniFi Protect
  • Docker

The Idea

The goal is to create a small C service that listens for MQTT commands from Home Assistant. When it receives a message, it uploads the corresponding sound and animation files to my UniFi G4 Doorbell using SFTP. It will also send back MQTT messages to Home Assistant to report progress or errors.

Eventually this will run as a container and accept commands like:

1
{ "type": "holiday", "name": "Christmas" }

or

1
{ "type": "custom", "folder": "birthday_party" }

That will let me create automations like changing doorbell sounds for holidays, or having a special animation when I know a package is arriving soon.

Early Progress

For this first part I focused on getting the basics working:

  • Set up VS Code for building and debugging C code
  • Created a Makefile (with a bit of ChatGPT help)
  • Integrated the Eclipse Paho MQTT C library
  • Added a simple .env file to load MQTT credentials
  • Built a basic loop that connects to the broker, receives a message, and sends an acknowledgment back

Once the connection worked, I sent a test message from the Home Assistant Developer tools → Actions using the mqtt.publish action.

1
2
3
4
action: mqtt.publish
data:
  topic: doorbell/set
  payload: '{"test":123}'

My program then output the received message to the terminal

VS Code Terminal Showing Program Output VS Code Terminal Showing Program Output

then echo the message back to Home Assistant.

Home Assistant MQTT Topic Listen Home Assistant MQTT Topic Listen

The Paho MQTT documentation was scattered, so I had ChatGPT collect examples from multiple sources and help build a working structure.

What’s Next

Next, I’ll start parsing JSON messages with cJSON so I can handle commands like setting a specific holiday or custom folder. After that, I’ll move on to the SFTP upload logic using libssh2.

The project is already live on GitHub under an MIT license, and I’ll keep pushing updates there as I go. You can follow the progress on GitHub – doorbell-mqtt-unifi

The end goal is simple:
Home Assistant publishes an MQTT message →
my program uploads the matching files to the doorbell →
then reports status back via MQTT.

Planned Features

Here’s what the full version will support:

  • Upload image and sound files stored in /assets/
  • Simple configuration mapping holidays to folders
  • MQTT control for setting the current animation/sound
  • Status messages back to Home Assistant
  • Logging and graceful shutdown for Docker
  • CI/CD pipeline to build and publish to GitHub Container Registry

Resources for This Project

ChrisHansenTech is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. As an Amazon Associate I earn from qualifying purchases.

These are the main tools, hardware, and references I’m using in this project:

Software

Hardware - Target devices

References

Why This Project Matters

This isn’t just about doorbells. It’s about using C to build something that interacts with real devices — something I can see and hear working. It’s also a way to stretch my C skills in networking, JSON parsing, and multi-file project design.

I’ll be posting progress updates as I go and tagging them under the Learning C and Assembly series. This one has me excited because it’s the first time I’m building something practical and open-source with what I’ve learned so far.

It’s still early days, but it already feels rewarding to see packets flying and Home Assistant reacting to C code I wrote.

Want to share your thoughts or ask a question?

Join the conversation

This blog runs on coffee, YAML, and the occasional dad joke.
If you’ve found a post helpful, you can support my work or ☕ buy me a coffee.

Curious about the gear I use? Check out my smart home and homelab setup.

This post is licensed under CC BY-NC-SA 4.0 by the author.