host = config('mqtt.host'); $this->username = config('mqtt.username'); $this->password = config('mqtt.password'); $this->cert_file = config('mqtt.certfile'); $this->port = config('mqtt.port'); $this->debug = config('mqtt.debug'); } public function ConnectAndPublish($topic, $msg, $client_id=null) { $id = empty($client_id) ? rand(0,999) : $client_id; $client = new MqttService($this->host,$this->port, $id, $this->cert_file, $this->debug); if ($client->connect(true, null, $this->username, $this->password)) { $client->publish($topic,$msg); $client->close(); return true; } return false; } public function ConnectAndSubscribe($topic, $proc, $client_id=null) { $id = empty($client_id) ? rand(0,999) : $client_id; $client = new MqttService($this->host,$this->port,$id, $this->cert_file, $this->debug); if ($client->connect(true, null, $this->username, $this->password)) { $topics[$topic] = array("qos" => 0, "function" => $proc); $client->subscribe($topics, 0); while($client->proc()) { } $client->close(); } return false; } }