Introduction: Taking Basic Electronics to the Internet (IoT) | Home Lights

This is tutorial four of the series where I take electronic devices which you normal control using an Arduino, to the internet using a Spark Core. After talking about the basics it time to raise the level a little bit and start taking everyday electronics to the internet.

In this tutorial I'm going to show you how to control your everyday home lighting system over the internet using a web browser. This system will also serve as a basic block of designing an advance home automation system. I have tried to keep this instructable as simple as possible, so that no one gets left behind.

You can check out my previous Instrutables, before diving right into this one as those Instructables are quite simple and help in getting started with the Particle Core or Particle Photon.

So lets get started.

Step 1: Tools and Components

Here is a list of all the tools and components to get started, you can get all of these on eBay or at a local hardware store.

  • Spark Core
  • Light Bulb and Holder
  • 3V Relay
  • Header Pins
  • An old 5V Mobile Charger (Has to work)
  • PCB
  • Wire
  • Diode 1N4004 or 1N4007
  • 6 inches X 6 inches PVC Box

This project involves soldering if you know how to solder you are good to go and if you are not there are plenty of tutorials on YouTube, with the help of which you can get started.

Note- You can also get a Photon form the Particle store for $19 all the code and the setup of this series will remain unchanged.

Step 2: Getting Started

If you have followed my previous insructables you already have setup your core and have it connected to the internet.

If you are here first, then you can check the step two of any of the previous instructables in the series for steps on how to get started. The steps involve -

  • Creating an account at Particle.io
  • Getting it connected to the internet.
  • Claiming a Core
  • Trying out Tinker
  • Trying out my previous instructables

If you have gone through all of these steps then you are good to proceed to the next step.

Step 3: Circuit

The circuit for this is really simple, all the components that make up the circuit is a Spark Core and a 3V Relay. The circuit diagram can be found above, I had a PCB etched and the etchable pdf file can be found in the attachments.

Solder all the components and try out the blink example as below you should hear the relay sound a click every 5 seconds, if you hear the sound everything went well and you can proceed to the next step.

<p>int led1 = D0; // Instead of writing D0 over and over again, we'll write led1<br>// You'll need to wire an LED to this one to see it blink.<br>int led2 = D7; // Instead of writing D7 over and over again, we'll write led2<br>// This one is the little blue LED on your board. On the Photon it is next to D7, and on the Core it is next to the USB jack.
// Having declared these variables, let's move on to the setup function.
// The setup function is a standard part of any microcontroller program.
// It runs only once when the device boots up or is reset.
void setup() {
  // We are going to tell our device that D0 and D7 (which we named led1 and led2 respectively) are going to be output
  // (That means that we will be sending voltage to them, rather than monitoring voltage that comes from them)
  // It's important you do this here, inside the setup() function rather than outside it or in the loop function.
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}
// Next we have the loop function, the other essential part of a microcontroller program.
// This routine gets repeated over and over, as quickly as possible and as many times as possible, after the setup function is called.
// Note: Code that blocks for too long (like more than 5 seconds), can make weird things happen (like dropping the network connection).  The built-in delay function shown below safely interleaves required background activity, so arbitrarily long delays can safely be done if you need them.
void loop() {
  // To blink the LED, first we'll turn it on...
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  // We'll leave it on for 1 second...
  delay(1000);
  // Then we'll turn it off...
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  // Wait 5 second...
  delay(5000);
  // And repeat!
}</p>

Step 4: Power Suply

We cannot power the core using the micro USB, as that would be really unconventional, nor can we use a battery as we have to keep recharging or replacing it. So a solution to that is to add a power source right into the box, the spark Core is capable of regulating voltages from 3V to 6V.

To get it powered up, I ripped open an old 5V 1A USB charger and added the circuit right in the box. The USB charger is powered by the same power line as that of the light bulb, in this is case its powered just before the relay comes in. The +5V is provided to the Vin terminal of the spark core and not through the micro USB.

Step 5: Spark Core Program

Feel free to modify and change the program as you like and if you come up with a similar project share it using the "I Made It" button.

So here is the program -

int led1 = D2;

void setup()
{

pinMode(led1, OUTPUT);

Spark.function("led",ledToggle);

digitalWrite(led1, LOW);

}

void loop()
{
// Nothing to do here
}

int ledToggle(String command) {

if (command=="on") { digitalWrite(led1,HIGH); return 1; } else if (command=="off") { digitalWrite(led1,LOW); return 0; } else { return -1; } }

Step 6: Browser Program

After flashing the program to the core its time to setup the browser program, all you need to do is download the zip file in the attachments and extract it. You would get an HTML file, run that file on any web browser and also make sure you have JavaScript enabled (it should be enabled by default).

Enter the access token and core ID in the appropriate fields and click on and the lights should turn on similarly off would turn it off. You would also get prompted with the Jason request which look something like the one in the above pic. A return value of 1 should indicate that the lights are on and a value of 0 indicates lights are off, any other value indicate an error has occurred.

Step 7: Whats Next

Hope you had fun replicating the project, if you had any error feel free to PM me or leave a comment below.

After turning the lights on and off lets add some logic to it and in the next tutorial I'm going to show you how to add a PIR sensor (motion sensor)to make things better. Also, I will explain on how to get started with IFTTT server to make your project smarter.

Soldering Challenge

Runner Up in the
Soldering Challenge