I am working with a raspberry pi3 lamp server to displaying the data from two temperatures sensors AM2302
I am following a tutorial to display the data in a gauge format, here is the link to the video https://www.youtube.com/watch?v=WyUM--RGLH0 ( it is in Spanish, I think that an expert with this stuff will understate the video without a problem. But I don't know too much about this stuff, I am amateur and really noob about coding.
I am using two PHP files one to read the data and display in the JSON format a tne another one to show it in the gauges. Right now I am able to read the data but the gauges don't work. they stay at zero enter image description here
I think it is how the data json is show it: my result is:
{"temperature":"27.8","humidity":"61.2"}
but in the tutorial the result format is
[{"temperature":"27.8","humidity":"61.2"}]
I guess is that my JSON is not displaying the square brackets as the video show at: the 1:07 Mins from the video
here is my index.php file
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Medidor Temperatura, Humedad</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js">
</script>
<script type="text/javascript"
src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['gauge']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Label', 'Value'],
['humidity', 0],
['temperature', 0]
]);
var options = {
width: 400, height: 400,
redFrom: 90, redTo: 100,
yellowFrom:75, yellowTo: 90,
minorTicks: 5
};
var chart = new
google.visualization.Gauge(document.getElementById("Medidores"));
chart.draw(data, options);
setInterval(function() {
var JSON=$.ajax({
url:"http:192.168.0.115/sensores.php",
dataType: 'json',
async: false}).responseText;
var Respuesta=jQuery.parseJSON(JSON);
data.setValue(0, 1,Respuesta[0].humidity);
data.setValue(1, 1,Respuesta[0].temperature);
chart.draw(data, options);
}, 1300);
}
</script>
</head>
<body>
<div id="Medidores" ></div>
here is the file that read the data is called: sensores.php ( the one that collects the data from the sensors).
<?php
// Settings
// host, user and password settings
$host = "localhost";
$user = "logger";
$password = "123456";
$database = "temperatures";
//how many hours backwards do you want results to be shown in web page.
$hours = 24;
// make connection to database
$connectdb = mysqli_connect($host,$user,$password)
or die ("Cannot reach database");
// select db
mysqli_select_db($connectdb,$database)
or die ("Cannot select database");
// sql command that selects all entires from current time and X hours backward
$sql="SELECT temperature, humidity FROM temperaturedata where sensor = 'Exterior' and dateandtime >= (NOW() - INTERVAL $hours HOUR) order by dateandtime desc LIMIT 1";
// set query to variable
$temperatures = mysqli_query($connectdb,$sql);
// create content to web page
?>
<?php
// loop all the results that were read from database and "draw" to web page
while($temperature=mysqli_fetch_assoc($temperatures))
$json=json_encode($temperature);
echo $json;
?>
I wanna to display the data from the two sensors in the index.php file but I even with one I will feel ok
In the while loop, build an array the []
indicators in JSON mean array.
It makes sence also because you could have more than one result from the query anyway.
$temps = [];
while($temperature = mysqli_fetch_assoc($temperatures)) {
$temps[] = $temperature;
}
echo json_encode($temps);;