Node Red Matrix-Chatbot

This commit is contained in:
Dustin Brunner 2021-04-24 09:23:55 +02:00
parent 98ad593b0a
commit 4ebbe74bba
9 changed files with 410 additions and 2 deletions

View File

@ -5,7 +5,8 @@ A MQTT Door- or Mailboxsensor using an ESP8266 Wifi Module, which notifies you i
![Alt text](pictures/mailbox.jpg?raw=true "https://www.swinginter.net/wp-content/uploads/2018/03/otelo-mailbox.png")
# How it Works
If the mailbox opens the ESP wakes up from deep-sleep, connects to Wifi and the MQTT-server and sents out a MQTT message, which is then processed by Node-Red. Node-Red however "converts" it into a Telegram message, which is then posted to my phone via a Telegram-bot.
If the mailbox opens the ESP wakes up from deep-sleep, connects to Wifi and the MQTT-server and sents out a MQTT message, which is then processed by Node-Red. Node-Red however "converts" it into a Matrix message, which is then posted to my phone via a Matrix-Chatbot.
## You Can find the Instructions how to create the Chatbot in the [node-red-Folder](node-red/README.md).
As a switch I used a magentic REED-switch, which CLOSES if a magnet gets close to it. This was the first problem because when the mailbox is closed (which is the case for the most time) the reedswitch is also closed. If we now want to get notified when the mailbox opens we need to use the opening reedswitch as a trigger to send the message.
@ -34,7 +35,7 @@ To get this result I used the following FINAL schematic:
![Alt text](pictures/Mailbox_sensor_layout.png?raw=true "Final PCB Layout")
The signal at the resetpin, when the reedswitch opens looks like that. By varying the value of the capacitor you can also change the pulse length:
![Alt text](pictures/Resetpuls_Transistor.bmp?raw=true "Reset signal")
![Alt text](pictures/Resetpuls_Transistor.jpg?raw=true "Reset signal")
# The code
The code is verry simple: After a reset it connects to wifi and an MQTT server where it publishes a message. After that it causes the ESP to enter an infinite deep sleep, which can only be interrupted by another reset of the ESP board. I used an ESP8266 Wifi Module but the code should work with any other ESP too.

57
node-red/README.md Normal file
View File

@ -0,0 +1,57 @@
# Integration in Node Red / Node-Red Matrix Chatbot
This is a follow-up Article of the MQTT Mailbox-Doorsensor-Project.
I want to show you how you can integrate the Doorsensor into Node-Red, where it triggers a Matrix Chatbot, which is then sending you a Message, that you have got a letter in the mailbox.
If you didnt already have a Matrix-Server, I recommend you to read my article about setting up a [Matrix Chatserver using Docker](https://www.brunweb.de/eigener-matrix-homeserver-mit-docker-einrichten/) (in German).
The Mailbox-Sensor is programmed in a way, that it publishes a MQTT-message on a specific topic, whenever the mailbox gets opened. Node Red is processing the message and sending out a message into a Matrix-Chatroom.
# 1. Setup Bot-User on the Matrix server
You first have to create a specific user on your matrix-server, that is only used by the bot. Name it as you want. If you have followed my Matrix-server setup instructions, you can do that in the synapse-admin webpanel.
Now you need to create a Chatroom for you and the Bot (The Personal chat with the bot is not working, due to End-to-End encryption). You can create this room with your personal account on the Matrix-server. Make sure to NOT enable room encryption. Now invite the Bot-user into the room.
After that login into the element-webpanel with your newly created bot-user and accept the invitation into the room.
# 2. Setup Node-Red
To configure the bot in Node-Red you first need to find out the Room-ID of your bot-room. For that click on the little „i“ in the top right corner of the element-webpanel. Then click on „room-properties“ and in the „advanced“ tab, you find the „Internal Room ID“. Copy it because you will need it later.
![Alt text](../pictures/room-properties.png "Room Properties")
In Node Red you first need to download the Node „node-red-contrib-matrix“. (There is also the „node-red-contrib-matrixbot“ Node availible, but this did **not** work for me because it wont connect to my matrix server).
Now you can import my flow, which is the file: `matrix-doorsensor-flow.json`.
![Alt text](../pictures/matrix-doorsensor-flow.png "matrix-doorsensor-flow")
You need to configure the „bot_room out“-Node: Fill your previously copied room-id into the „Room“ field. Then click on the server-field, where you need to fill in your server-address. Dont fill the User-Token field by yourself. Instead fill in the username and password of your bot-user and click on the little arrow in the „Generate Token“ tab. This will create a user-token for you. If you ever have connectivity problems to your matrix-server, just delete the Token from the token field and generate a new one like described earlier.
You can test if your configuration is working by clicking on the inject „1“- node. You should receive a message in your bot-room.
If it does not work make sure that the bot-user is not logged in on the element-webpanel anymore. I have noticed, that the when I login the bot-user on any other client, the generated access token is not valid anymore. If this happens just create a new one and you will be fine :).
# Going further
You can expand your bot by adding an input-node to the mix. Now you can text commands into the bot-room and the bot answers to them.
In my configuration I added a opportunity to Request the current weather from my weatherstation. Also the bot textes me at 8 oclock in the morning automatically the current temperature.
My complete flow looks like this:
![matrix-chatbot](../pictures/matrix-chatbot.png "matrix-chatbot")
You can download it, it is the File: `matrix-chatbot.json` from Mailbox-Doorsensor Repository.
<br>
<br>
<p xmlns:dct="http://purl.org/dc/terms/" xmlns:cc="http://creativecommons.org/ns#" class="license-text">This work by <span property="cc:attributionName">Dustin Brunner</span> is licensed under <a rel="license" href="https://creativecommons.org/licenses/by/4.0">CC BY 4.0<img style="height:15px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" /><img style="height:15px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" /></a></p>
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/"><img alt="Creative Commons Lizenzvertrag" style="border-width:0" src="https://i.creativecommons.org/l/by/4.0/88x31.png" /></a><br />Dieses Werk von <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">Dustin Brunner</span> ist lizenziert unter einer <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Namensnennung 4.0 International Lizenz</a>.

View File

@ -0,0 +1,228 @@
[
{
"id": "d727f9f0.40df78",
"type": "inject",
"z": "825dd022.49967",
"name": "",
"topic": "",
"payload": "1",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 570,
"y": 560,
"wires": [
[
"458a6413.7502a4"
]
]
},
{
"id": "b6977877.47316",
"type": "function",
"z": "825dd022.49967",
"name": "Post ist da",
"func": "var nachricht = {};\n\nnachricht.payload = \"Die Post ist da!\";\n\nreturn nachricht;",
"outputs": 1,
"noerr": 0,
"x": 970,
"y": 560,
"wires": [
[
"fc3dbdfa.5e41e"
]
]
},
{
"id": "d7f372ab.b85138",
"type": "function",
"z": "825dd022.49967",
"name": "Guten Morgen Temperatur",
"func": "var temp_boden = global.get(\"Temperatur_Boden\");\nvar temp_2m = global.get(\"Temperatur_2m\");\n\nvar nachricht = {};\n\nnachricht.payload = \"Guten Morgen, draußen sind aktuell\\n\"+ temp_2m + \" °C (2m)\\n\" + temp_boden + \" °C (Boden)!\";\n\nreturn nachricht;\n",
"outputs": 1,
"noerr": 0,
"x": 800,
"y": 620,
"wires": [
[
"fc3dbdfa.5e41e"
]
]
},
{
"id": "657df136.f66d88",
"type": "inject",
"z": "825dd022.49967",
"name": "Jeden Morgen um 8",
"topic": "",
"payload": "1",
"payloadType": "str",
"repeat": "",
"crontab": "00 08 * * *",
"once": false,
"onceDelay": 0.1,
"x": 540,
"y": 620,
"wires": [
[
"d7f372ab.b85138"
]
]
},
{
"id": "b071a851.246c58",
"type": "mqtt in",
"z": "825dd022.49967",
"name": "",
"topic": "briefkasten/sensor",
"qos": "2",
"datatype": "auto",
"broker": "3bfdb428.8d8e3c",
"x": 550,
"y": 500,
"wires": [
[
"458a6413.7502a4"
]
]
},
{
"id": "b7a014ce.f0ef9",
"type": "function",
"z": "825dd022.49967",
"name": "Eingabe verarbeiten",
"func": "if(msg.payload.startsWith(\"B\"))\n{\n var anweisung = msg.payload;\n anweisung = anweisung.replace(\" \",\"\");\n\n var nachricht = {};\n\n if (anweisung == 'Bt'){\n var temp_boden = global.get(\"Temperatur_Boden\");\n var temp_2m = global.get(\"Temperatur_2m\");\n \n nachricht.payload = \"Aktuelle Temperatur:\\n\"+ \n temp_2m + \" °C (2m)\\n\" + \n temp_boden + \" °C (Boden)!\";\n }\n \n else if (anweisung == 'Bs'){\n nachricht.payload = \"Folgende Befehle können verwendet werden:\\n Bs -> Diese Liste aufrufen \\n Bt -> Aktuelle Temperatur\";\n }\n \n else{\n nachricht.payload = \"Unbekannte Anweisung, Anweisungsliste kann mit 'Bs' aufgerufen werden.\"\n }\n \n return nachricht;\n}\n\n\n\n",
"outputs": 1,
"noerr": 0,
"x": 780,
"y": 700,
"wires": [
[
"fc3dbdfa.5e41e"
]
]
},
{
"id": "a82b4a1b.8c36",
"type": "delay",
"z": "825dd022.49967",
"name": "Delay 100ms",
"pauseType": "delay",
"timeout": "100",
"timeoutUnits": "milliseconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"x": 570,
"y": 700,
"wires": [
[
"b7a014ce.f0ef9"
]
]
},
{
"id": "db9da62c.e59dd8",
"type": "debug",
"z": "825dd022.49967",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 570,
"y": 740,
"wires": []
},
{
"id": "458a6413.7502a4",
"type": "delay",
"z": "825dd022.49967",
"name": "Limit 1 per minute",
"pauseType": "rate",
"timeout": "5",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "minute",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": true,
"x": 770,
"y": 560,
"wires": [
[
"b6977877.47316"
]
]
},
{
"id": "dcb2d1ec.a94a18",
"type": "matrix-recvtext",
"z": "825dd022.49967",
"name": "bot_room in",
"room": "!Qbooooooooooooo:Chat",
"server": "ad05a9d3.0f4cd",
"filterself": true,
"x": 330,
"y": 700,
"wires": [
[
"a82b4a1b.8c36",
"db9da62c.e59dd8"
]
]
},
{
"id": "fc3dbdfa.5e41e",
"type": "matrix-sendtext",
"z": "825dd022.49967",
"name": "bot_room out",
"room": "!Qbooooooooooooo:Chat",
"server": "ad05a9d3.0f4cd",
"notice": false,
"x": 1180,
"y": 560,
"wires": []
},
{
"id": "3bfdb428.8d8e3c",
"type": "mqtt-broker",
"z": "",
"name": "MQTT_Server",
"broker": "192.168.123.123",
"port": "1883",
"clientid": "",
"usetls": false,
"compatmode": true,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willPayload": ""
},
{
"id": "ad05a9d3.0f4cd",
"type": "matrix-server",
"z": "",
"name": "bot_Chat",
"server": "http://192.168.123.124:8008",
"token": "ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
"userid": "@bot:Chat",
"room": ""
}
]

