michelle1404 Posted September 21, 2021 Share Posted September 21, 2021 I am developing a script for my client where i should be displaying data continuously coming from MQTT and my script is in PHP. Now i can publish and subscribe to topics but only after refreshing i can see the published data in my page. Do i need to use any script for this or how is it? Here is my subscribe page <?php require('phpMQTT.php'); $server = 'localhost'; // change if necessary $port = 1883; // change if necessary $username = '*****'; // set your username $password = '******'; // set your password $client_id = 'MyClient'; // make sure this is unique for connecting to sever - you could use uniqid() $mqtt = new Bluerhinos\phpMQTT($server, $port, $client_id); if(!$mqtt->connect(true, NULL, $username, $password)) { exit(1); } $mqtt->debug = true; $topics['xf/printservice/registration'] = array("qos"=>0, "function"=>"procmsg"); $mqtt->subscribe($topics,0); $start_time = time(); $done = 0; while (!$done && !hasTimedout() && $mqtt->proc()) { } $mqtt->close(); function procmsg($topic,$msg) { global $done; $done = 1; echo "Msg Recieved: ".date("r")."\nTopic:{$topic}\n$msg\n"; } function hasTimedout() { global $start_time; return (time() - $start_time > 10); } I am running this in localhost. How to keep displaying data without refreshing the page . is there any tutorial/script available ? please suggest. Quote Link to comment https://forums.phpfreaks.com/topic/313790-display-mqtt-data-on-php-page-without-refreshing-the-page/ Share on other sites More sharing options...
ginerjm Posted September 21, 2021 Share Posted September 21, 2021 I"m going to guess that you can do this with JS but not php. Quote Link to comment https://forums.phpfreaks.com/topic/313790-display-mqtt-data-on-php-page-without-refreshing-the-page/#findComment-1590205 Share on other sites More sharing options...
michelle1404 Posted September 21, 2021 Author Share Posted September 21, 2021 Can you please any kind of tutorial or example for this to achieve? I couldn't get anything on this. Quote Link to comment https://forums.phpfreaks.com/topic/313790-display-mqtt-data-on-php-page-without-refreshing-the-page/#findComment-1590206 Share on other sites More sharing options...
ginerjm Posted September 21, 2021 Share Posted September 21, 2021 Why not move your post to the js forum instead? Quote Link to comment https://forums.phpfreaks.com/topic/313790-display-mqtt-data-on-php-page-without-refreshing-the-page/#findComment-1590207 Share on other sites More sharing options...
Barand Posted September 21, 2021 Share Posted September 21, 2021 Does this help? Quote Link to comment https://forums.phpfreaks.com/topic/313790-display-mqtt-data-on-php-page-without-refreshing-the-page/#findComment-1590208 Share on other sites More sharing options...
michelle1404 Posted September 21, 2021 Author Share Posted September 21, 2021 (edited) @Barand. Thanks. Atleast i found something to try on now. I will try this to integrate with my php code. Edited September 21, 2021 by michelle1404 Quote Link to comment https://forums.phpfreaks.com/topic/313790-display-mqtt-data-on-php-page-without-refreshing-the-page/#findComment-1590209 Share on other sites More sharing options...
Barand Posted September 21, 2021 Share Posted September 21, 2021 I have moved it for you. Quote Link to comment https://forums.phpfreaks.com/topic/313790-display-mqtt-data-on-php-page-without-refreshing-the-page/#findComment-1590210 Share on other sites More sharing options...
Barand Posted September 21, 2021 Share Posted September 21, 2021 3 minutes ago, michelle1404 said: I will try this to integrate with my php code. Good luck with that. The MQTT is javascript, which runs in the client browser. PHP runs on the server. Quote Link to comment https://forums.phpfreaks.com/topic/313790-display-mqtt-data-on-php-page-without-refreshing-the-page/#findComment-1590211 Share on other sites More sharing options...
michelle1404 Posted October 9, 2021 Author Share Posted October 9, 2021 @Barand Thanks, the logic worked but i have used this code. Now i want to get the data to variable in php thru javascript instead of displaying it based on id. as i do this <script> var res = "helloo"; </script> <?php $variable = "<script>document.write(res)</script>"; echo $variable; ?> it works. so as the same if it inside function onMessageArrived(r_message){ out_msg="Message received "+r_message.payloadString+"<br>"; out_msg=out_msg+"Message received Topic "+r_message.destinationName; //console.log("Message received ",r_message.payloadString); console.log(out_msg); document.getElementById("messages").innerHTML =out_msg; var topic=r_message.destinationName; if(topic=="house/outside-light") { document.getElementById("outside-light").innerHTML =r_message.payloadString; } if(topic=="house/outside-temperature") { var ot = r_message.payloadString; //document.getElementById("outside-temp").innerHTML =r_message.payloadString; } } <?php $test="<script>document.write(ot)</script>"; echo $test; ?> it won't work. Basically i want to get the data into a php variable. where i am going wrong, please suggest. Quote Link to comment https://forums.phpfreaks.com/topic/313790-display-mqtt-data-on-php-page-without-refreshing-the-page/#findComment-1590821 Share on other sites More sharing options...
michelle1404 Posted October 13, 2021 Author Share Posted October 13, 2021 I could't get the solution for this. How can it get this value into a variable? Quote Link to comment https://forums.phpfreaks.com/topic/313790-display-mqtt-data-on-php-page-without-refreshing-the-page/#findComment-1590973 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.