-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmarthome.html
75 lines (59 loc) · 2.21 KB
/
smarthome.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html>
<body>
<h1>Smart Home</h1>
<button type="button" onclick="publish('ON')">Lamp ON</button>
<button type="button" onclick="publish('OFF')">Lamp OFF</button>
<p id="lights">Lights are on?</p>
<p id="Door">Door is open?</p>
<script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.20.2.js"></script>
<script>
var pubnub = new PubNub({
publishKey : 'pub-c-2d8f55f6-daa7-467b-923b-6a1e6570c9fc',
subscribeKey : 'sub-c-1575c412-2116-11e8-a7d0-2e884fd949d2',
});
function publish(a){
var publishConfig =
{
channel : "ch1",
message : a
};
pubnub.publish(publishConfig, function(status, response){
console.log(status, response);
});
}
pubnub.addListener({
message: function(m) {
// handle message
var channelName = m.channel; // The channel for which the message belongs
var channelGroup = m.subscription; // The channel group or wildcard subscription match (if exists)
var pubTT = m.timetoken; // Publish timetoken
var msg = m.message; // The Payload
var publisher = m.publisher; //The Publisher
document.getElementById("lights").innerHTML = msg;
console.log(msg)
},
presence: function(p) {
// handle presence
var action = p.action; // Can be join, leave, state-change or timeout
var channelName = p.channel; // The channel for which the message belongs
var occupancy = p.occupancy; // No. of users connected with the channel
var state = p.state; // User State
var channelGroup = p.subscription; // The channel group or wildcard subscription match (if exists)
var publishTime = p.timestamp; // Publish timetoken
var timetoken = p.timetoken; // Current timetoken
var uuid = p.uuid; // UUIDs of users who are connected with the channel
},
status: function(s) {
var affectedChannelGroups = s.affectedChannelGroups;
var affectedChannels = s.affectedChannels;
var category = s.category;
var operation = s.operation;
}
});
pubnub.subscribe({
channels: ['ch2'],
});
</script>
</body>
</html>