0
votes

I'm new to the PHP world, facing problem while connecting to MQTT.

I'm using the phpMQTT.php library, and I'm using the IP address to connect to the MQTT broker. I'm trying to publish to MQTT broker, getting error in phpMQTT.php library file

The error is: stream_socket_client(): unable to connect to tcp://...*:8083 (Connection timed out)

facing problem in below code:

if ($this->cafile) {
            $socketContext = stream_context_create(["ssl" => [
                "verify_peer_name" => true,
                "cafile" => $this->cafile
                ]]);
            $this->socket = stream_socket_client("tls://" . $this->address . ":" . $this->port, $errno, $errstr, 60, STREAM_CLIENT_CONNECT, $socketContext);
        } else {
            $this->socket = stream_socket_client("tcp://" . $this->address . ":" . $this->port, $errno, $errstr, 60, STREAM_CLIENT_CONNECT);
        }
1
Please do not just create a new user to re-ask a closed question. Just adding the error without the code you used doesn't really help us get to the root cause. Please read the doc about what a good question needhardillb

1 Answers

0
votes

From the phpMQTT.php library
( source at https://github.com/bluerhinos/phpMQTT/blob/master/phpMQTT.php ) ,
you have to set the following details as shown in the source code.

/* sets the broker details */  

function broker($address, $port, $clientid, $cafile = NULL){
    $this->address = $address;
    $this->port = $port;
    $this->clientid = $clientid;
    $this->cafile = $cafile;
}

If you have a firewall running - do open the port that you are using as well.