Der Raspberry Pi muss wie folgt eingerichtet werden. Dazu habe ich die Schritte von Thomas Varnish befolgt. Diese gebe ich zum Teil auf deutsch wieder . Die grau hinterlegten Textpassagen habe ich von seiner Seite übernommen.
Quelle: https://www.instructables.com/id/How-to-Use-MQTT-With-the-Raspberry-Pi-and-ESP8266/, Thomas Varnish, Stand 25.07.2020
Raspberry Pi Mosquitto einrichten:
Als erstes bietet es sich immer an, das System upzudaten.
sudo apt-get update
sudo apt-get upgrade
Wenn das geschafft ist, muss das Programm "mosquitto" und die zugehörigen Client-Pakete "mosquitto-clients packages" installiert werden.
sudo apt-get install mosquitto -y
sudo apt-get install mosquitto-clients -y
Der Broker (Pi) kann noch so konfiguriert werden, dass er immer mit Authentifikation arbeitet. Das habe ich in meinem Beispiel nicht getan.
"When you've finished installing these two packages, we are going to need to configure the broker. The mosquitto broker's configuration file is located at /etc/mosquitto/mosquitto.conf, so open this with your favourite text editor. If you don't have a favourite text editor or don't know how to use any of the command line editors, I'll be using nano so you can follow along:
sudo nano /etc/mosquitto/mosquitto.conf
At the bottom of this file, you should see the line:
include_dir /etc/mosquitto/conf.d
Delete this line. Add the following lines to the bottom of the file.
allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883
"
Hier zeige ich euch, wie ihr einen Wemos über Wlan mit dem Raspberry Pi verbinden könnt. Der Pi hat dabei die IP Adresse 192.168.0.21 und der Wemos 192.168.0.12 in meinem Testaufbau.
Node Red Code:
[{"id":"93e9be28.af3db8","type":"mqtt in","z":"7e38b285.ff7dfc","name":"","topic":"W1/Q1","qos":"0","datatype":"utf8","broker":"90cfe57d.ac6208","x":270,"y":380,"wires":[["b5dddcb9.e7b7c8"]]},{"id":"5a140d1f.8ba264","type":"mqtt out","z":"7e38b285.ff7dfc","name":"","topic":"W1/A1","qos":"0","retain":"false","broker":"ab54d225.819f78","x":730,"y":840,"wires":[]},{"id":"6340cf35.bb5a6","type":"ui_switch","z":"7e38b285.ff7dfc","name":"LED Wemos","label":"LED Wemos","tooltip":"","group":"2cf1f40e.da8e5c","order":7,"width":"3","height":"1","passthru":false,"decouple":"false","topic":"","style":"","onvalue":"An","onvalueType":"str","onicon":"","oncolor":"","offvalue":"Aus","offvalueType":"str","officon":"","offcolor":"","x":550,"y":840,"wires":[["5a140d1f.8ba264","40b3620.17a4aa"]]},{"id":"b5dddcb9.e7b7c8","type":"ui_led","z":"7e38b285.ff7dfc","group":"2cf1f40e.da8e5c","order":3,"width":0,"height":0,"label":"Eingang 1","labelPlacement":"left","labelAlignment":"center","colorForValue":[{"color":"red","value":"Aus","valueType":"str"},{"color":"green","value":"An","valueType":"str"}],"allowColorForValueInMessage":false,"name":"Eingang 1","x":770,"y":400,"wires":[]},{"id":"fd596c42.fa8338","type":"ui_led","z":"7e38b285.ff7dfc","group":"2cf1f40e.da8e5c","order":4,"width":0,"height":0,"label":"Eingang 2","labelPlacement":"left","labelAlignment":"center","colorForValue":[{"color":"red","value":"Aus","valueType":"str"},{"color":"green","value":"An","valueType":"str"}],"allowColorForValueInMessage":false,"name":"Eingang 2","x":770,"y":440,"wires":[]},{"id":"edc5a36f.b087e","type":"ui_led","z":"7e38b285.ff7dfc","group":"2cf1f40e.da8e5c","order":5,"width":0,"height":0,"label":"Eingang 3","labelPlacement":"left","labelAlignment":"center","colorForValue":[{"color":"red","value":"Aus","valueType":"str"},{"color":"green","value":"An","valueType":"str"}],"allowColorForValueInMessage":false,"name":"Eingang 3","x":770,"y":480,"wires":[]},{"id":"49ebf72f.df7138","type":"mqtt in","z":"7e38b285.ff7dfc","name":"","topic":"W1/Q2","qos":"0","datatype":"utf8","broker":"90cfe57d.ac6208","x":270,"y":440,"wires":[["fd596c42.fa8338"]]},{"id":"4607e8ad.c84408","type":"mqtt in","z":"7e38b285.ff7dfc","name":"","topic":"W1/Q3","qos":"0","datatype":"utf8","broker":"90cfe57d.ac6208","x":270,"y":480,"wires":[["edc5a36f.b087e"]]},{"id":"4c4c70f2.0b8908","type":"mqtt in","z":"7e38b285.ff7dfc","name":"","topic":"W1/ADC0","qos":"2","datatype":"utf8","broker":"90cfe57d.ac6208","x":280,"y":540,"wires":[["33d15eb.7dfcfa2","5893e466.88015c"]]},{"id":"c3cb6aad.1e883","type":"mqtt in","z":"7e38b285.ff7dfc","name":"","topic":"W1/Empfang1","qos":"2","datatype":"utf8","broker":"90cfe57d.ac6208","x":290,"y":580,"wires":[["2b4b6222.a33106"]]},{"id":"b38e836a.4203f8","type":"mqtt out","z":"7e38b285.ff7dfc","name":"","topic":"W1/Status","qos":"0","retain":"false","broker":"f5411f95.63a5e8","x":730,"y":980,"wires":[]},{"id":"62c2d048.8c9878","type":"inject","z":"7e38b285.ff7dfc","name":"","topic":"","payload":"1","payloadType":"str","repeat":"60","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":980,"wires":[["b38e836a.4203f8"]]},{"id":"33d15eb.7dfcfa2","type":"function","z":"7e38b285.ff7dfc","name":"","func":"msg.payload = \"ADC: \" + msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":540,"wires":[["ef786572.5d9bf"]]},{"id":"2b4b6222.a33106","type":"function","z":"7e38b285.ff7dfc","name":"","func":"msg.payload = \"Empfang: \" + msg.payload + \" db\";\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":580,"wires":[["ef786572.5d9bf"]]},{"id":"ef786572.5d9bf","type":"join","z":"7e38b285.ff7dfc","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":" ; ","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":670,"y":560,"wires":[["b02fd36f.4362d8"]]},{"id":"96c17c1a.30acb","type":"mqtt in","z":"7e38b285.ff7dfc","name":"","topic":"W1/A1_","qos":"2","datatype":"utf8","broker":"90cfe57d.ac6208","x":310,"y":840,"wires":[["6340cf35.bb5a6","40b3620.17a4aa"]]},{"id":"b02fd36f.4362d8","type":"ui_text","z":"7e38b285.ff7dfc","group":"2cf1f40e.da8e5c","order":2,"width":0,"height":0,"name":"","label":"Status","format":"{{msg.payload}}","layout":"row-spread","x":830,"y":560,"wires":[]},{"id":"5893e466.88015c","type":"ui_chart","z":"7e38b285.ff7dfc","name":"","group":"2cf1f40e.da8e5c","order":6,"width":0,"height":0,"label":"Spannung","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"86400","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"outputs":1,"x":610,"y":720,"wires":[["7f45af85.f7f03","b9c523a2.e194c"]]},{"id":"7f45af85.f7f03","type":"debug","z":"7e38b285.ff7dfc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1050,"y":720,"wires":[]},{"id":"b9c523a2.e194c","type":"change","z":"7e38b285.ff7dfc","name":"","rules":[{"t":"move","p":"payload[0].data","pt":"msg","to":"test","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":780,"wires":[["1b8be2c6.a1598d"]]},{"id":"1b8be2c6.a1598d","type":"debug","z":"7e38b285.ff7dfc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"test","targetType":"msg","x":1110,"y":780,"wires":[]},{"id":"3112d68c.c95752","type":"ui_button","z":"7e38b285.ff7dfc","name":"","group":"2cf1f40e.da8e5c","order":1,"width":0,"height":0,"passthru":false,"label":"Status aktualisieren","tooltip":"","color":"","bgcolor":"","icon":"","payload":"1","payloadType":"str","topic":"","x":300,"y":1020,"wires":[["b38e836a.4203f8","6b7b1142.28855"]]},{"id":"40b3620.17a4aa","type":"ui_led","z":"7e38b285.ff7dfc","group":"2cf1f40e.da8e5c","order":8,"width":"3","height":"1","label":" ","labelPlacement":"left","labelAlignment":"center","colorForValue":[{"color":"red","value":"Aus","valueType":"str"},{"color":"green","value":"An","valueType":"str"}],"allowColorForValueInMessage":false,"name":"","x":730,"y":900,"wires":[]},{"id":"6b7b1142.28855","type":"mqtt out","z":"7e38b285.ff7dfc","name":"","topic":"W2/Status","qos":"0","retain":"false","broker":"f5411f95.63a5e8","x":730,"y":1040,"wires":[]},{"id":"90cfe57d.ac6208","type":"mqtt-broker","z":"","name":"Pi","broker":"192.168.0.21","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"ab54d225.819f78","type":"mqtt-broker","z":"","name":"Wemos 1","broker":"192.168.0.21","port":"1883","clientid":"192.168.0.12","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"2cf1f40e.da8e5c","type":"ui_group","z":"","name":"MQTT","tab":"1dd12c96.4a3fd3","disp":true,"width":"6","collapse":false},{"id":"f5411f95.63a5e8","type":"mqtt-broker","z":"","name":"Wemos 2","broker":"192.168.0.21","port":"1883","clientid":"192.168.0.14","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"1dd12c96.4a3fd3","type":"ui_tab","z":"","name":"Home","icon":"track_changes","order":1,"disabled":false,"hidden":false}]
In der Arduino Software müsst ihr folgende Bibliothek verwenden: PubSubCLient von Nick O'Leary
Am Wemos ist derzeit nur ein Spannungsteiler am A0 (Analog Eingang) dran, mit dem ich die Spannung runterbreche. Die Massen von USB-Kabel (Spannungsversorgung des Wemos) und dem Ausgang des Solarladereglers sind verbunden (beides vom gleichen Gerät).
Anschlüsse:
A0 -- Mittelpunkt Spannungsteiler
Arduino Code Beispiel: