variables within a function - doubts

Asked

Viewed 33 times

1

Good afternoon,

I want to put together a script that:

  • subscribe to a topic in a Broker mqtt
  • payload of this topic is used for a database query.
  • return of this query will be used to publish another topic.

to do so, I found a mqtt class that has the subscription function, and displays payload. using the example, expand with the database query.

alone, everything works. but putting together, presents error.

the script:

.... definições ......
mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id);
if(!$mqtt->connect(true, NULL, $username, $password)) {
  echo "Failed to connect to Mqtt: ";
  exit(1);
}    

$con = new mysqli($mysql_host, $mysql_user, $mysql_pass, $mysql_mydb );
if ($con -> connect_errno) {
   echo "Failed to connect to MySQL: " . $con -> connect_error;
   exit(2);
}
$con->set_charset('utf8mb4'); // always set the charset

$mqtt->debug = true;

$topics['rfid/card/ping'] = array('qos' => 0, 'function' => 'procMsg');
$mqtt->subscribe($topics, 0);
while($mqtt->proc()) {
}
$mqtt->close();

function procMsg($topic, $msg){
 echo 'Msg Recieved: ' . date('r') . "\n";
 echo "Topic: {$topic}\n";
 echo "Payload: {$msg}\n";
 echo $msg;
 echo "\n";
 // query con
 //$tagid = "39EAB06D";
 $query = "SELECT name, id  FROM rfidtags WHERE id = ?";
 $stmt = $con->prepare($query);
 $stmt->bind_param('s', $msg);
 $stmt->execute();
 $stmt->store_result();
 $stmt->bind_result($name, $id);
 if ($stmt->fetch()) {
    echo "$name $id\n";
    echo "1";
 } else {function procMsg
    echo "failed to fetch data\n";
    echo "2";
 }
 $con->close();
}

executing the code, the following errors come out:

paulo@hp15pw:~/TOOLS/MQTT-PHP$ php subscribe-query-publish.php 
Mon, 19 Jul 2021 12:22:32 -0300: Received CMD: 3 (PUBLISH)
Mon, 19 Jul 2021 12:22:32 -0300: Fetching: 24 bytes
Msg Recieved: Mon, 19 Jul 2021 12:22:32 -0300
Topic: rfid/card/ping
Payload: 39EAB06D
39EAB06D
PHP Notice:  Undefined variable: con in /home/paulo/TOOLS/MQTT- 
PHP/subscribe-query-publish.php on line 51
PHP Fatal error:  Uncaught Error: Call to a member function prepare() on 
null in /home/paulo/TOOLS/MQTT-PHP/subscribe-query-publish.php:51
Stack trace:
#0 [internal function]: procMsg()
#1 /home/paulo/TOOLS/MQTT-PHP/phpMQTT.php(482): call_user_func()
#2 /home/paulo/TOOLS/MQTT-PHP/phpMQTT.php(547): Bluerhinos\phpMQTT- 
>message()
#3 /home/paulo/TOOLS/MQTT-PHP/subscribe-query-publish.php(36): 
Bluerhinos\phpMQTT->proc()
#4 {main}
thrown in /home/paulo/TOOLS/MQTT-PHP/subscribe-query-publish.php on line 51

understood that in Function procMsg the variables defined are not recognized.

while($mqtt->proc()) { ) is the loop that subscribes to the topic in Broker mqtt.

within this loop, once $msg or be the payload is displayed, check it into the bunch of data.

how can I execute the BD query within this function, returning $name and $id to the main?

  • Actually the question would be, "Why the variable $con is undefined?

  • why it is being used inside a function - Function procMsg($topic, $msg) - the question is to do it inside the function.

  • Passes it as argument

  • ok, how? this is the question.

  • Where the function is being called procMsg()?

  • $Topics['rfid/card/ping'] = array('Qos' => 0, 'Function' => 'procMsg'); $mqtt->subscribe($Topics, 0); while($mqtt->proc()) {}

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.