CodeIgniter Forums
Periodic view update - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Periodic view update (/showthread.php?tid=75090)



Periodic view update - TSY - 12-19-2019

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>


RE: Periodic view update - badger - 12-20-2019

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


RE: Periodic view update - TSY - 12-21-2019

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