Back to projectsVolver a los proyectos

DNS · Open Source

ESP32-DNS-Cloudflare

Updating DNS with an ESP32

Project imageImagen del proyecto

ESP32-DNS-Cloudflare is a pretty self-explanatory title for this project: updating your Cloudflare DNS with an ESP32.

This is actually the second iteration of the project: I wrote the first one in C, and it worked perfectly for a year, but I wanted to make it a little more ergonomic. It also had the big problem that my WiFi credentials and the Cloudflare API token were written directly into the code, so I never turned it into a project or pushed it to a repository, the project was a folder gathering dust in a corner of my PC.

Maybe I should talk about the application itself: why I made it, what it does, and most importantly, why you, dear reader, would choose to use it.

The pain #

I don't want to pay for a separate Netflix account, especially when my parents have shared their account with me for the last 13 years or so, and it works perfectly fine. When I moved out, I wanted to keep using the account, but some executives don't like that idea. Luckily, my parents have an Asus router with Open VPN installed, so the idea is simple: set up the VPN, connect to my parents' network whenever I want to watch something, and be happy.

Not so fast! The problem is that their IP is dynamic, and every so often (like after a big blackout) I have to ask my dad to tell me their new IP, update it in the DNS, and only then can I watch my favorite show.

The solution, of course, is to have a Server at my parents' house (more like a Raspberry Pi), with ddclient installed (and maybe Jellyfin, to be honest) so the Raspi keeps a DNS record up to date. But, dear reader, you probably know that Raspis keep getting more expensive, and I wanted to set up Home Assistant at my place using that very Raspberry Pi, the same one that was at my parents' house updating the DNS. So the solution was to free it up.

Checking the network's IP and updating a DNS record is so simple, that I'd rather spend a couple of Watts on a microcontroller than several more Watts on a whole SBC. And that's how this project was born.

I also wanted a way to connect to my local network over a VPN the same way, but that meant setting up two ESP32s: making custom firmware with the WiFi credentials and the Cloudflare API token in the code, and checking that everything worked... too much work to do twice, so I had to implement a web portal to configure it, a pretty logo, and learn to use MicroPython.

Relevant XKCD
xkcd #1205: Is It Worth the Time?

Features #

This little piece of Software (or should I call it Firmware? At this point I honestly don't know) handles some things really elegantly: once the script is uploaded to the Microcontroller, it automatically starts in AP mode, the user connects over WiFi, and then accesses the web portal.

WiFi credentials

The Microcontroller scans the SSIDs, you type the password and give the device a Hostname, very handy for connecting to the microcontroller later.

Then you set up the DNS, through a similar interface. You'll need an API Token, your account's Zone ID, and the DNS record name. That's it! The app is all set, and you can forget about it. The application will periodically check whether the IP has changed, and if it has, it'll update the DNS record.

I added some pretty LED animations, and that way you can tell at a glance what the device is up to: slow breathing for AP mode, 1 blink when checking the IP, 3 blinks when updating the DNS, etc.

Overall, I think the app ended up being very intuitive, and the LED animations are really useful for checking whether the app is working without hooking up the debugger, even after being on for months. And it's pretty robust: in the two places where it's running, I haven't had any problems! (well, at least none related to the app itself). It uses little power, and I mean really little, and honestly it looks really cool, it's a tiny little gadget that does its job tirelessly. For me those are enough reasons to use it, it solves a real problem, it doesn't need a bulky server, just a really small microcontroller, and really, it just works.

Hunting bugs #

As I said before, I had a C version of this same idea running for something like a year, and I never had problems, but after implementing it in MicroPython, I noticed that sometimes it would stop working. I thought it was a problem with the reimplementation, maybe I had a memory leak, maybe the Runtime was too heavy for this task, I don't know. So I started digging, and I didn't find anything. Using the Agent I added more debug output, a diagnostic test, and nothing, I couldn't find the cause, but the bug was there: the Microcontroller wouldn't connect to the Cloudflare endpoint.

A couple of weeks ago, I had another ESP32 running ESPHome firmware, and it would randomly stop working. Now I had two microcontrollers running different firmwares with a similar problem! I ended up finding what triggered the problem, but not the actual cause. I'll update this page as soon as I finish working on that.