Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscribe query and publish - how? #126

Open
pw44 opened this issue Jul 19, 2021 · 3 comments
Open

subscribe query and publish - how? #126

pw44 opened this issue Jul 19, 2021 · 3 comments

Comments

@pw44
Copy link

pw44 commented Jul 19, 2021

I'm trying the following:

subscribe to a topic
by paylod make a query to a database
publish to another topic.

`
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()) {
// echo "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 {
echo "failed to fetch data\n";
echo "2";
}
$con->close();
}
`

but the error:

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

any clue?

@anhlephuoc
Copy link

The $con variable you used in the procMsg() function is a local version because it is undeclared. An uninitialised $con is used.
If you want to refer to the GLOBAL version of the $con you initialised earlier, then you should declare as such in the function procMsg().

@pw44
Copy link
Author

pw44 commented Jul 20, 2021

hi, thx for answering.

solved it in a much easier way:

`
<?php

 require('phpMQTT.php');


 $server = 'hp15pw';     // change if necessary
 $port = 1883;                     // change if necessary
 $username = 'mqttuser';                   // set your username
 $password = 'mqttpass';                   // set your password
 $client_id = 'phpMQTTDB'; // make sure this is unique for connecting to sever - you could use uniqid()

 $mysql_host = 'localhost';
 $mysql_port = '';
 $mysql_user = 'dbuser';
 $mysql_pass = 'dbpass';
 $mysql_mydb = 'rfidcards';

 $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;

 while (true) {

       $msg = $mqtt->subscribeAndWaitForMessage('rfid/card/ping', 0);
       echo "$msg";

       // 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";
           $pong="1";
       } else {
           echo "failed to fetch data\n";
           $pong= "2";
       }
       $mqtt->publish('rfid/card/pong', "$pong", 0, false);

   }

  $con->close(); 
  $mqtt->close();

`

@anhlephuoc
Copy link

The problem here is with your own basic PHP coding. Nothing to do phpMQTT code.
You have received a suggestion for fixing you program; and you also have your own fix.
May be it's time to close the issue!. No body else can fix your own code for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants