--
參考資源
除了上述資源外,一開始其實使用的是 php-mqtt/client 這個,不過軟體太肥,使用麻煩效能也差,才又找了 phpMQTT 使用
--
範例程式
主要是在網頁上執行 publish() 發送,因此只需要可連線,可發送即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php require('../3rdParty/phpMQTT.php'); $server = 'mq.tongxinmao.com'; // change if necessary $port = 18830; // change if necessary $username = ''; // set your username $password = ''; // set your password $client_id = 'phpMQTT-publisher'; // make sure this is unique for connecting to sever - you could use uniqid() $mqtt = new \phpMQTT($server, $port, $client_id); if ($mqtt->connect(true, NULL)) { $mqtt->publish('esp8266/test', '{"LED":"'. $_GET['led'] .'"}', 0, false); $mqtt->close(); } else { echo "Time out!\n"; } |
--
SSL 範例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php // MQTT const MQTT_Server = '172.16.1.220'; const MQTT_Port = 8883; const MQTT_Username = 'user'; const MQTT_Password = 'password'; require_once __DIR__ . "/phpMQTT.php"; $mqtt = new \phpMQTT(MQTT_Server, MQTT_Port, 'EMS_'.uniqid(), __DIR__.'/my_root_ca.pem'); if ($mqtt->connect(true, NULL, MQTT_Username, MQTT_Password)) { $alert = [ 'id'=>$event_id, 'event'=>'PM2.5', 'time'=>date('Y-m-d H:i:s'), 'period'=>'start', 'set'=>$pm25_alarm_up, 'value'=>$data['Value']['PM2.5'] ]; $mqtt->publish('DAE/Alert/'.$channel['Token'], json_encode($alert), 0, false); $mqtt->close(); } |
--
1,674 total views, 1 views today