Skip to content

Instantly share code, notes, and snippets.

@froemken
Created December 13, 2018 23:39
Show Gist options
  • Save froemken/b0e665a7ddbc42a97c028f46ef5aa72e to your computer and use it in GitHub Desktop.
Save froemken/b0e665a7ddbc42a97c028f46ef5aa72e to your computer and use it in GitHub Desktop.
With this script you may see, why your Dyson Pure Hot and Cool will not connect with MQTT. My is not 3.1.1 compatible
<?php
$errNo = 0;
$errMsg = '';
// throws WARNING if hostname is invalid. Add @.
if ($res = @fsockopen($ipAddress, 1883, $errNo, $errMsg, 10)) {
//var_dump(chr(63>>8));
//var_dump(bin2hex('11110000'));
$packetType = '1';
$flag = '0';
$remainingChars = '00';
$fixedHeader = $packetType . $flag . $remainingChars;
// variable header 10 bytes
$msb = '00';
$lsb = '04';
$mByte = dechex(ord('M'));
$qByte = dechex(ord('Q'));
$tByte = dechex(ord('T')); // twice
$protocollLevel = '04'; // Always 4
$useUsername = '0';
$usePassword = '0';
$willRetain = '0';
$willQos = '00'; // 0, 1, 2, but never 3
$willFlag = '0'; // if 0 QoS and Retain must be 0, too
$cleanSession = '1'; // 0 = store session for reconnect; 1 = always create new connection
$keepAliveMsb = '00'; // 0 = deactivate
$keepAliveLsb = '00'; // 0 = deactivate
$connectFlag = dechex(bindec(
$useUsername . $usePassword . $willRetain . $willQos . $willFlag . $cleanSession . '0'
));
$variableHeader = $msb . $lsb . $mByte . $qByte . $tByte . $tByte . $protocollLevel . $connectFlag . $keepAliveMsb . $keepAliveLsb;
$client = [];
$client[] = dechex(ord('s'));
$client[] = dechex(ord('f'));
$client[] = dechex(ord('1'));
$client[] = dechex(ord('7'));
$client[] = dechex(ord('0'));
$client[] = dechex(ord('1'));
$client[] = dechex(ord('7'));
$client[] = dechex(ord('9'));
$clientId = implode('', $client);
$payLoad = dechex(count($client)) . $clientId;
fwrite($res, hex2bin($fixedHeader . $variableHeader . $payLoad));
//$disconnect = hex2bin('F0');
//fwrite($res, $disconnect);
while (!feof($res)) {
$result = fgets($res, 1024);
$splitResult = str_split(bin2hex($result));
if (
$splitResult[0] === dechex(2)
&& $splitResult[1] === dechex(0)
&& $splitResult[2] . $splitResult[3] === '02'
) {
var_dump('CONNACK received');
} else {
var_dump('Disconnected');
}
if ($splitResult[4] . $splitResult[5] === '00') {
var_dump('No Session');
} else {
var_dump('Session created');
}
switch ($splitResult[6] . $splitResult[7]) {
case '00':
var_dump('Connection accepted');
break;
case '01':
var_dump('The Server does not support the level of the MQTT protocol requested by the Client');
break;
case '02':
var_dump('The Client identifier is correct UTF-8 but not allowed by the Server');
break;
case '03':
var_dump('The Network Connection has been made but the MQTT service is unavailable');
break;
case '04':
var_dump('The data in the user name or password is malformed');
break;
case '05':
var_dump('The Client is not authorized to connect');
break;
}
printf('%b', $result);
}
fclose($res);
} else {
var_dump($errNo);
var_dump($errMsg);
}
die();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment