IoT Tutorial Part II : Control LED over the Net with Simple API

shaun Big Data Architect and lover of all thing distributed and Scalable.

In our previous tutorial we connected our Particle Argon device to the internet, wired in an LED light and then flashed some code that makes it blink. Now we are going to going to add some functionality to be able to control it over the internet.

Step 1:  Setup your hardware


If you have not setup your device please refer to previous lab

IoT Tutorial Part I : Create a Simple IoT Device that Blinks LED.

Your finished product should look like this.

Step 2: Create your Particle function


Login to Particle IDE  and open the source code in the Particle Web IDE for Web Connected LED.  Read over the code and get a good idea of what the device does.

Change line 9 to:

int led1 = D6; 

Flash the code to your device.

Step 3: Test your code


Go to the Particle console devices tab.   Click on your Argon device you flashed your code to.  There is a function section on the right side of your screen.  You should see a function for LED.  Go ahead and type “on” without the quotes in the blank.

If you look back in your IDE in the code on line 23.  You will see where we defined the LED function.

Particle.function(“led”,ledToggle);

You can click the call button and this will simulate a command being passes to the led function that serves as a toggle.  You should see the light bulb come on!

Now replace the “on” text with “off” click off and your light will turn off.

So what happened here?

The Particle dashboard abstracts away some of the lower level API activity and the test functionality gives us a predefined environment for easily testing the API but under the hood we basically did the following.

  • registered our device and in turn particle.io created an API address for our device
  • flashed code that creates an API endpoint for our function, serves that endpoint and responds to the command by running the ledToggle function.
  • we sent a POST request using our test functionality in the Particle console to the API endpoint with a command(on or off)
  • our particle api server recieves the POST request and parses the payload(“on” or “off” value)
  • runs the ledToggle function with the command we passed as an argument and either turns the light on or off

Great job.  Now we are ready to create our own client to send API requests to our Particle Server.  This form will function as a toggle button for our LED.

Step 4 : Create a Web Form to Toggle your LED


Sign up for https://codepen.io/ . You can quickly prottype some HTML from this site.

In the Particle build console get your device ID by clicking on the cross hairs icon in the menu and selecting your Argon device.    Make note of it.

In the Particle build console get your token by clicking on the settings icon in the menu.  This is your API key that will allow you to authorize your device.  Keep it in a safe place.

 

Copy the code below or visit github repo for this tutorial.  Paste this code to your code pen HTML box.  Replace “your-device-ID-goes-here” with your actual device ID and “your-access-token-goes-here” with your actual access token.

---------------------------</pre>
/* Paste the code between the dashes below into a .txt file and save it as an .html file. Replace your-device-ID-goes-here with your actual device ID and your-access-token-goes-here with your actual access token.

---------------------------
<!-- Replace your-device-ID-goes-here with your actual device ID
and replace your-access-token-goes-here with your actual access token-->
<!DOCTYPE>
<html>
<body>
<center>
<br>
<br>
<br>
<form action="https://api.particle.io/v1/devices/your-device-ID-goes-here/led?access_token=your-access-token-goes-here" method="POST">
Tell your device what to do!<br>
<br>
<input type="radio" name="arg" value="on">Turn the LED on.
<br>
<input type="radio" name="arg" value="off">Turn the LED off.
<br>
<br>
<input type="submit" value="Do it!">
</form>
</center>
</body>
</html>
---------------------------
*/
<pre>

After you have pasted the HTML and entered the token and device ID you will see an HTML form below.  Use the toggle to turn on and off your light.  You will see a response page after you have clicked the button that confirms the status of your device.

What is happening here?

When you click the “Do it!” button your making a post request to the API endpoint https://api.particle.io/v1/devices/your-device-ID-goes-here/led?access_token=your-access-token-goes-here and passing an argument that is telling your application to either turn the LED on or off.  Our API server serves the request by parsing the command and calling the toggleLed function.

 

 

Tags : IoT Lab Tutorial
shaun

Big Data Architect and lover of all thing distributed and Scalable.

Related Posts

IoT Tutorial Part I : Create a Simple IoT Device that Blinks LED

IoT Tutorial Part I : Create a Simple IoT Device that Blinks LED

March 21, 2019

In this tutorial we will create a IoT device using the Particle Argon Kit.  The Argon is WI-FI enabled development board that can act as a standalone endpoint or part of particle mesh network.  We will be using it as an endpoint.  First you will need to setup your particle device. Step 1: Setup Your … Continue reading IoT Tutorial Part I : Create a Simple IoT Device that Blinks LED

Read More
Connecting to Nest Camera API and Getting Live URL

Connecting to Nest Camera API and Getting Live URL

March 8, 2019

I love our smart cameras but I am tired of waiting for rollouts of new features before I can use the camera. I would also like to store images and video automatically to my own cloud storage for later batch analysis with some home grown ML models. In this tutorial I am going to walk … Continue reading Connecting to Nest Camera API and Getting Live URL

Read More