Authenticate paho mqtt client for EspMilightHub (using mosquitto)

Hi all,

I set up an esp8266 with @matt1’s EspMilightHub firmware. Now I’m trying (and for now, failing) to connect the hub to openHAB (which is running on an openhabian raspi).

First, I installed mosquitto via openhabian-config. Then, as instructed in the linked repo, I copied the jar files for the milight hub binding from here into the addons folder. This binding is based on the paho mqtt client. But now I receive a MqttSecurityException every 10 seconds in the log file, stating that I am not authorized to connect:

20:13:00.781 [ERROR] [ub.handler.EspMilightHubBridgeHandler] - Error: Could not connect to MQTT broker.{}
org.eclipse.paho.client.mqttv3.MqttSecurityException: Not authorized to connect
        at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28) ~[?:?]
        at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:1040) ~[?:?]
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:151) ~[?:?]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]

Where do I have to set which connection options? I tried the following variants:

  • stored mosquitto password for the user openhabian in the /etc/openhab2/services/mqtt.cfg file
  • stored password for the milight hub in the /etc/openhab2/services/mqtt.cfg file
  • no mqtt.cfg at all.

Nothing of the above helped.

However, from my naive point of view, it looks as if the broker is running. At least I could do mosquitto_pub in one shell and receive the message from a second shell with mosquitto_sub …

It would be great if you could help me!
+++ newbie disclaimer: please forgive me any stupid questions :slight_smile: +++
(Question was asked here before.)

Best,
Christoph

Although I’m not using this binding it would be of interest how you did setup your bridge and thing!

Hi opus, you can find a detailed explanation in the repo. I carefully tried not to deviate from these instructions.

Do you speak of a thing in terms of openHAB? I did not yet create a thing file for the milight devices - first, I should get mqtt running, shouldn’t I?

The ESPMilightHub binding runs it’s own MQTT client. You don’t need to install the MQTT binding to run it.

These are all for MQTT V1

After adding the ESPMilightHUB biding, you need to add a ESPMilightHub Bridge thing
In the configutation for the thing you need to add the MQTT broker address and password.

Did you install an MQTT broker? If yes, which one, I recommend Mosquitto.
If not, do it.

2 Likes

@vzorglub, thank you very much!

After adding the ESPMilightHUB biding, you need to add a ESPMilightHub Bridge thing
In the configutation for the thing you need to add the MQTT broker address and password.

I did not realize I had to do this before using the broker. Makes sense …
Now I added the EspMilightHub and thing and entered user:password. And the errors in the log have disappeared! :smiley:

Now I need to connect my milight remote (that’s why I set up the hub). Unfortunately, running mosquitto_sub -u openhabian -P mymosquittopassword -p 1883 -v -t 'milight/#' does not show any messages when I press a key at the remote. The milight hub web interface, however, logs each keypress when sniffing is activated.
Do I first need to add a thing for the remote? How could I find out its device ID?

The ESPMilightHub binding runs it’s own MQTT client. You don’t need to install the MQTT binding to run it.

I did not install the MQTT binding. That is, I had installed it by accident but then uninstalled it again.

Did you install an MQTT broker? If yes, which one, I recommend Mosquitto.

Mosquitto is installed.

1 Like

That sounds to me as if you should start to read Here.

1 Like

Thank you again. This is what I wrote now:

Bridge espmilighthub:esp8266Bridge:Auto001 [ADDR="tcp://localhost:1883", MQTT_USERNAME="openhabian", MQTT_PASSWORD="mymosquittopassword", 1TRIGGERS_NIGHT_MODE=true, DELAY_BETWEEN_MQTT=40, POWERFAILS_TO_MINDIM=true, AUTOCTEMP_MAXDIMMED_TEMPERATURE=350, FAVOURITE_WHITE=200, HUB_IP="192.168.178.67"]
{
	Thing fut089 0x24FB2 "Remote"
}

And it looks like this:
image

But still, mosquitto_sub does not list any messages when I use the remote.

Is fut089 actually the right thingTypeId? I did not buy any milight bulbs, only the remote.
Is 0x24FB2 actually the right deviceId? This is an example package sniffed from the bridge:

fut089 packet received (9 bytes):
Raw packet: 55 5D 08 3F 1D FB 76 1D 03 

Decoded:
Key      : 55
b1       : 25
ID       : 24FB
Command  : 01
Argument : 02
Sequence : 85
Group    : 02
Checksum : EB

The problem is, my remote can control eight groups and there are also some master keys that yield the group 00. Do I need to create nine separate things? And neither 0x24FB2 nor 0x24FB02 nor 0x24FB gave me any hits in mosquitto_sub

