bluerhinos / phpMQTT

a simple php class to connect/publish/subscribe to a MQTT broker
Other
773 stars 460 forks source link

Easy subscribe huge change #85

Closed frmatias closed 4 years ago

frmatias commented 5 years ago

https://packagist.org/packages/frmatias/easy-mqtt Subcribe changes --

You only need use 3 statements Check examples files to know what inputs you should use $this->connect(params) $msg = $this->subscribe(params) $this->close()

skyynet commented 5 years ago

Thanks for developing this further. I'm trying to read my Sonoff Dual R2 Wi-Fi relay wis ESPurna Firmware and have setup my MQTT broker on my local Synology. The Sonoff successfully publishes to /home/garden/mowgli/relay/0 which I can read with MQTTbox on my Mac.

I am trying to read this relay on my local MAMP installation running PHP 7.2 and had to remove the namespace Bluerhinos at the beginning of the phpMQTT.php

Also first I forgot to echo $msg in subscribe.php. Maybe you could insert it for dummys like me.

skyynet commented 5 years ago

While it's working, I have two issues. First reading a value from the broker is very slow Second I don't know how to read multiple values. Working sample: `$mqtt->connect(true, NULL, $username, $password); $relais0 = $mqtt->subscribe('/home/garden/mowgli/relay/0', 0); echo 'Relais 0: '.substr($relais0,-1).'
'; $mqtt->close();

$mqtt->connect(true, NULL, $username, $password); $relais1 = $mqtt->subscribe('/home/garden/mowgli/relay/1', 0); echo 'Relais 1: '.substr($relais1,-1).'
'; $mqtt->close();`

There must be a way to read multiple values without connecting and closing every time in between.

frmatias commented 5 years ago

you are closing the connection using $mqtt->close(). If you stay with the connection open you can read everything, but php is stateless, so you need call this function again everytime. i'm working in a separete project based on phpMQTT, search for easyMQTT on packgist. I will treat the returned messenger and also add that a cron job to call the function and save in a database the results, maybe i will focus on laravel.

frmatias commented 5 years ago

easyMQTT i cleaned every useless function. I'm working about the connection delay. https://packagist.org/packages/frmatias/easy-mqtt

skyynet commented 5 years ago

Thanks. Gonna follow your new repository.

bluerhinos commented 4 years ago

Canned Response: Subscribe is not designed for web requests, it should be used in a long running process. I do plan to add a subscribeAndWaitForMessage() function #95