9 minute read

A personal & technical story about an ambitious project.

🔬 Minuscule Preface

Greetings, dear reader. I’m usually pretty direct, so let’s skip the formalities. You’re here for one of these reasons:

  • You found my post through a social network. Thank you for visiting, enjoy reading!
  • You stumbled upon this page because you wish to build a similar system. In this case, welcome! I hope this blog post can serve as a good example.
  • You are a web crawler. I dare you to click this box if you aren’t a robot: ☐
  • You are stalking my blog. Why not take it up a notch and just message me directly instead? Maybe we’ll have a good chat!

Regardless of why you’re here, what I am about to share with you is a personal story, from my own intimate viewpoint. It’s going to be a ride. Why don’t you hop on board and give it a try?

🔬 If you’re interested in reading the whole paper I wrote on this project, you can do so here.

🖼️ Background

It’s December 2023. After a long semester of studying abroad, I’m wholeheartedly excited to go back home to my family and forget all about school for a while. Amidst my vacation plans, though, a naughty detail was looming: my final year Capstone Project. But of course, how could I forget about it?! Where I study, I cannot finish my degree without a project.

But what is a Capstone Project, you say? To be fair, I don’t think I sufficiently know the academically correct answer either - and I’ve completed it. Unfortunately, there seems to be a lot of variation in the academic world regarding what exactly a final year project should represent, often creating an ambiguity that confuses everyone who participates. To me, a Capstone Project ought to be a culminating achievement; the pinnacle of the formative years of a student, with a spice of ingenuity. And as a Computer Science major, I was hoping I could make a “symphony” of everything I had learned: Frontend, Backend, Infrastructure, Stuffed Green Peppers, whatever.

That’s the dream I had in my head, at least. I know that ideas always seem more lucrative than they actually are, so I tried to keep my expectations low. But what could I make that reflected all the skills I had learned?

💡 The Idea Lights Up Like Those Lightbulbs in Cartoons

In an era where most Capstone Projects were dominated by neural networks, I was highly discouraged from making one without the involvement of AI, by getting highly encouraged to involve a NN. It’s probably really trendy for colleges to claim “we had X projects about Artificial Intelligence”, so I don’t blame them too much. Regardless, I knew how long it takes to make a semi-useful neural network. In addition, I had spent so much time working instead of socializing during my previous college years; if I had to create a neural network too I was pretty certain I’d collapse into a black hole, hence, I opted against it. I was not about to throw away my opportunity at making something awesome to the non-merciful lords of SGD.

So, what’s novel? I vividly remember my instructor telling me that “novel” is a sexy word to have on your paper. Well, hmm, I always liked the Internet of Things. That’s pretty novel, isn’t it? Maybe it’s not as trendy as neural networks are nowadays, but at least I won’t have to spend 80% of my time solving shape errors.

And IoT it was.

🐱 Ok It’s IoT But Like Is That A Buzzword Only Or Is It Actually Useful?

As much time as it would have saved me time and effort, I’d rather not make anything at all than make something that isn’t useful (entertainment is useful, by the way!). It’s against Space Patrol law. Really though, trying to find something that is both innovative, useful, cheap to make and possible within a super-short timespan with one extremely tired employee really narrows down your options.

So in one of my daily yoga sessions, I had the wonderful idea of making a device that can turn anything “smart”. Although smart plugs have existed for a while, I didn’t care. I was going to make my own rendition of them.

Device Diagram

And I present to you… the use case! Originally when I thought about this, I wanted to make something universal: Something that truly every homeowner could find useful in their own way. That was formatted in the shape of a plug that could connect to your home Wi-Fi and allow you to remotely turn the appliance on and off. Furthermore, it would double up as a power measurement unit. That was it for the device. You just plugged it in. Set it up. It worked. I’m not even 25 yet, but I still find some space in my head for the nostalgia of the time when electronics were like that.

The best thing about this device would be the creativity of where you put it. Could be your water heater, your computer, your lamp - the choice is all yours. I do like this idea of “your device your choice”, after all, shouldn’t the consumer have the final say regarding what happens to their products?

⏰ Time Is Ticking, Get To Work

Despite my great desire at the time to outline the research in a thorough paper and kick my feet up for the remaining time in the semester, I wasn’t only doing this to pass the class. The importance was to get a fully-functional prototype out. This lead me to the unthinkable… I actually had to make a Jira board for this one, with a predicted end date for every task.

