Download en installeer Mosquitto op je Raspberry PI:
Commando regels:
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
Voor de Wheezy versie van raspbian voer je uit:
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
Of voor de Jessie versie van Raspbian:
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
Of voor de Stretch versie van Raspbian:
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
sudo -i
apt-get update
Om te zien welke ‘mosquitto pakketten we kunnen installeren voer je uit:
apt-cache search mosquitto
En je installeerd mosquitto nu met:
apt-get install mosquitto
link to the arduino code: https://create.arduino.cc/editor/LogMaker360/8e970f25-fb19-4b9a-8045-9ab864e3ec95/preview
Python code is in the arduino sketch.
command line to see all the published topics and text:
mosquitto_sub -v -t "#"
to see a specific topic like “ledStatus”:
mosquitto_sub -h 192.168.0.106 -i testLED -t ledStatus
//repace the ip address with your broker IP.
to publish a new message to a topic (ledStatus):
mosquitto_pub -h 192.168.0.106 -t ledStatus -m "0"
// replace IP with your broker IP
install the python mqtt library paho-mqtt on Raspberry pi:
pip install paho-mqtt
Link to install mosquitto on Windows: https://sivatechworld.wordpress.com/2015/06/11/step-by-step-installing-and-configuring-mosquitto-with-windows-7/
— UPDATE VOOR RASPBIAN STRETCH —
1. Verwijder Init.d referenties welke zijn aangemakt bij de Mosquitto installatie, we stoppen de service met:
sudo /etc/init.d/mosquitto stop
En verwijderen deze met:
sudo update-rc.d mosquitto remove
2. We maken nu eerst een wachtwoordbestand aan:
sudo mosquitto_passwd -c /etc/mosquitto/passwd
Geef het te gebruiken wachtwoord op de prompt
dat mag mosquitto zijn
En we hebben een database nodig. Maak de map waar de databasebestanden worden opgeslagen, en zorg dat mosquitto eigenaar is:
mkdir /var/lib/mosquitto/Sudo chown mosquitto:mosquitto /var/lib/mosquitto/ -R
Nu maken we de Mosquitto configuratie in /etc/mosquitto/mosquitto.conf
Zet de volgende regels in je mosquitto.conf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Place your local configuration in /etc/mosquitto/ # # A full description of the configuration file is at /usr/share/doc/mosquitto/examples/mosquitto.conf.example # Config file for mosquitto # # See mosquitto.conf(5) for more information. pid_file /run/mosquitto.pid user mosquitto max_queued_messages 200 message_size_limit 0 allow_zero_length_clientid true allow_duplicate_messages false listener 1883 autosave_interval 900 autosave_on_changes false persistence true persistence_location /var/lib/mosquitto/ persistence_file mosquitto.db allow_anonymous true password_file /etc/mosquitto/passwd |
En ve voeren uit in de terminal: mosquitto -c /etc/mosquitto/mosquitto.conf
Dit start Mosquitto, als alles goed is gegaan.
In een nieuwe terminal voeren we uit: netstat -at
Een werkende Mosquitto is nu zichtbaar op de PI en luistert naar poort 1883.
3. Nu creeren we de ‘systemd service’ om Mosquitto automatisch met de PI mee op te starten
We voeren uit: sudo Nano /etc/systemd/system/mosquitto.service
En voeg daar deze regels toe.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[Unit] Description=Mosquitto MQTT Broker daemon ConditionPathExists=/etc/mosquitto/mosquitto.conf After=network.target Requires=network.target [Service] Type=forking RemainAfterExit=no StartLimitInterval=0 PIDFile=/run/mosquitto.pid ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d ExecReload=/bin/kill -HUP $MAINPID Restart=on-failure RestartSec=2 [Install] WantedBy=multi-user.target |
Nu enablen we de service, starten deze en kijken naar de status:
Voer uit:
sudo systemctl enable mosquitto.service
sudo systemctl start mosquitto.service
sudo systemctl status mosquitto.service -l
Als alles is goed gegaan zien we nu een groene Mosquitto service.
Opmerking: De config en service definition zijn interlinked via het path van de PID file.
Pingback:Joan
Pingback:colani