bad file
This commit is contained in:
parent
df5f113c92
commit
49768a7849
1 changed files with 126 additions and 228 deletions
354
chata/index.html
354
chata/index.html
|
@ -1,231 +1,129 @@
|
||||||
<html>
|
<!DOCTYPE html>
|
||||||
<head>
|
<html lang="en">
|
||||||
<meta charset="utf-8">
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
<meta charset="utf-8">
|
||||||
<title>WiFi test</title>
|
<title>MQTT</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;
|
.button {
|
||||||
}
|
background-color: #04AA6D; border: none; color: white; padding: 15px 32px; text-align: center;
|
||||||
.slidecontainer {
|
text-decoration: none; display: inline-block; font-size: 48px; width: 100%; }
|
||||||
width: 100%; /* Width of the outside container */
|
</style>
|
||||||
}
|
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
|
||||||
.slider {
|
</head>
|
||||||
-webkit-appearance: none;
|
<body>
|
||||||
width: 100%;
|
<h1>Dálkové ovládání chaty.</h1>
|
||||||
height: 50px;
|
<table>
|
||||||
border-radius: 5px;
|
<tr>
|
||||||
background: #00FF00;
|
<td><div><label for="pass">Password : </label><input type="password" id="pass" name="password" minlength="8" required /><div>
|
||||||
outline: none;
|
</td><td><button class="button" id="ConnButton" onClick="Connection();">Connect</button></td>
|
||||||
opacity: 0.7;
|
<tr>
|
||||||
-webkit-transition: .2s;
|
<tr>
|
||||||
transition: opacity .2s;
|
<td><button class="button" id="MainButton" onClick="chPower(Main);" disabled>Hlavní vypínač</button></td>
|
||||||
}
|
<td><button class="button" id="RefrButton" onClick="chPower(Refr);" disabled>Lednice</button></td>
|
||||||
.slider::-webkit-slider-thumb {
|
</tr>
|
||||||
-webkit-appearance: none;
|
<tr>
|
||||||
appearance: none;
|
<td colspan=2><textarea class="emscripten" id="output" rows="10" spellcheck="false"></textarea></td>
|
||||||
width: 25px;
|
</tr>
|
||||||
height: 50px;
|
</table>
|
||||||
background: #A000A0;
|
<script>
|
||||||
cursor: pointer;
|
const onColor = '#00ff00';
|
||||||
}
|
const ofColor = '#ff0000';
|
||||||
.slider::-moz-range-thumb {
|
const TopElem = 'chata/';
|
||||||
width: 25px;
|
const InpTops = TopElem + 'status/';
|
||||||
height: 50px;
|
const OutTops = TopElem + 'command/';
|
||||||
background: #A000A0;
|
const ConnBtn = document.getElementById('ConnButton');
|
||||||
cursor: pointer;
|
const TextOut = document.getElementById('output');
|
||||||
}
|
const url = 'wss://8d84362aa39a41c38a465a2edd8bcae4.s1.eu.hivemq.cloud:8884/mqtt'
|
||||||
.button {
|
// Create an MQTT client instance
|
||||||
background-color: #00F0F0;
|
const options = {
|
||||||
border: none;
|
// Clean session
|
||||||
color: black;
|
clean: true,
|
||||||
padding: 15px 32px;
|
connectTimeout: 4000,
|
||||||
text-align: center;
|
// Authentication
|
||||||
text-decoration: none;
|
clientId: 'js-' + Date.now().toString(16) + '-client',
|
||||||
display: inline-block;
|
username: 'Kizarm',
|
||||||
font-size: 32px;
|
password: '********',
|
||||||
border: 2px solid #4CAF50;
|
}
|
||||||
}
|
var Connected = false;
|
||||||
.row {
|
var Main = {status:false, btn:document.getElementById('MainButton'), tstr:'mainpw'};
|
||||||
display : flex;
|
var Refr = {status:false, btn:document.getElementById('RefrButton'), tstr:'refrig'};
|
||||||
align-items : center;
|
var client;
|
||||||
margin-bottom: 15px;
|
function ManagePwd () {
|
||||||
}
|
const pass = localStorage.getItem('mqtt_password');
|
||||||
.color-box {
|
if (!pass) localStorage.setItem('mqtt_password', document.getElementById('pass').value);
|
||||||
width: 200px;
|
else document.getElementById('pass').value = pass;
|
||||||
height:100px;
|
};
|
||||||
background-color: #F0F0F0;
|
|
||||||
}
|
|
||||||
#potProgress {
|
|
||||||
width: 100%;
|
|
||||||
background-color: #F0F0F0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#potBar {
|
ManagePwd ();
|
||||||
width: 100%;
|
function Connection() {
|
||||||
height: 30px;
|
if (!mqtt) {
|
||||||
background-color: #FF0000;
|
TextOut.value = 'MQTT not supported !!!\n';
|
||||||
text-align: center;
|
return;
|
||||||
line-height: 30px;
|
}
|
||||||
color: white;
|
options.password = document.getElementById('pass').value;
|
||||||
|
// console.log (options);
|
||||||
|
if (Connected) {
|
||||||
|
Connected = false;
|
||||||
|
client.end()
|
||||||
|
ConnBtn.innerHTML = 'Connect';
|
||||||
|
Main.btn.disabled = true;
|
||||||
|
Refr.btn.disabled = true;
|
||||||
|
TextOut.value = "Disconnected\n"
|
||||||
|
} else {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
</style>
|
}
|
||||||
</head>
|
// Receive messages
|
||||||
<body>
|
client.on('message', function (topic, message) {
|
||||||
<h1>WebSocket pwm - adc</h1>
|
const tstr = topic.toString();
|
||||||
<p>
|
const tmsg = message.toString();
|
||||||
Host: <input id="webSocketHost" type="text" value="localhost:1234"/>
|
// message is Buffer
|
||||||
</p>
|
TextOut.value += tstr + ' : ' + tmsg + '\n';
|
||||||
<p>
|
if (tstr === InpTops + Main.tstr) {
|
||||||
<button class="button" id="connectButton" onClick="initWebSocket();">Connect</button>
|
ChangeStatus (Main, tmsg);
|
||||||
<button class="button" id="disconnectButton" onClick="stopWebSocket();" disabled>Disconnect</button>
|
} else if (tstr === InpTops + Refr.tstr) {
|
||||||
<button class="button" onClick="checkSocket();">State</button>
|
ChangeStatus (Refr, tmsg);
|
||||||
</p>
|
}
|
||||||
<p id="debugText">DEBUG LINE</p>
|
})
|
||||||
<p>
|
}
|
||||||
<div id="potProgress">
|
}
|
||||||
<div id="potBar">100%</div>
|
function chPower (relay) {
|
||||||
</div>
|
if (relay.status) {
|
||||||
<div class="slidecontainer">
|
relay.status = false;
|
||||||
<input type="range" min="1" max="100" value="0" class="slider" id="led1Range" oninput="sendMessage(0);" disabled>
|
client.publish(OutTops + relay.tstr, '0');
|
||||||
<input type="range" min="1" max="100" value="0" class="slider" id="led2Range" oninput="sendMessage(1);" disabled>
|
TextOut.value += relay.tstr + ' off\n';
|
||||||
</div>
|
} else {
|
||||||
|
relay.status = true;
|
||||||
<div class="row">
|
client.publish(OutTops + relay.tstr, '1');
|
||||||
<div class="color-box" id="in0">0</div>
|
TextOut.value += relay.tstr + ' on\n';
|
||||||
<div class="color-box" id="in1">1</div>
|
}
|
||||||
</div>
|
}
|
||||||
</p>
|
</script>
|
||||||
|
</body
|
||||||
<script type="text/javascript">
|
|
||||||
function js_get_id () {
|
|
||||||
var host;
|
|
||||||
const locate = window.location;
|
|
||||||
if (locate.protocol == 'file:') host = 'ws://unknown';
|
|
||||||
else if (locate.port) host = 'ws://' + locate.hostname + ':' + (parseInt(locate.port, 10) + 1);
|
|
||||||
else host = 'ws://' + locate.hostname + '/ws';
|
|
||||||
console.log ('host is: ' + host);
|
|
||||||
return host;
|
|
||||||
}
|
|
||||||
const server_id = document.getElementById ('webSocketHost');
|
|
||||||
server_id.value = js_get_id ();
|
|
||||||
var debugText = document.getElementById("debugText");
|
|
||||||
function debug (message) {
|
|
||||||
debugText.innerHTML = message
|
|
||||||
}
|
|
||||||
|
|
||||||
function sendMessage (n) {
|
|
||||||
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() {
|
|
||||||
if (websocket)
|
|
||||||
websocket.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkSocket() {
|
|
||||||
if (websocket != null) {
|
|
||||||
var stateStr;
|
|
||||||
switch (websocket.readyState) {
|
|
||||||
case 0: {
|
|
||||||
stateStr = "CONNECTING";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
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 {
|
|
||||||
debug("WebSocket is null");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue