User Tools

Site Tools


uphp:functions:mqtt_subscribe

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
uphp:functions:mqtt_subscribe [2017/09/27 10:46]
admin
uphp:functions:mqtt_subscribe [2021/09/13 05:57] (current)
Line 8: Line 8:
  
 <well size="sm"><html> <well size="sm"><html>
-<span style="font-size:125%">mqtt_publish (+<span style="font-size:125%">mqtt_subscribe (
 <span style="color:green">string <span style="color:green">string
 <span style="color:blue">$channel<span style="color:black">, <span style="color:blue">$channel<span style="color:black">,
Line 30: Line 30:
 <code php> <code php>
 <?  <? 
- 
 // Use the # as a wildcard so any messages to /cluster/1/ or its sub channels will be received // Use the # as a wildcard so any messages to /cluster/1/ or its sub channels will be received
  
Line 36: Line 35:
 ?> ?>
 </code> </code>
 +
 +====Example Callback====
 +Callbacks can use the ''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>
 +
 +====Note=====
 +On WattmonPRO & MINI the maximum packet size is 128 bytes.  On WattmonMega, M2 & Mini2 the maximum packet size is 2048 bytes.
  
 ====See Also==== ====See Also====
uphp/functions/mqtt_subscribe.1506509202.txt.gz · Last modified: 2021/09/13 05:56 (external edit)