PHP MQTT订阅不一致 [英] PHP MQTT subscribe not consistent

查看:298
本文介绍了PHP MQTT订阅不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在基于PHP的页面上使用MQTT显示一些值. PHP代码包含订户.我正在为MQTT经纪人使用Bluemix IoT服务.而且,消息是通过Python代码在本地计算机上发布的.

I am trying to display some values using MQTT on a PHP based page. The PHP code contains the subscriber. I am using Bluemix IoT service for the MQTT broker. Also, the messages are published via Python code on local machine.

当我尝试使用页面刷新在页面上显示值时,页面有时无法显示值.由于Bluemix IoT服务已成功显示值,因此在发布者端没有问题.

When I try to display values on pages using page refresh, the page sometimes fails to display the values. There is no problem at the publisher end as the values are successfully displayed by Bluemix IoT service.

我的代码如下:

<?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>

有人可以指导我去哪里吗?

Can some one guide where am I going wrong?

推荐答案

而不是使用meta指定刷新,请尝试使用php header("Refresh:5");
有一个旧的堆栈溢出威胁 d讨论了在PHP中使用元对标头进行刷新.

Rather than using meta to specify the refresh, try using php header("Refresh:5");
There is an old stack overflow thread discussing using meta versus header for refrseh in php.

这篇关于PHP MQTT订阅不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