You can also create a multi-screen web app that interacts with your Photon device through Particle Cloud. Your multi-screen web app will consist of an HTML file namedindex.html
, a CSS file named style.css
, and a JavaScript file named script.js
.
Your web app will be a single-page application that dynamically shows different "screens." Your web app will only have one HTML page, but different <div>
sections in the HTML will represent different screens. Your web app will only display one specific <div>
screen at a time, while hiding the other <div>
screens.
This section contains starter code that you can use for your HTML, CSS, and JS files. You'll need to add to the starter code (and modify certain parts).
This starter code has been designed to mimic the appearance and interaction of a native mobile app. A persistent navigation menu with icons will be displayed at the bottom of the viewport to allow the user to switch between screens to perform different tasks or view different information.
By default, this app code is designed to show 4 different screens (but you can modify the code to change the number of screens). This app code also has a <div>
section that can be use to display an incoming notification from your smart device.
You can use this starter code for your HTML file named index.html
:
COPY CODE: When using this IoT code guidebook, you can copy a code block simply by clicking the copy icon displayed in the upper right of the code block.
This HTML has been set up to display 4 different screens. However, you can modify the HTML to display more screens (or fewer screens).
This HTML does several things:
It loads two CSS stylesheet files.
It has <div>
sections for a notification message and 4 different screens.
It has a <nav>
section to show the navigation menu for the screens.
It loads three JavaScript files.
In the <head>
section, there is a <link>
tag to load a CSS stylesheet file named style.css
that you'll use to modify the appearance of certain HTML elements in your web app.
There is also a <link>
tag to load a CSS stylesheet from Font Awesome. This will allow you to display different icons using HTML code that you'll copy from Font Awesome.
At the beginning of the <body>
section, there is a <div>
section with an id name of"notification"
that can be used to display a popup notification message. This <div> will normally be hidden, but you can use JavaScript to display the notification.
If your web app won't need to display notifications, you can delete this <div>
section (though you can just leave it and simply not use it – in case, you change your mind later).
For example, when an event notification is received from your smart device, your JavaScript can make the notification <div>
appear (and can customize the message within the notification, if needed). The notification will appear as a popup at the top of the current screen (though you can change the position if desired).
In the middle of the <body>
section, there are several <div>
sections to represent the different screens in your web app. Each <div>
section has the same class name of screen
to classify it as a screen but also has a unique id name to identify it as a specific screen: screen1
, screen2
, screen3
, or screen4
.
The starter code within each <div>
simply displays a heading for the name of the screen (so you can see that clicking the navigation menu actually does switch screens). You'll remove (or revise) this heading, and add your own HTML for each screen to display text, images, buttons, etc.
Towards the end of the <body>
section, there is a <nav>
section to display a navigation menu for switching between the different screens.
The <nav>
contains anchor tags (<a>
) for each screen. Normally, an anchor tag contains a link to a different HTML page, but in this case, the tags simply have an onclick
attribute that will call a custom function in your JavaScript that will display a specific <div>
screen.
Each anchor tag displays an icon from Font Awesome (using special <i>
code), along with a text label. You can change the icon and the text label. In the starter code, each icon has the Font Awesome fa-2x
class added, in order to display the icon at double size.
At the end of the <body>
section, there are <script>
tags to load several JavaScript files into your web app:
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.
You can use this starter code for your CSS file named style.css
:
This CSS styles certain elements, classes, and IDs within your web app. However, you can modify some of this CSS if desired.
You'll typically want to add CSS to style other HTML elements in your web app, in order to produce the desired layout and appearance for your app's user interface.
You can use this starter code for your JS file named script.js
:
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.
This JS creates a new Particle()
object and assigns it to a global variable named particle
. This object has built-in methods (functions) that can be used to interact with your Photon device through Particle Cloud.
This JS also declares global variables to store your Photon device ID and access token. You must modify these lines to list your actual device ID and access token, which you will need to get from your team's Particle Build account.
This JS contains functions to show a specific screen in your web app (and also highlight the current screen in the navigation menu). There is also a function to show the notification (if your web app needs to display popup notifications).
Then you'll need to add the other necessary JS for your web app. Because your HTML file loaded the Particle API JS library and jQuery JS library, you can include Particle statements and jQuery statements within your JS code.
Review the reference section on Particle Cloud to learn how to make your web app JS interact with your Photon device app.
If you want to learn more about web development or need a quick reference, consult these tutorials and references from W3Schools: