• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Periodic view update

#1
Hello,

I have been working with home automation (Raspberry with Firebird database) and made basic web page which shows data from database. There is also more "real time" communication with Raspberry via socket communication. Below are my controller and one view (current_status_view) which I would like to be updated for example on every second. 
I have been reading  of javascript and JQuery and XMLHttpRequest but I'm not sure how to implement this periodic update feature.
Any help is appreciated.

File Db_controller.php

<?php
class Db_controller extends CI_Controller
{
    function index()
{
error_reporting(1);
$this->load->view('header');

/* Get current power consumption and temperature */
$currentStatus = getOnlineData();
$this->load->view('current_status_view',$currentStatus);


/* Min and max temperatures on different years */
$this->load->model('maxmin_year_model');         
$data['records']=$this->maxmin_year_model->maxMinYearTemperaturesGet();
$this->load->view('maxmin_year_view',$data);

/* Max and min temperatures from the beginning of time */
$this->load->model('maxmin_temp_model');         
$data['records']=$this->maxmin_temp_model->maxMinTemperaturesGet();
$this->load->view('maxmin_temp_view',$data);


/* Temperature history */
$this->load->model('db_model');         
$data['records']=$this->db_model->showTemperatureLog();
$this->load->view('db_view',$data);

$this->load->view('footer');
    }
}

function getOnlineData()
{
$reply = ['???','???'];
$server = '127.0.0.1';
$port  = 9930;
$udpFailure = 0;

/* Communication with c-program running on Raspberry ... */
if(($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
/* Set timeout */
socket_set_option($sock,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>3, "usec"=>0));
$request ='Send data';
$requestLen = strlen($request);

//Send message to the server
if(socket_sendto($sock, $request , strlen($request) , 0 , $server , $port))
{
//Receive reply from server
if(socket_recv ( $sock , $reply , 2045 , MSG_WAITALL ) === FALSE)
$udpFailure = 1;
}

socket_close($sock);
}

/* Convert comma separated values in string to array */
/* array[0] = Power as watts, array[1] = current temperature, [hundreths of celcius] */
if ($udpFailure == 0)
{
$auxArray = str_getcsv($reply);
$currentStatus = array("watts" => $auxArray[0],"temperature" => $auxArray[1]/100);
}
else
{
$currentStatus = array("watts" => 'Fault',"temperature" => 'Fault');
}

return $currentStatus;
}
?>


File current_status_view.php

<html>

<head>

</head>
<style>
td {text-align:center;}

table { margin: 0px; padding: 0px;}

h1, h2, h3, h4, h5, h6{
margin-top:20px;
margin-bottom:20px;
}
</style>

<h3>Just now...</h3>
<body>
    <pre>
      <?php
  echo "<table border=5><tr><th>Power [W]</th><th>Temperature</th></tr>";
      echo "<td>".$watts."</td><td>".$temperature."</td></tr>";
      echo "</table>";
      ?>
    </pre>
</body>

</html>
Reply

#2
I use ci with mqtt as messaging service. When a door or whatever opens, a message is published. Browsers subscribe to whatever you want to display. So on my browser displaying my house alarm, I can see in real time the moment a door opens/closes, no need for a periodic check.
Bill
Reply

#3
(12-20-2019, 02:50 AM)badger Wrote: I use ci with mqtt as messaging service. When a door or whatever opens, a message is published. Browsers subscribe to whatever you want to display. So on my browser displaying my house alarm, I can see in real time the moment a door opens/closes, no need for a periodic check.
Bill

Thank’s badger, I will check that  mqtt solution...
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.