26-04-2020, 17:48
(26-04-2020, 13:26)bombenm Wrote: ... The issue is that updating the AMQ scretch with the same data, the AirQuality_log.txt did not receive any upgrate. It is my first time with the PHP files. What I wrong? Thank you.
Hi bombenm,
That is the normal behaviour of the example script published some posts above.
If you want to add a new line on the log file for each register, edit the example script and just change this line:
Code:
$fp = fopen($logfile, 'w');to
Code:
$fp = fopen($logfile, 'a');Please note that this isn't the best way of keep a log of the AQM data, the better way is to use a script which insert each new received data in a SQL data base.
Here is another example script, which inserts data on an SQL table, please note that you should first create the SQL db and the table.
Also note this example script automatically delete records old than "retainUnit"
PHP Code:
<?php
// AirQuality_LogToSQL v1.2
// 14/06/2018
// Add some description here...
// EDIT THIS NEXT SECTION CAREFULLY
// ----------------------------------------------------------------
// Your security key you will pass in the URL, change this to
// something unique to you
//$key = 'letmein';
//
// The server host name or number running your MySQL database
// usually 127.0.0.1 or localhost will suffice
$dbhost = '127.0.0.1';
//
// The username used to log-in to your database server
//$dbuser = 'user';
//
// The password used to log-in to your database server
//$dbpassword = 'password';
//
// The name of the MySQL database we will store the tables in
//$database = 'database';
//
// A better way of entering your login details is to put them in a separate script
// and include this here. This script should be placed in an area accessible to PHP
// but not web users. Your login details will not then be exposed by crashing this
// script.
// e.g. ...
include ('/FULL_PATH_TO/db_rw_details.php');
// Enable debug messages? Always disable for production use
$debug = false;
date_default_timezone_set('Europe/Lisbon');
$timestamp = date('Y-m-d H:i:s');
$logfile = './logs/AirQuality_Log.txt';
$param_table = 'AirQuality'; // This is the name of the table to store AQM data
$param_retainVal = 3; // Amount used by the retainUnit
$param_retainUnit = 'week'; // Valid Values: 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year');
$param_privatekey = '';
$param_PM25 = 0;
$param_PM100 = 0;
$param_AQI = 0;
$param_CO2 = 0;
$param_DevID = "N/A";
$lf = "\n";
if (!empty($_GET['privatekey']))
{
$param_privatekey = $_GET['privatekey'];
}
if (!empty($_GET['PM25']))
{
$param_PM25 = $_GET['PM25'];
}
if (!empty($_GET['PM100']))
{
$param_PM100 = $_GET['PM100'];
}
if (!empty($_GET['AQI']))
{
$param_AQI = $_GET['AQI'];
}
if (!empty($_GET['CO2']))
{
$param_CO2 = $_GET['CO2'];
}
if (!empty($_GET['DevID']))
{
$param_DevID = $_GET['DevID'];
}
// check for the 'private' key
if ($key !== '' && $param_privatekey !== $key) {
die('Error: Failed security key check:' . $param_privatekey);
}
$conn = new mysqli($dbhost, $dbuser, $dbpassword, $database);
if ($conn->connect_error)
{
//die("Erro ao conectar ao servidor MySQL: " . $conn->connect_error);
}
$sql = "INSERT INTO " . $param_table . " (LogDateTime, DeviceID, PM25, PM100, AQI, CO2) VALUES ('" . $timestamp . "', '" . $param_DevID . "', '" . $param_PM25 . "', '" . $param_PM100 . "', '" . $param_AQI . "', '" . $param_CO2 . "')";
$conn->query($sql);
if ($param_retainVal > 0)
{
$DeleteEntries = "DELETE IGNORE FROM $param_table WHERE LogDateTime < DATE_SUB(NOW(), INTERVAL $param_retainVal $param_retainUnit )";
$conn->query($DeleteEntries);
}
$conn->close();
$fp = fopen($logfile, 'w');
chmod($logfile, 0770);
fwrite($fp, '['.$timestamp.'];'.$param_DevID .';'.$param_PM25 .';'.$param_PM100 .';'.$param_AQI .';'.$param_CO2 .';');
fclose($fp);
?>Code above may / should be changed / adapted for each personal case.
Below is an example of the db_rw_details.php file
PHP Code:
<?php
// EDIT THIS NEXT SECTION CAREFULLY
// ----------------------------------------------------------------
// Your security key you will pass in the URL, change this to
// something unique to you
$key="YOUR_KEY";
//
// The server host name or number running your MySQL database
// usually 127.0.0.1 or localhost will suffice
$dbhost = "127.0.0.1";
//
// The username used to log-in to your database server
$dbuser ="DB_USER";
//
// The password used to log-in to your database server
$dbpassword ="DB_PASSWORD";
//
// The name of the MySQL database we will store the tables in
$database ="DB_NAME";
?>Rgs

