-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTopics.php
executable file
·40 lines (32 loc) · 924 Bytes
/
getTopics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
include("resources/phpMQTT.php");
error_reporting(0);
$results = [];
$topics = "WEATHER/a,AHMS/pir,AHMS/move,AHMS/door";
if (isset($_GET["topic"])) { $topics = $_GET["topic"]; }
$mqtt = new phpMQTT("192.168.1.20", 1883, "PHP MQTT Client", "ahmsclient", "ahms2013");
if ($mqtt->connect())
{
foreach(explode(",",$topics) as $topic)
{
$myTopics = [];
$myTopics[$topic] = array("qos"=>0, "function"=>"procmsg");
$mqtt->subscribe($myTopics);
if($mqtt->proc() == 0)
{
array_push($results, array ( "status" => "no message", "topic" => $topic, "message" => "" ));
}
}
$mqtt->close();
}
else
{
array_push($results, array ( "status" => "ok", "no connection" => $topic, "message" => "" ));
}
echo json_encode($results);
function procmsg($topic,$message)
{
global $results;
array_push($results,array ( "status" => "ok", "topic" => $topic, "message" => $message ));
}
?>