Compare commits

..

6 Commits

Author SHA1 Message Date
Chenc 9addc4bf7f composer 2019-12-08 12:04:16 +08:00
Chenc e267d30f75 Array and string offset access syntax with curly braces is deprecated 2019-12-08 11:55:59 +08:00
Salman Zafar 794934bc62 Merge branch 'development' 2019-10-29 17:39:06 +05:00
Salman Zafar 7148ef7228 added qos and retan flags for publishing and subsrcbe methods 2019-10-29 17:01:02 +05:00
salman zafar 5ec057142d
Update composer.json 2019-09-23 19:16:46 +05:00
Salman Zafar 7911a8cb6a added support for laravel 6 2019-09-23 14:32:39 +05:00
5 changed files with 29 additions and 19 deletions

View File

@ -1,6 +1,6 @@
# Laravel MQTT Package # Laravel MQTT Package
A simple Laravel 5 Library to connect/publish/subscribe to MQTT broker A simple Laravel 5 and 6 Library to connect/publish/subscribe to MQTT broker
Based on [bluerhinos/phpMQTT](https://github.com/bluerhinos/phpMQTT) Based on [bluerhinos/phpMQTT](https://github.com/bluerhinos/phpMQTT)
@ -16,6 +16,8 @@ composer require salmanzafar/laravel-mqtt
* 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 * 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)
@ -51,6 +53,8 @@ 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

View File

@ -1,6 +1,6 @@
{ {
"name": "salmanzafar/laravel-mqtt", "name": "salmanzafar-chenc/laravel-mqtt",
"description": "A simple Laravel 5 Library to connect/publish/subscribe to MQTT broker", "description": "A simple Laravel 5 and 6 Library to connect/publish/subscribe to MQTT broker",
"type": "library", "type": "library",
"license": "MIT", "license": "MIT",
"authors": [ "authors": [

View File

@ -40,6 +40,8 @@ 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()
{ {
@ -49,6 +51,8 @@ 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');
} }
@ -61,7 +65,7 @@ class Mqtt
if ($client->connect(true, null, $this->username, $this->password)) if ($client->connect(true, null, $this->username, $this->password))
{ {
$client->publish($topic,$msg); $client->publish($topic,$msg, $this->qos, $this->retain);
$client->close(); $client->close();
return true; return true;
@ -81,7 +85,7 @@ class Mqtt
{ {
$topics[$topic] = array("qos" => 0, "function" => $proc); $topics[$topic] = array("qos" => 0, "function" => $proc);
$client->subscribe($topics, 0); $client->subscribe($topics, $this->qos);
while($client->proc()) while($client->proc())
{ {

View File

@ -107,16 +107,16 @@ 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();
@ -184,8 +184,8 @@ class MqttService
/* disconnect: sends a proper disconnect 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);
} }
@ -212,7 +212,7 @@ 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);
@ -220,7 +220,7 @@ class MqttService
/* message: processes a received 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;
@ -309,7 +309,7 @@ class MqttService
$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++;
@ -347,9 +347,9 @@ class MqttService
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);
} }
} }

View File

@ -13,5 +13,7 @@ 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
]; ];