move UART bridge -> tutorials

This commit is contained in:
Sergio R. Caprile authored and Sergio R. Caprile committed 2025-02-26 15:08:06 -03:00
commit aeda7e5520
66 files changed
+56 -40

No files matched your search

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Device Dashboard</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
</head>
<body></body>
<script type="module" src="main.js"></script>
</html>
+153
View File
@@ -0,0 +1,153 @@
'use strict';
import {Component, h, html, render, useEffect, useState, useRef} from './preact.min.js';
const Message = m => html`<span
style="color: ${m.message.uart ? '#444' : '#373'};">
${m.message.data}
</span>`;
const App = function(props) {
const [cfg, setCfg] = useState({tcp: {}, ws: {}, mqtt: {}});
const [messages, setMessages] = useState([]);
const [connected, setConnected] = useState(false);
const [txt, setTxt] = useState('');
const [ws, setWs] = useState(null);
const refresh = () =>
fetch('/api/config/get').then(r => r.json()).then(r => setCfg(r));
const getport = (url, v) => ((url || '').match(/.*:(\d+)/) || ['', v])[1];
const watchWebsocket = function() {
// Connect to websocker port, to implement WS console
var reconnect = function() {
var port = getport(cfg.ws.url, 4002);
var l = window.location, proto = l.protocol.replace('http', 'ws');
var tid, url = `${proto}//${l.hostname}:${port}/ws`;
// console.log(url);
var ws = new WebSocket(url);
ws.onopen = () => {
setConnected(true);
setWs(ws);
};
ws.onmessage = ev => {
// console.log(ev, ev.data);
setMessages(x => x.concat([{data: ev.data, uart: true}]));
};
ws.onclose = function() {
clearTimeout(tid);
tid = setTimeout(reconnect, 1000);
setConnected(false);
setWs(null);
};
};
reconnect();
};
useEffect(() => {
refresh();
watchWebsocket();
}, []);
const sendmessage = ev => {
setMessages(x => x.concat([{data: txt + '\n', uart: false}]));
if (ws) ws.send(txt + '\n');
setTxt('');
};
const onchange = ev => fetch('/api/config/set', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(cfg),
}).then(r => ws && ws.close());
const set = obj => setCfg(x => Object.assign(x, obj));
const nset = (n,obj) => setCfg(x => Object.assign(x, {[n]: Object.assign(x[n],obj)}));
const setTx = ev => set({tx: parseInt(ev.target.value)});
const setRx = ev => set({rx: parseInt(ev.target.value)});
const setBaud = ev => set({baud: parseInt(ev.target.value)});
const setTcpUrl = ev => nset('tcp', {url: `tcp://0.0.0.0:${ev.target.value}`});
const setWsUrl = ev => nset('ws',{url: `ws://0.0.0.0:${ev.target.value}`});
const setMqttUrl = ev => nset('mqtt',{url: ev.target.value});
const setTcpEna = ev => (nset('tcp',{enable: ev.target.checked}), onchange());
const setWsEna = ev => (nset('ws',{enable: ev.target.checked}), onchange());
const setMqttEna = ev =>(nset('mqtt', {enable: ev.target.checked}), onchange());
return html`
<div class="container">
<h1 style="margin-bottom: 0;">UART \u27F7 network bridge </h1>
<pre class="d-none">${JSON.stringify(cfg, null, 2)}</pre>
<div class="row">
<div class="col col-4">
<h3>UART configuration</h3>
<div class="d-flex pr-1 my-1">
<label class="addon">UART TX pin</label>
<input style="width: 5em;" value=${cfg.tx} onchange=${onchange}
oninput=${setTx} />
</div><div class="d-flex pr-1 my-1">
<label class="addon">UART RX pin</label>
<input style="width: 5em;" value=${cfg.rx} onchange=${onchange}
oninput=${setRx} />
</div><div class="d-flex pr-1 my-y">
<label class="addon">UART Baud</label>
<input style="width: 5em;" value=${cfg.baud} onchange=${onchange}
oninput=${setBaud} />
</div>
</div>
<div class="col col-8">
<h3>Network configuration</h3>
<div class="d-flex my-1">
<label class="addon">Local TCP port</label>
<input style="min-width: 4em; flex: 1 100%;"
value=${getport(cfg.tcp.url, 4001)} onchange=${onchange}
oninput=${setTcpUrl} />
<label class="ml-1 d-flex label"><input type="checkbox"
checked=${cfg.tcp.enable} onchange=${setTcpEna} /> enable</label>
</div><div class="d-flex my-1">
<label class="addon">Local WS port</label>
<input style="flex: 1 100%;"
value=${getport(cfg.ws.url, 4002)} onchange=${onchange}
oninput=${setWsUrl} />
<label class="ml-1 d-flex label"><input type="checkbox"
checked=${cfg.ws.enable} onchange=${setWsEna} /> enable</label>
</div><div class="d-flex my-1">
<label class="addon">Remote MQTT</label>
<input style="flex: 1 100%;"
value=${cfg.mqtt.url} onchange=${onchange}
oninput=${setMqttUrl} />
<label class="ml-1 d-flex label"><input type="checkbox"
checked=${cfg.mqtt.enable} onchange=${setMqttEna} /> enable</label>
</div>
</div>
</div>
<div class="msg">
Note: to connect over MQTT,
open <a href="http://www.hivemq.com/demos/websocket-client/">
console</a>, subscribe to b/tx and publish to b/rx<br/>
Note: to connect over TCP, use netcat utility:<br/>
$ nc ${location.hostname} ${getport(cfg.tcp.url, 4001)}
</div>
<div style="margin-top: 2em;">
<b>UART console</b><span style="margin-left: 1em; color: #777;">works
over the local WS port. WebSocket status: </span><span
style="color: ${connected ? 'teal' : 'red'};">
\u25cf ${connected ? 'connected' : 'disconnected'} </span>
</div>
<div style="margin: 0.5em 0; display: flex">
<input placeholder="to send data, type and press enter..." style="flex: 1 100%;"
value=${txt} onchange=${sendmessage}
oninput=${ev => setTxt(ev.target.value)} />
<button style="margin-left: 1em;"
onclick=${ev => setMessages([])}>clear</button>
</div>
<pre style="height: 15em; overflow: auto;">
${messages.map(message => h(Message, {message}))}
</pre>
</div>`;
};
window.onload = () => render(h(App), document.body);
+1
View File
@@ -0,0 +1 @@
var e,n,_,t,o,r,u,l={},i=[],c=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i;function s(e,n){for(var _ in n)e[_]=n[_];return e}function f(e){var n=e.parentNode;n&&n.removeChild(e)}function a(n,_,t){var o,r,u,l={};for(u in _)"key"==u?o=_[u]:"ref"==u?r=_[u]:l[u]=_[u];if(arguments.length>2&&(l.children=arguments.length>3?e.call(arguments,2):t),"function"==typeof n&&null!=n.defaultProps)for(u in n.defaultProps)void 0===l[u]&&(l[u]=n.defaultProps[u]);return p(n,l,o,r,null)}function p(e,t,o,r,u){var l={type:e,props:t,key:o,ref:r,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,__h:null,constructor:void 0,__v:null==u?++_:u};return null!=n.vnode&&n.vnode(l),l}function h(e){return e.children}function d(e,n){this.props=e,this.context=n}function v(e,n){if(null==n)return e.__?v(e.__,e.__.__k.indexOf(e)+1):null;for(var _;n<e.__k.length;n++)if(null!=(_=e.__k[n])&&null!=_.__e)return _.__e;return"function"==typeof e.type?v(e):null}function y(e){var n,_;if(null!=(e=e.__)&&null!=e.__c){for(e.__e=e.__c.base=null,n=0;n<e.__k.length;n++)if(null!=(_=e.__k[n])&&null!=_.__e){e.__e=e.__c.base=_.__e;break}return y(e)}}function m(e){(!e.__d&&(e.__d=!0)&&t.push(e)&&!g.__r++||r!==n.debounceRendering)&&((r=n.debounceRendering)||o)(g)}function g(){for(var e;g.__r=t.length;)e=t.sort(function(e,n){return e.__v.__b-n.__v.__b}),t=[],e.some(function(e){var n,_,t,o,r,u;e.__d&&(r=(o=(n=e).__v).__e,(u=n.__P)&&(_=[],(t=s({},o)).__v=o.__v+1,P(u,o,t,n.__n,void 0!==u.ownerSVGElement,null!=o.__h?[r]:null,_,null==r?v(o):r,o.__h),D(_,o),o.__e!=r&&y(o)))})}function k(e,n,_,t,o,r,u,c,s,f){var a,d,y,m,g,k,x,H=t&&t.__k||i,E=H.length;for(_.__k=[],a=0;a<n.length;a++)if(null!=(m=_.__k[a]=null==(m=n[a])||"boolean"==typeof m?null:"string"==typeof m||"number"==typeof m||"bigint"==typeof m?p(null,m,null,null,m):Array.isArray(m)?p(h,{children:m},null,null,null):m.__b>0?p(m.type,m.props,m.key,null,m.__v):m)){if(m.__=_,m.__b=_.__b+1,null===(y=H[a])||y&&m.key==y.key&&m.type===y.type)H[a]=void 0;else for(d=0;d<E;d++){if((y=H[d])&&m.key==y.key&&m.type===y.type){H[d]=void 0;break}y=null}P(e,m,y=y||l,o,r,u,c,s,f),g=m.__e,(d=m.ref)&&y.ref!=d&&(x||(x=[]),y.ref&&x.push(y.ref,null,m),x.push(d,m.__c||g,m)),null!=g?(null==k&&(k=g),"function"==typeof m.type&&null!=m.__k&&m.__k===y.__k?m.__d=s=b(m,s,e):s=C(e,m,y,H,g,s),f||"option"!==_.type?"function"==typeof _.type&&(_.__d=s):e.value=""):s&&y.__e==s&&s.parentNode!=e&&(s=v(y))}for(_.__e=k,a=E;a--;)null!=H[a]&&("function"==typeof _.type&&null!=H[a].__e&&H[a].__e==_.__d&&(_.__d=v(t,a+1)),U(H[a],H[a]));if(x)for(a=0;a<x.length;a++)T(x[a],x[++a],x[++a])}function b(e,n,_){var t,o;for(t=0;t<e.__k.length;t++)(o=e.__k[t])&&(o.__=e,n="function"==typeof o.type?b(o,n,_):C(_,o,o,e.__k,o.__e,n));return n}function C(e,n,_,t,o,r){var u,l,i;if(void 0!==n.__d)u=n.__d,n.__d=void 0;else if(null==_||o!=r||null==o.parentNode)e:if(null==r||r.parentNode!==e)e.appendChild(o),u=null;else{for(l=r,i=0;(l=l.nextSibling)&&i<t.length;i+=2)if(l==o)break e;e.insertBefore(o,r),u=r}return void 0!==u?u:o.nextSibling}function x(e,n,_){"-"===n[0]?e.setProperty(n,_):e[n]=null==_?"":"number"!=typeof _||c.test(n)?_:_+"px"}function H(e,n,_,t,o){var r;e:if("style"===n)if("string"==typeof _)e.style.cssText=_;else{if("string"==typeof t&&(e.style.cssText=t=""),t)for(n in t)_&&n in _||x(e.style,n,"");if(_)for(n in _)t&&_[n]===t[n]||x(e.style,n,_[n])}else if("o"===n[0]&&"n"===n[1])r=n!==(n=n.replace(/Capture$/,"")),n=n.toLowerCase()in e?n.toLowerCase().slice(2):n.slice(2),e.l||(e.l={}),e.l[n+r]=_,_?t||e.addEventListener(n,r?S:E,r):e.removeEventListener(n,r?S:E,r);else if("dangerouslySetInnerHTML"!==n){if(o)n=n.replace(/xlink[H:h]/,"h").replace(/sName$/,"s");else if("href"!==n&&"list"!==n&&"form"!==n&&"tabIndex"!==n&&"download"!==n&&n in e)try{e[n]=null==_?"":_;break e}catch(e){}"function"==typeof _||(null!=_&&(!1!==_||"a"===n[0]&&"r"===n[1])?e.setAttribute(n,_):e.removeAttribute(n))}}function E(e){this.l[e.type+!1](n.event?n.event(e):e)}function S(e){this.l[e.type+!0](n.event?n.event(e):e)}function P(e,_,t,o,r,u,l,i,c){var f,a,p,v,y,m,g,b,C,x,H,E=_.type;if(void 0!==_.constructor)return null;null!=t.__h&&(c=t.__h,i=_.__e=t.__e,_.__h=null,u=[i]),(f=n.__b)&&f(_);try{e:if("function"==typeof E){if(b=_.props,C=(f=E.contextType)&&o[f.__c],x=f?C?C.props.value:f.__:o,t.__c?g=(a=_.__c=t.__c).__=a.__E:("prototype"in E&&E.prototype.render?_.__c=a=new E(b,x):(_.__c=a=new d(b,x),a.constructor=E,a.render=A),C&&C.sub(a),a.props=b,a.state||(a.state={}),a.context=x,a.__n=o,p=a.__d=!0,a.__h=[]),null==a.__s&&(a.__s=a.state),null!=E.getDerivedStateFromProps&&(a.__s==a.state&&(a.__s=s({},a.__s)),s(a.__s,E.getDerivedStateFromProps(b,a.__s))),v=a.props,y=a.state,p)null==E.getDerivedStateFromProps&&null!=a.componentWillMount&&a.componentWillMount(),null!=a.componentDidMount&&a.__h.push(a.componentDidMount);else{if(null==E.getDerivedStateFromProps&&b!==v&&null!=a.componentWillReceiveProps&&a.componentWillReceiveProps(b,x),!a.__e&&null!=a.shouldComponentUpdate&&!1===a.shouldCompLine truncated
@@ -0,0 +1,46 @@
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; font: 16px sans-serif; }
select, input, label::before, textarea { outline: none; box-shadow:none !important; border: 1px solid #ccc !important; }
pre { color: #373; font-family: monospace; font-weight: bolder; font-size: smaller; background: #ddd; padding: 1em; border-radius: 0.2em; }
textarea, input { border: 1px solid #ccc; padding: 0.3em 0.5em;}
textarea, input, .addon, .label { font-size: 14px; }
a, a:visited, a:active { color: #55f; }
body {background: #555; }
.addon { color: #999; min-width: 7.5em; }
.label { color: #999; }
.btn {
background: #ccc; border-radius: 0.3em; border: 0; color: #fff; cursor: pointer;
display: inline-block; padding: 0.6em 2em; font-weight: bolder;
}
.btn[disabled] { opacity: 0.5; cursor: auto;}
.smooth { transition: all .2s; }
.container { margin: 2em auto; max-width: 620px; background: white; padding: 1em; border-radius: 0.5em; }
.d-flex { display: flex; align-items: center; }
.d-none { display: none; }
.border { border: 1px solid #ddd; }
.rounded { border-radius: 0.5em; }
.nowrap { white-space: nowrap; }
.msg { background: #def; border-left: 5px solid #59d; padding: 0.5em; font-size: 90%; margin: 1em 0; color: #777; }
.input { background: #fea; padding: 0.2em 1em; border-radius: 0.4em; }
.output { background: #aef; padding: 0.2em 1em; border-radius: 0.4em; }
.pr-1 { padding-right: 0.5em; }
.my-1 { margin-top: 0.3em; margin-bottom: 0.3em;}
.ml-1 { margin-left: 0.5em; }
/* Grid */
.row { display: flex; flex-wrap: wrap; }
.col { margin: 0; padding: 0; overflow: auto; }
.col-12 { width: 100%; }
.col-11 { width: 91.66%; }
.col-10 { width: 83.33%; }
.col-9 { width: 75%; }
.col-8 { width: 66.66%; }
.col-7 { width: 58.33%; }
.col-6 { width: 50%; }
.col-5 { width: 41.66%; }
.col-4 { width: 33.33%; }
.col-3 { width: 25%; }
.col-2 { width: 16.66%; }
.col-1 { width: 8.33%; }
@media (min-width: 1310px) { .container { margin: auto; width: 1270px; } }
@media (max-width: 920px) { .row .col { width: 100%; } }