Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement