1

I'm new here and not really experienced with highcharts / javascripts and variable data from SQL.

The Situation is that I've got three Pi3 with DHT22 Sensor, the Scripts on the Pis working well, the data is stored to my NAS into a Maria5 DB.

I've managed to display the data (date/time, temperature, humidity) in a table for each sensor: Data shown in a table

I've also managed to display the date/time and temperature (OR humidity) of all three Pi3Sensors (data) in separate highcharts on one page: three charts with temperature

What I want to do now is to show the temperature AND humidity in each highchart of the single Pi's charts. When I enter the 2nd yAxis Data (Series 2) for the humidity manually, then it's working: 2nd data series entered manually

There must be something wrong with the variables of temperature and humidity data. I searched a lot online but wasn't able to find something that helps, or I'm just not able to understand it the right way...

In the DB are the fields 'dateandtime' (2018-04-18 15:05:00), 'unix_timestamp' (1524056702), 'sensor' (value "1", "2" or "3" for each sensor), 'temperature' and 'humidity.

The html page should be alright (index.html);

<html>
<head>
<title>Temperatur Uebersicht</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<script type="text/javascript" src="data.js" ></script>
</head>
<body>
<div id="chart1" style="height: 400px; margin: 0 auto"></div>
</body>
</html>

The php file (values1.php) with the SQL connection / statement;

