<?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);
?>