Advertisement
Guest User

equiva-php-mqtt

a guest
Dec 22nd, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3. define('BROKER', '192.168.0.250');
  4. define('PORT', 1883);
  5. define('CLIENT_ID', "pubclient_" + getmypid());
  6.  
  7. $client = new Mosquitto\Client(CLIENT_ID);
  8. $client->onConnect('connect');
  9. $client->onDisconnect('disconnect');
  10. $client->onSubscribe('subscribe');
  11. $client->onMessage('message');
  12. $client->connect(BROKER, PORT, 60);
  13. $client->subscribe('Garagentuere/#', 1); // Subscribe to all messages
  14. $client->loopForever();
  15.  
  16. function publishvalue($prefix, $param, $message) {
  17.         $topic = $prefix.$param;
  18.         $client = new Mosquitto\Client();
  19.         //$client->setCredentials('S1','123456');
  20.         $client->connect("192.168.0.250", 1883, 5);
  21.         $client->publish($topic, $message, 1, 0);
  22.         $client->disconnect();
  23.         unset($client);
  24. }
  25.  
  26. function connect($r) {
  27.         echo "Received response code {$r}\n";
  28. }
  29.  
  30. function subscribe() {
  31.         echo "Successful subscribed to a topic.\n";
  32. }
  33.  
  34. function message($message) {
  35.         //echo getDatetimeNow().": ";
  36.         printf("Msg received on %s payload: >>>>>%s<<<<<\n", $message->topic, $message->payload);
  37.         if ($message->topic == 'Garagentuere/status/set') {
  38.                 doorcontrol($message->payload);
  39.         }
  40.         return;
  41. }
  42.  
  43. function endsWith($haystack, $needle) {
  44.     return substr_compare($haystack, $needle, -strlen($needle)) === 0;
  45. }
  46.  
  47. function disconnect() {
  48.         //echo "Disconnected cleanly. Attempt to reconnect in 5s.\n";
  49.         echo "Disconnected cleanly. Exiting.\n";
  50.         exit();
  51.         sleep(5);
  52.         $client = new Mosquitto\Client(CLIENT_ID);
  53.         $client->onConnect('connect');
  54.         $client->onDisconnect('disconnect');
  55.         $client->onSubscribe('subscribe');
  56.         $client->onMessage('message');
  57.         $client->connect(BROKER, PORT, 60);
  58.         $client->subscribe('Garagentuere/#', 1); // Subscribe to all messages
  59.         $client->loopForever();
  60. }
  61.  
  62. function doorcontrol($param) {
  63.         $sudo = '/usr/bin/sudo ';
  64.         $cmd = '/usr/local/bin/keyble-sendcommand --address 00:11:22:33:44:55 --user_id 1 --user_key XXXXX --command ';
  65.  
  66.         //echo getDatetimeNow().": executing doorcontrol with param >>>>>".$param."<<<<<\n";
  67.         echo "executing doorcontrol with param >>>>>".$param."<<<<<\n";
  68.         //$param = 'status';
  69.         $result = exec($sudo.$cmd.$param.' 2>&1', $output, $retcode);
  70.  
  71.         if ($retcode != 0) {
  72.                 //echo getDatetimeNow().": errorcode: ".$retcode."\n";
  73.                 echo "errorcode: ".$retcode."\n";
  74.                 print_r($output);
  75.                 unset($output);
  76.                 return;
  77.         } else {
  78.                 publishvalue('Garagentuere/', 'status', $result);
  79.                 //echo getDatetimeNow().": Changed and published new state >>>>>".$result."<<<<<\n";
  80.                 echo "Changed and published new state >>>>>".$result."<<<<<\n";
  81.         }
  82. }
  83.  
  84. function getDatetimeNow() {
  85.     $tz_object = new DateTimeZone('Europe/Vienna');
  86.     //date_default_timezone_set('Europe/Vienna');
  87.  
  88.     $datetime = new DateTime();
  89.     $datetime->setTimezone($tz_object);
  90.     return $datetime->format('Y\-m\-d\ h:i:s');
  91. }
  92.  
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement