Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. <?php
  2. // include class
  3. require('phpMQTT.php');
  4. // set configuration values
  5. if( getenv("VCAP_SERVICES") ) {
  6.  
  7.  
  8. // get MySQL service configuration from Bluemix
  9.  
  10.  
  11. $services = getenv("VCAP_SERVICES");
  12. $services_json = json_decode($services, true);
  13.  
  14. $mysql_config = $services_json["iotf-service"][0]["credentials"];
  15.  
  16. $org_id = $mysql_config["org"];
  17.  
  18.  
  19. $port = $mysql_config["mqtt_u_port"];
  20.  
  21.  
  22. $username = $mysql_config["apiKey"];
  23.  
  24.  
  25. $password = $mysql_config["apiToken"];
  26.  
  27.  
  28. }
  29.  
  30. // set configuration values
  31. $config = array(
  32. 'org_id' => $org_id,
  33. 'port' => $port,
  34. 'app_id' => 'm',
  35. 'iotf_api_key' => $username,
  36. 'iotf_api_secret' => $password,
  37. 'device_id' => 'trial',
  38. 'qos' => 1
  39.  
  40. );
  41.  
  42. global $Val_A;
  43. global $Val_B;
  44. global $Val_C;
  45. global $Val_D;
  46.  
  47. //Read already existing file
  48.  
  49. $ini_b = parse_ini_file("data.ini",true);
  50.  
  51. $Val_A = $ini_b['Data']['A'];
  52. $Val_B = $ini_b['Data']['B'];
  53. $Val_C = $ini_b['Data']['C'];
  54. $Val_D = $ini_b['Data']['D'];
  55.  
  56.  
  57. $config['server'] = $config['org_id'] . '.messaging.internetofthings.ibmcloud.com';
  58.  
  59.  
  60. $config['client_id'] = 'a:' . $config['org_id'] . ':' . $config['app_id'];
  61.  
  62. #echo $config['client_id'];
  63.  
  64. // initialize client
  65. $mqtt_dev = new phpMQTT($config['server'], $config['port'], $config['client_id']);
  66. $mqtt_dev->debug = false;
  67.  
  68.  
  69. // connect to broker
  70. if(!$mqtt_dev->connect(true, null, $config['iotf_api_key'], $config['iotf_api_secret'])){
  71. echo 'ERROR: Could not connect to IoT cloud';
  72. exit();
  73. }
  74. else
  75. {
  76. #echo "Success";
  77. }
  78.  
  79. $topics['iot-2/type/newdevice/id/' . $config['device_id'] . '/evt/status/fmt/json'] =
  80. array('qos' =>1, 'function' => 'getLocation');
  81.  
  82.  
  83. $mqtt_dev->subscribe($topics, 1);
  84.  
  85. $elapsedSeconds = 0;
  86.  
  87. while ($mqtt_dev->proc(true)) {
  88.  
  89. #echo json_encode($json);
  90.  
  91. if (count($location) == 2) {
  92.  
  93.  
  94.  
  95. break;
  96. }
  97.  
  98. if ($elapsedSeconds == 5) {
  99.  
  100. break;
  101. }
  102.  
  103. $elapsedSeconds++;
  104. sleep(1);
  105.  
  106. }
  107.  
  108. // disconnect
  109.  
  110. //I have tried commenting this too
  111. $mqtt_dev->close();
  112.  
  113. function getLocation($topic, $msg) {
  114.  
  115. global $location;
  116. global $json;
  117.  
  118. $json = json_decode($msg);
  119.  
  120. $Val_A = $json->A;
  121. $Val_B = $json->B;
  122. $Val_C = $json->C;
  123. $Val_D = $json->D;
  124.  
  125. //Read already existing file
  126.  
  127. $ini_backup = parse_ini_file("data.ini",true);
  128.  
  129. $ValA_b = $ini_backup['Data']['A'];
  130. $ValB_b = $ini_backup['Data']['B'];
  131. $ValC_b = $ini_backup['Data']['C'];
  132. $ValD_b = $ini_backup['Data']['D'];
  133.  
  134.  
  135. if($Val_A != 0)
  136. {
  137. $ValA_b = $Val_A;
  138. }
  139. else
  140. {
  141. $Val_A = $ValA_b;
  142. }
  143.  
  144. if($Val_B != 0)
  145. {
  146. $ValB_b = $Val_B;
  147. }
  148. else
  149. {
  150. $Val_B = $ValB_b;
  151. }
  152.  
  153. if($Val_C != 0)
  154. {
  155. $ValC_b = $Val_C;
  156. }
  157. else
  158. {
  159. $Val_C = $ValC_b;
  160. }
  161.  
  162. if($Val_D != 0)
  163. {
  164. $ValD_b = $Val_D;
  165. }
  166. else
  167. {
  168. $Val_D = $ValD_b;
  169. }
  170.  
  171.  
  172.  
  173. $file = fopen("data.ini","w");
  174.  
  175. fwrite($file,"[Data]". "n" );
  176. fwrite($file,"A =" . $ValA_b . "n" );
  177. fwrite($file,"B =" . $ValB_b . "n" );
  178. fwrite($file,"C =" . $ValC_b . "n" );
  179. fwrite($file,"D =" . $ValD_b . "n" );
  180.  
  181. fclose($file);
  182.  
  183.  
  184. return $location;
  185. }
  186.  
  187. ?>
  188.  
  189. <!DOCTYPE html>
  190. <html lang="en">
  191. <head>
  192.  
  193. <meta http-equiv="refresh" content="5" >
  194. <div id="footer">
  195. This page will automatically reload every 5 seconds. <br/>
  196. </div>
  197. <label for="A">A</label>
  198. <input type="text" value="<?php echo $Val_A ?>" />
  199. <label for="B">B</label>
  200. <input type="text" value="<?php echo $Val_B ?>" />
  201. <label for="C">C</label>
  202. <input type="text" value="<?php echo $Val_C ?>" />
  203. <label for="D">D</label>
  204. <input type="text" value="<?php echo $Val_D ?>" />
  205.  
  206. </body>
  207. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement