Compare commits
No commits in common. "master" and "v1.0.0" have entirely different histories.
22
Readme.md
22
Readme.md
|
|
@ -1,11 +1,9 @@
|
||||||
# Laravel MQTT Package
|
# Laravel MQTT Package
|
||||||
|
|
||||||
A simple Laravel 5 and 6 Library to connect/publish/subscribe to MQTT broker
|
A simple Laravel 5 Library to connect/publish to MQTT broker
|
||||||
|
|
||||||
Based on [bluerhinos/phpMQTT](https://github.com/bluerhinos/phpMQTT)
|
Based on [bluerhinos/phpMQTT](https://github.com/bluerhinos/phpMQTT)
|
||||||
|
|
||||||
For Example see this [repo](https://github.com/salmanzafar949/Laravel-Mqtt-Example)
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
```
|
```
|
||||||
composer require salmanzafar/laravel-mqtt
|
composer require salmanzafar/laravel-mqtt
|
||||||
|
|
@ -15,12 +13,8 @@ composer require salmanzafar/laravel-mqtt
|
||||||
* Name and Password Authentication
|
* Name and Password Authentication
|
||||||
* Certificate Protection for end to end encryption
|
* Certificate Protection for end to end encryption
|
||||||
* Enable Debug mode to make it easier for debugging
|
* Enable Debug mode to make it easier for debugging
|
||||||
* Now you can also set Client_id of your choice and if you don't want just simply don't use or set it to null
|
|
||||||
* Set QOS flag directly from config file
|
|
||||||
* Set Retain flag directly from config file
|
|
||||||
|
|
||||||
## Enable the package (Optional)
|
## Enable the package (Optional)
|
||||||
|
|
||||||
This package implements Laravel auto-discovery feature. After you install it the package provider and facade are added automatically for laravel >= 5.5.
|
This package implements Laravel auto-discovery feature. After you install it the package provider and facade are added automatically for laravel >= 5.5.
|
||||||
|
|
||||||
__This step is only required if you are using laravel version <5.5__
|
__This step is only required if you are using laravel version <5.5__
|
||||||
|
|
@ -53,8 +47,6 @@ php artisan vendor:publish --provider="Salman\Mqtt\MqttServiceProvider"
|
||||||
'certfile' => env('mqtt_cert_file',''),
|
'certfile' => env('mqtt_cert_file',''),
|
||||||
'port' => env('mqtt_port','1883'),
|
'port' => env('mqtt_port','1883'),
|
||||||
'debug' => env('mqtt_debug',false) //Optional Parameter to enable debugging set it to True
|
'debug' => env('mqtt_debug',false) //Optional Parameter to enable debugging set it to True
|
||||||
'qos' => env('mqtt_qos', 0), // set quality of service here
|
|
||||||
'retain' => env('mqtt_retain', 0) // it should be 0 or 1 Whether the message should be retained.- Retain Flag
|
|
||||||
```
|
```
|
||||||
#### Publishing topic
|
#### Publishing topic
|
||||||
|
|
||||||
|
|
@ -64,8 +56,7 @@ use Salman\Mqtt\MqttClass\Mqtt;
|
||||||
public function SendMsgViaMqtt($topic, $message)
|
public function SendMsgViaMqtt($topic, $message)
|
||||||
{
|
{
|
||||||
$mqtt = new Mqtt();
|
$mqtt = new Mqtt();
|
||||||
$client_id = Auth::user()->id;
|
$output = $mqtt->ConnectAndPublish($topic, $message);
|
||||||
$output = $mqtt->ConnectAndPublish($topic, $message, $client_id);
|
|
||||||
|
|
||||||
if ($output === true)
|
if ($output === true)
|
||||||
{
|
{
|
||||||
|
|
@ -82,9 +73,7 @@ use Mqtt;
|
||||||
|
|
||||||
public function SendMsgViaMqtt($topic, $message)
|
public function SendMsgViaMqtt($topic, $message)
|
||||||
{
|
{
|
||||||
$client_id = Auth::user()->id;
|
$output = Mqtt::ConnectAndPublish($topic, $message);
|
||||||
|
|
||||||
$output = Mqtt::ConnectAndPublish($topic, $message, $client_id);
|
|
||||||
|
|
||||||
if ($output === true)
|
if ($output === true)
|
||||||
{
|
{
|
||||||
|
|
@ -103,12 +92,11 @@ use Salman\Mqtt\MqttClass\Mqtt;
|
||||||
public function SubscribetoTopic($topic)
|
public function SubscribetoTopic($topic)
|
||||||
{
|
{
|
||||||
$mqtt = new Mqtt();
|
$mqtt = new Mqtt();
|
||||||
$client_id = Auth::user()->id;
|
|
||||||
$mqtt->ConnectAndSubscribe($topic, function($topic, $msg){
|
$mqtt->ConnectAndSubscribe($topic, function($topic, $msg){
|
||||||
echo "Msg Received: \n";
|
echo "Msg Received: \n";
|
||||||
echo "Topic: {$topic}\n\n";
|
echo "Topic: {$topic}\n\n";
|
||||||
echo "\t$msg\n\n";
|
echo "\t$msg\n\n";
|
||||||
}, $client_id);
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -124,7 +112,7 @@ public function SubscribetoTopic($topic)
|
||||||
echo "Msg Received: \n";
|
echo "Msg Received: \n";
|
||||||
echo "Topic: {$topic}\n\n";
|
echo "Topic: {$topic}\n\n";
|
||||||
echo "\t$msg\n\n";
|
echo "\t$msg\n\n";
|
||||||
},$client_id);
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "salmanzafar-chenc/laravel-mqtt",
|
"name": "salmanzafar/laravel-mqtt",
|
||||||
"description": "A simple Laravel 5 and 6 Library to connect/publish/subscribe to MQTT broker",
|
"description": "A simple Laravel 5 Library to connect/publish to MQTT broker",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"authors": [
|
"authors": [
|
||||||
|
|
|
||||||
|
|
@ -40,8 +40,6 @@ class Mqtt
|
||||||
protected $password = null;
|
protected $password = null;
|
||||||
protected $port = null;
|
protected $port = null;
|
||||||
protected $debug = null;
|
protected $debug = null;
|
||||||
protected $qos = 0;
|
|
||||||
protected $retain = 0;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
|
@ -51,21 +49,17 @@ class Mqtt
|
||||||
$this->cert_file = config('mqtt.certfile');
|
$this->cert_file = config('mqtt.certfile');
|
||||||
$this->port = config('mqtt.port');
|
$this->port = config('mqtt.port');
|
||||||
$this->debug = config('mqtt.debug');
|
$this->debug = config('mqtt.debug');
|
||||||
$this->qos = config('mqtt.qos');
|
|
||||||
$this->retain = config('mqtt.retain');
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function ConnectAndPublish($topic, $msg, $client_id=null)
|
public function ConnectAndPublish($topic, $msg)
|
||||||
{
|
{
|
||||||
$id = empty($client_id) ? rand(0,999) : $client_id;
|
$client = new MqttService($this->host,$this->port, rand(0,100), $this->cert_file, $this->debug);
|
||||||
|
|
||||||
$client = new MqttService($this->host,$this->port, $id, $this->cert_file, $this->debug);
|
|
||||||
|
|
||||||
if ($client->connect(true, null, $this->username, $this->password))
|
if ($client->connect(true, null, $this->username, $this->password))
|
||||||
{
|
{
|
||||||
$client->publish($topic,$msg, $this->qos, $this->retain);
|
$client->publish($topic,$msg);
|
||||||
$client->close();
|
$client->close();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -75,17 +69,15 @@ class Mqtt
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ConnectAndSubscribe($topic, $proc, $client_id=null)
|
public function ConnectAndSubscribe($topic, $proc)
|
||||||
{
|
{
|
||||||
$id = empty($client_id) ? rand(0,999) : $client_id;
|
$client = new MqttService($this->host,$this->port, rand(0,100), $this->cert_file, $this->debug);
|
||||||
|
|
||||||
$client = new MqttService($this->host,$this->port,$id, $this->cert_file, $this->debug);
|
|
||||||
|
|
||||||
if ($client->connect(true, null, $this->username, $this->password))
|
if ($client->connect(true, null, $this->username, $this->password))
|
||||||
{
|
{
|
||||||
$topics[$topic] = array("qos" => 0, "function" => $proc);
|
$topics[$topic] = array("qos" => 0, "function" => $proc);
|
||||||
|
|
||||||
$client->subscribe($topics, $this->qos);
|
$client->subscribe($topics, 0);
|
||||||
|
|
||||||
while($client->proc())
|
while($client->proc())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -9,22 +9,21 @@
|
||||||
namespace Salman\Mqtt\MqttClass;
|
namespace Salman\Mqtt\MqttClass;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
A simple php class to connect/publish/Subscribe to an MQTT broker
|
A simple php class to connect/publish to an MQTT broker
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* phpMQTT */
|
/* phpMQTT */
|
||||||
|
|
||||||
class MqttService
|
class MqttService
|
||||||
{
|
{
|
||||||
private $socket; /* holds the socket */
|
private $socket; /* holds the socket */
|
||||||
private $msgid = 1; /* counter for message id */
|
private $msgid = 1; /* counter for message id */
|
||||||
public $keepalive = 10; /* default keepalive timer */
|
public $keepalive = 10; /* default keepalive timmer */
|
||||||
public $timesinceping; /* host unix time, used to detect disconnects */
|
public $timesinceping; /* host unix time, used to detect disconects */
|
||||||
public $topics = array(); /* used to store currently subscribed topics */
|
public $topics = array(); /* used to store currently subscribed topics */
|
||||||
public $debug = false; /* should output debug messages */
|
public $debug = false; /* should output debug messages */
|
||||||
public $address; /* broker address */
|
public $address; /* broker address */
|
||||||
public $port; /* broker port */
|
public $port; /* broker port */
|
||||||
public $clientid; /* client id sent to broker */
|
public $clientid; /* client id sent to brocker */
|
||||||
public $will; /* stores the will of the client */
|
public $will; /* stores the will of the client */
|
||||||
private $username; /* stores username */
|
private $username; /* stores username */
|
||||||
private $password; /* stores password */
|
private $password; /* stores password */
|
||||||
|
|
@ -34,7 +33,6 @@ class MqttService
|
||||||
$this->debug = $debug;
|
$this->debug = $debug;
|
||||||
$this->broker($address, $port, $clientid, $cafile);
|
$this->broker($address, $port, $clientid, $cafile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* sets the broker details */
|
/* sets the broker details */
|
||||||
function broker($address, $port, $clientid, $cafile = NULL){
|
function broker($address, $port, $clientid, $cafile = NULL){
|
||||||
$this->address = $address;
|
$this->address = $address;
|
||||||
|
|
@ -42,14 +40,12 @@ class MqttService
|
||||||
$this->clientid = $clientid;
|
$this->clientid = $clientid;
|
||||||
$this->cafile = $cafile;
|
$this->cafile = $cafile;
|
||||||
}
|
}
|
||||||
|
|
||||||
function connect_auto($clean = true, $will = NULL, $username = NULL, $password = NULL){
|
function connect_auto($clean = true, $will = NULL, $username = NULL, $password = NULL){
|
||||||
while($this->connect($clean, $will, $username, $password)==false){
|
while($this->connect($clean, $will, $username, $password)==false){
|
||||||
sleep(10);
|
sleep(10);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* connects to the broker
|
/* connects to the broker
|
||||||
inputs: $clean: should the client send a clean session flag */
|
inputs: $clean: should the client send a clean session flag */
|
||||||
function connect($clean = true, $will = NULL, $username = NULL, $password = NULL){
|
function connect($clean = true, $will = NULL, $username = NULL, $password = NULL){
|
||||||
|
|
@ -107,22 +103,21 @@ class MqttService
|
||||||
if($this->username) $buffer .= $this->strwritestring($this->username,$i);
|
if($this->username) $buffer .= $this->strwritestring($this->username,$i);
|
||||||
if($this->password) $buffer .= $this->strwritestring($this->password,$i);
|
if($this->password) $buffer .= $this->strwritestring($this->password,$i);
|
||||||
$head = " ";
|
$head = " ";
|
||||||
$head[0] = chr(0x10);
|
$head{0} = chr(0x10);
|
||||||
$head[1] = chr($i);
|
$head{1} = chr($i);
|
||||||
fwrite($this->socket, $head, 2);
|
fwrite($this->socket, $head, 2);
|
||||||
fwrite($this->socket, $buffer);
|
fwrite($this->socket, $buffer);
|
||||||
$string = $this->read(4);
|
$string = $this->read(4);
|
||||||
if(ord($string[0])>>4 == 2 && $string[3] == chr(0)){
|
if(ord($string{0})>>4 == 2 && $string{3} == chr(0)){
|
||||||
if($this->debug) echo "Connected to Broker\n";
|
if($this->debug) echo "Connected to Broker\n";
|
||||||
}else{
|
}else{
|
||||||
error_log(sprintf("Connection failed! (Error: 0x%02x 0x%02x)\n",
|
error_log(sprintf("Connection failed! (Error: 0x%02x 0x%02x)\n",
|
||||||
ord($string[0]),ord($string[3])));
|
ord($string{0}),ord($string{3})));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$this->timesinceping = time();
|
$this->timesinceping = time();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read: reads in so many bytes */
|
/* read: reads in so many bytes */
|
||||||
function read($int = 8192, $nb = false){
|
function read($int = 8192, $nb = false){
|
||||||
// print_r(socket_get_status($this->socket));
|
// print_r(socket_get_status($this->socket));
|
||||||
|
|
@ -145,7 +140,6 @@ class MqttService
|
||||||
|
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* subscribe: subscribes to topics */
|
/* subscribe: subscribes to topics */
|
||||||
function subscribe($topics, $qos = 0){
|
function subscribe($topics, $qos = 0){
|
||||||
$i = 0;
|
$i = 0;
|
||||||
|
|
@ -171,7 +165,6 @@ class MqttService
|
||||||
$bytes = ord(substr($string,1,1));
|
$bytes = ord(substr($string,1,1));
|
||||||
$string = $this->read($bytes);
|
$string = $this->read($bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ping: sends a keep alive ping */
|
/* ping: sends a keep alive ping */
|
||||||
function ping(){
|
function ping(){
|
||||||
$head = " ";
|
$head = " ";
|
||||||
|
|
@ -180,21 +173,18 @@ class MqttService
|
||||||
fwrite($this->socket, $head, 2);
|
fwrite($this->socket, $head, 2);
|
||||||
if($this->debug) echo "ping sent\n";
|
if($this->debug) echo "ping sent\n";
|
||||||
}
|
}
|
||||||
|
/* disconnect: sends a proper disconect cmd */
|
||||||
/* disconnect: sends a proper disconnect cmd */
|
|
||||||
function disconnect(){
|
function disconnect(){
|
||||||
$head = " ";
|
$head = " ";
|
||||||
$head[0] = chr(0xe0);
|
$head{0} = chr(0xe0);
|
||||||
$head[1] = chr(0x00);
|
$head{1} = chr(0x00);
|
||||||
fwrite($this->socket, $head, 2);
|
fwrite($this->socket, $head, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* close: sends a proper disconect, then closes the socket */
|
/* close: sends a proper disconect, then closes the socket */
|
||||||
function close(){
|
function close(){
|
||||||
$this->disconnect();
|
$this->disconnect();
|
||||||
stream_socket_shutdown($this->socket, STREAM_SHUT_WR);
|
stream_socket_shutdown($this->socket, STREAM_SHUT_WR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* publish: publishes $content on a $topic */
|
/* publish: publishes $content on a $topic */
|
||||||
function publish($topic, $content, $qos = 0, $retain = 0){
|
function publish($topic, $content, $qos = 0, $retain = 0){
|
||||||
$i = 0;
|
$i = 0;
|
||||||
|
|
@ -212,15 +202,14 @@ class MqttService
|
||||||
$cmd = 0x30;
|
$cmd = 0x30;
|
||||||
if($qos) $cmd += $qos << 1;
|
if($qos) $cmd += $qos << 1;
|
||||||
if($retain) $cmd += 1;
|
if($retain) $cmd += 1;
|
||||||
$head[0] = chr($cmd);
|
$head{0} = chr($cmd);
|
||||||
$head .= $this->setmsglength($i);
|
$head .= $this->setmsglength($i);
|
||||||
fwrite($this->socket, $head, strlen($head));
|
fwrite($this->socket, $head, strlen($head));
|
||||||
fwrite($this->socket, $buffer, $i);
|
fwrite($this->socket, $buffer, $i);
|
||||||
}
|
}
|
||||||
|
/* message: processes a recieved topic */
|
||||||
/* message: processes a received topic */
|
|
||||||
function message($msg){
|
function message($msg){
|
||||||
$tlen = (ord($msg[0])<<8) + ord($msg[1]);
|
$tlen = (ord($msg{0})<<8) + ord($msg{1});
|
||||||
$topic = substr($msg,2,$tlen);
|
$topic = substr($msg,2,$tlen);
|
||||||
$msg = substr($msg,($tlen+2));
|
$msg = substr($msg,($tlen+2));
|
||||||
$found = 0;
|
$found = 0;
|
||||||
|
|
@ -238,8 +227,7 @@ class MqttService
|
||||||
}
|
}
|
||||||
if($this->debug && !$found) echo "msg recieved but no match in subscriptions\n";
|
if($this->debug && !$found) echo "msg recieved but no match in subscriptions\n";
|
||||||
}
|
}
|
||||||
|
/* proc: the processing loop for an "allways on" client
|
||||||
/* proc: the processing loop for an "always on" client
|
|
||||||
set true when you are doing other stuff in the loop good for watching something else at the same time */
|
set true when you are doing other stuff in the loop good for watching something else at the same time */
|
||||||
function proc( $loop = true){
|
function proc( $loop = true){
|
||||||
if(1){
|
if(1){
|
||||||
|
|
@ -303,20 +291,18 @@ class MqttService
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* getmsglength: */
|
/* getmsglength: */
|
||||||
function getmsglength(&$msg, &$i){
|
function getmsglength(&$msg, &$i){
|
||||||
$multiplier = 1;
|
$multiplier = 1;
|
||||||
$value = 0 ;
|
$value = 0 ;
|
||||||
do{
|
do{
|
||||||
$digit = ord($msg[$i]);
|
$digit = ord($msg{$i});
|
||||||
$value += ($digit & 127) * $multiplier;
|
$value += ($digit & 127) * $multiplier;
|
||||||
$multiplier *= 128;
|
$multiplier *= 128;
|
||||||
$i++;
|
$i++;
|
||||||
}while (($digit & 128) != 0);
|
}while (($digit & 128) != 0);
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setmsglength: */
|
/* setmsglength: */
|
||||||
function setmsglength($len){
|
function setmsglength($len){
|
||||||
$string = "";
|
$string = "";
|
||||||
|
|
@ -330,7 +316,6 @@ class MqttService
|
||||||
}while ( $len > 0 );
|
}while ( $len > 0 );
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* strwritestring: writes a string to a buffer */
|
/* strwritestring: writes a string to a buffer */
|
||||||
function strwritestring($str, &$i){
|
function strwritestring($str, &$i){
|
||||||
$ret = " ";
|
$ret = " ";
|
||||||
|
|
@ -343,13 +328,12 @@ class MqttService
|
||||||
$i += ($len+2);
|
$i += ($len+2);
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
function printstr($string){
|
function printstr($string){
|
||||||
$strlen = strlen($string);
|
$strlen = strlen($string);
|
||||||
for($j=0;$j<$strlen;$j++){
|
for($j=0;$j<$strlen;$j++){
|
||||||
$num = ord($string[$j]);
|
$num = ord($string{$j});
|
||||||
if($num > 31)
|
if($num > 31)
|
||||||
$chr = $string[$j]; else $chr = " ";
|
$chr = $string{$j}; else $chr = " ";
|
||||||
printf("%4d: %08b : 0x%02x : %s \n",$j,$num,$num,$chr);
|
printf("%4d: %08b : 0x%02x : %s \n",$j,$num,$num,$chr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,5 @@ return [
|
||||||
'username' => env('mqtt_username',''),
|
'username' => env('mqtt_username',''),
|
||||||
'certfile' => env('mqtt_cert_file',''),
|
'certfile' => env('mqtt_cert_file',''),
|
||||||
'port' => env('mqtt_port','1883'),
|
'port' => env('mqtt_port','1883'),
|
||||||
'debug' => env('mqtt_debug',false), //Optional Parameter to enable debugging set it to True
|
'debug' => env('mqtt_debug',false) //Optional Parameter to enable debugging set it to True
|
||||||
'qos' => env('mqtt_qos', 0), // set quality of service here
|
|
||||||
'retain' => env('mqtt_retain', 0) // it should be 0 or 1 Whether the message should be retained.- Retain Flag
|
|
||||||
];
|
];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue