Sunday, August 3, 2014

Smart Socket : A small step to "Internet of Things"

It was a long weekend and I did not have anything planned. I was on a "couch potato" mode watching 4 movies in a row. It was very unproductive and I was just sitting upright in my bed with a pitcher of water and a sack of chips when the sun sets and it got dark. I needed to turn on the lights in the living room and few lights in the kitchen but i didn't want to stand up. I wished I could just do that with an app on my phone. So after finishing the 4th movie. I jumped into my computer and started searching about such ideas. The first interesting thing that i found was the Wemo, a home automation product by Belkin. They featured a product that is very similar to what I need. They feature of main socket that can be turned ON and OFF from the internet. I then encountered the phrase "Internet of Things" which refers to the technology behind internet connected devices. They are not limited to home but are also getting attention from factories and manufacturers now. I read an article that predicted the market of "Internet of Things" to grow to 17 billion in 4 to 5 years from now.

The Inner Geek Kicks In

Instead of buying that product, why not make my own? I know exactly how they work, I have the skills required to build such electronic hardware and I have more than enough of the software programming skill required for this. Who knows, this could be a potential business in the future. What I needed was the materials, so i searched for tiny micro-controller chips and board with built-it WiFi modules. I have couple of Raspberry Pi development boards but I needed something very small that can fit inside a light switch or inside a mains socket (convenience outlet). This search has lead me to the "Electric Imp". It is a very tiny micro controller with built-in WiFi package inside an SD card form factor. The chips is cloud-enabled. You write the firmware in the cloud and you control and monitor it from the cloud. It is hardwired to communicate to the Electric Imp's cloud platform and services. This make it easier to play around with.

I ordered 2 of the little devil (the Imp card's logo is a little devil) online. They shipped it and deliver to me from New York. Since I have not done electronics for a long while, I don't currently own the required basic tools like a soldering iron with me. So I also ordered a set of tools which included a bread board and generic PCB, soldering iron, etc online from a local provider, I also ordered the materials required for the project in the same online store. The orders arrived after 2 days.

Basic tool set including a multimeter.
Power Supply

Yesterday, I finally had some time to put things together to develop a proof of concept prototype that can fit inside a switch box and or a mains junction box. I started out by figuring out where to get a power supply that will generate 5 Volts that is enough to power the Imp's board and the relay coils at the same time. Then i turned my eyes on an unused USB cellphone charger plug that I got from a neighborhood shop for $4. The plug generates 5V at 1A which is more than enough and it quite small. So I took it apart and remove the main board from the plug housing.

The cheap USB cellphone charger.

The Imps 

The Imp cards and the April basic development boards.
The first thing I did after receiving this little devils was to run a very simple program so I took the breadboard and started assembling a simple circuit with a single RGB LED. I made the LED controllable from the web through REST web service. I was able to change the color from red to green to blue. I could also command led to turn on or off or blink at a specified frequency. The experiment was enough to tell that this little thing is awesome. The thing has also built-in Analog to Digital converters that you can use to read analog signals such as temperature, pressure, etc. It can also provide analog output signals through PWM to control analog things such as servo motors. You can read more about it from here ->

The cloud-based IDE looks like this. The source code is written in Squirrel programming language. In the future Electric IMP is going to migrate to Erlang.

The programming model is purely asynchronous. The communication between the agent and the device is event-driven. It feel like writing nodeJS application.
Switching Circuit

Time to replace the LED with a relay. To be able to control high current load such as an light bulb or AC or a fan, you need to channel the signal from the Imps output pin to control a relay or a MOSFET (a power transistor) which in turn will control the actual high-current load. I decided to use relays over MOSFETs as they are cheaper.

Imp development board soldered on top or a PCB fitted with a relay, terminal blocks and the salvaged power supply (the vertical board).
The image above shows the assembled product. The power supply board is positioned vertically so that they will fit inside the convenience outlet backing box.

I used a relay driver chip to drive the relay. Unfortunately, I bought the chip in a wrong packaging. I got the very small surface-mount type which is very difficult to solder with a regular iron and this board. But I managed to do it by soldering the chip from the back of the board and simply separating the Vcc and Ground pins. Then I used the rest of the pins in parallel as one switching channel since I only needed one channel anyway.

The relay driver chip partially soldered.
After many year without practice, I think my soldering skills was not that bad. ;)


The very first test did not really work quite well. After few debugging, I figured out that I connected the relay's common pin wrongly. It turns out that I misunderstood the relay driver chips datasheet so I went back to it and reconnected the relay in the right way (positive-common, not negative-common). I was to excited to see it worked that I did not even ready the data sheet well.

Finally, using the same code I wrote for the RGB LED experiment, from a web service, I told the IMP to blink the LED and I hear the relay ticking. It means the electro-mechanical l contact is moving back and forth from ON to OFF position. This means that the board is ready to go inside the box.

Fitting Together

Because of earlier careful planning, fitting the board inside the box was easy. It's a tight fit but it works. I used a couple of terminal blocks where I can easily connect and disconnect the load and source sides of the electrical circuitry. This way I can take away the board and use it somewhere else whenever I need it.


In Production

I created a small mobile website using ASP.NET MVC4 and deployed it to my existing Windows Azure web space. After 4 straight hours of working on the electronics and the Squirrel (the imp's programming language) source code and the small mobile website. Time to package the product and use it in a the real world.

With a total cost of S$45 including the Imp card, I now can control any appliances plugged in to this socket/outlet from anywhere around the world. In a mass produced production version, this production cost can be reduced to less than $10 per unit and the size of the board of the final product can be reduced to up to only 10 % of the size of this prototype. If the product sells at 20$ each, that sound like business eh?

Checkout the demo I did at Hackware Singapore: