A Foray Into Home Automation

This post was published in 2018 and is kept here for reference. It may contain information that's outdated or inaccurate.

This is an overview of why and how I decided to automate part of my home, and build my first IoT device…

I recently decided to change the opening direction of the door in my home office. Previously the door opened in to the room, limiting the usable space next to the door, now it opens against a wall.

It wasn’t until I’d finished changing the door over that I realised that the location of the light switch was now inconvenient as it now sits behind the door when the door is open.

So what am I to do? Put up with having to be mildly inconvenienced on the occasion that I want to turn the light in the office on or off? No! I decided this would be a great use for some automation. I didn’t fancy messing around with relay switches and hooking directly in to the 240v power lines, so I figured I should go for something non-intrusive.

I’ve seen lots of examples of people using Arduino, a servo and some custom made parts to turn their light switches on and off by physically effecting the switch of the light switch.

I’ve got a 3D printer (I’m currently collating a second blog to record and show off what I’ve been doing with it!), I’ve got some knowledge of Arduino and things, so why not give it a go?

I already had a couple of types of Arduino board at my disposal and some servo’s from another unfinished project. They’re SG90 (plastic gear) and MG90S (metal gear) servo’s with 1.8-2.2kg/cm of torque (at 4.8v)– not very powerful!

I decided I’d go with an ESP8266 because it’s cheap, features WiFi built-in and is compatible with Arduino. I bought a NodeMCU board with integrated ESP8266 – two for less than £10 on Amazon!

For the servo I bought a MG996R servo with 9.4kg/cm of torque (at 4.8v) – I’ll be powering the servo off of a 3.3v pin on the NodeMCU board, so this should be powerful enough to turn the light on and off… Just look at the size of it!

Designing the hardware

Most examples of this that I’ve seen were for American-style switches, they look a bit like this:

I live in the UK, and here our switches typically look something like this:

With variations on that for 2-gang (2 switches) 3-gang, and dimmer style switches. The light switch in my office is just a single switch, so that simplifies things a bit.

I looked online to find some measurements for the switch and where the screw holes and things are positioned, and this is all I could find:

So I took my calipers and measured the actual switch I’ve got.

My plan was to design and print a mounting plate that screws over the existing light switch plate using the existing screws.

This mounting plate would then house the NodeMCU board and position the axle of the servo so that it can turn the light switch on and off. Here’s what I came up with in TinkerCAD:

This first version worked very nicely and fit perfectly. The arms that hold the servo in place were not strong enough and broke pretty much immediately.

My original plan for mounting the NodeMCU board was to screw through some M2 screws in to recessed nuts in the back of the mounting plate, but this didn’t work very well so eventually I decided to reverse that and have the screws permentantly affixed from the back and have the nuts removable.

This worked pretty well, but having to hold the servo in place with Gorilla tape wasn’t ideal for a permenant installation.

Because of the servo mounting failure I went back to TinkerCAD and tried to improve this a bit, utilising the screw mounting holes on the servo. I again hit a problem with the servo mounts breaking as I installed the servo, so decided to attach one screw and add more Gorilla tape until I get around to printing another design.

Hooking up to the cloud – Blynk and you’ll miss it!

The software side of this automation was very straight forward. I’m using Blynk to send commands to the NodeMCU/ESP8266 over the internet.

Blynk is easy to setup using the Arduino IDE. I simply took the Blynk NodeMCU Servo example, added my auth key, wireless SSID and wireless key, flashed the ESP8266 over USB and then connected the servo and power to the NodeMCU board. When the ESP8266 powers up it then automatically connects to the WiFi and awaits commands.

This is the entirety of the code:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

Servo servo;

BLYNK_WRITE(V3) {             // listen on virtual pin 3
  servo.attach(2);            // servo is on gpio2 (d4)
  servo.write(param.asInt()); // move to rotation value passed to v3 pin
  delay(1000);                // ensure that rotation has finished
  servo.detach();             // stop the servo when done
}

void setup() {
  char auth[] = "## auth key ##";
  char ssid[] = "## ssid ##";
  char pass[] = "## psk ##";
  Blynk.begin(auth, ssid, pass);
}

void loop(){
  Blynk.run();
}

Blynk provides an Android app to let you talk to your Blynk-connected devices. On there I simply setup a button that sends a value to the Blynk to configure the rotation angle of the servo.

With my servo I use a rotation of 35 for On and 125 for Off. These values will probably be different for you depending on how your servo is mounted, and at what angle the servo arm is attached.

I’m currently revisiting the design of the mounting plate from scratch using Fusion 360, and thinking of ways to make the servo easily detachable so that the light can be used like it’s the year 2017 or something, but for now it’s working great!

And that’s it, my first bit of home automation; My first IoT device. I’ve scratched the itch, and now I’m thinking about what else I can automate. I’ve got a second NodeMCU waiting to be used after all.

Recent posts:

[Discussion]

NathanSuperStar's avatar picture

NathanSuperStar replied on via reddit.com

It would be more professional if you connected a relay switch to the lights in series with the AC source; therefore, you can hide the electronics inside the ceiling. But your method works too.

15goudreau's avatar picture

15goudreau replied on via reddit.com

So I think this is pretty cool but I’m curious about the execution. Why not just use something like home assistant, a motion sensor and a smart switch? Maybe a little more expensive but you can expand it so much easier than this setup. Glad it worked out though!

digiblur's avatar picture

digiblur replied on via reddit.com

Love it! The esp8266 based products are amazing with so many possibilities and easy to add features and such.

You just have to watch out for the Zwave trolls that will bash you down to a pulp that your wifi network will crash and burn with 10 devices on it no matter if you have dropped thousands on a your wifi access points.

b1g_bake's avatar picture

b1g_bake replied on via reddit.com

I assume you own the place if you are changing the swing of the door. Why not just finish the job and move the switch to the correct side of the door where someone would expect to find it.

The project is cool for sure, but I think it’s just a band-aid.

oerman35's avatar picture

oerman35 commented on via github.com

This is awesome, thanks for the blog post. I’m keen to try it out 😁 I have one question… Are you still able to flick the light switch manually, using your finger, or does the servo prevent you from doing so?

I recently started experimenting with some DIY projects using a Wemos board (similar to the NodeMCU), and my first project was automating my garage door. I hooked it up to my Google Assistant using IFTTT and also built my own web app in Angular and .Net to control it. My next project might just be this one of yours 😁

Max Glenister's avatar picture

Max Glenister commented on via github.com

With the plate I designed originally you could sort of manually manipulate the servo out of the way to flip the switch, but it wasn’t what I would call completely accessible!

I had plans to make a version where the servo could be rotated out of the way, but in the end (and after an office re-shuffle) I ended up uninstalling the automation from the light switch…

Maybe if I get bored in the future I’ll revisit this!

danielthepope's avatar picture

danielthepope mentioned on via danielthepope.wordpress.com

Back in Christmas 2019 I put some individually addressable LEDs around our fireplace to make it look festive. I claimed back then that it was a temporary installation, but 18 months later it’s still there. When I first installed these LEDs, I was controlling them with an Espruino microcontroller. It…

This post is also available in plain text