User Tools

Site Tools


uphp:functions:mqtt_subscribe

This is an old revision of the document!


mqtt_subscribe

WMPRO, WMMINI FW >= 1.1074 WMMEGA FW >= 2.1078

Subscribe to an MQTT channel

Description

mqtt_publish ( string $channel, string $callback )

Parameters

$channel: Channel to subscribe to. See MQTT Configuration

$callback: Callback script to execute upon receipt of a message

Return Values

1 if successful and 0 on failure

Example

<? 
 
// Use the # as a wildcard so any messages to /cluster/1/ or its sub channels will be received
 
mqtt_subscribe("/cluster/1/#","/scripts/tests/mqttsub.cgi");
?>

Example Callback

Callbacks can use the php:stdin file to read input. Use fgets() to get the first line, which will be the full channel. The following lines will be the data received. <code php> <? $f=fopen(“php:stdin”,“r”); $st=fgets($f);

  $msg=fgets($f);
  $response="";
  if (substr($msg,0,3)=='get') {
      $variable=substr($msg,4,strlen($msg)-3);
      $response=$variable.":".$_GLOBALS[$variable];
  }

if (strpos($st,getmac()) || strpos($st,'/all')) {

  if (strpos($st,"get")) {    
          mqtt_publish("/cluster/1/ret/".getmac(),$response);
  } else {
      if (strpos($st,"ret")) {    
          print("Notification was properly posted");
      }
  }

} ?> </code>

See Also

mqtt_publish() - Publish to an MQTT channel

uphp/functions/mqtt_subscribe.1506510343.txt.gz · Last modified: 2021/09/13 05:56 (external edit)