I am trying to send data from my arduino to Highcharts via ethernet following this two tutorials: 1.http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-gauge/ 2.Highcharts live data
As I am very new to javascript could someone explain to me what this code does:
var series = chart.series[0] //(what is series[0]??? What is the "[0]" for?)
Here I am sending also my modified index file:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Arduino SD Card Web Page using Ajax</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script>
<script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>
<script>
var chart;
function GetArduinoInputs()
{
nocache = "&nocache=" + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
var analog = this.responseText;
var d = new Date();
var date = d.getTime();
var point = [date, analog];
var series = chart.series[0],
shift = series.data.length > 20; // shift if the series is
// longer than 20
// add the point
chart.series[0].addPoint(point, true, shift);
}
}
}
}
request.open("GET", "ajax_inputs" + nocache, true);
request.send(null);
setTimeout('GetArduinoInputs()', 1000);
}
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'spline',
events: {
load: GetArduinoInputs
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150,
maxZoom: 20 * 1000
},
yAxis: {
minPadding: 0.2,
maxPadding: 0.2,
title: {
text: 'Value',
margin: 80
}
},
series: [{
name: 'Random data',
data: []
}]
});
});
</script>
</head>
<body onload="GetArduinoInputs()">
<div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>
<div style="width: 800px; margin: 0 auto"></div>
</body>
</html>
My arduino is sending just a value e.g 22. The result is that Highcharts behave erratically with no values displayed on it . (although the chart is rolling with time passing by on x-axis).
What could be wrong on this code?
Any help would be much appreciated!
Thank you in advance!
chart.series[0]is the first element in the series array. If you have only one series of data in the chart, this would be it. If you have multiple series of data they would bechart.series[0],chart.series[1], etc.