View File

@ -0,0 +1,122 @@
[
{
"id": "d727f9f0.40df78",
"type": "inject",
"z": "825dd022.49967",
"name": "",
"topic": "",
"payload": "1",
"payloadType": "str",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 570,
"y": 560,
"wires": [
[
"458a6413.7502a4"
]
]
},
{
"id": "b6977877.47316",
"type": "function",
"z": "825dd022.49967",
"name": "Post ist da",
"func": "var nachricht = {};\n\nnachricht.payload = \"Die Post ist da!\";\n\nreturn nachricht;",
"outputs": 1,
"noerr": 0,
"x": 970,
"y": 560,
"wires": [
[
"fc3dbdfa.5e41e"
]
]
},
{
"id": "b071a851.246c58",
"type": "mqtt in",
"z": "825dd022.49967",
"name": "",
"topic": "briefkasten/sensor",
"qos": "2",
"datatype": "auto",
"broker": "3bfdb428.8d8e3c",
"x": 550,
"y": 500,
"wires": [
[
"458a6413.7502a4"
]
]
},
{
"id": "458a6413.7502a4",
"type": "delay",
"z": "825dd022.49967",
"name": "Limit 1 per minute",
"pauseType": "rate",
"timeout": "5",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "minute",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": true,
"x": 770,
"y": 560,
"wires": [
[
"b6977877.47316"
]
]
},
{
"id": "fc3dbdfa.5e41e",
"type": "matrix-sendtext",
"z": "825dd022.49967",
"name": "bot_room out",
"room": "!Qbooooooooooooo:Chat",
"server": "ad05a9d3.0f4cd",
"notice": false,
"x": 1180,
"y": 560,
"wires": []
},
{
"id": "3bfdb428.8d8e3c",
"type": "mqtt-broker",
"z": "",
"name": "MQTT_Server",
"broker": "192.168.123.123",
"port": "1883",
"clientid": "",
"usetls": false,
"compatmode": true,
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"willTopic": "",
"willQos": "0",
"willPayload": ""
},
{
"id": "ad05a9d3.0f4cd",
"type": "matrix-server",
"z": "",
"name": "bot_Chat",
"server": "http://192.168.123.124:8008",
"token": "ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
"userid": "@bot:Chat",
"room": ""
}
]

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

BIN
pictures/matrix-chatbot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB