Jump to content

Recommended Posts

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.

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.

  • 3 weeks later...

@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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.