<?php

    ////include("access.php");
	////session_start(); //We need to have static variables
    ////$Esp8266SensorURL = $esp8266_9702_GetSensors_URL;

	//---------------------------------------------------------------------
	//Functions
	//---------------------------------------------------------------------
	//
	//---------------------------------------------------------------------

    //Insert "spaces" spaces
	function insertSpace($spaces) {
					for ($cnt=0; $cnt<$spaces; $cnt++) {
									$sp .= "&nbsp";
					}
					return $sp;
	}
	//---------------------------------------------------------------------
	//Connect to database
	//---------------------------------------------------------------------

	$link = mysqli_connect("localhost", $mysqlUser, $mysqlPass, $mysqlUser);

	if (mysqli_connect_error()) {
					die("Could not connect to database");
	}
	//---------------------------------------------------------------------
	//Get latest values from database
	//---------------------------------------------------------------------
	$query = "SELECT * FROM temperature ORDER BY id DESC LIMIT 1";

	if($result=mysqli_query($link, $query)) {
					$row = mysqli_fetch_array($result);
					$last_id = $row['id'];
					$last_in = $row['inside'];
					$last_ou = $row['outside'];
					$last_at = $row['attic'];
					$last_hu = $row['humidity'];
	}
	//---------------------------------------------------------------------
	//Get Sensor Data
	//---------------------------------------------------------------------

	//----------------------------------------
	//-----Get current LOCAL time
	//----------------------------------------
	$now = time();
	date_default_timezone_set('America/Los_Angeles');
	$localtime_assoc = localtime($now, true);

	//Time
	$now_hr = sprintf('%02u',$localtime_assoc['tm_hour']);
	$now_mn = sprintf('%02u',$localtime_assoc['tm_min']);
	$now_sc = sprintf('%02u',$localtime_assoc['tm_sec']);
	$now_tm = $now_hr.":".$now_mn.":".$now_sc;

	//Date
	$now_yr = $localtime_assoc['tm_year']+1900;
	$now_mo = sprintf('%02u',$localtime_assoc['tm_mon']+1);
	$now_dy = sprintf('%02u',$localtime_assoc['tm_mday']);
	$now_dt = $now_yr."-".$now_mo."-".$now_dy;

	//Get Temperatures
	$temp_in = getEsp8266Sensor($Esp8266SensorURL,"Y","DS_TempInside");
	$temp_ou = getEsp8266Sensor($Esp8266SensorURL,"N","DS_TempOutside");
	$temp_at = getEsp8266Sensor($Esp8266SensorURL,"N","DS_TempAttic");

	//Get Humidity
	$temp_hu = getEsp8266Sensor($Esp8266SensorURL,"N","DH_Humidity");

	//Get Pressure
	$temp_pr = getEsp8266Sensor($Esp8266SensorURL,"N","B_Pressure");

	//Get Moorpark Pressure
	$temp_pr_mpk = getMpkBaroPres();

	//Get Wifi Restarts
	$temp_wifi = getEsp8266Sensor($Esp8266SensorURL,"N","WIFI_Restarts");

	//Get Free Heap and ms since ESP8266 started
	$temp_hp = getEsp8266Sensor($Esp8266SensorURL,"N","SYS_Heap");
	$temp_tm = getEsp8266Sensor($Esp8266SensorURL,"N","SYS_Time");

	//Insert a record into mySql
	$query = "INSERT INTO `temperature` (`inside`,`outside`,`attic`,`humidity`,`time`,`date`,`FreeHeap`,`SysTime`,`fetches`,`Pressure`,`PressureMpk`,`WifiRestarts`)
	VALUES('".$temp_in."','".$temp_ou."','".$temp_at."','".$temp_hu."','".$now_tm."','".$now_dt."','".$temp_hp."','".$temp_tm."','".$_SESSION['fetches']."','".$temp_pr."','".$temp_pr_mpk."','".$temp_wifi."')";

	//Add Data to ThingsSpeak Channel 33251
	$ThingsSpeakURL = "https://api.thingspeak.com/update?key=".$thingspeak33251."&field1=".$temp_in."&field2=".$temp_ou."&field3=".$temp_at."&field4=".$temp_hu."&field5=".$temp_tm."&field6=".$temp_hp."&field7=".$temp_wifi."&field8=".$temp_pr;
	get_fcontent($ThingsSpeakURL);

	//Get Thingspeak Channel 45834 field1:Max System Time)
	$ThingsSpeakURL = "https://api.thingspeak.com/channels/45834/fields/field1/last?api_key=JOBQ5SEVCFNRLRV4";
	$max_up = getEsp8266Sensor($ThingsSpeakURL,"Y","field1");
	//echo "Thingspeak: ".$max_up."<br>";
	//echo "ESP8266:    ".$temp_tm."<br>";

	if($temp_tm > $max_up) {
		//Update ThingSpeak field1
		$ThingsSpeakURL = "https://api.thingspeak.com/update?key=".$thingspeak45834."&field1=".$temp_tm;
		get_fcontent($ThingsSpeakURL);
	}


	//Report Results
	if ($result=mysqli_query($link, $query)) {
		echo('<br>Success<br>');
	}
	else {
		echo('<br>Failed<br>');
	}
	//echo("Moorpark Baro: ".$temp_pr_mpk."<br>");

?>