Guest User

Untitled

a guest
Feb 3rd, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. <html>
  2. <form action="test.php" method="post">
  3. <INPUT name="btn_submit" TYPE="submit" VALUE="ON">
  4. <INPUT name="btn_submit" TYPE="submit" VALUE="OFF">
  5.  
  6. <INPUT name="btn_submit" TYPE="submit" VALUE="REQUERY">
  7.  
  8. </form>
  9. </body>
  10. </html>
  11.  
  12. <?php
  13. require("phpMQTT.php");
  14. $username="user";
  15. $password="pass";
  16. $mqtt = new phpMQTT("m20.cloudmqtt.com", 12213,"phpMQTT Pub Examplexxx"); //Change client name to something unique
  17.  
  18.  
  19. function subread(){
  20. echo "subscribing<br>";
  21.  
  22. if(!$mqtt->connect(true, NULL, $username, $password)) { exit(1);}
  23. $topics['#'] = array("qos"=>0, "function"=>"procmsg");
  24. $mqtt->subscribe($topics,0);
  25. $start_time = time(); $done = 0;
  26. while (!$done && !hasTimedout() && $mqtt->proc()) { } $mqtt->close();
  27. }
  28.  
  29.  
  30. function procmsg($topic,$msg) { global $done; $done = 0; echo "Message Received: ".date("r")."<br>"."nTopic:{$topic}n$msgn";}
  31. function hasTimedout() {global $start_time; return (time() - $start_time > 5); }
  32.  
  33.  
  34. switch ($_REQUEST['btn_submit']){
  35. case "ON":
  36. subread();
  37.  
  38. if($mqtt->connect(true, NULL, $username, $password)){
  39. $mqtt->publish("cmnd/JM/POWER","ON",0);
  40. $mqtt->close();
  41. }
  42. break;
  43. case "OFF":
  44. if($mqtt->connect(true, NULL, $username, $password)){
  45. $mqtt->publish("cmnd/JM/POWER","OFF",0);
  46. $mqtt->close();
  47. }
  48. break;
  49. case "REQUERY":
  50. subread();
  51. break;
  52. }
  53.  
  54. ?>
Add Comment
Please, Sign In to add comment