VAWT live. Pentru realizarea unui grafic de putere in timp real sunt necesare urmatoarele :
un fisier „putere.php” care extrage valorile din baza de date SQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
<?php function connect_db() { mysql_connect("Localhost", "user", "parola") or die(mysql_error()); mysql_select_db("nume baza de date") or die(mysql_error()); } function showUsageTable() { echo "<a href=index.php>Grafic in timp real</a><br>"; echo "<center>Putere generata turbina</center><br>"; connect_db(); $result = mysql_query("SELECT * FROM tempLog WHERE timeStamp >= CURRENT_TIMESTAMP - INTERVAL 1 HOUR ORDER BY timeStamp DESC") or die (mysql_error()); ?> <table border="1" align=center> <tr><td width=160>Data si timp</td><td width=120>Temperatura (C)</td><td width=120>Umiditate (%)</td><td width=120>Curent (A)</td><td width=120>Tensiune (V)</td><td width=120>Putere (W) </td></tr> <?php while ($row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td>".$row['timeStamp']."</td>"; echo "<td>".$row['temperature']."</td>"; echo "<td>".$row['humidity']."</td>"; echo "<td>".$row['curent']."</td>"; echo "<td>".$row['tensiune']."</td>"; echo "<td>".$row['putere']."</td>"; echo "</tr>"; } ?> </table> <?php } function getRange($from, $to) { connect_db(); $sql="SELECT * FROM tempLog WHERE timeStamp BETWEEN '${from}' AND '${to}' ORDER BY timeStamp ASC"; $result = mysql_query($sql) or die (mysql_error()); $ar=array(); while ($row = mysql_fetch_assoc($result)) { $pwr=intval($row['putere']); if ($pwr != 0){ array_push($ar, array(strtotime($row['timeStamp'])*1000, $pwr)); } } return $ar; } function getRangeJsn($from, $to) { header("Content-type: text/json"); $ar = getRange($from, $to); echo json_encode($ar); } function getLast($minutes) { $tm = time(); $from = date('Y-m-d H:i:s', $tm - $minutes*60); $to = date('Y-m-d H:i:s', $tm); return getRangeJsn($from, $to); } function getNow() { header("Content-type: text/json"); connect_db(); $result = mysql_query("SELECT * FROM tempLog ORDER BY timeStamp DESC LIMIT 1") or die (mysql_error()); $row = mysql_fetch_assoc($result); if ($row){ $ret = array(strtotime($row['timeStamp'])*1000, intval($row['putere'])); echo json_encode($ret); } } function updateputere($val) { connect_db(); $result = "INSERT INTO tempLog (putere) VALUES (".$val.")"; $add_member = mysql_query($result); } foreach($_GET as $key => $val){ switch ($key){ case "last": getLast($val); return; case "now": getNow(); return; case "add": updateputere($val); return; break; } } showUsageTable(); ?> |
VAWT live
Un fisier „index.php” care seteaza perioada de afisare a valorilor (implicit 60 minute) , rata de refresh (implicit 5 secunde) si genereaza graficul puterii generate folosind Highcharts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Monitorizare turbina eoliana verticala</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript"> $(function () { var updatePeriod=5; var timeWindow=60; //------------------------------------------------------------------ var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else{ // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } function formatUnits(watts) { var units=['W', 'KW', 'MW', 'GW']; var unit = 0; while (Math.floor(watts) > 1000){ unit++; watts /= 1000.0; } var str=""+watts+" "+units[unit]; return str; } var chart; var firstSampleTime; function requestData() { $.ajax({ url: 'putere.php?now', cache: false, success: function(point) { var series = chart.series[0]; // Start shifting (removing old samples) the graph to show only // samples for last 'timeWindow' minutes var shift = (point[0]-firstSampleTime)/1000.0 > timeWindow*60; // Add new point to the chart if (series.data.length > 1){ var str = "Putere generata: "+formatUnits(point[1]); document.getElementById("myDiv").innerHTML="<h2>"+str+"</h2>"; if (point[1] != 0){ chart.series[0].addPoint(point, true, shift); } } // Schedule another update setTimeout(requestData, updatePeriod*1000); }, error: function(result) { var str = "Sistem offline. Incercare reconectare..."; document.getElementById("myDiv").innerHTML="<h2>"+str+"</h2>"; setTimeout(requestData, updatePeriod*1000); } }); } function prefetchData() { document.getElementById("myDiv").innerHTML="<h2>Se transfera date...</h2>"; $.ajax({ url: 'power.php?last='+timeWindow, cache: false, success: function(points) { chart.series[0].setData(points, true); var str = "Putere generata: "+formatUnits(points[points.length-1][1]); document.getElementById("myDiv").innerHTML="<h2>"+str+"</h2>"; // Store timestamp of the first received sample firstSampleTime=points[0][0]; }, error: function(result) { var str = "ERROR: "+String(result); document.getElementById("myDiv").innerHTML="<h2>"+str+"</h2>"; setTimeout(prefetchData, updatePeriod*1000); } }); return; } function chartNow() { chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'spline', marginRight: 10, events: { load: requestData } }, title: { text: 'Putere turbina (interval de '+timeWindow+' minute)' }, xAxis: { type: 'datetime', // Resolution on the x axis. tickPixelInterval: 100 }, yAxis: { title: { text: 'Putere (W)' }, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { // This will appear when you hover above the graph in the chart formatter: function() { return 'Timp: '+Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br>'+ 'Putere generata: '+Highcharts.numberFormat(this.y, 2)+' Watts'; } }, legend: { enabled: false }, exporting: { enabled: false }, series: [{ marker: { radius: 2 }, name: 'Putere (Watts)', }] }); } $(document).ready(function() { Highcharts.setOptions({ global: { useUTC: false } }); chartNow(); prefetchData(); }); }); </script> </head> <body> <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/themes/gray.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> <table width=100%> <tr> <td> <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> <div id="myDiv"><h2>Usage now: 0 Watts</h2></div> <a href="putere.php">Vezi datele</a><BR> </td> </tr> </table> </body> <head> </html> |