如何将数据从温度传感器发送到html文件?

时间:2016-03-28 18:53:47

标签: html sensor

我在终端中使用此命令显示传感器检测到的温度cat /sys/bus/w1/devices/28-000005330085/w1_slave | grep "t=" | awk -F "t=" '{print $2/1000}' 实际上,我的html文件返回一个图表,其y轴表示随机数。我想用传感器提供的数据替换这些数字,但我不知道怎么做!这是我的带随机数的html文件

<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Highcharts Example</title>

		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
		<style type="text/css">
${demo.css}
		</style>
		<script type="text/javascript">
$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });

        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function () {

                        // set up the updating of the chart each second
                        var series = this.series[0];
                        setInterval(function () {
                            var x = (new Date()).getTime(), // current time
                                y = Math.random() * 40 ;
                            series.addPoint([x, y], true, true);
                        }, 3000);
                    }
                }
            },
            title: {
                text: 'Live random data'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.series.name + '</b><br/>' +
                        Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                        Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Random data',
                data: (function () {
                    // generate an array of random data
                    var data = [],
                        time = (new Date()).getTime(),
                        i;

                    for (i = -19; i <= 0; i += 1) {
                        data.push({
                            x: time + i * 1000,
                            y: Math.random()
                        });
                    }
                    return data;
                }())
            }]
        });
    });
});
		</script>
	</head>
	<body>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

	</body>
</html>

1 个答案:

答案 0 :(得分:0)

一旦您的客户端读取您的HTML文件,它就会超出您的服务器并且加载了加载时存在的所有信息(例如,您的数据点可以由服务器填充(PHP?)读取您的传感器编写的文件,但不会在浏览器上动态更新 - 除非您编写一个函数(JavaScript)来不时从服务器获取信息。 例如,在你的

setInterval(function () {
                            var x = (new Date()).getTime(), // current time
                                y = Math.random() * 40 ;
                            series.addPoint([x, y], true, true);
                        }, 3000);

您可以编写一个ajax函数来从服务器获取最新的点。您的传感器可以将临时数据写入由ajax调用导致服务器经常读取的文件。您还可以使用websockets更新每个传感器更新的本地页面数据。