Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset = "utf-8">
- <title>Arduino MySQL PHP</title>
- </head>
- <body>
- <h1>Arduino MySQL PHP</h1>
- <table width="500" border="1" cellspacing="2" cellspacing="5">
- <tr>
- <td><b>ID</b></td>
- <td><b>Timestamp</b></td>
- <td><b>Value</b></td>
- </tr>
- <?php
- // Prepare variables for database connection
- $dbusername = "arduino"; // enter database username, I used "arduino" in step 2.2
- $dbpassword = "arduinotest"; // enter database password, I used "arduinotest" in step 2.2
- $server = "localhost"; // IMPORTANT: if you are using XAMPP enter "localhost", but if you have an online website enter its address, ie."www.yourwebsite.com"
- // Connect to your database
- $dbconnect = mysqli_connect($server, $dbusername, $dbpassword);
- $dbselect = mysqli_select_db($dbconnect,"test");
- // Prepare the SQL statement
- $sql = "INSERT INTO test.sensor (value) VALUES ('".@$_GET["value"]."')";
- // Execute SQL statement
- mysqli_query($dbconnect,$sql);
- ?>
- <?php
- $url=$_SERVER['REQUEST_URI'];
- header("Refresh: 5; URL=$url"); // Refresh the webpage every 5 seconds
- ?>
- <?php
- error_reporting(0);
- // Connect to database
- // 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")
- $con=mysqli_connect("192.168.1.3","arduino","arduinotest","test");
- // Retrieve all records and display them
- $result = mysqli_query($con,'SELECT * FROM sensor ORDER BY id DESC');
- if(!result)
- {
- echo ("Error: ".mysqli_error($connect));
- exit();
- }
- // Process every record
- while($row = mysqli_fetch_array($result))
- {
- echo "<tr>";
- echo "<td>" . $row['id'] . "</td>";
- echo "<td>" . $row['time'] . "</td>";
- echo "<td>" . $row['value'] . "</td>";
- echo "</tr>";
- }
- // Close the connection
- mysqli_close($con);
- ?>
- </table>
- </body>
- </html>
- #include <LTask.h>
- #include <LWiFi.h>
- #include <LWiFiClient.h>
- #define WIFI_AP "EvaLan"
- #define WIFI_PASSWORD "0915102587"
- #define WIFI_AUTH LWIFI_WPA
- #define SITE_URL "localhost/write_data.php"
- //#define DEVICE_KEY "RjfZBZcaXOGMEHOu"
- //#define DEVICE_ID "Da2isWRw"
- //LWiFiClient content;
- //char server[] = "192.168.1.3";
- LWiFiClient client;
- String value = "";
- int pin = 8;
- unsigned long duration;
- unsigned long starttime;
- unsigned long sampletime_ms = 30000;
- unsigned long lowpulseoccupancy = 0;
- float ratio = 0;
- float concentration = 0;
- void setup()
- {
- LTask.begin();
- LWiFi.begin();
- Serial.begin(9600);
- pinMode(8, INPUT);
- starttime = millis();
- while (!Serial)delay(100);
- Serial.println("Connecting to AP");
- while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
- {
- Serial.println("Waiting for connected to AP...");
- delay(1000);
- }
- /*if (LWiFi.connectWEP(WIFI_AP, WIFI_PASSWORD) >= 0) {
- Serial.println("Connect ok");
- } else {
- Serial.println("Connect fail");
- }*/
- }
- //boolean disconnectedMsg = false;
- void loop()
- {
- if (LWiFi.status() == LWIFI_STATUS_DISCONNECTED) {
- Serial.println("Reconnected to AP...");
- while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
- {
- Serial.println("Waiting for reconnected to AP...");
- delay(1000);
- }
- }
- duration = pulseIn(pin, LOW);
- lowpulseoccupancy = lowpulseoccupancy + duration;
- if ((millis() - starttime) > sampletime_ms)
- {
- ratio = lowpulseoccupancy / (sampletime_ms * 10.0);
- concentration = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62;
- //Serial.print("concentration=");
- //Serial.println(concentration);
- lowpulseoccupancy = 0;
- starttime = millis();
- value = String("concentration") + "= " + String(concentration);
- }
- //client.println("connected");
- //String data = "value=" + value_str ;
- client.print("POST/write_data.php");
- client.println(" HTTP/1.1");
- client.println("Host:192.168.1.3");
- //client.print("value=");
- client.println("Content-Type: application/x-www-form-urlencoded");
- client.print("Content-Length: ");
- client.println("Connection:close");
- client.println();
- client.println(value.length());
- client.print(value);
- Serial.println(value);
- Serial.print("value = ");
- //Serial.println(value);
- client.stop();
- delay(10000);
- }
Add Comment
Please, Sign In to add comment