Web App - Multiple Screens
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.

HTML

You can use this starter code for your HTML file named index.html:
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<meta charset="utf-8">
5
<meta name="viewport" content="width=device-width, initial-scale=1">
6
<title>Smart Device Web App</title>
7
<!-- Load Font Awesome for Icons -->
8
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
9
<!-- Load CSS stylesheet -->
10
<link rel="stylesheet" href="style.css">
11
</head>
12
<body>
13
<div id="notification">
14
<span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span>
15
<!-- HTML for Notification -->
16
<p>Notification Message</p>
17
18
</div>
19
<div class="screen" id="screen1">
20
<!-- HTML for Screen 1 -->
21
<h1>Screen 1</h1>
22
23
</div>
24
<div class="screen" id="screen2">
25
<!-- HTML for Screen 2 -->
26
<h1>Screen 2</h1>
27
28
</div>
29
<div class="screen" id="screen3">
30
<!-- HTML for Screen 3 -->
31
<h1>Screen 3</h1>
32
33
</div>
34
<div class="screen" id="screen4">
35
<!-- HTML for Screen 4 -->
36
<h1>Screen 4</h1>
37
38
</div>
39
<div id="navigation">
40
<nav>
41
<!-- Menu icons from Font Awesome -->
42
<!-- Search for icons at: https://fontawesome.com/icons?d=gallery&m=free -->
43
<a class="menu active" onclick="showScreen1()"><i class="fas fa-home fa-2x"></i><br>Screen 1</a>
44
<a class="menu" onclick="showScreen2()"><i class="fas fa-user fa-2x"></i><br>Screen 2</a>
45
<a class="menu" onclick="showScreen3()"><i class="fas fa-cog fa-2x"></i><br>Screen 3</a>
46
<a class="menu" onclick="showScreen4()"><i class="fas fa-bars fa-2x"></i><br>Screen 4</a>
47
</nav>
48
</div>
49
<!-- Load JavaScript files -->
50
<script src="https://cdnjs.cloudflare.com/ajax/libs/particle-api-js/7.3.0/particle.min.js"></script>
51
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
52
<script src="script.js"></script>
53
</body>
54
</html>
Copied!
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:
  1. 1.
    It loads two CSS stylesheet files.
  2. 2.
    It has <div> sections for a notification message and 4 different screens.
  3. 3.
    It has a <nav> section to show the navigation menu for the screens.
  4. 4.
    It loads three JavaScript files.

LOAD CSS STYLESHEETs

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.

<DIV> FOR NOTIFICATION

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).

<DIV> FOR EACH SCREEN

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.

<NAV> FOR SCREEN MENU

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.

LOAD JAVASCRIPT FILES

At the end of the <body> section, there are <script> tags to load several JavaScript files into your web app:
  1. 1.
    Particle API JS library: particle.min.js
  2. 2.
    jQuery JS library: jquery.min.js
  3. 3.
    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.

CSS

