PHP MQTT subscribe not consistent

萝らか妹 提交于 2019-12-12 02:15:34

问题


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.

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.

My code is as follows:

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


回答1:


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.



来源:https://stackoverflow.com/questions/42532305/php-mqtt-subscribe-not-consistent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!