Advertisement
Guest User

Untitled

a guest
Dec 18th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. <?php
  2. include('./connect.php');
  3. $link=Connection();
  4.  
  5. // $db = mysql_connect($db_host, $db_user, $db_password);
  6. //mysql_select_db("test_arduino");
  7.  
  8. $query = "SELECT timeStamp, temperature, humidity FROM test_arduino.tempLog WHERE timestamp >= DATE_SUB(CURRENT_DATE, INTERVAL 1 WEEK)";
  9. echo $query;
  10. echo "<br>";
  11. // The Chart table contains two fields: weekly_task and percentage
  12. // This example will display a pie chart. If you need other charts such as a Bar chart, you will need to modify the code a little to make it work with bar chart and other charts
  13. // $sth = mysql_query("SELECT timeStamp, temperature, humidity FROM test_arduino.tempLog WHERE timestamp >= DATE_SUB(CURRENT_DATE, INTERVAL 1 WEEK)");
  14.  
  15.  
  16. $sth = mysql_query($query , $link);
  17. echo "<br>";
  18. echo $sth;
  19.  
  20. $data = array (
  21. 'cols' => array(
  22. array('id' => 'timestamp', 'label' => 'time', 'type' => 'datetime'),
  23. array('id' => 'temperature', 'label' => 'Temp F', 'type' => 'number'),
  24. array('id' => 'humidity', 'label' => 'Humidity', 'type' => 'number'),
  25. array('id' => 'temperature', 'label' => 'Temp C', 'type' => 'number')
  26. ),
  27. 'rows' => array()
  28. );
  29. echo "<br>";
  30. echo $data;
  31.  
  32. $rows = array();
  33.  
  34. while ($res = mysql_fetch_assoc($sth)) {
  35. // assumes dates are patterned 'yyyy-MM-dd hh:mm:ss'
  36. preg_match('/(d{4})-(d{2})-(d{2})s(d{2}):(d{2}):(d{2})/', $res['timestamp'], $match);
  37. $year = (int) $match[1];
  38. $month = (int) $match[2] - 1; // convert to zero-index to match javascript's dates
  39. $day = (int) $match[3];
  40. $hours = (int) $match[4];
  41. $minutes = (int) $match[5];
  42. $seconds = (int) $match[6];
  43. array_push($data['rows'], array('c' => array(
  44. array('v' => 'Date(' . date('Y,n,d,H,i,s', strtotime($res['timestamp'])).')'),
  45. array('v' => floatval($res['temperature'])),
  46. array('v' => floatval($res['humidity'])),
  47. array('v' => floatval($res['temperature']))
  48. )));
  49. }
  50.  
  51. echo "<h2>JSON Table</h2>";
  52. echo $jsonTable;
  53. // mysql_close($link);
  54. ?>
  55.  
  56. <html>
  57. <head>
  58.  
  59. <!--Load the Ajax API-->
  60. <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  61. <script type="text/javascript">
  62.  
  63. // Load the Visualization API and the piechart package.
  64. google.load('visualization', '1.1', {'packages':['annotationchart']});;
  65.  
  66. // Set a callback to run when the Google Visualization API is loaded.
  67. google.setOnLoadCallback(drawChart);
  68.  
  69. function drawChart() {
  70.  
  71. // Create our data table out of JSON data loaded from server.
  72. var bar_chart_data = new google.visualization.DataTable(<?php echo json_encode($data); ?>);;
  73.  
  74. var options = {
  75. 'title':'Sensor 1 reading.',
  76. 'width':800,
  77. 'height':300, legend: { position: 'bottom' },};
  78.  
  79. // Instantiate and draw our chart, passing in some options.
  80. // Do not forget to check your div ID
  81. var chart = new google.visualization.AnnotationChart(document.getElementById('chart_div'));
  82. chart.draw(bar_chart_data, options);
  83.  
  84.  
  85.  
  86.  
  87. }
  88.  
  89.  
  90. </script>
  91. </head>
  92.  
  93. <body>
  94.  
  95.  
  96. <!--<script>setInterval(function(){location.reload();}, 80000)</script>-->
  97. <div id="chart_div"></div>
  98. <br />
  99.  
  100.  
  101. </body>
  102. </html>
  103.  
  104. <?php
  105.  
  106. function Connection(){
  107. $server="localhost";
  108. $user="root";
  109. $pass="mysql";
  110. $db="test_arduino";
  111.  
  112. $connection = mysql_connect($server, $user, $pass);
  113.  
  114. if (!$connection) {
  115. die('MySQL ERROR: ' . mysql_error());
  116. }
  117.  
  118. mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );
  119.  
  120. return $connection;
  121. }
  122. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement