Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
SmartHome/script.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
55 lines (42 sloc)
2 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setColor(e) { | |
var target = e.target, | |
status = e.target.classList.contains('active'); | |
e.target.classList.add(status ? 'inactive' : 'active'); | |
e.target.classList.remove(status ? 'active' : 'inactive'); | |
} | |
function startConnect() { | |
// Generate a random client ID | |
clientID = "clientID-" + parseInt(Math.random() * 100); | |
// Print output for the user in the messages div | |
document.getElementById("messages").innerHTML += '<span>Connecting to: ' + "test.mosqutto.org" + ' on port: ' + "8080" + '</span><br/>'; | |
document.getElementById("messages").innerHTML += '<span>Using the following client value: ' + clientID + '</span><br/>'; | |
// Initialize new Paho client connection | |
client = new Paho.MQTT.Client("test.mosqutto.org", 8080 , clientID); | |
// Set callback handlers | |
client.onConnectionLost = onConnectionLost; | |
client.onMessageArrived = onMessageArrived; | |
// Connect the client, if successful, call onConnect function | |
client.connect({ | |
onSuccess: onConnect, | |
}); | |
} | |
// Called when the client connects | |
function onConnect() { | |
// Fetch the MQTT topic from the form | |
// Print output for the user in the messages div | |
document.getElementById("messages").innerHTML += '<span>Subscribing to: ' + topic + '</span><br/>'; | |
// Subscribe to the requested topic | |
client.subscribe("302CEM/Team2/lights01"); | |
} | |
// Called when the client loses its connection | |
function onConnectionLost(responseObject) { | |
document.getElementById("messages").innerHTML += '<span>ERROR: Connection lost</span><br/>'; | |
if (responseObject.errorCode !== 0) { | |
document.getElementById("messages").innerHTML += '<span>ERROR: ' + + responseObject.errorMessage + '</span><br/>'; | |
} | |
} | |
// Called when a message arrives | |
function onMessageArrived(message) { | |
console.log("onMessageArrived: " + message.payloadString); | |
document.getElementById("messages").innerHTML += '<span>Topic: ' + message.destinationName + ' | ' + message.payloadString + '</span><br/>'; | |
} | |