I’m sorry, but now it’s getting deep into the specialities of mylight and this binding. Without having such equipment I can’t help any further.

Check the EspMilightHub thread. This long one.
I have never used remotes myself so can’t help here but there was plenty of advice on the main thread.

1 Like

Wow, that linked thread is really long (estimated reading time: 2 hours) :astonished: But my use case appears to be quite rare: Setting up the milight remote without possessing any milight bulb at all …
However, Google, try & error helped me to figure out the following:

I needed to fill in the following fields in the milight hub’s MQTT settings:

  • MQTT server - openhab (that’s my raspi)
  • MQTT update topic pattern - milight/updates/:device_id/:device_type/:group_id
  • MQTT username and password

Now mosquitto_sub -u openhabian -P mymosquittopassword -p 1883 -v -t 'milight/#' shows a positive output when I press a key on the remote!

milight/updates/0x24FB/fut089/7 {"state":"ON"}
milight/states/0x24FB/fut089/7 {"state":"ON","level":67,"bulb_mode":"white"}

Next problem is to connect these MQTT updates to openHAB items. I added items for level and color of the remote via PaperUI, but they don’t refresh when I use the remote. There are also no matching log items. Any idea what I could be missing now or where to start searching for an error?

Yes post your channel set-up please and your logs, thanks

Items file:

Dimmer Remote_C1_Level "C Remote Level" { channel="espmilighthub:fut089:Auto001:0x24FB:level" }
Color Remote_C1_Color "C Remote Color" { channel="espmilighthub:fut089:Auto001:0x24FB:colour" }

Things file see above

log:tail in the Karaf console does not give me any relevant log item (the only kind of lines appearing is 18:53:48.402 [INFO ] [smarthome.event.ItemStateChangedEvent] - Sensor_OpenHabPi_Temperature changed from 61.8 to 62.8).

Then , again, post you Thing and channels please

Things

Bridge espmilighthub:esp8266Bridge:Auto001 [ADDR="tcp://localhost:1883", MQTT_USERNAME="openhabian", MQTT_PASSWORD="mymosquittopassword", 1TRIGGERS_NIGHT_MODE=true, DELAY_BETWEEN_MQTT=40, POWERFAILS_TO_MINDIM=true, AUTOCTEMP_MAXDIMMED_TEMPERATURE=350, FAVOURITE_WHITE=200, HUB_IP="192.168.178.67"]
{
	Thing fut089 0x24FB "Remote"
}

Anything else required for the channels?

What cahnnels are available with the remoter thing?

Hi,

so here is the solution I worked out until now. I installed the regular MQTT binding and parse the MQTT messages manually.

Things:

Bridge mqtt:broker:mosquitto [
	host="localhost", secure=false,
	username="openhabian", 
	password="mymosquittopassword"
] {
	Thing topic Remote_C1 "C-Remote" {
	Channels:
		Type string: master "C-Remote Master" [ stateTopic="milight/updates/0x24FB/fut089/0" ]
	}
}

Set up mqtt.cfg.

Items:

String Remote_C1_Master "Remote-C Master [%s]" { channel="mqtt:topic:mosquitto:Remote_C1:master" }

Rules:

rule "Remote_C1_Generic"
when
	Item Remote_C1_Master changed
then
	val String json = (Remote_C1_Master.state as StringType).toString
	{
		val sBrightness = transform("JSONPATH", "$.brightness", json)
		if (sBrightness != json) {
			val Number brightness = Math.round(Integer::parseInt(sBrightness) / 255.0 * 100)
			Strip_C_CouchBackground_Dimmer.sendCommand(brightness)
		}
	}
	{
		val sHue = transform("JSONPATH", "$.hue", json)
		if (sHue != json) {
			val hue = new DecimalType(Integer::parseInt(sHue))
			var color = Strip_C_CouchBackground_Color.state as HSBType
			color = new HSBType(hue, color.saturation, color.brightness)
			Strip_C_CouchBackground_Color.sendCommand(color)
		}
	}
	{
		val sSaturation = transform("JSONPATH", "$.saturation", json)
		if (sSaturation != json) {
			val saturation = new PercentType(Integer::parseInt(sSaturation))
			var color = Strip_C_CouchBackground_Color.state as HSBType
			color = new HSBType(color.hue, saturation, color.brightness)
			Strip_C_CouchBackground_Color.sendCommand(color)
		}
	}
	{
		val sState = transform("JSONPATH", "$.state", json)
		if (sState != json) {
			var state = if (sState == "OFF") OFF else ON
			Strip_C_CouchBackground_Color.sendCommand(state)
		}
	}
end

To me, this feels very hacky, but at least it works. Any hints how to improve code quality or design are highly welcome :slight_smile:

1 Like