0
我想在基於PHP的頁面上使用MQTT顯示一些值。 PHP代碼包含訂閱者。我正在爲MQTT代理使用Bluemix IoT服務。此外,這些消息通過本地機器上的Python代碼發佈。PHP MQTT訂閱不一致
當我嘗試使用頁面刷新在頁面上顯示值時,頁面有時無法顯示值。發佈者端沒有問題,因爲Bluemix IoT服務成功顯示值。
我的代碼如下:
<?php
// include class
require('phpMQTT.php');
// set configuration values
if(getenv("VCAP_SERVICES")) {
// get MySQL service configuration from Bluemix
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services, true);
$mysql_config = $services_json["iotf-service"][0]["credentials"];
$org_id = $mysql_config["org"];
$port = $mysql_config["mqtt_u_port"];
$username = $mysql_config["apiKey"];
$password = $mysql_config["apiToken"];
}
// set configuration values
$config = array(
'org_id' => $org_id,
'port' => $port,
'app_id' => 'm',
'iotf_api_key' => $username,
'iotf_api_secret' => $password,
'device_id' => 'trial',
'qos' => 1
);
global $Val_A;
global $Val_B;
global $Val_C;
global $Val_D;
//Read already existing file
$ini_b = parse_ini_file("data.ini",true);
$Val_A = $ini_b['Data']['A'];
$Val_B = $ini_b['Data']['B'];
$Val_C = $ini_b['Data']['C'];
$Val_D = $ini_b['Data']['D'];
$config['server'] = $config['org_id'] . '.messaging.internetofthings.ibmcloud.com';
$config['client_id'] = 'a:' . $config['org_id'] . ':' . $config['app_id'];
#echo $config['client_id'];
// initialize client
$mqtt_dev = new phpMQTT($config['server'], $config['port'], $config['client_id']);
$mqtt_dev->debug = false;
// connect to broker
if(!$mqtt_dev->connect(true, null, $config['iotf_api_key'], $config['iotf_api_secret'])){
echo 'ERROR: Could not connect to IoT cloud';
exit();
}
else
{
#echo "Success";
}
$topics['iot-2/type/newdevice/id/' . $config['device_id'] . '/evt/status/fmt/json'] =
array('qos' =>1, 'function' => 'getLocation');
$mqtt_dev->subscribe($topics, 1);
$elapsedSeconds = 0;
while ($mqtt_dev->proc(true)) {
#echo json_encode($json);
if (count($location) == 2) {
break;
}
if ($elapsedSeconds == 5) {
break;
}
$elapsedSeconds++;
sleep(1);
}
// disconnect
//I have tried commenting this too
$mqtt_dev->close();
function getLocation($topic, $msg) {
global $location;
global $json;
$json = json_decode($msg);
$Val_A = $json->A;
$Val_B = $json->B;
$Val_C = $json->C;
$Val_D = $json->D;
//Read already existing file
$ini_backup = parse_ini_file("data.ini",true);
$ValA_b = $ini_backup['Data']['A'];
$ValB_b = $ini_backup['Data']['B'];
$ValC_b = $ini_backup['Data']['C'];
$ValD_b = $ini_backup['Data']['D'];
if($Val_A != 0)
{
$ValA_b = $Val_A;
}
else
{
$Val_A = $ValA_b;
}
if($Val_B != 0)
{
$ValB_b = $Val_B;
}
else
{
$Val_B = $ValB_b;
}
if($Val_C != 0)
{
$ValC_b = $Val_C;
}
else
{
$Val_C = $ValC_b;
}
if($Val_D != 0)
{
$ValD_b = $Val_D;
}
else
{
$Val_D = $ValD_b;
}
$file = fopen("data.ini","w");
fwrite($file,"[Data]". "\n");
fwrite($file,"A =" . $ValA_b . "\n");
fwrite($file,"B =" . $ValB_b . "\n");
fwrite($file,"C =" . $ValC_b . "\n");
fwrite($file,"D =" . $ValD_b . "\n");
fclose($file);
return $location;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="5" >
<div id="footer">
This page will automatically reload every 5 seconds. <br/>
</div>
<label for="A">A</label>
<input type="text" value="<?php echo $Val_A ?>" />
<label for="B">B</label>
<input type="text" value="<?php echo $Val_B ?>" />
<label for="C">C</label>
<input type="text" value="<?php echo $Val_C ?>" />
<label for="D">D</label>
<input type="text" value="<?php echo $Val_D ?>" />
</body>
</html>
能有人指導我要去哪裏錯了?
謝謝你的回覆。我會嘗試。 – Sid411
問題不在於刷新。我想這對用戶和經紀人有一些幫助。我試圖使用retain標誌作爲true,以便代理存儲最後的值。但有時用戶仍然失敗。 – Sid411
Watson IoT Platform爲MQTT消息的保留消息功能提供了有限的支持。如果在從設備,網關或應用程序發送的MQTT消息中將保留的消息標誌設置爲true,則消息將作爲未保留的消息處理。 Watson IoT平臺組織無權發佈保留的消息。當Watson IoT Platform服務設置爲true時,將覆蓋保留的消息標誌,並將消息處理爲保留的消息標誌設置爲false。 https://console.ng.bluemix.net/docs/services/IoT/reference/mqtt/index.html#version-support – ValerieLampkin