Comments (3)
I think it is because you are calling $mqtt->disconnect()
in both methods. The ConnectionManager
(which is what is used behind the MQTT
facade) does not know that you are disconnecting and will not call connect()
for you the second time.
My advice would be to remove both calls to $mqtt->disconnect()
and add one single call to MQTT::disconnect()
before returning the response.
from laravel-client.
Thank you very much. I removed all $mqtt->disconnect();
, but somtimes the returned message is empty:
[
{
device_id: 10000,
status: "Online",
power: "OFF"
},
{
device_id: 10001,
status: "Online",
power: "OFF"
},
{
device_id: 10002,
status: "Online",
power: ""
},
{
device_id: 10003,
status: "",
power: ""
},
{
device_id: 10004,
status: "",
power: ""
}
]
My code now:
public function subDeviceAll(Request $request)
{
$device = Device::all();
$dataDevice = [];
foreach ($device as $key => $value) {
//$data['data_device'] = $value;
$data['device_id'] = $value['device_id'];
$data['status'] = $this->checkDeviceOnline($value['device_id']);
$data['power'] = $this->checkDevicePower($value['device_id']);
$dataDevice[] = $data;
}
return response()->json($dataDevice, 200);
}
public function checkDeviceOnline($device_id)
{
$device = Device::where('device_id', $device_id)->first();
$mqtt = MQTT::connection();
$res = '';
$topic = $device['topic'];
$mqtt->publish('cmnd/' . $topic . '/POWER', '', 2);
$mqtt->subscribe('tele/' . $topic . '/LWT', function (string $topic, string $message) use ($mqtt, &$res) {
$res = $message;
$mqtt->interrupt();
}, 1);
$mqtt->loop(true);
return $res;
}
public function checkDevicePower($device_id)
{
$device = Device::where('device_id', $device_id)->first();
$mqtt = MQTT::connection();
$res = '';
$topic = $device['topic'];
$mqtt->publish('cmnd/' . $topic . '/POWER', '', 2);
$mqtt->subscribe('stat/' . $topic . '/POWER', function (string $topic, string $message) use ($mqtt, &$res) {
$res = $message;
$mqtt->interrupt();
}, 1);
$mqtt->loop(true);
return $res;
}
from laravel-client.
Well, that has nothing to do with the library but your devices. I cannot help you with that part.
from laravel-client.
Related Issues (20)
- could be use/configured as broadcast driver? HOT 3
- Godaddy Server(Shared Hosting) connecting to AWS iot core showing error. HOT 1
- Which Version of Laravel does it support? HOT 1
- Can this be used to make a MQTT sink? HOT 1
- Does it support auto reconnect? HOT 1
- Websocket port HOT 3
- Can the client publish messages on two different ports (TCP and WebSockets)? HOT 1
- Publish message as a bytes array HOT 8
- Support for Laravel 10 HOT 2
- Detect if response is not received from MQTT broker for specified duration. HOT 8
- SSL/TLS connection configuration HOT 6
- Is it possible to do subscribing to mutliple topics on 1 command? HOT 1
- How do you reset the command under supervisor when something changes in controller? HOT 3
- The broker did not acknowledge the connection attempt within the configured connection timeout period HOT 4
- plans to support php-mqtt client v2.0? HOT 2
- Cannot dispatch job inside subscribe closure HOT 1
- Certificate file on different disk HOT 1
- Settings without useBlockingSocket option HOT 1
- Enhancement Proposal: Addition of interrupt and subscribe methods to MQTT Facade and ConnectionManager Class HOT 2
- Unsubscribe HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-client.