Compare commits
No commits in common. "master" and "v1.0.1" have entirely different histories.
|
|
@ -1,6 +1,6 @@
|
|||
# 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/subscribe to MQTT broker
|
||||
|
||||
Based on [bluerhinos/phpMQTT](https://github.com/bluerhinos/phpMQTT)
|
||||
|
||||
|
|
@ -16,8 +16,6 @@ composer require salmanzafar/laravel-mqtt
|
|||
* Certificate Protection for end to end encryption
|
||||
* 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)
|
||||
|
||||
|
|
@ -53,8 +51,6 @@ php artisan vendor:publish --provider="Salman\Mqtt\MqttServiceProvider"
|
|||
'certfile' => env('mqtt_cert_file',''),
|
||||
'port' => env('mqtt_port','1883'),
|
||||
'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
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "salmanzafar-chenc/laravel-mqtt",
|
||||
"description": "A simple Laravel 5 and 6 Library to connect/publish/subscribe to MQTT broker",
|
||||
"name": "salmanzafar/laravel-mqtt",
|
||||
"description": "A simple Laravel 5 Library to connect/publish/subscribe to MQTT broker",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
|
|
|
|||
|
|
@ -40,8 +40,6 @@ class Mqtt
|
|||
protected $password = null;
|
||||
protected $port = null;
|
||||
protected $debug = null;
|
||||
protected $qos = 0;
|
||||
protected $retain = 0;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
|
@ -51,8 +49,6 @@ class Mqtt
|
|||
$this->cert_file = config('mqtt.certfile');
|
||||
$this->port = config('mqtt.port');
|
||||
$this->debug = config('mqtt.debug');
|
||||
$this->qos = config('mqtt.qos');
|
||||
$this->retain = config('mqtt.retain');
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -65,7 +61,7 @@ class Mqtt
|
|||
|
||||
if ($client->connect(true, null, $this->username, $this->password))
|
||||
{
|
||||
$client->publish($topic,$msg, $this->qos, $this->retain);
|
||||
$client->publish($topic,$msg);
|
||||
$client->close();
|
||||
|
||||
return true;
|
||||
|
|
@ -85,7 +81,7 @@ class Mqtt
|
|||
{
|
||||
$topics[$topic] = array("qos" => 0, "function" => $proc);
|
||||
|
||||
$client->subscribe($topics, $this->qos);
|
||||
$client->subscribe($topics, 0);
|
||||
|
||||
while($client->proc())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -107,16 +107,16 @@ class MqttService
|
|||
if($this->username) $buffer .= $this->strwritestring($this->username,$i);
|
||||
if($this->password) $buffer .= $this->strwritestring($this->password,$i);
|
||||
$head = " ";
|
||||
$head[0] = chr(0x10);
|
||||
$head[1] = chr($i);
|
||||
$head{0} = chr(0x10);
|
||||
$head{1} = chr($i);
|
||||
fwrite($this->socket, $head, 2);
|
||||
fwrite($this->socket, $buffer);
|
||||
$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";
|
||||
}else{
|
||||
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;
|
||||
}
|
||||
$this->timesinceping = time();
|
||||
|
|
@ -184,8 +184,8 @@ class MqttService
|
|||
/* disconnect: sends a proper disconnect cmd */
|
||||
function disconnect(){
|
||||
$head = " ";
|
||||
$head[0] = chr(0xe0);
|
||||
$head[1] = chr(0x00);
|
||||
$head{0} = chr(0xe0);
|
||||
$head{1} = chr(0x00);
|
||||
fwrite($this->socket, $head, 2);
|
||||
}
|
||||
|
||||
|
|
@ -212,7 +212,7 @@ class MqttService
|
|||
$cmd = 0x30;
|
||||
if($qos) $cmd += $qos << 1;
|
||||
if($retain) $cmd += 1;
|
||||
$head[0] = chr($cmd);
|
||||
$head{0} = chr($cmd);
|
||||
$head .= $this->setmsglength($i);
|
||||
fwrite($this->socket, $head, strlen($head));
|
||||
fwrite($this->socket, $buffer, $i);
|
||||
|
|
@ -220,7 +220,7 @@ class MqttService
|
|||
|
||||
/* message: processes a received topic */
|
||||
function message($msg){
|
||||
$tlen = (ord($msg[0])<<8) + ord($msg[1]);
|
||||
$tlen = (ord($msg{0})<<8) + ord($msg{1});
|
||||
$topic = substr($msg,2,$tlen);
|
||||
$msg = substr($msg,($tlen+2));
|
||||
$found = 0;
|
||||
|
|
@ -309,7 +309,7 @@ class MqttService
|
|||
$multiplier = 1;
|
||||
$value = 0 ;
|
||||
do{
|
||||
$digit = ord($msg[$i]);
|
||||
$digit = ord($msg{$i});
|
||||
$value += ($digit & 127) * $multiplier;
|
||||
$multiplier *= 128;
|
||||
$i++;
|
||||
|
|
@ -347,9 +347,9 @@ class MqttService
|
|||
function printstr($string){
|
||||
$strlen = strlen($string);
|
||||
for($j=0;$j<$strlen;$j++){
|
||||
$num = ord($string[$j]);
|
||||
$num = ord($string{$j});
|
||||
if($num > 31)
|
||||
$chr = $string[$j]; else $chr = " ";
|
||||
$chr = $string{$j}; else $chr = " ";
|
||||
printf("%4d: %08b : 0x%02x : %s \n",$j,$num,$num,$chr);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,12 +8,10 @@
|
|||
|
||||
return [
|
||||
|
||||
'host' => env('mqtt_host','127.0.0.1'),
|
||||
'host' => env('mqtt_host','127.0.0.1'),
|
||||
'password' => env('mqtt_password',''),
|
||||
'username' => env('mqtt_username',''),
|
||||
'certfile' => env('mqtt_cert_file',''),
|
||||
'port' => env('mqtt_port','1883'),
|
||||
'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
|
||||
'port' => env('mqtt_port','1883'),
|
||||
'debug' => env('mqtt_debug',false) //Optional Parameter to enable debugging set it to True
|
||||
];
|
||||
|
|
|
|||
Loading…
Reference in New Issue