⚠️ Task & Time management was an essential skill to have during the lifetime of this project. I’d recommend that you hone your skills on the matter, if that isn’t your strong suit. Try cutting vegetables while you spend your time in a boring work meeting.

🌎 Time Isn’t Ticking Just For Me, However

The strong premise and #1 reason as per why I invested my time in even creating this project, is because - as I often see graffiti’d - “There is no Planet B”. Earth is our home for the forseeable future. How about we take the best care of it that we can? With climate change in our doorstep, we do not have too long at all to take action; it is but the most important dilemma of our century.

Of course, there may be those who are too inclined to pursue their own life’s endeavors. However, even small changes that only take a few minutes a day can help a clean, efficient environment around you to thrive.

🔌 The Device Is Born: The Hardware

Starting work on the software side immediately is quite a tempting idea. As someone who writes code on the daily, staying within my comfort zone is probably the easiest thing I could get started with. However, what if I made the whole infrastructure and failed to work out the actual device itself?

Firstly, I started with the design. Using a few simple and cheap components like a Current Transformer, a Relay, and a disassembled 5V USB charger as a power supply, I managed to arrange them in a way that allowed the device to switch another one ON/OFF, and also measure its’ current. All this, while being wired up to an Arduino Nano IoT, since it was the most accessible tiny development board.

Device Schema

And although having been exclusively on the software seat for a while, I grabbed my soldering iron and pressed on. After a few failed attempts, I successfully made my first rough prototype:

Device Prototype Image

⚠️ Note that eventually I made a custom plastic shell for this device. It is inadvisable to leave electronics exposed, especially those that work with mains voltage!

This was of course, an iterative process. I went through many different prototypes and “updates” until I was relatively happy with what I had made.

🧠 Now To Give It A Soul: The Firmware

Having only tested the device with spaghetti code up to this point, everything worked relatively okay. However, the programmer itch inside of me never pulled away, and forced me to refactor the code to something more acceptable for a thesis. The device also needed to handle errors gracefully, and to remain resilient in case of power failure. Since this device had a complex context process, a state machine was the best option for the design of its mode of operation:

Device Logical Schema

🐝 A Bee Can’t Be Lonely: The Architecture

Scalability was a large part of the infrastructure of this project. Handling many IoT devices is no easy feat; some of the largest manufacturers in the world face issues due to the large message volumes and the unpredictability that comes with it. I strongly believe that there’s no optimization too early for IoT products, since those that were designed with scalability in mind from the start usually outperform those that only started thinking about it later on.

This phenomenon occurrs due to the the difficult nature of IoT device management: If you try to change the direction of a small stream, it’s much easier to do so. However, once that stream becomes a full-blown river, it’ll be really hard to redirect it whole.

With these points in mind, the infrastructure was designed as a set of microservices, with the REST API, EMQX Broker having an autoscaler (+ the Neo4j Graph DB with manual scaling by using sharding).

The most important part is probably the MQTT EMQX broker, sitting in-between the backend service and the IoT devices. This broker makes sure that messages are exchanged in an asynchronous way between the backend and the devices.

Infrastructure Schema

⚛️ The REST (haha, get it?) of The Backend Components

In order to separate concerns and maximize the security and performance of the application, I figured that the best choice was to make a few different services instead of a monolithic application:

  1. Backend: The Business Logic Component
    Crunches data, and handles general user actions.
  2. Scheduler: Custom CRON Implementation for MQTT
    Runs timed actions that need to be executed periodically, or at a specific time.
  3. Auth: Authorization & Authentication Service
    Handles user && device auth in an opaque way, to maximize security.

To not bloat this article too much, I decided to keep the description of these components short. Check out more detailed information about each component by following the links above (achieved through clicking on their name).

📱 The App

Saving the best for last, I worked on the application’s design in the end. Since I was tight on time, some un-centered divs weren’t going to affect my final functionality if I didn’t have the chance to finish everything.

The control panel that can be used as an interface between the device and the user. Supports setup flows, scheduling ON/OFF times, real-time device monitoring & statistics.

Paddy Insights Page

Thanks to the Quasar framework, it can run on pretty much any device!

🏁 Final Thoughts

This was probably one of the most challenging projects that I’ve worked on so far. With it I learned how to properly use Terraform resources and built my first full cross-platform WebApp. But most importantly, I believe that this project served as a nice proof of concept for a device that embeds itself nicely in the IoT Green revolution.