It's not that hard to use a part of pass2php for this:
script_device_pass2php.lua (goes in the lua scripts folder):
Code: Select all
for d,s in pairs(devicechanged)
do
os.execute('curl -X POST -d "d='..d.."&s="..s..'" http://127.0.0.1/secure/pass2php.php &')
end
commandArray={}
return commandArray
The lua script posts the new status in realtime to the pass2php page. There they can be catched with $_REQUEST['d'] for the devicename and $_REQUEST['s'] for the status.
in /var/www/html/secure/pass2php.php just the minimal to catch those:
Code: Select all
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$device=$_POST['d'];
$status=$_POST['s'];
//Do something with the vars...
}
?>
For example post to a MySQL database the timestamp, devicename and the status:
Code: Select all
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$device=$_POST['d'];
$status=$_POST['s'];
$db=new mysqli('server','user','password','database');
if($db->connect_errno>0){die('Unable to connect to database ['.$db->connect_error.']');}
$query="INSERT IGNORE INTO `statusses` (`stamp`,`device`,`status`) VALUES (CURRENT_TIMESTAMP,'$device','$status');";
if(!$result=$db->query($query)){die('There was an error running the query ['.$query.'-'.$db->error.']');}
}
?>
Problem here is that every status change would be stored in the database and so the database would grow fast. If you still want to add data in realtime but only for a few devices you add an if statement:
Code: Select all
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$device=$_POST['d'];
$status=$_POST['s'];
if(in_array($device,array('devicename1','devicename2','devicename3','devicename4','devicename5'))){
$db=new mysqli('server','user','password','database');
if($db->connect_errno>0){die('Unable to connect to database ['.$db->connect_error.']');}
$query="INSERT IGNORE INTO `statusses` (`stamp`,`device`,`status`) VALUES (CURRENT_TIMESTAMP,'$device','$status');";
if(!$result=$db->query($query)){die('There was an error running the query ['.$query.'-'.$db->error.']');}
}
}
?>
What I did is posting the data to temporary files and then grab them together to put them in one record. This is then very easy to grab the data and create a grap with several temperatures. /var/log/cache is a tmpfs folder
Code: Select all
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$device=$_POST['d'];
$status=$_POST['s'];
if(in_array($device,array('buiten_temp','living_temp','badkamer_temp','kamer_temp','tobi_temp','alex_temp','zolder_temp'))){
setstatus($device,$status);
}
}
function setstatus($name,$value){file_put_contents('/var/log/cache/s'.$name.'.cache',$value);}
?>
And then combine the several statusses each minute with a cron job:
Code: Select all
<?php
$buiten_temp=status('buiten_temp');
$stamp=sprintf("%s",date("Y-m-d H:i"));
$living=status('living_temp');
$badkamer=status('badkamer_temp');
$kamer=status('kamer_temp');
$tobi=status('tobi_temp');
$alex=status('alex_temp');
$zolder=status('zolder_temp');
$query="INSERT IGNORE INTO `temp` (`stamp`,`buiten`,`living`,`badkamer`,`kamer`,`tobi`,`alex`,`zolder`) VALUES ('$stamp','$buiten_temp','$living','$badkamer','$kamer','$tobi','$alex','$zolder');";
$db=new mysqli('server','user','password','database');
if($db->connect_errno>0)die('Unable to connect to database ['.$db->connect_error.']');
if(!$result=$db->query($query))die('There was an error running the query ['.$query.'-'.$db->error.']');
?>