Guest User

Untitled

a guest
Nov 17th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset = "utf-8">
  5. <title>Arduino MySQL PHP</title>
  6. </head>
  7.  
  8. <body>
  9. <h1>Arduino MySQL PHP</h1>
  10.  
  11. <table width="500" border="1" cellspacing="2" cellspacing="5">
  12.  
  13. <tr>
  14. <td><b>ID</b></td>
  15. <td><b>Timestamp</b></td>
  16. <td><b>Value</b></td>
  17. </tr>
  18. <?php
  19.  
  20. // Prepare variables for database connection
  21.  
  22. $dbusername = "arduino"; // enter database username, I used "arduino" in step 2.2
  23. $dbpassword = "arduinotest"; // enter database password, I used "arduinotest" in step 2.2
  24. $server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"
  25.  
  26. // Connect to your database
  27.  
  28. $dbconnect = mysqli_connect($server, $dbusername, $dbpassword);
  29. $dbselect = mysqli_select_db($dbconnect,"test");
  30.  
  31. // Prepare the SQL statement
  32.  
  33. $sql = "INSERT INTO test.sensor (value) VALUES ('".@$_GET["value"]."')";
  34.  
  35. // Execute SQL statement
  36.  
  37. mysqli_query($dbconnect,$sql);
  38. ?>
  39. <?php
  40. $url=$_SERVER['REQUEST_URI'];
  41. header("Refresh: 5; URL=$url"); // Refresh the webpage every 5 seconds
  42. ?>
  43. <?php
  44. error_reporting(0);
  45. // Connect to database
  46.  
  47. // IMPORTANT: If you are using XAMPP you will have to enter your computer IP address here, if you are using webpage enter webpage address (ie. "www.yourwebpage.com")
  48. $con=mysqli_connect("192.168.1.3","arduino","arduinotest","test");
  49.  
  50. // Retrieve all records and display them
  51. $result = mysqli_query($con,'SELECT * FROM sensor ORDER BY id DESC');
  52. if(!result)
  53. {
  54. echo ("Error: ".mysqli_error($connect));
  55. exit();
  56. }
  57. // Process every record
  58.  
  59. while($row = mysqli_fetch_array($result))
  60. {
  61. echo "<tr>";
  62. echo "<td>" . $row['id'] . "</td>";
  63. echo "<td>" . $row['time'] . "</td>";
  64. echo "<td>" . $row['value'] . "</td>";
  65. echo "</tr>";
  66.  
  67. }
  68.  
  69. // Close the connection
  70. mysqli_close($con);
  71. ?>
  72. </table>
  73. </body>
  74. </html>
  75.  
  76. #include <LTask.h>
  77. #include <LWiFi.h>
  78. #include <LWiFiClient.h>
  79.  
  80. #define WIFI_AP "EvaLan"
  81. #define WIFI_PASSWORD "0915102587"
  82. #define WIFI_AUTH LWIFI_WPA
  83. #define SITE_URL "localhost/write_data.php"
  84.  
  85. //#define DEVICE_KEY "RjfZBZcaXOGMEHOu"
  86. //#define DEVICE_ID "Da2isWRw"
  87.  
  88. //LWiFiClient content;
  89. //char server[] = "192.168.1.3";
  90. LWiFiClient client;
  91. String value = "";
  92.  
  93. int pin = 8;
  94. unsigned long duration;
  95. unsigned long starttime;
  96. unsigned long sampletime_ms = 30000;
  97. unsigned long lowpulseoccupancy = 0;
  98. float ratio = 0;
  99. float concentration = 0;
  100.  
  101. void setup()
  102. {
  103. LTask.begin();
  104. LWiFi.begin();
  105. Serial.begin(9600);
  106. pinMode(8, INPUT);
  107. starttime = millis();
  108. while (!Serial)delay(100);
  109. Serial.println("Connecting to AP");
  110. while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  111. {
  112. Serial.println("Waiting for connected to AP...");
  113. delay(1000);
  114. }
  115. /*if (LWiFi.connectWEP(WIFI_AP, WIFI_PASSWORD) >= 0) {
  116. Serial.println("Connect ok");
  117. } else {
  118. Serial.println("Connect fail");
  119. }*/
  120. }
  121. //boolean disconnectedMsg = false;
  122. void loop()
  123. {
  124. if (LWiFi.status() == LWIFI_STATUS_DISCONNECTED) {
  125. Serial.println("Reconnected to AP...");
  126. while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  127. {
  128. Serial.println("Waiting for reconnected to AP...");
  129. delay(1000);
  130. }
  131. }
  132. duration = pulseIn(pin, LOW);
  133. lowpulseoccupancy = lowpulseoccupancy + duration;
  134. if ((millis() - starttime) > sampletime_ms)
  135. {
  136. ratio = lowpulseoccupancy / (sampletime_ms * 10.0);
  137. concentration = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62;
  138. //Serial.print("concentration=");
  139. //Serial.println(concentration);
  140. lowpulseoccupancy = 0;
  141. starttime = millis();
  142. value = String("concentration") + "= " + String(concentration);
  143. }
  144. //client.println("connected");
  145. //String data = "value=" + value_str ;
  146. client.print("POST/write_data.php");
  147. client.println(" HTTP/1.1");
  148. client.println("Host:192.168.1.3");
  149. //client.print("value=");
  150. client.println("Content-Type: application/x-www-form-urlencoded");
  151. client.print("Content-Length: ");
  152. client.println("Connection:close");
  153. client.println();
  154. client.println(value.length());
  155. client.print(value);
  156.  
  157. Serial.println(value);
  158. Serial.print("value = ");
  159. //Serial.println(value);
  160. client.stop();
  161. delay(10000);
  162. }
Add Comment
Please, Sign In to add comment