php-mqtt can't authenticate with MQTT Broker running in HomeAssistant
Dewieinns opened this issue · 2 comments
I am having issues getting php-mqtt to authenticate with the MQTT broker running within HomeAssistant. I have several ESP8622 devices successfully publishing data to my MQTT instance running in HA using authentication. I am attempting to publish data using php-mqtt and receiving an error messages in the MQTT Broker log about receiving null for the username or password:
2023-02-26 08:47:15: New connection from 192.168.1.130:56734 on port 1883.
error: received null username or password for unpwd check
2023-02-26 08:47:15: Client <unknown> disconnected, not authorised.
I have identical credentials specified for the php-mqtt script as used for the ESP8622 devices, it's not a credentials problem (but maybe php-mqtt isn't sending them properly?) The bare-bones script I am trying to debug with is as follows, you can see I specify the MQTT version, I've tried 3.1 and 3.1.1
<?php
require('../vendor/autoload.php');
use \PhpMqtt\Client\MqttClient;
use \PhpMqtt\Client\ConnectionSettings;
$server = 'ADDRESS_IS_HERE';
$port = 1883;
$clientId = 'CLIENT_ID_IS_HERE';
$username = 'USERNAME_IS_HERE';
$password = 'PASSWORD_IS_HERE';
$clean_session = false;
$mqtt_version = MqttClient::MQTT_3_1_1;
$connectionSettings = new ConnectionSettings();
$connectionSettings
->setUsername($username)
->setPassword($password);
$mqtt = new MqttClient($server, $port, $clientId, $mqtt_version);
$mqtt->connect($connectionSettings, $clean_session);
printf("client connected\n");
for ($i = 0; $i< 10; $i++) {
$payload = array(
'protocol' => 'tcp',
'date' => date('Y-m-d H:i:s'),
'url' => 'https://github.com/emqx/MQTT-Client-Examples'
);
$mqtt->publish(
// topic
'emqx/test',
// payload
json_encode($payload),
// qos
0,
// retain
true
);
printf("msg $i send\n");
sleep(1);
}
?>
Looking at the apache error logs where the php script lives it tells the same story:
Uncaught PhpMqtt\\Client\\Exceptions\\ConnectingToBrokerFailedException: [6] Establishing a connection to the MQTT broker failed: The configured broker responded with unauthorized. in redacted/vendor/php-mqtt/client/src/MessageProcessors/Mqtt31MessageProcessor.php:252
Stack trace:
#0 redacted/vendor/php-mqtt/client/src/MqttClient.php(380): PhpMqtt\\Client\\MessageProcessors\\Mqtt31MessageProcessor->handleConnectAcknowledgement()
#1 redacted/vendor/php-mqtt/client/src/MqttClient.php(159): PhpMqtt\\Client\\MqttClient->performConnectionHandshake()
#2 redacted/vendor/php-mqtt/client/src/MqttClient.php(144): PhpMqtt\\Client\\MqttClient->connectInternal()
#3 redacted.php(23): PhpMqtt\\Client\\MqttClient->connect()
#4 {main}\n thrown in redacted/vendor/php-mqtt/client/src/MessageProcessors/Mqtt31MessageProcessor.php on line 252
I have also asked for help over at the HomeAssistant community where another user seemed to be having the same problem:
https://community.home-assistant.io/t/login-to-mosquitto-broker-fails-after-last-update/419446/90
Well as follow-up... I've managed to figure this out myself. Hopefully the following helps someone else.
The tutorials I was following set up the Connectionsettings as such:
$connectionSettings = new ConnectionSettings();
$connectionSettings
->setUsername($username)
->setPassword($password);
however this wasn't working. In looking at official examples I noticed they were being setup like so:
$connectionSettings = (new ConnectionSettings)
->setUsername($username)
->setPassword($password);
which works perfectly...
Yep, the ConnectionSettings
use a fluent interface and are immutable, which means each call returns a new instance of the settings. This is necessary to avoid that the settings are changed once the connection has been initialized.
Reassigning the variable would have worked as well though:
$connectionSettings = new ConnectionSettings();
$connectionSettings = $connectionSettings
->setUsername($username)
->setPassword($password);
Glad you figured it out though, might be helpful for others in the future as well! 👍