The defining feature of an IoT device is its ability to send and receive information through the internet. This allows an IoT device to interact with other apps or other IoT devices. For this project, you'll be creating a web app that interacts with your Photon device through the internet.
All of your Photon's internet communications are routed through the Particle Cloud service, which offers these ways that a web app can interact with a Photon device through Particle Cloud:
A web app can get the value of a Photon device variable
A web app can call a custom function on a Photon device
A web app can get event notifications from a Photon device
In order to make your Photon device interact with your web app, you'll need to add special code to both your Photon device app and your web app:
The Particle firmware on your Photon device has built-in cloud functions to allow your device to interact with a web app through Particle Cloud. You'll need to add code statements using these cloud functions in your Photon device app.
There is a Particle API JavaScript library that provides methods (functions) to allow your web app to interact with your Photon device app through Particle Cloud. You'll need to load this JS library in your web app's HTML file, and then add code statements using the library's methods in your web app's JavaScript file.
Your Photon device app can send event notifications to your web app through Particle Cloud. The event notification can also include data (in the form of a text string).
For example, your Photon device could monitor a building using motion sensors. If a sensor detects motion, an event notification could be sent to your web app to alert you. The notification could also include data, such as the location of the specific motion sensor that was triggered.
Your Photon device app will use the Particle.publish()
method to send an event notification through Particle Cloud. The event notification can also include additional data (as text).
A "cloud event" will be created that sends the event notification to your web app.
Your web app will use the particle.getEventStream()
method to start listening for event notifications streamed through Particle Cloud.
By default, Particle Cloud will clear each cloud event after 60 seconds. This is to prevent your web app from receiving outdated notifications.
Your Photon device app will use the Particle.publish()
method to send an event notification through Particle Cloud to your web app. The event notification can be sent with or without data.
Particle Cloud will allow your Photon device to send about 1 event notification per second. If your device sends event notifications more frequently than this, Particle Cloud will intentionally slow down your event stream (with a 4-second delay), which can cause issues with your web app performance.
To prevent this, include a delay()
of at least 1 second after a Particle.publish()
statement (because a 1-second delay added by you is better than a 4-second delay added by Particle Cloud).
An event notification can be sent without any additional data. It acts like a simple alert of a specific event or condition occurring.
Add this code (be sure to modify) in your Photon app wherever an event notification should be sent (typically somewhere in the loop()
function or a custom function):
In this case, the Particle.publish()
method has just one parameter inside its parentheses:
The cloud event name, which can be up to 63 characters in length. The name must be listed within double quotation marks. Change "cloudEvent"
to the actual name that you want to use.
Alternatively, an event notification can be sent with additional data in the form of a text string.
Add this code (be sure to modify) in your Photon app wherever an event notification should be sent (typically somewhere in the loop()
function or a custom function):
In this case, the Particle.publish()
method has two parameters inside its parentheses:
The cloud event name, which can be up to 63 characters in length. The name must be listed within double quotation marks. Change "cloudEvent"
to the actual name that you want to use.
The event data, which is a text string that can be up to 255 characters in length. You can directly list the data as text within double quotation marks, or you can list the name of a String variable that stores the data. Change "data"
to the actual text data (or String variable name) that should be sent with the event notification.
Your web app JS will use the particle.getEventStream()
method to start listening for event notifications streamed through Particle Cloud.
Add this code (be sure to modify) in your web app JS:
MODIFY CODE: You will need to make these changes to the example code above:
Change "cloudEvent"
to the name of your cloud event whose value you want
Add code inside the particle.getEventStream()
method to do something when an event notification is received. If the event notification includes text data, the data is stored as: feed.data
The particle.getEventStream()
method requires your Photon device ID, the name of your cloud event, and your Photon access token:
myDevice
is a global variable in your web app JS that should store your Photon device ID
"cloudEvent"
is the name of your cloud event that you want to receive notifications from. The name must be listed inside double quotation marks. Be sure to change "cloudEvent"
to the actual name of your cloud event.
myToken
is a global variable in your web app JS that should store your Photon access token
If an event notification includes any data, this text data gets temporarily stored in a local variable called: feed.data
You will need to add code within the particle.getVariable()
method to do something when an event notification is received – and if applicable, do something with the text stored in feed.data
.
For example, you could add code to:
Display a message in your web app (by using jQuery to modify the HTML)
Change the style of your web app (by using jQuery to modify the CSS)
Save the text data in your web app (by assigning its value to a JS global variable)
Your Photon device app can share a custom function through Particle Cloud, so your web app can call the function to make it run on your Photon device.
For example, your Photon device app could share a custom function that toggles an LED light on or off, so your web app would be able to call this function to remotely turn the light on or off.
Your Photon device app will use the Particle.function()
method to share a custom function through Particle Cloud.
A "cloud function" will be created that acts like a reference to the custom function in your device app.
Your web app will use the particle.callFunction()
method to make the custom function run on your Photon device by calling its cloud function reference.
Your Photon device app will use the Particle.function()
method to share a custom function through Particle Cloud by creating a cloud function. In addition, you will also need to modify the custom function to work with Particle Cloud.
Add this code statement (be sure to modify) within the setup()
function of your Photon app:
The Particle.function()
method requires two parameters inside its parentheses (in this order):
The cloud function name, which can be up to 12 characters in length. If possible (for simplicity), make your cloud function name match your device function name. However, the cloud function is allowed to have a different name. The cloud function name must be listed within double quotation marks without parentheses after its name. Change "cloudFunc"
to the actual name that you want to use for the cloud function.
The Photon device function name, which is the custom function in your Photon device app that will be shared through Particle Cloud. List the function name without parentheses after its name. Change deviceFunc
to the actual name of the custom function in your Photon device app that you want to share.
Particle Cloud will let your Photon device share up to 15 cloud functions at one time. Each of your cloud functions in Particle Cloud has to be given a unique name (up to 12 characters in length).
If your Photon device app needs to share multiple functions, use a separate Particle.function()
statement for each device function being shared.
In order to share a custom function in your Photon device app with Particle Cloud, the custom function must be modified to do the following:
The custom function must accept a String parameter when the function is called.
The custom function must return an integer value when the function is performed.
For example, a typical custom function in your device app would have this generic format:
The code for this custom function would need to be modified to accept a String parameter (i.e., text) and return an integer value (i.e., whole number). Here's a modified version of the function:
Here are the 3 modifications that were made to the custom function:
String data
is listed inside the parentheses after the function name. This represents the parameter that the function will accept: String
is the data type for the parameter (i.e., text) , and data
is the name of a local variable that will receive and store the parameter value (text string). If desired, you could use a different variable name other than data
.
int
is listed in front of the function name (instead of void
), which indicates the function will return an integer value (whole number).
The code statement return 1;
is included inside the function (at the end before the closing curly brace), which will return an integer value of 1. This is the simplest way to have the function return a value.
IMPORTANT: Your custom function must have these modifications – even if your custom function doesn't do anything with the text passed into the data
parameter – and even if your device app doesn't do anything with the integer value returned by the custom function.
Once a custom function in your Photon device app has been modified to be shared through Particle Cloud, your Photon app (and your web app) must include a text parameter when calling the function.
If the custom function doesn't actually do anything with the parameter, then this text string parameter can be any text – even an empty text string of ""
will work.
For example, in your Photon app, a code statement to call the custom function would be:
deviceFunction
represents the name of the custom function. Change this to the name of your custom function.
"text"
represents the text string being passed into the function as a parameter. If this text parameter isn't actually used within the function, then it can be any text string enclosed within double quotation marks – even an empty text string of ""
will work.
As stated previously, the Photon device function being shared through Particle Cloud must accept a String parameter. The function doesn't actually have to do anything with this text data (other than receive it when the function is called).
However, depending on what your custom function does, the text string passed into the data
parameter could be used to decide what action(s) are performed within the custom function.
For example, imagine you created a custom function named turnLight()
to turn an LED light either on or off based on the value of a text string passed into the function as a parameter:
To turn on the LED, your Photon app (or your web app) would call turnLight()
and include"on"
as the parameter. In your Photon app, the code statement would be: turnLight("on");
To turn off the LED, your Photon app (or your web app) would call turnLight()
function and include "off"
as the parameter. In your Photon app, the code statement would be: turnLight("off");
Your web app JS will use the particle.callFunction()
method to make a custom function run on your Photon device by calling its cloud function reference in Particle Cloud.
Add this code (be sure to modify) in your web app JS:
MODIFY CODE: You will need to make these changes to the example code above:
Change webFunction()
to the name you want to use for your custom function
Change "cloudFunc"
to the name of your cloud function you want to call
If necessary, change "text"
to a different text parameter – only necessary if your Photon device function uses the text parameter to decide what actions are performed.
This code adds a custom function named webFunction()
to your web app JS. This custom function contains the particle.callFunction()
method.
Be sure to change webFunction()
to the actual name that you want to use for this JS function. If helpful, you could use the same name as the Photon device function being called. For example, if the JS function is supposed to call a function named toggleLight()
in your Photon device app, you could also name the JS function as toggleLight()
.
The particle.callFunction()
method requires your Photon device ID, the name of your cloud function, an argument (a String parameter for the Photon function), and your Photon access token:
myDevice
is a global variable in your web app JS that should store your Photon device ID
"cloudFunc"
is the name of your cloud function, which must be listed inside double quotation marks. Be sure to change "cloudFunc"
to the actual name of your cloud function.
"text"
is the String parameter that will be passed into the Photon device function. If this text parameter isn't actually used within the Photon function, then it can be any text string enclosed within double quotation marks – even an empty text string of ""
will work.
myToken
is a global variable in your web app JS that should store your Photon access token
Your web app will need a way to call the JS function which contains the particle.callFunction()
method that makes a function run on your Photon device.
One common way to do this is to add a button in your web app HTML that can be clicked by the user. The button should have an onclick event attribute that will call the JS function (which will in turn call your Photon function).
Add this to your web app HTML file within the <body>
section where you want the button to appear:
Change webFunction()
to the name of the JS function to be called when the button is clicked. This should be a JS function that contains a particle.callFunction()
method.
Change Button Label
to whatever text you want displayed in the button as its label. Be sure to use a label that will make sense to the user.
NOTE: You could use a different HTML element other than a button. Just be sure it will be clear to the user that the element is "clickable" (and it's clear what will happen when it is clicked).
In order for your web app to interact with your Photon device through Particle Cloud, you'll need to complete the following preparation steps:
Your web app HTML file needs to load several JS files (such as: Particle API JS library, etc.).
Your web app JS file needs to create a new Particle()
object.
Your web app JS file needs to declare variables to store your Photon device ID and access token.
Once these steps have been completed, you'll be ready to add code in your web app JS to read device variables, call device functions, and get device event notifications.
Your web app HTML file (index.html
) should include <script>
tags to load these JavaScript files:
Particle API JS library: particle.min.js
jQuery JS library: jquery.min.js
Your web app JS file: script.js
The Particle API JS library contains methods to allow your web app to interact with your Photon device through Particle Cloud. You'll use Particle methods in your web app JS file.
The jQuery JS library contains methods that make it easy to modify the content and style of your web app by dynamically changing its HTML and CSS. You'll use jQuery methods in your web app JS file.
JQUERY = OPTIONAL: Loading jQuery is optional. However, it will be much easier to code your web app JS if you can incorporate jQuery statements.
IMPORTANT: The <script>
tag in your HTML that loads the Particle API JS file (particle.min.js
) must be listed before the <script>
tag that loads your web app JS file (script.js
). Otherwise, if their order is reversed, your web app won't be able to interact with your Photon.
If possible, use a content delivery network (such as cdnjs or jsDelivr) to load the Particle API JS library and jQuery JS library.
Your web app JS file will be loaded as a local file from your web app folder (where your HTML and CSS files are located).
Add this to your web app HTML file within the <body>
section (just before the closing </body>
tag):
Alternatively, you could download local copies of the Particle API JS library and jQuery JS library. You will want the minified version of each file (min.js
), which has a smaller file size. If necessary, consult with your teacher to be sure you obtain the correct files.
Place the copies of the files into your web app folder (where your HTML and CSS files are located).
Add this to your web app HTML file within the <body>
section (just before the closing </body>
tag):
Be sure the file names listed in the <script>
tags match the file names in your web app folder.
Be sure your web app folder contains a JavaScript file named: script.js
This web app JS file will contain your JavaScript code to interact with your Photon device through Particle Cloud. Your JS code will also dynamically modify your HTML and CSS to display updated information from your Photon device.
The Particle API JS library defines a class called Particle()
that can be used to create an object variable with built-in methods (functions) to interact with a Photon device through Particle Cloud.
Your web app JS code must create a new Particle()
object, and assign it to a global variable, which is typically just named particle
.
Add this code statement at the beginning of your web app JS code:
In order for a web app to interact with a Photon device through Particle Cloud, the web app must provide a correct device ID and access token with each request. This ensures your web app communicates with the correct device – and prevents unauthorized apps from communicating with your device.
Your web app JS code will declare global variables to store your Photon device ID and access token. You'll get their values from your Particle Build account.
Add this code towards the beginning of your web app JS code, and then modify it:
IMPORTANT: You must modify this JS code to insert your actual Photon device ID and access token. Otherwise, your web app will not work properly.
Your Photon's unique device ID is listed in the Devices menu of your Particle Build account:
Click the Devices icon in the left navigation bar.
In the Devices menu panel, click the drop-down arrow to the right of your Photon device name.
Select and copy the device ID.
Paste the device ID into your JS code as the value for myDevice
(the device ID must be listed within quotation marks).
Your Photon's unique access token is listed in the Settings menu of your Particle Build account:
Click the Settings icon in the left navigation bar.
In the Settings menu panel, select and copy the access token.
Paste the access token into your JS code as the value for myToken
(the access token must be listed within quotation marks).
RESET TOKEN: If desired, you can reset your access token in the Settings menu of Particle Build. This generates a new random access token. However, your existing web apps will need to be updated with the new access token, in order to connect to Particle Cloud.
Your Photon device app can share a variable through Particle Cloud, so your web app can get the value of the variable.
For example, your Photon device app could measure the temperature of a room using a sensor, store the measurement in a variable, and share this variable's value through Particle Cloud, so your web app can get the variable's value and display it.
Your Photon device app will use the Particle.variable()
method to share the value of a device variable through Particle Cloud.
A "cloud variable" will be created to store the value of your device variable. Whenever the value of your device variable changes, the value stored in the cloud variable will be automatically synced to match.
Your web app will use the particle.getVariable()
method to get the value of your cloud variable.
Your Photon device app will use the Particle.variable()
method to share the value of a device variable through Particle Cloud by creating a cloud variable.
Add this code statement (be sure to modify) within the setup()
function of your Photon app:
The Particle.variable()
method requires two parameters inside its parentheses (in this order):
The cloud variable name, which can be up to 12 characters in length. If possible (for simplicity), make your cloud variable name match your device variable name. However, the cloud variable is allowed to have a different name. The cloud variable name must be listed within double quotation marks. Change "cloudVar"
to the actual name that you want to use for the cloud variable.
The Photon device variable name, which is the variable in your Photon device app whose value will be shared in Particle Cloud. Change deviceVar
to the actual name of the variable in your Photon app that you want to share.
Particle Cloud will let your Photon device share up to 20 cloud variables at one time. Each of your cloud variables in Particle Cloud has to be given a unique name (up to 12 characters in length).
If your Photon device app needs to share multiple variables, use a separate Particle.variable()
statement for each device variable being shared.
NOTE: The only data types that are allowed to be shared as cloud variables are:
int
(whole numbers)
double
(decimal numbers)
String
(text, up to 622 characters)
Be sure each Photon device variable being shared as a cloud variable uses one of these data types.
Your web app JS will use the particle.getVariable()
method to get the value of a Photon device variable stored as a cloud variable in Particle Cloud.
Add this code (be sure to modify) in your web app JS:
MODIFY CODE: You will need to make these changes to the example code above:
Change webFunction()
to the name you want to use for your custom function
Change "cloudVar"
to the name of your cloud variable whose value you want
Add code inside the particle.getVariable()
method to do something with the variable value returned as: data.body.result
This code adds a custom function named webFunction()
to your web app JS. This custom function contains a call to the particle.getVariable()
method, which will also contain code you'll add to perform action(s) based on the variable value returned by Particle Cloud.
Be sure to change webFunction()
to the actual name that you want to use for this JS function. For example, if the function is supposed to get the value of a temperature variable from your Photon device, you might name the function something like: getTemperature()
The particle.getVariable()
method requires your Photon device ID, the name of your cloud variable, and your Photon access token:
myDevice
is a global variable in your web app JS that should store your Photon device ID
"cloudVar"
is the name of your cloud variable, which must be listed inside double quotation marks. Be sure to change "cloudVar"
to the actual name of your cloud variable.
myToken
is a global variable in your web app JS that should store your Photon access token
When Particle Cloud returns the value of your cloud variable, this value gets temporarily stored in a local variable called: data.body.result
You will need to add code within the particle.getVariable()
method to do something with the value stored in data.body.result
.
For example, you could add code to:
Display the value in your web app (by using jQuery to modify the HTML)
Change the style of your web app based on the value (by using jQuery to modify the CSS)
Save the value in your web app (by assigning the value to a JS global variable)
If your web app needs to continuously monitor the value of a cloud variable, you can use the window.setInterval()
method to automatically call (perform) a function at a set time interval (such as every 0.5 seconds, etc.).
Add this code statement (be sure to modify) near the top of your web app JS:
The window.setInterval()
method requires two parameters inside its parentheses (in this order):
The name of the function to be called, which will be the name of the custom function in your JS that contains the particle.getVariable()
method for the variable you need to continuously monitor. The custom function's name should be listed without a set of parentheses. Change webFunction
to the name of your custom function.
The time interval between calls, which will be the amount of time between each call to the function. The time interval is specified in milliseconds (1000 ms = 1 second). In this case, the interval was set to 2000
ms (2 seconds). Change this value to an appropriate time interval for your web app.
If you want to get the values of multiple cloud variables at the same time, you can include separate calls to the particle.getVariable()
method within the same custom function:
If your web app needs to get different cloud variables at different times, then create separate custom functions to get each cloud variable individually: