MQTT Setup - some beginner's questions

Hi there,
I am about to setup a lab environment for openhab learning, development and staging. The configuration consists of 3 Raspberry Pi3B+ with Raspbian Stretch.

RPi1: General Purpose System
RPi2: Print server and Mosquitto-Broker
RPi3: Openhab 2.4

Now I struggle to implement a simple switch, that shall be switched by MQTT-messages. Some help would be appreciated.

I’ve already installed Mosquitto on all three Raspis. I’ve tested the Broker functionality of RPi2 successfully: Messages can be published by RPi1 to Broker RPi2 and can be subsribed by RPi3 from Broker RPi2. Everything works neatly.

Now it comes to Openhab:

I’ve installed the MQTT binding and created a thing…

Bridge mqtt:broker:Pi2MqttBroker [ host="192.168.12.1", secure=false ]
{
	Thing topic W88Lab {
	Channels:
		Type switch : MqSw1 "Mqtt Lab Switch 1" [ stateTopic="MqSw1/enabled", commandTopic="MqSw1/set", on="ON", off="OFF"]
	}
}
...

and created a corresponding item:

```csv
Switch W88LabMqSw1 "MQTT Lab Sw 1" { channel="mqtt:topic:Pi2MqttBroker:W88Lab:MqSw1" }

Furthermore I’ve diplayed this switch in a sitemap:

sitemap W88Lab label="SmartW88 Labor"
{
	Frame label="MQTT Labor"{
		Switch item=W88LabMqSw1 label="W88LabMqSw1"
	}
}

The openhab.log shows, that thing and item are installed and the MQTT bridge has established a client session at the broker (the broker protocoll confirmed that). In the Papier-UI the things are online - everything looks fine, so far.

My expectation was, that openhab now is listening on inbound MQTT-Messages and I can controll my new MQTT-Switch from RPi1 by pubishing:

mosquitto_pub -h 192.168.12.1 -t W88Lab/MqSw1/set -m ON

Unfortuately nada. The switch in my sitemap remains OFF.

Where I am wrong?

stateTopic="MqSw1/enabled"
mosquitto_pub -h 192.168.12.1 -t W88Lab/MqSw1/set -m ON

See the difference in topic?

Problem is solved. A misunderstanding of the MQTT generic switch

Can you tick the solution, please?

Why did you install Mosquitto on all three? You only need the one broker on RPi2.

Honestly, I just gave it up to declare the MQTT stuff in things and items files. I’ve done that in Papier-UI, of course with proper topics (W88Lab/MqSw1/state and W88Lab/MqSw1/set). I am not happy with that, yet, because I like much more the concept of having all the stuff in a file. But for now I’ve got the job done - or almost done, because there is one issue I don’t understand:

I defined a switch item from the channel with the two topics:

State Topic: W88Lab/MqSw1/state
Command Topic: W88Lab/MqSw1/set

  • As supposed, if I publish -t W88Lab/MqSw1/state - m ON / OFF, the switch changes its state.
  • If I toggle the switch in Paper-UI (Command), Openhab publishes -t W88Lab/MqSw1/set -m ON / OFF.

I expected, that the command topic will also be published, when I change the state by MQTT. But it doesn’t.

That’s right, but I wanted to have a full fledged mosquitto test bed, where sender, broker and receiver reside on different systems.

You don’t need a broker on the sender nor the receiver. Both pub/sub communicate with one central broker and the broker is almost never on the same machine as the clients.

This is true.

By “install mosquitto” did you mean that you set up and configured the daemon, or just that you installed the OS packages so you would have the tools installed? Having the tools is handy but having extra daemons running can be confusing.

A new state received on stateTopic will only update the state. If you want to cascade it as a command you need to toggle the “is command” property in the paperUI

I’ve already tried that without success.

Excuse me for being unprecise. I’ve installed the mosquitto package on all three servers but I actually use RPi1 and RPi3 as mosquitto clients while RPi2 is configured as broker (runs as demon). And of course I use the -h option in every publish or subscribe command in order to not getting confused.