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.
Attachments
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.

Runner Up in the
Soldering Challenge
11 Comments
8 years ago on Introduction
I have an idea that I've been working on. I hope you guys can tell me if this ible will be applicable. I need to automatically restart my main router and modem once a day so that I could have a better internet experience. It's either I make a sensor on which i ping a certain IP address and if that ping request fail, modem automatically restart or I set a specific time within the day on which the modem automatically restart.
Reply 8 years ago on Introduction
I've thought about doing this (at home and the office) with two outlet timers set to turn off for fifteen minutes then back on at a time they're not being used. I would stagger them so that the modem turns on first and then the router. I am certainly guilty of the tendency to over-geek things, but wouldn't this do the same thing?
Reply 8 years ago on Introduction
Ya its possible to do that using an Particle Core or Photon....
8 years ago on Introduction
Having read
your 3 previous tutorials, I was eagerly waiting for this one, but I’m a bit
disappointed:
do you keep talking about the Core, which is no longer available and replaced
by the Photon although you say this tutorial is also valid for the Photon?
schema has been updated to include a flyback diode but you didn’t update the
list of components and there is no other way to see which exact component you
used.
the Photon has 8 digital pins, I was expecting a tutorial to control 8
different lights using a relay like this: http://www.banggood.com/5V-8-Channel-Relay-Module-Board-For-Arduino-PIC-AVR-DSP-ARM-p-74110.html
straight from an app on a smartphone. A simple interface would be OK E.g. where
each number from 1 to 8 would switch on and off each of the 8 items individually
and 0 would mean ALL OFF and 9 would mean ALL ON
Maybe in a future tutorial?
Reply 8 years ago on Introduction
I did not know that the core is no longer available, anyways this tutorial applies the same for the photon as well.
I have an instructable coming up with multiple relays, this instructable was meant to be simple.
The relays will be controlled using an android app so stay tuned......
8 years ago on Introduction
Whenever working with mains voltage make sure you keep your circuit and the mains WELL isolated. The roughly 1.5mm between mains and the coil traces (red circle) is not enough, it shoud be at least 5mm, but the more the better. In this special case it's not that important tough, nobody will even get close to the circuit because it's wireless, so you don't need to etch a new board. But keep it in mind for your next projeccts.
Otherwise it's a neat application.
Reply 8 years ago on Introduction
nice comment this website need vote or like button :D
Reply 8 years ago on Introduction
Thank you!
8 years ago on Introduction
You need to add a flyback diode on your relay or it will end up frying the micro controller. It would also be a good idea to use a transistor to drive the relay since they can pull a good bit of current.
Reply 8 years ago on Introduction
Thanks for letting me know, I have updated the circuit diagram.
8 years ago on Introduction
Good instructable .
But you need to use transistor and flyback diode as soon as possible ..