-
Notifications
You must be signed in to change notification settings - Fork 460
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
Comments
The $con variable you used in the procMsg() function is a local version because it is undeclared. An uninitialised $con is used. |
hi, thx for answering. solved it in a much easier way: `
` |
The problem here is with your own basic PHP coding. Nothing to do phpMQTT code. |
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-
any clue?
The text was updated successfully, but these errors were encountered: