This commit is contained in:
Kizarm 2024-07-05 17:41:59 +02:00
parent df5f113c92
commit 49768a7849

View file

@ -1,231 +1,129 @@
<html> <!DOCTYPE html>
<head> <html lang="en">
<head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>MQTT</title>
<title>WiFi test</title>
<style> <style>
button:disabled { body {background:rgb(255,255,196);}
background: #ccc; .emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
} textarea.emscripten { font-family: monospace; font-size: 18px; width: 100%; overflow-x: scroll;
input:disabled { white-space: pre; background: black; color: rgb(0,255,0);}
background: #ccc;
}
.slidecontainer {
width: 100%; /* Width of the outside container */
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 50px;
border-radius: 5px;
background: #00FF00;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 50px;
background: #A000A0;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 25px;
height: 50px;
background: #A000A0;
cursor: pointer;
}
.button { .button {
background-color: #00F0F0; background-color: #04AA6D; border: none; color: white; padding: 15px 32px; text-align: center;
border: none; text-decoration: none; display: inline-block; font-size: 48px; width: 100%; }
color: black;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 32px;
border: 2px solid #4CAF50;
}
.row {
display : flex;
align-items : center;
margin-bottom: 15px;
}
.color-box {
width: 200px;
height:100px;
background-color: #F0F0F0;
}
#potProgress {
width: 100%;
background-color: #F0F0F0;
}
#potBar {
width: 100%;
height: 30px;
background-color: #FF0000;
text-align: center;
line-height: 30px;
color: white;
}
</style> </style>
</head> <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
</head>
<body> <body>
<h1>WebSocket pwm - adc</h1> <h1>Dálkové ovládání chaty.</h1>
<p> <table>
Host: <input id="webSocketHost" type="text" value="localhost:1234"/> <tr>
</p> <td><div><label for="pass">Password : </label><input type="password" id="pass" name="password" minlength="8" required /><div>
<p> </td><td><button class="button" id="ConnButton" onClick="Connection();">Connect</button></td>
<button class="button" id="connectButton" onClick="initWebSocket();">Connect</button> <tr>
<button class="button" id="disconnectButton" onClick="stopWebSocket();" disabled>Disconnect</button> <tr>
<button class="button" onClick="checkSocket();">State</button> <td><button class="button" id="MainButton" onClick="chPower(Main);" disabled>Hlavní vypínač</button></td>
</p> <td><button class="button" id="RefrButton" onClick="chPower(Refr);" disabled>Lednice</button></td>
<p id="debugText">DEBUG LINE</p> </tr>
<p> <tr>
<div id="potProgress"> <td colspan=2><textarea class="emscripten" id="output" rows="10" spellcheck="false"></textarea></td>
<div id="potBar">100%</div> </tr>
</div> </table>
<div class="slidecontainer"> <script>
<input type="range" min="1" max="100" value="0" class="slider" id="led1Range" oninput="sendMessage(0);" disabled> const onColor = '#00ff00';
<input type="range" min="1" max="100" value="0" class="slider" id="led2Range" oninput="sendMessage(1);" disabled> const ofColor = '#ff0000';
</div> const TopElem = 'chata/';
const InpTops = TopElem + 'status/';
<div class="row"> const OutTops = TopElem + 'command/';
<div class="color-box" id="in0">0</div> const ConnBtn = document.getElementById('ConnButton');
<div class="color-box" id="in1">1</div> const TextOut = document.getElementById('output');
</div> const url = 'wss://8d84362aa39a41c38a465a2edd8bcae4.s1.eu.hivemq.cloud:8884/mqtt'
</p> // Create an MQTT client instance
const options = {
<script type="text/javascript"> // Clean session
function js_get_id () { clean: true,
var host; connectTimeout: 4000,
const locate = window.location; // Authentication
if (locate.protocol == 'file:') host = 'ws://unknown'; clientId: 'js-' + Date.now().toString(16) + '-client',
else if (locate.port) host = 'ws://' + locate.hostname + ':' + (parseInt(locate.port, 10) + 1); username: 'Kizarm',
else host = 'ws://' + locate.hostname + '/ws'; password: '********',
console.log ('host is: ' + host);
return host;
} }
const server_id = document.getElementById ('webSocketHost'); var Connected = false;
server_id.value = js_get_id (); var Main = {status:false, btn:document.getElementById('MainButton'), tstr:'mainpw'};
var debugText = document.getElementById("debugText"); var Refr = {status:false, btn:document.getElementById('RefrButton'), tstr:'refrig'};
function debug (message) { var client;
debugText.innerHTML = message function ManagePwd () {
} const pass = localStorage.getItem('mqtt_password');
if (!pass) localStorage.setItem('mqtt_password', document.getElementById('pass').value);
function sendMessage (n) { else document.getElementById('pass').value = pass;
var nval = 0;
switch (n) {
case 0: nval = document.getElementById("led1Range").value; break;
case 1: nval = document.getElementById("led2Range").value; break;
default: return;
}
if ( websocket != null ) {
var strToSend = n + '/' + nval;
websocket.send (strToSend);
}
}
function setPotValue (val) {
potenciometer = document.getElementById ('potBar');
const percent = val + '%';
potenciometer.style.width = percent;
potenciometer.innerHTML = percent;
}
function setButValue (id, val) {
const mbut = document.getElementById(id);
if (val) mbut.style.backgroundColor = '#00FF00';
else mbut.style.backgroundColor = '#F0F0F0';
}
function setSldValue (id, val) {
const msld = document.getElementById(id);
msld.value = val;
}
var websocket = null;
function initWebSocket() {
try {
if (typeof MozWebSocket == 'function')
WebSocket = MozWebSocket;
if ( websocket && websocket.readyState == 1 )
websocket.close();
var wsUri = server_id.value;
websocket = new WebSocket( wsUri );
websocket.onopen = function (evt) {
debug("CONNECTED");
document.getElementById("connectButton").disabled = true;
document.getElementById("disconnectButton").disabled = false;
document.getElementById("led1Range").disabled = false;
document.getElementById("led2Range").disabled = false;
}; };
websocket.onclose = function (evt) {
debug("DISCONNECTED");
document.getElementById("connectButton").disabled = false;
document.getElementById("disconnectButton").disabled = true;
document.getElementById("led1Range").disabled = true;
document.getElementById("led2Range").disabled = true;
};
websocket.onmessage = function (evt) {
// console.log (evt.data);
var list = evt.data.split ('/');
if (list.length != 2) return;
var cmd = parseInt (list[0], 10);
var val = parseInt (list[1], 10);
switch (cmd) {
case 0: setPotValue (val); break;
case 1: setButValue ('in0', val); break;
case 3: setButValue ('in1', val); break;
case 4: setSldValue ('led1Range', val); break;
case 5: setSldValue ('led2Range', val); break;
default: break;
}
};
websocket.onerror = function (evt) {
debug('ERROR: ' + evt.data);
};
} catch (exception) {
debug('ERROR: ' + exception);
}
}
function stopWebSocket() { ManagePwd ();
if (websocket) function Connection() {
websocket.close(); if (!mqtt) {
TextOut.value = 'MQTT not supported !!!\n';
return;
} }
options.password = document.getElementById('pass').value;
function checkSocket() { // console.log (options);
if (websocket != null) { if (Connected) {
var stateStr; Connected = false;
switch (websocket.readyState) { client.end()
case 0: { ConnBtn.innerHTML = 'Connect';
stateStr = "CONNECTING"; Main.btn.disabled = true;
break; Refr.btn.disabled = true;
} TextOut.value = "Disconnected\n"
case 1: {
stateStr = "OPEN";
break;
}
case 2: {
stateStr = "CLOSING";
break;
}
case 3: {
stateStr = "CLOSED";
break;
}
default: {
stateStr = "UNKNOW";
break;
}
}
debug("WebSocket state = " + websocket.readyState + " ( " + stateStr + " )");
} else { } else {
debug("WebSocket is null"); client = mqtt.connect(url, options)
client.on('connect', function () {
Connected = true;
TextOut.value += 'Connected to ' + url + '\n';
ConnBtn.innerHTML = 'Disconnect';
Main.btn.disabled = false;
Refr.btn.disabled = false;
// Subscribe to a topic
client.subscribe(InpTops + '#', function (err) {
if (!err) {
// Publish a message to a topic
client.publish(OutTops, '???');
}
})
})
function ChangeStatus (relay, tmsg) {
if (tmsg === '0') {
relay.status = false;
relay.btn.style.background = ofColor;
} else {
relay.status = true;
relay.btn.style.background = onColor;
}
}
// Receive messages
client.on('message', function (topic, message) {
const tstr = topic.toString();
const tmsg = message.toString();
// message is Buffer
TextOut.value += tstr + ' : ' + tmsg + '\n';
if (tstr === InpTops + Main.tstr) {
ChangeStatus (Main, tmsg);
} else if (tstr === InpTops + Refr.tstr) {
ChangeStatus (Refr, tmsg);
}
})
}
}
function chPower (relay) {
if (relay.status) {
relay.status = false;
client.publish(OutTops + relay.tstr, '0');
TextOut.value += relay.tstr + ' off\n';
} else {
relay.status = true;
client.publish(OutTops + relay.tstr, '1');
TextOut.value += relay.tstr + ' on\n';
} }
} }
</script> </script>
</body> </body
</html> </html>