Example uses a shelly sensor, which has a offset mode in its new firmware. So below is not needed any more. But the example can be used for any calibration/adjustments.
I’ve put two examples in the NodeRed function
First the solution
var temperature=msg.payload.tC;
var humidity=msg.payload.tF;
// calc new offset example
// simple offset + 2.3 off real measurement
msg.payload.tC=temperature + 2.3;
// more complex example
// take two measurements (with a big difference)
msg.payload.tF=1.11 * (humidity - 1);
return msg;
First adjustment is plus or minus a value. Second is more precise when the temperature needs more adjusting
Dots on the red line are measured values Blue is how it should be
So measured was 2.8 but should be 2 And measured 14.5 but needs to be 15
slope = (14.5-2.8)/(15-2) = 0.9
To get the multiplication factor = 1/0.9 = 1.1111
=(2.8-heightadjust)*1,1111 should give us 2
2/1.1111 = 2.8 – heightadjust
1.8 = 2.8 – heightadjust = 1
So the formula is realhumid = 1,1111 * ( measuredhumid – adjust )
Our bedroom has a shelly dimmer, this one is connected with a wall switch and is being controlled by Domoticz, Home Assistant and NodeRed.
I had to fix some stuff, so this was a perfect time to jot down some notes.
I’ve wired it up like this: (I’ve got a Line wire in my ceiling socket, so i placed the module there instead of the wall socket)
Configure the Shelly as mentioned in the manual. After that do the following: Advance > Developer settings : (Enable CoIot if you want a easy auto detect for Home Assistant) Enable MQTT (This will DISABLE cloud ! ) Server: the ip number of your Mqtt Broker
Next I did was:
Now the wall switch will change/toggle what the current state is. (If the light is off, and you switch it on using Mqtt, you probably needed to hit the switch two times to turn it off again. Not so with this setting.)
My Nodered Flow ( Not using the Shelly palette nodes in this example)
The Mqtt IN node sets the state of the switch when you use the wall switch! Cozy turns light on at a specific level. The slider send an off command when 0% selected, else an on command and the sliders value.
But I didn’t like the continuous logins with automated logins.
So below solution is what i’ve implemented for now.
I’ve installed the IOT extra package from Mikrotik, now I can send MQTT messages from my Wifi enabled Mikrotiks to my Mosquitto broker. (Download extra package zip, extract iot-7.x-arm.npk, upload this to your mikrotik files folder, and reboot) The script I’m running on my Mikrotik, sends the active wifi connections with the comments. ( When a comment is set in the Access List, then it’s a know connection )
[admin@RB40111] /iot/mqtt> export
# may/15/2023 21:45:12 by RouterOS 7.9
# software id = xxxx-xxxx
#
# model = RB4011iGS+5HacQ2HnD
# serial number = xxxxxxxxxxxxxxxxx
/iot mqtt brokers
add address=10.1.x.y client-id=rb4011 name=NR
I made the following script on my MT named mqtt
:local broker "NR"
# MQTT topic where the message should be published
:local topic "rb4011/mac"
:foreach i in=[/interface wireless registration-table print proplist=mac-address as-value] do={
:local message "$i"
/iot mqtt publish broker=$broker topic=$topic message=$message
}
A schedule is needed to run this script every 15 minutes
[admin@RB40111] /system/scheduler> export
# may/15/2023 21:48:14 by RouterOS 7.9
# software id = xxxx-xxx
#
# model = RB4011iGS+5HacQ2HnD
# serial number = xxxxxxxxxxx
/system scheduler
add interval=15m name=mqtt on-event=mqtt policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
start-date=may/15/2023 start-time=13:30:54
Now all wifi connections will be send to topic rb4011/mac.
# Example
.id=*6a;comment=Mobile Henri wlan2;mac-address=44:46:87:xx:xx:xx
Using NodeRed I can make filters and notifications
Below function: get Mac and Comment from payload, if the comment is empty then it is a unknown connection … so send me a warning using Pushover.
// filter function
var output = msg.payload.split(";");
var comment = (output[1].split("="));
var mac = (output[2].split("="));
msg.payload={};
msg.payload = mac[1];
if (comment[1] == "") {
return msg;
}
// is xx:xx:xx:xx:xx:xx online? example
var output = msg.payload.split(";");
var comment = (output[1].split("="));
var mac = (output[2].split("="));
msg.payload={};
msg.payload = mac[1];
if (mac[1] == "xx:xx:xx:xx:xx:xx") {
return msg;
}
Now i’m getting a notification when an unknown wifi connection is made on my Access Point. ( I going to implement the Access List from MT at a later point. No access when not in the Access List)
Using a mqtt server with websockets and a website with the Eclipse Paho JavaScript Client
Above video: Joystick sends movement through the internet to my mqtt server, laptop is fetching a webpage from one of my webservers. A piece of javascript connects via websockets to the mqtt server and realtime changes the displayed image.
I intend to display maps and views into a maze you can walk through. like: (shamelessly stolen image from the internet)
I already have written another piece of code which generates a maze, and tells you using samples which way to go.
CODE
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
<script type = "text/javascript"
src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
var connected_flag=0
var mqtt;
var reconnectTimeout = 2000;
var host="MQTTSERVER";
var port=8084;
var sub_topic="web/#";
function onConnectionLost(){
connected_flag=0;
document.getElementById("status").innerHTML = "Connection to HQ Lost";
}
function onFailure(message) {
console.log("Failed");
setTimeout(MQTTconnect, reconnectTimeout);
}
function onMessageArrived(r_message){
var topic=r_message.destinationName;
document.getElementById("status").innerHTML = "";
if(topic=="web/mapviewer")
{
$(".deze").attr("src","tiles/" + r_message.payloadString + ".png");
}
}
function onConnected(recon,url){
console.log(" in onConnected " +reconn);
}
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
connected_flag=1
document.getElementById("status").innerHTML = "Connection made to HQ";
mqtt.subscribe(sub_topic);
}
function MQTTconnect() {
console.log("connecting to "+ host +" "+ port);
var x=Math.floor(Math.random() * 10000);
var cname="controlform-"+x;
//var cname="escape1";
mqtt = new Paho.MQTT.Client(host,port,cname);
var options = {
useSSL:true,
timeout: 3,
onSuccess: onConnect,
onFailure: onFailure,
};
mqtt.onConnectionLost = onConnectionLost;
mqtt.onMessageArrived = onMessageArrived;
mqtt.connect(options);
return false;
}
function sub_topics(){
document.getElementById("messages").innerHTML ="";
if (connected_flag==0){
out_msg="<b>Not Connected so can't subscribe</b>"
console.log(out_msg);
document.getElementById("messages").innerHTML = out_msg;
return false;
}
var stopic= document.forms["subs"]["Stopic"].value;
console.log("Subscribing to topic ="+stopic);
mqtt.subscribe(stopic);
return false;
}
function send_message(msg,topic){
if (connected_flag==0){
out_msg="<b>Not Connected so can't send</b>"
console.log(out_msg);
document.getElementById("messages").innerHTML = out_msg;
return false;
}
var value=msg.value;
console.log("value= "+value);
console.log("topic= "+topic);
message = new Paho.MQTT.Message(value);
message.destinationName = "web/"+topic;
mqtt.send(message);
return false;
}
</script>
<style>
@media only screen and (max-width: 600px) {
div {
max-width: 320px;
}
img {
max-width: 320px;
}
}
div.imageview {
color: white;
background: gray;
padding: 15px;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}
</style>
</head>
<body onload="MQTTconnect()">
<div class=imageview>
<img class="deze" src="tiles/00.png">
<div id="status">Connection Status: Not Connected</div>
</div>
</body>
</html>