Запуск PHP и JQuery скриптов каждые 5 секунд cra sh веб-браузер - PullRequest
0 голосов
/ 19 июня 2020

У меня есть файл HTML, который запускает сценарий PHP каждые 5 секунд без конца, но браузер не запускается примерно через 20 минут и вылетает. Я не уверен, почему.

Вот код HTML:

<!DOCTYPE html>
<html>
<head>
<title>Demo IoT</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.js"></script>

<script type="text/javascript">
<!--
$(document).ready(function(){    
loadstation();   
});

function loadstation(){
   var cacheBuster = Math.random().toString(36).slice(2);
   $('#sensor').attr('src', 'sensor_value.php?q=' + cacheBuster);

   setTimeout(loadstation, 5000);
}
//-->
</script>

</head>
<body>

<div style="position: absolute; top: 250px; left: 65px; width: 50px; height: 50px; background-color: white;"><a href="graphs1.html"><img id="sensor1" src="" border=0></a></div>

</body>
</html>

edit1: Вот мой код php sensor_value php:

     <?php
     $thevalue='';
     $dsn = "mysql:host=localhost;dbname=Sensors_DataBase";
     $user = "jack";
     $passwd = "bobby123";

    try
    {
    $pdo = new PDO($dsn, $user, $passwd);
    } catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }

    $sqlQuery = "SELECT * FROM Sensors_DataBase.Sensors_Table WHERE sensor_name = 'GasBoss'";
    $statement = $pdo->prepare($sqlQuery);
    $statement->execute();

    while($rs = $statement->fetch())
    {
      $thevalue = $rs["sensor_reading"];
    }

    if ($thevalue<0){
        $img = imagecreatefrompng("/var/www/html/blue-button.png");
    }
    else if ($thevalue<=20){
        $img = imagecreatefrompng("/var/www/html/green-buttton.png");
    }
    else if (($thevalue>=20) && ($thevalue<=40)){
        $img = imagecreatefrompng("/var/www/html/yellow-button.png");
    }else if ($thevalue>=40){
        $img = imagecreatefrompng("/var/www/html/red-button.png");
    }

imageAlphaBlending($img, true);
imageSaveAlpha($img, true);

$black = imagecolorallocate($img, 0, 0, 0);

$txt_space = imagettfbbox(19, 0, '/var/www/html/arial.ttf', $thevalue);

$txt_width = abs($txt_space[4] - $txt_space[0]);
$txt_height = abs($txt_space[3] - $txt_space[1]);

$image_width = imagesx($img);  
$image_height = imagesy($img);

$x = abs($image_width - $txt_width) /2;
$y = abs($image_height - $txt_height) /2;

// Add text to image:
imagettftext($img, 19, 0, $x, $y+5, $black, '/var/www/html/arial.ttf', $thevalue);  
imagettftext($img, 10,0,25,85, $black, '/var/www/html/arial.ttf' , 'GasBoss');      

header("Content-Type: image/png");
imagepng($img); 
imagedestroy($img);

?>
...