0

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?

Sid411
  • 703
  • 2
  • 9
  • 25

1 Answers1

0

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.

Community
  • 1
  • 1
ValerieLampkin
  • 2,620
  • 13
  • 27
  • Thank you for the reply. I will try that. – Sid411 Mar 02 '17 at 05:27
  • The problem is not with refresh. I guess it has got something to with subscriber and broker. I have tried using the retain flag as true so that broker stores the last value. But still the subscriber fails sometimes. – Sid411 Mar 03 '17 at 09:42
  • 1
    Watson IoT Platform provides limited support for retained messages feature of MQTT messaging. If the retained message flag is set to true in MQTT message that is sent from a device, gateway, or application the message is handled as an unretained message. Watson IoT Platform organizations are not authorized to publish retained messages. Watson IoT Platform service overrides the retained message flag when it is set to true and processes the message as if the retained message flag is set to false. https://console.ng.bluemix.net/docs/services/IoT/reference/mqtt/index.html#version-support – ValerieLampkin Mar 03 '17 at 13:19
  • 1
    Thanks a lot for that. That helped. I decided to go with the exec method after reading this. Thank you. – Sid411 Mar 08 '17 at 15:01