Building a Smart Socket

ATANDA ABDULSEMIU
5 min readDec 27, 2020

It’s been a strange year in a lot of ways especially with a global pandemic forcing us to spend more time indoors. Interestingly, I just moved to the US just before the cases started surging and we all had to start quarantine. One of the things I quickly realize is how lazy I am to turn off my room’s lamp which is located far away from my bed near my workspace after grabbing my last cup of water for the night. Fortunately, I have some understanding of electrical systems so I decided to brush up on some of that knowledge. In this article, it is assumed that you have some programming skills and some understanding of electrical systems. Enough talk, let’s get started.

What do you need?

*there are other options for these components based on what you are familiar with or if you live in a country that uses the metric system

Prototyping

It’s common practice in Engineering to not immediately build out the finished product but rather build a test that you can play with and correct features as needed and for that you’ll need the following

  • Breadboard
  • Jumper cables
  • 2 LEDs
  • 2 560ohm resistors

Once you have all the items listed above, you can start getting some code together. Open your Arduino IDE and paste in the code below

Web controllers relay switches program

After pasting the code above in your Arduino IDE, create an arduino_secrets header file to hold required variables about your home network SSID, password and secret request authentication token.

Prototype with two relays controlling two LEDs

Assembling the prototype above is pretty straightforward. However, if you are not familiar with how to use a breadboard, you might want to check out this tutorial from Sparkfun.

Testing your Prototype

If you were able to successfully validate the code above and uploaded it to the correctly assembled prototype, you are ready to turn on the lights. To do that, you’d need to download Postman or even use your favorite browser.

After successfully uploading the code into you MCU, open the serial monitor on the Arduino IDE to see if your MCU is able to connect to your home internet and use the IP address you see as the host for your Postman requests.

In case you missed it, there are six endpoints that our smart system can handle and they are as follows

  • GET / to see the welcome message
  • GET /light/H to turn on the red LED
  • GET /light/L to turn off the red LED
  • GET /desk/H to turn on the blue LED
  • GET /desk/L to turn off the blue LED
  • GET /status to know the state of our smart system

Note that any endpoint that changes the state of the relay needs the authentication token that is defined in your arduino_secrets header file.

If everything went as planned, you should have a behavior similar to the one below

Smart Light System Prototype test

Hurray! you got your prototype working, now you need to modify your existing circuit to work with your home power supply so you can start doing some control via the web but first let’s go over some safety tips.

Safety Tips

  • Do not connect your device to the mains while you are working on it
  • Do not troubleshoot your system while it’s connected to the mains
  • Do not use a container that can conduct electricity for your circuit
  • If you have children, take extra caution with the casing of your circuit so that it is child safe.
  • Do not create a situation that could result in a short circuit
  • Ensure that the area where you are connecting your circuit to has a circuit break in case you misconnect something
  • Do not operate your circuit in a damp or wet environment
  • When not in use, disconnect your circuit from the mains and store properly

The safety tips above is by no means an exhaustive list on Electrical system safety, please consult electrical safety standards like the one published by OSHA (Occupational Safety and Health Administration) here.

Now that we’ve got safety tips out of the way, let’s take a look at the schematics.

Schematic Diagram

two phase smart power supply

From the diagram above, you can see that the Microcontroller Unit (MCU) has its own DC power supply that can be made available through the power adapter in the list of materials above. The MCU is supposed to send digital signal to the relays to open or close the power supply of its connected output socket.

Assembly

In my assembled device, I made use of a delivery box to house my circuit. However, I recommend you get creative with your assembly.

assembling of smart power supply system

In the assembly above, you can see that I’m making use of a plastic base to make sure that the Microcontroller Unit (MCU) is adequately protected from AC Power source please implement some form of that in your assembly to prevent mistakenly frying your MCU incase there is some leak from your power supply.

Testing your final circuit

Please go through the safety tips section of this article once again and plug in your circuit in the following order

  • Plug in the power to your MCU first
  • Plug in the appliances that you’d like to control
  • Plug in your circuit’s power plug

If you’ve been able to assemble your circuit correctly, you should still be able to use the endpoints we used in testing the prototype for your final result.

You can decide to build a mobile app to control your system like I did or keep doing that with your Postman application.

Mobile interface for Smart Socket System

Gotchas!

  • Sparkfun wifi shield usually comes with the header unsoldered so you’d need to do some soldering there
  • Spark wifi shield’s wifi antenna can easily get interference from the Arduino UNO I2C port. Make sure that the wifi shield it the topmost module in the MCU stack.

Resources

Until the next article, Thank you for reading, I hope this is helpful

--

--

ATANDA ABDULSEMIU

Muslim | Idealistic-Realist | Hedonist | Software Engineer