Hello, i’m sending data from an ESP32 to HiveMQ cloud and it works perfectly. Now i need to get those data and put them in a sql database connected to my web app.
I’m trying to do that using a php library i found called phpMQTT.php, but i can’t connect to the broker.
I tried ports 8883 and 1883 but don’t work.
Here is the code i’m using:
<?php
require('phpMQTT.php');
$server = 'xxx.s2.eu.hivemq.cloud'; // change if necessary
$port = 8883; // change if necessary
$username = 'xxx'; // set your username
$password = 'xxx'; // set your password
$client_id = uniqid('mqtt_client_');
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);
if ($mqtt->connect(true, NULL, $username, $password)) {
$mqtt->publish('esp32/connection_count', 'Hello World! at ' . date('r'), 0, false);
$mqtt->close();
} else {
echo "Time out!\n";
}
Thanks
Check out my code sample below of how I put this phpMQTT library to work with HiveMQ Cloud.
Example: publish.php
<?php
require('../phpMQTT.php');
$server = 'TYPE_YOUR_HIVEMQ_CLUSTER_URL'; // change if necessary
$port = 8883; // change if necessary
$username = 'TYPE_YOUR_USERNAME'; // set your username
$password = 'TYPE_YOUR_PASSWORD'; // set your password
$client_id = 'phpMQTT-publisher'; // make sure this is unique for connecting to sever - you could use uniqid()
$cafile = 'isrgrootx1.pem'; // HiveMQ Cloud CA
$mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id, $cafile);
if ($mqtt->connect(true, NULL, $username, $password)) {
$mqtt->publish('bluerhinos/phpMQTT/examples/publishtest', 'Hello World! at ' . date('r'), 0, false);
$mqtt->close();
} else {
echo "Time out!\n";
}