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.
If you have not setup your device please refer to previous lab
IoT Tutorial Part I : Create a Simple IoT Device that Blinks LED.
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.
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.
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.
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.
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.
Big Data Enthusiast and lover of all things distributed and scalable.
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 LEDRead More
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 URLRead More