-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMQTTSSL.js
More file actions
138 lines (117 loc) · 3.96 KB
/
MQTTSSL.js
File metadata and controls
138 lines (117 loc) · 3.96 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* MQTT with SSL - Websockets
* This is meant to use alonside the HTML/CSS counterpart in the same directory with the name
* WebConsole.html and WebConsole.css, Notice that WebConsole.html it´s not intended to be a resource to be
* called but a copy-paste resource.
*
* Uses MQTT Paho (Websockets).
* Uses jQuery.
* Uses a broker from cloudmqtt.com which serves from AWS. The final service by mqttcloud.com offers a free
* version with up to 5 simultaneous connections and limited transfer speed (but hey! it´s free AND secure,
* as a reference take mosquitto.org secure ports and see if it´s easier, it´s not).
* Doesn´t use cookies as the Phone version (in this same directory).
* Uses an interactive console to send messages and perform commands such as LEDon, LEDoff, shut, con, ...
*
*/
var usrID;
var usrCmd;
var hideWarnings=false;
var connectionState=false;
function command(usrCmd, usrID){
if(usrID == "$"){
if(usrCmd === "LEDon" || usrCmd === "LEDoff"){
if(connectionState === true){
sendConsoleMsg("LED state updated!","yesMsg");
MQTTsend(usrCmd);
}
else sendConsoleMsg("No connection. Use 'con' to connect","noMsg");
}
else if(usrCmd === "con"){
try { MQTTconnect();
sendConsoleMsg("Conectado!","yesMsg"); }
catch(e) { sendConsoleMsg(e,"noMsg");}
}
else if(usrCmd === "client"){
try { MQTTmake();
sendConsoleMsg("Client created. Use 'con' to connect","yesMsg"); }
catch(e){ sendConsoleMsg(e,"noMsg");}
}
else if(usrCmd === "shut"){
try { client.disconnect();
sendConsoleMsg("Disconnected.","yesMsg"); }
catch(e){ sendConsoleMsg(e,"noMsg");}
}
else if(usrCmd === "clr"){
try { $("#consoleOutput").text("");
sendConsoleMsg("Cleared!","yesMsg"); }
catch(e){ sendConsoleMsg(e,"noMsg");}
}
else{
if(connectionState === false ){
try { MQTTconnect();
sendConsoleMsg("Conectado!","yesMsg"); }
catch(e) { sendConsoleMsg(e,"noMsg");}
}
else{
sendConsoleMsg("sent: " + usrCmd);
if(hideWarnings === false){
sendConsoleMsg("Warning: intern/extern command not found. Trying to send anyway","warnMsg");
}
try { MQTTsend(usrCmd); }
catch(e) { sendConsoleMsg(e,"noMsg");}
}
}
}
else{
sendConsoleMsg("Incorrect ID","noMsg");
}
}
$("#btnHideWarn").click(function(){
hideWarnings = !hideWarnings;
});
function sendConsoleMsg(msg, type){
var outp = $("#consoleOutput");
var result = "<span style='display:block'" + (type?"class=' consoleMsg " + type + " '>":">") + msg + "</span>"
outp.append(result);
outp.scrollTop(outp[0].scrollHeight);
}
var host = "tailor.cloudmqtt.com";
var port = 31345;
var topic = "t123";
var MQTTmessage;
var connectOptions = {
"timeout":30,
"userName":"yziinyrs",
"password":"cgAduprcUNln",
"onSuccess":onConnect,
"onFailure":onConnectionLost,
"useSSL":true
};
window.onload = MQTTmake();
function MQTTmake() {
client = new Paho.MQTT.Client(host, port,topic);
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
}
function MQTTconnect(){
client.connect(connectOptions);
}
function MQTTsend(msg) {
message = new Paho.MQTT.Message(msg);
message.destinationName = topic;
client.send(message);
}
function onConnect() {
console.log("onConnect: connected!");
client.subscribe(topic);
connectionState = true;
}
function onConnectionLost(responseObject) {
console.log("onConnectionLost:"+responseObject.errorMessage);
connectionState = false;
}
function onMessageArrived(message) {
MQTTmessage = message.payloadString;
sendConsoleMsg("Response: " + MQTTmessage);
console.log("onMessageArrived:"+MQTTmessage);
}