<?php
$con = mysql_connect("localhost","user","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("piSensors", $con);
$result = mysql_query("SELECT * 
                       FROM `temperaturedata` 
                       WHERE sensor=1 
                       ORDER BY unix_timestamp DESC
                       LIMIT 288 ") 
                       or die ("Connection error");
while($row = mysql_fetch_array($result)) {
echo $row['unix_timestamp'] . "/" . $row['temperature'] . "/" . $row['humidity'] . "/" ;
}
mysql_close($con);
?>

...and the javascript (data.js) with the two series. The data of the 2nd serie is entered manually. Here I don't know how to do it with the variables, that the actual data (of humidity) from sql is also shown in the same chart like the temperature:

  $(function() {

    var x_values1 = [];
    var y_values1 = [];

    var switch1 = true;
    $.get('values1.php', function(data1) {

        data1 = data1.split('/');
        for (var i in data1)
        {
            if (switch1 == true)
            {
                var ts = timeConverter(data1[i]);
                x_values1.push(ts);
                switch1 = false;
            }
            else
            {
                y_values1.push(parseFloat(data1[i]));
                switch1 = true;
            }

        }
        x_values1.pop();

        $('#chart1').highcharts({
            chart : {
                type : 'spline'
            },
            title : {
                text : 'Pi1'
            },
            subtitle : {
                text : 'PiSensor # 1'
            },
            xAxis : {
                title : {
                    text : 'Datum & Zeit'
                },
                categories : x_values1,
                reversed : true
            },
            yAxis : [{
                title : {
                    text : 'Temperatur'
                },  
                labels : {
                    formatter : function() {
                        return this.value + ' C'
                    }
                }
            }, {
                lineWidth: 1,
                opposite: true,
                title: {
                    text: 'Luftfeuchtigkeit'
                },
                labels : {
                    formatter : function() {
                        return this.value + ' %'
                    }
                }
            }],
            tooltip : {
                crosshairs : true,
                shared : true,
                valueSuffix : ''
            },
            plotOptions : {
                spline : {
                    marker : {
                        radius : 4,
                        lineColor : '#666666',
                        lineWidth : 1
                    }
                }
            },
            series : [{

                name : 'Temperatur',
                data : y_values1,
                tooltip : {
                    valueSuffix: ' C'
                }
            }, {
                name : 'Luftfeuchtigkeit',
                data : [20,30,40,20],
                yAxis : 1,
                tooltip : {
                    valueSuffix : ' %'
                }
            }]
        });
    });
});


function timeConverter(UNIX_timestamp){
  var a = new Date(UNIX_timestamp * 1000);
  var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  var year = a.getFullYear();
  var month = months[a.getMonth()];
  var date = a.getDate();
  var hour = a.getHours();
  var min = a.getMinutes() < 10 ? '0' + a.getMinutes() : a.getMinutes();
  var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min ;
  return time;
}

If anyone could help out or route me on the right way, I would be really thankfull!

Edit:

I tried to manage the highchart with the two yAxis data series with this variant. Output of "values1.php" (unix_timestamp / temperature / humidity):

1524060302/27.3/36.4/1524060002/27.3/36.4/1524059702/27.3/36.4/

Output of "values1.php" with dateandtime (dateandtime / temperature / humidity):

2018-04-18 16:05:00/27.3/36.4/2018-04-18 16:00:00/27.3/36.4/2018-04-18 15:55:00/27.3/36.4/

Picture of the DB structure: DB structure

Picture of the DB values: DB values

Edit (19.04.2018 / 12:25)

I changed the php code like you said:

<?php
$con = mysql_connect("localhost","user","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("piSensors", $con);
$result = mysql_query("SELECT * 
                       FROM `temperaturedata` 
                       WHERE sensor=1 
                       ORDER BY unix_timestamp DESC
                       LIMIT 288 ") 
                       or die ("Connection error");
while($row = mysql_fetch_array($result)) {
echo $row['unix_timestamp'] . "/" . $row['temperature'] . "/" . $row['humidity'] . ";" ;
}
mysql_close($con);
?>

The output of the php file is now like that:

1524134103/26.1/38.1;1524133802/26.1/38.2;1524133502/26.1/37.8;1524133202/26.2/37.9;

Also I made the changes in the javascript:

 $(function() {

    var x_values1 = [];
    var y_values1 = [];
    var y_values2 = [];

    var switch1 = true;
    $.get('values1.php', function(data1) {

          data1 = data1.split(';').reverse();
            for (var i in data1) {
            let tmpData = data1[i].split('/');
            y_values1.push({x: parseInt(tmpData[0])*1000, y: parseFloat(tmpData[1])});
            y_values2.push({x: parseInt(tmpData[0])*1000, y: parseFloat(tmpData[2])});
  }
        //x_values1.pop();
        console.log(y_values1);

        $('#chart1').highcharts({
            chart : {
                type : 'spline'
            },
            title : {
                text : 'Buro'
            },
            subtitle : {
                text : 'PiSensor # 1'
            },
            xAxis : {
                title : {
                    text : 'Datum & Zeit'
                },
                categories : x_values1,
                reversed : false
            },
            yAxis : [{
                title : {
                    text : 'Temperatur'
                },  
                labels : {
                    formatter : function() {
                        return this.value + ' C'
                    }
                }
            }, {
                lineWidth: 1,
                opposite: true,
                title: {
                    text: 'Luftfeuchtigkeit'
                },
                labels : {
                    formatter : function() {
                        return this.value + ' %'
                    }
                }
            }],
            tooltip : {
                crosshairs : true,
                shared : true,
                valueSuffix : ''
            },
            plotOptions : {
                spline : {
                    marker : {
                        radius : 4,
                        lineWidth : 1
                    }
                }
            },
            series : [{

                name : 'Temperatur',
                data : y_values1,
                color : '#FF0033',
                tooltip : {
                    valueSuffix: ' C'
                }
            }, {
                name : 'Luftfeuchtigkeit',
                data : y_values2,
                dashStyle: 'shortdot',
                color : '#58ACFA',
                zones : [{
                    value: 45,
                    color: '#00cc00',
                }, {
                    value: 60,
                    color: '#ff9900',
                }, {
                    value: 100,
                    color: '#ff0000',
                }],
                yAxis : 1,
                tooltip : {
                    valueSuffix : ' %'
                }
            }]
        });
    });
});

function timeConverter(UNIX_timestamp){
  var a = new Date(UNIX_timestamp * 1000);
  var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  var year = a.getFullYear();
  var month = months[a.getMonth()];
  var date = a.getDate();
  var hour = a.getHours();
  var min = a.getMinutes() < 10 ? '0' + a.getMinutes() : a.getMinutes();
  var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min ;
  return time;
}

I added some zone-colors for the humidity (Luftfeuchtigkeit) and another dashStyle. This changes are working well.

But there's a problem with the xAxis Date & Time (or unix_timestamp right now). xAxis DateTime - Unix_Timestamp Problem in the HighCharts

The SQL statement is still the same (unix_timestamp) and the time converter function at the end is also not changed.

Outi
  • 11
  • 2
  • I would suggest you use time in milliseconds for your `xAxis` and set `xAxis` `type: 'datetime'`. Then you could change your `timeconverter` function to do only the first action, i.e. `UNIX_timestamp * 1000`. Then you can format the timestamp freely. As for the question, could you include some snippet of the output from `values1.php`. That would allow people to troubleshoot this easier. – ewolden Apr 18 '18 at 13:52
  • Thanks for your fast reply. Here some output (unix_timestamp / temperature / humidity) of values1.php; 1524060002/27.3/36.4/1524059702/27.3/36.4/1524059403/27.3/36.4/1524059102/27.3/36.4/1524058805/27.3/36.4/1524058503/27.3/36.4/1524058202/27.3/36.4/1524057902/27.4/36.4/1524057602/27.3/36.4/ – Outi Apr 18 '18 at 14:02
  • I can also change the 'unix_timestamp' to 'dateandtime', the output looks then like the following: 2018-04-18 16:00:00/27.3/36.4/2018-04-18 15:55:00/27.3/36.4/2018-04-18 15:50:00/27.3/36.4/2018-04-18 15:45:00/27.3/36.4/2018-04-18 15:40:00/27.3/36.4/2018-04-18 15:35:00/27.3/36.4/2018-04-18 15:30:00/27.3/36.4/2018-04-18 15:25:00/27.4/36.4/2018-04-18 15:20:00/27.3/36.4/2018-04-18 15:15:00/27.3/36.4/2018-04-18 15:10:00/27.3/36.4/2018-04-18 15:05:00/27.3/36.3/ – Outi Apr 18 '18 at 14:04
  • Looking at your code, I can see one potential issue. You have a switch with 2 parameters in your for loop, but your php code gives you 3 parameters. So two options you have are either: change the for loop to account for 3 parameters. Or you could change the php to give you a easier to read list of values, like this: https://jsfiddle.net/ewolden/3hgzjyca/20/. I changed the line terminator to be `;`, the for loop to parse the series according to this, and lastly changed the xAxis to use datetime. – ewolden Apr 18 '18 at 14:31
  • Big thx ewolden! I changed the line terminator in php like you said to ";", with your adjusted js code it shows now the temperature AND humidity in the highchart! :-) but it shows the time in unix_timestamp and not in the datetime format. When I try to change the php to get the timestamp, the highchart will be messed up. – Outi Apr 19 '18 at 08:06

1 Answers1

0

Okay, I managed now to change the xAxis (datetime) with the following code:

        xAxis : {
            type : 'datetime',
            labels: {
            format: '{value:%e.%m. %H:%M}'
            },
            title : {
                text : 'Datum & Zeit'
            },
            categories : x_values1,
            reversed : false
        },

But I've still two Points which should be changed. One is the shown time on the HighCharts. The hour is 2 hours to late, the local/server time is +2 hours. I don't know how to add +2 hours to the HighChart Output on the xAxis and Tooltip, or how to adjust the time calculation.

Here are two screenshots of the mentioned problem:

Screenshot 1, 2h time difference

Screenshot 2, SQL DB Data (correct time)

The used javascript code looks now like that:

 $(function() {

    var x_values1 = [];
    var y_values1 = [];
    var y_values2 = [];

    var switch1 = true;
    $.get('values1.php', function(data1) {

          data1 = data1.split(';').reverse();
            for (var i in data1) {
            let tmpData = data1[i].split('/');
            y_values1.push({x: parseInt(tmpData[0])*1000, y: parseFloat(tmpData[1])});
            y_values2.push({x: parseInt(tmpData[0])*1000, y: parseFloat(tmpData[2])});
  }
        //x_values1.pop();
        console.log(y_values1);

        $('#chart1').highcharts({
            chart : {
                type : 'spline'
            },
            title : {
                text : 'Buro'
            },
            subtitle : {
                text : 'PiSensor # 1'
            },
            xAxis : {
                type : 'datetime',
                labels: {
                format: '{value:%e.%m. %H:%M}'
                },
                title : {
                    text : 'Datum & Zeit'
                },
                categories : x_values1,
                reversed : false
            },
            yAxis : [{
                title : {
                    text : 'Temperatur'
                },  
                labels : {
                    formatter : function() {
                        return this.value + ' C'
                    }
                }
            }, {
                lineWidth: 1,
                opposite: true,
                title: {
                    text: 'Luftfeuchtigkeit'
                },
                labels : {
                    formatter : function() {
                        return this.value + ' %'
                    }
                }
            }],
            tooltip : {
                crosshairs : true,
                shared : true,
                valueSuffix : '',
            },
            plotOptions : {
                spline : {
                    marker : {
                        radius : 4,
                        lineWidth : 1
                    }
                }
            },
            series : [{
                name : 'Temperatur',
                data : y_values1,
                color : '#FF0033',
                tooltip : {
                    valueSuffix: ' C'
                }
                }, {
                name : 'Luftfeuchtigkeit',
                data : y_values2,
                dashStyle: 'shortdot',
                color : '#58ACFA',
                zones : [{
                    value: 40,
                    color: '#FE9A2E',
                }, {
                    value: 60,
                    color: '#2E9AFE',
                }, {
                    value: 100,
                    color: '#FE9A2E',
                }],
                yAxis : 1,
                tooltip : {
                    valueSuffix : ' %'
                }
            }]
        });
    });
});

 $(function() {

    var x_values3 = [];
    var y_values3 = [];
    var y_values4 = [];

    var switch1 = true;
    $.get('values2.php', function(data2) {

          data2 = data2.split(';').reverse();
            for (var i in data2) {
            let tmpData = data2[i].split('/');
            y_values3.push({x: parseInt(tmpData[0])*1000, y: parseFloat(tmpData[1])});
            y_values4.push({x: parseInt(tmpData[0])*1000, y: parseFloat(tmpData[2])});
  }
        //x_values1.pop();
        console.log(y_values3);

        $('#chart2').highcharts({
            chart : {
                type : 'spline'
            },
            title : {
                text : 'Wohnzimmer'
            },
            subtitle : {
                text : 'PiSensor # 2'
            },
            xAxis : {
                type : 'datetime',
                labels: {
                format: '{value:%e.%m. %H:%M}'
                },
                title : {
                    text : 'Datum & Zeit'
                },
                categories : x_values3,
                reversed : false
            },
            yAxis : [{
                title : {
                    text : 'Temperatur'
                },  
                labels : {
                    formatter : function() {
                        return this.value + ' C'
                    }
                }
            }, {
                lineWidth: 1,
                opposite: true,
                title: {
                    text: 'Luftfeuchtigkeit'
                },
                labels : {
                    formatter : function() {
                        return this.value + ' %'
                    }
                }
            }],
            tooltip : {
                crosshairs : true,
                shared : true,
                valueSuffix : ''
            },
            plotOptions : {
                spline : {
                    marker : {
                        radius : 4,
                        lineWidth : 1
                    }
                }
            },
            series : [{
                name : 'Temperatur',
                data : y_values3,
                color : '#FF0033',
                tooltip : {
                    valueSuffix: ' C'
                }
            }, {
                name : 'Luftfeuchtigkeit',
                data : y_values4,
                dashStyle: 'shortdot',
                color : '#58ACFA',
                zones : [{
                    value: 40,
                    color: '#FE9A2E',
                }, {
                    value: 60,
                    color: '#2E9AFE',
                }, {
                    value: 100,
                    color: '#FE9A2E',
                }],
                yAxis : 1,
                tooltip : {
                    valueSuffix : ' %'
                }
            }]
        });
    });
});

 $(function() {

    var x_values5 = [];
    var y_values5 = [];
    var y_values6 = [];

    var switch1 = true;
    $.get('values3.php', function(data3) {

          data3 = data3.split(';').reverse();
            for (var i in data3) {
            let tmpData = data3[i].split('/');
            y_values5.push({x: parseInt(tmpData[0])*1000, y: parseFloat(tmpData[1])});
            y_values6.push({x: parseInt(tmpData[0])*1000, y: parseFloat(tmpData[2])});
  }
        //x_values1.pop();
        console.log(y_values5);

        $('#chart3').highcharts({
            chart : {
                type : 'spline'
            },
            title : {
                text : 'Schlafzimmer'
            },
            subtitle : {
                text : 'PiSensor # 3'
            },
            xAxis : {
                type : 'datetime',
                labels: {
                format: '{value:%e.%m. %H:%M}'
                },
                title : {
                    text : 'Datum & Zeit'
                },
                categories : x_values5,
                reversed : false
            },
            yAxis : [{
                title : {
                    text : 'Temperatur'
                },  
                labels : {
                    formatter : function() {
                        return this.value + ' C'
                    }
                }
            }, {
                lineWidth: 1,
                opposite: true,
                title: {
                    text: 'Luftfeuchtigkeit'
                },
                labels : {
                    formatter : function() {
                        return this.value + ' %'
                    }
                }
            }],
            tooltip : {
                crosshairs : true,
                shared : true,
                valueSuffix : ''
            },
            plotOptions : {
                spline : {
                    marker : {
                        radius : 4,
                        lineWidth : 1
                    }
                }
            },
            series : [{
                name : 'Temperatur',
                color : '#FF0033',
                data : y_values5,
                tooltip : {
                    valueSuffix: ' C'
                }
            }, {
                name : 'Luftfeuchtigkeit',
                data : y_values6,
                dashStyle: 'shortdot',
                color : '#58ACFA',
                zones : [{
                    value: 40,
                    color: '#FE9A2E',
                }, {
                    value: 60,
                    color: '#2E9AFE',
                }, {
                    value: 100,
                    color: '#FE9A2E',
                }],
                yAxis : 1,
                tooltip : {
                    valueSuffix : ' %'
                }
            }]
        });
    });
});

I think the point is somewhere there:

            for (var i in data1) {
            let tmpData = data1[i].split('/');
            y_values1.push({x: parseInt(tmpData[0])*1000, y: parseFloat(tmpData[1])});
            y_values2.push({x: parseInt(tmpData[0])*1000, y: parseFloat(tmpData[2])});

values1.php

<?php
$con = mysql_connect("localhost","user","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("piSensors", $con);
$result = mysql_query("SELECT * 
                       FROM `temperaturedata` 
                       WHERE sensor=1 
                       ORDER BY unix_timestamp DESC
                       LIMIT 288 ") 
                       or die ("Connection error");
while($row = mysql_fetch_array($result)) {
echo $row['unix_timestamp'] . "/" . $row['temperature'] . "/" . $row['humidity'] . ";" ;
}
mysql_close($con);
?>

And the 2nd Point is the Tooltip Information. There should also displayed only the information as on the xAxis is shown. Like "day.month hour.min" (%e.%m. %H:%M)

Maybe someone can help me out here... thx.

Edit (20.04.2018, 13:19)

I added following code to fix the issue with the time difference of 2 hours:

Highcharts.setOptions({
time: {
    timezoneOffset: -2 * 60
}
});

correct display of time

(The SensorData is recorded every 5 minutes)

Now I just have to fix the tooltip information with the date/time format (only display %H:%M)

Outi
  • 11
  • 2