Advertisment

Build Your Own Internet of Things Device

author-image
Anuj Sharma
New Update

If you loved the Raspberry Pi and Kickstarter (https://www.kickstarter.com/), then you probably know what the Spark Core can do. If you don't, then here's a quick introduction. The Spark Core is a tiny little chip that's making waves in the world of Internet of Things. So much so that I feel it will become the foundation stone for the IoT in times to come.

In a nutshell, the Spark Core is a tiny Wi-Fi development board to easily create internet-connected hardware. You can power it over USB and start controlling various devices like LEDs, switches, motors, etc and for collecting data over the internet.

Advertisment

Just like the Raspberry Pi, this device also comes with a set of GPIO pins (16 to be precise), but unlike the Raspberry Pi, the Spark Core has 8 Digital and 8 Analog GPIO pins. GPIO, or general purpose input output pins are a set of pins that can be controlled with a little bit of programming using various languages or even simple shell scripts. The pins can either be used for reading a signal input or to send a signal output.

The difference between the digital and analog pins is that, in digital you can only read or write 0 or 1 state. Which is basically 0v or 3.3v of DC power. So either you can trigger a relay or a LED on or off. Similarly, a sensor can only be read in the form of on or off. So either a sensor is reading a value or it's not reading. There is no possibility of reading intermediate values unless you hack it and use some other means, like time differential to calculate the differential value. For example by using a basic RC circuit.

With analog, you can actually generate or read multiple levels of values between 0V to 3.3V. Now assume using a thermal or light sensor to capture pretty accurate results.

What's The Spark Core Then?

Spark Core is an open source hardware controller with 8 digital and 8 analog GPIO pins. When you buy a Spark core (it comes for about $45), you get a tiny programmable device which can use your Wi-Fi to connect to the Spark Cloud from where you can securely control your device using API calls and even write and push Wiring codes (the programming language used to control it), directly from your browser to your device. Access to the Spark Cloud is free for life once you buy a device.

Advertisment

If you don't want to connect it to the Internet, then you can also do the things locally for a more secure and controlled deployment. But the Cloud backend makes it very easy for enthusiasts to control the device from anywhere without getting into static IP or NATing hassles.

To configure the device, you can use a smartphone app, called Spark of course, which is freely downloadable for both Android and iOS devices. It lets you find and connect the device to the desired Wi-Fi. It also comes with a simple interface called Tinker. This lets you control all GPIO pins of the Spark Core, without writing even a single line of code. So if all you need is to trigger a few relays or fire up a few LEDs through your phone, then whatever you need is all ready there and ready to be used.

But if you really want to play with it, then here's a quick demo to create a simple Internet switch that can be turned on or off by simply making an API call.

I am leaving the basic setup of the Spark Core on https://www.spark.io/start. If you own or are planning to get a Spark core, then just visit this site. It will guide you to configure your core for the first time and create an account on Spark's cloud.

Once you are done with the first time configuration, then you should have a working Spark Core connected to the internet over your Wi-Fi network. You'll get an operational http://Spark.io account with the following things:

1. Your spark core's registered name on the spark's cloud. For me it is "ROYSCORE". You can find the name of your registered cores by going to https://www.spark.io/build and clicking on the "Cores" icon in the left hand side of the page. See the screenshot below for reference

Advertisment

2. Your API Access Token. You can find it by clicking on the settings button just below the Cores button. See the screenshot for reference.

3. A simple assembly as shown in the image below. What we have done is put the Spark Core on a breadboard and connected a simple LED that takes 3.3V DC on Pins D1 and D2. The positive pin of the LED is connected to D1, which means we have to trigger pin D1 to turn the LED on or off. You can put in a simple relay which can work with 3.3V and can trigger any AC load to use it with any home appliance, and control it over the internet.

And that's all. Now on the same page (https://www.spark.io/build), click on the code button and the click on "Create New App" button. See the screenshot to know where to find these buttons.

Now, give a nice name to the app. We called it INTERNETSWITCH, and paste the below Wiring code to the right hand side of the window:

/* initializing a Spark function to parse API call */

int ledControl(String command);

// Lets call Pin D1 IntSwitch

int IntSwitch = D1;

// The Setup routine.

void setup()

{

//Register Spark function

Spark.function("switchstate", ledControl);

// Initialize D1 pin as a digital output pin

pinMode(IntSwitch, OUTPUT)

}

void loop()

{

}

int ledControl(String command)

{

if (command == "1") {

digitalWrite(IntSwitch, HIGH); // Turn the Switch On

return 1;

} else {

digitalWrite(IntSwitch, LOW); // Turn the Switch Off

return 0;

}

}

Once the code is written, click the top button on the left hand pane to flash your Core with this app. Your Core's LED should start blinking in violet. This shows that the firmware is getting flashed with the new code. Once the LED is back in its blue glow, your core is ready to take commands over the API. Now let's build the API call.

Below is the simple API call which you can make using the curl command:

To Turn Off the Switch

#curl -X POST -H Cache-Control:no-cache -H Content-Type:application/x-www-form-urlencoded -d ‘access_token=&commad=0' https://api.spark.io/v1/devices//switchstate

To Turn On the Switch

#curl -X POST -H Cache-Control:no-cache -H Content-Type:application/x-www-form-urlencoded -d ‘access_token=&commad=1' https://api.spark.io/v1/devices//switchstate

Just that the change in both the API calls is the value of the command parameter which is 0 when we wanted to turn the switch off and 1 when we wanted it to be on.

That's all for now. But, I have ordered a simple temperature sensor for myself and I am going to hook it up with the "Spark Core" and do the following with it:

Advertisment

1. Setup a temperature controlled switch for my home air conditioner, so that whenever the temperature of my room goes beyond 32-degrees, the AC is automatically turned on. Whenever the temperature drops below 21-degree, it's turned off. Plus, there will be an over the Internet manual override button.

2. Create and maintain a temperature log of my home which is updated every 10 minutes.

Now, if you are interested to learn how, write to us at pcquest@cybermedia.co.in.

Advertisment