You can use this starter code for your CSS file named style.css:
1
/* Add or modify CSS for your web app */
2
3
* {
4
box-sizing: border-box; /* use traditional box model sizing */
5
}
6
7
body {
8
font-family: Helvetica, Arial, sans-serif;
9
font-size: 1em;
10
margin: 0;
11
padding: 0;
12
text-align: center;
13
}
14
15
/* #notification */
16
#notification {
17
display: none; /* hide until needed */
18
position: fixed; /* fix to viewport */
19
top: 0; /* place at top of viewport */
20
left: 50%;
21
transform: translate(-50%, 10px);
22
max-width: 300px;
23
width: calc(100vw - 20px);
24
margin: 0 auto;
25
padding: 10px 20px;
26
background-color: #fbfbfb;
27
border-radius: 10px;
28
border: 1px solid #bbbbbb;
29
box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.3);
30
text-align: left;
31
}
32
33
/* close button for notification */
34
.closebtn {
35
margin: -15px -15px 10px 10px;
36
padding: 10px;
37
color: #888888;
38
font-weight: bold;
39
float: right;
40
font-size: 1.5em;
41
cursor: pointer;
42
transition: 0.3s;
43
}
44
45
/* hover effect for close button */
46
.closebtn:hover {
47
color: #000000;
48
}
49
50
/* .screen class applies to all screens */
51
.screen {
52
max-width: 480px; /* mimic largest phone width */
53
width: 100vw; /* width of viewport */
54
height: calc(100vh - 4.5em); /* height of viewport minus height of #navigation menu */
55
margin: 0 auto;
56
padding: 20px 10px;
57
overflow: auto;
58
border: 1px solid #888888;
59
/* can add other CSS for all screens, such as background-color, etc. */
60
61
}
62
63
/* show #screen1 when app first starts */
64
#screen1 {
65
display: block;
66
}
67
68
/* hide other screens when app first starts */
69
#screen2, #screen3, #screen4 {
70
display: none;
71
}
72
73
/* can add custom CSS for each specific screen */
74
#screen1 {
75
76
}
77
78
#screen2 {
79
80
}
81
82
#screen3 {
83
84
}
85
86
#screen4 {
87
88
}
89
90
/* #navigation menu fixed to bottom of viewport */
91
#navigation {
92
position: fixed; /* fix to viewport */
93
bottom: 0; /* place at bottom of viewport */
94
left: 50%;
95
transform: translate(-50%, 0);
96
max-width: 480px; /* mimic largest phone width */
97
width: 100vw; /* width of viewport */
98
height: 4.5em;
99
text-align: center;
100
background-color: #ffffff;
101
border: 1px solid #888888;
102
}
103
104
/* nav menu options */
105
nav > a {
106
display: inline-block;
107
/* for max-width and width, divide by number of screens */
108
max-width: calc((480px / 4) - 4px);
109
width: calc((100vw / 4) - 4px);
110
margin: 0;
111
padding: 0.5em 0;
112
text-align: center;
113
background-color: #ffffff;
114
color: #888888;
115
transition: 0.3s;
116
}
117
118
/* hover effect for nav menu options */
119
nav > a:hover {
120
background-color: #eeeeee;
121
}
122
123
/* .active class indicates current screen within nav menu */
124
nav > a.active {
125
background-color: #ffffff;
126
color: #0099ff;
127
}
128
129
/* nav menu icon label */
130
nav > a > p {
131
font-size: 0.75em;
132
margin: 0.5em 0 0;
133
}
134
135
/* can add CSS for other elements, classes, or IDs */
136
Copied!
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.

JS

You can use this starter code for your JS file named script.js:
1
var particle = new Particle();
2
var myDevice = "0000"; // Photon device ID
3
var myToken = "0000"; // Photon access token
4
5
// functions that hide all screens & then show a specific screen
6
// these functions also update navigation menu to highlight active screen
7
function showScreen1() {
8
$(".screen").hide();
9
$("#screen1").show();
10
$(".menu").removeClass("active");
11
$(".menu").eq(0).addClass("active"); // eq(0) = 1st menu item
12
}
13
14
function showScreen2() {
15
$(".screen").hide();
16
$("#screen2").show();
17
$(".menu").removeClass("active");
18
$(".menu").eq(1).addClass("active"); // eq(1) = 2nd menu item
19
}
20
21
function showScreen3() {
22
$(".screen").hide();
23
$("#screen3").show();
24
$(".menu").removeClass("active");
25
$(".menu").eq(2).addClass("active"); // eq(2) = 3rd menu item
26
}
27
28
function showScreen4() {
29
$(".screen").hide();
30
$("#screen4").show();
31
$(".menu").removeClass("active");
32
$(".menu").eq(3).addClass("active"); // eq(3) = 4th menu item
33
}
34
35
function showNotification() {
36
// choose temporary or persistent notification
37
// only use one - comment out unused option
38
39
// temporary - closes automatically after delay (can also close manually)
40
$("#notification").slideDown("fast").delay(5000).slideUp();
41
42
// persistent - must close manually
43
//$("#notification").slideDown("fast");
44
}
45
46
// Add other JS for your smart device web app
47
Copied!
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.

Resources

If you want to learn more about web development or need a quick reference, consult these tutorials and references from W3Schools:
Last modified 2yr ago
Copy link