Mục tiêu của project này là để Raspberry Pi của bạn chạy một LAMP Server cho phép bạn lưu trữ các giá trị cảm biến từ ESP32 hoặc ESP8266. Bạn có thể theo dõi dữ liệu cảm biến trên bất kỳ trình duyệt nào trong mạng cục bộ của bạn.
Đây là tổng quan về project:

Mục lục
1. Tạo cơ sở dữ liệu
Mở trình duyệt của bạn và gõ http://Your-Raspberry-Pi-IP–Address /phpmyadmin), bạn sẽ thấy trang đăng nhập cho giao diện web phpMyAdmin.

Sau khi đăng nhập, bạn sẽ thấy một trang tương tự:

Chọn “Databases” ở phía trên thanh menu, trong phần “Create database” tạo:
- esp_data
- utf8mb4_general_ci
Nhấn Create button.

Cơ sở dữ liệu mới của bạn đã được tạo thành công. Bây giờ, hãy lưu tên cơ sở dữ liệu của bạn vì bạn sẽ cần nó sau này:
- Database name: esp_data
Tạo một bảng SQL:
Sau khi tạo cơ sở dữ liệu, trong thanh bên trái chọn tên cơ sở dữ liệu của bạn là Esp_data :

Sao chép truy vấn SQL trong đoạn mã sau:
CREATE TABLE SensorData (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
sensor VARCHAR(30) NOT NULL,
location VARCHAR(30) NOT NULL,
value1 VARCHAR(10),
value2 VARCHAR(10),
value3 VARCHAR(10),
reading_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
Mở tab SQL, dán nó vào trường truy vấn SQL và nhấn nút Go để tạo bảng:

Nếu bảng của bạn được tạo thành công:

Sau đó, bạn sẽ thấy table mới được tạo ra tên là SensorData trong esp_data database như trong hình dưới đây:

2. PHP Script HTTP POST – Insert Data vào MySQL Database
Trong phần này, chúng tôi sẽ tạo một tập lệnh PHP chịu trách nhiệm nhận các requests đến từ ESP32 hoặc ESP8266 và Insert Data vào MySQL Database.
Nhập lệnh sau đây để tạo file trong thư mục /var/www/html html :
pi@raspberrypi:~ $ nano /var/www/html/post-esp-data.php
Sao chép tập lệnh PHP sau vào file vừa tạo (post-esp-data.php):
<?php
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
$servername = "localhost";
// REPLACE with your Database name
$dbname = "REPLACE_WITH_YOUR_DATABASE_NAME";
// REPLACE with Database user
$username = "REPLACE_WITH_YOUR_USERNAME";
// REPLACE with Database user password
$password = "REPLACE_WITH_YOUR_PASSWORD";
// Keep this API Key value to be compatible with the ESP32 code provided in the project page.
// If you change this value, the ESP32 sketch needs to match
$api_key_value = "tPmAT5Ab3j7F9";
$api_key= $sensor = $location = $value1 = $value2 = $value3 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$api_key = test_input($_POST["api_key"]);
if($api_key == $api_key_value) {
$sensor = test_input($_POST["sensor"]);
$location = test_input($_POST["location"]);
$value1 = test_input($_POST["value1"]);
$value2 = test_input($_POST["value2"]);
$value3 = test_input($_POST["value3"]);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO SensorData (sensor, location, value1, value2, value3)
VALUES ('" . $sensor . "', '" . $location . "', '" . $value1 . "', '" . $value2 . "', '" . $value3 . "')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
else {
echo "Wrong API Key provided.";
}
}
else {
echo "No data posted with HTTP POST.";
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Trước khi lưu file, bạn cần sửa đổi các biến $dbname , $username và $password của bạn:
// Your Database name
$dbname = "esp_data";
// Your Database user
$username = "root";
// Your Database user password
$password = "YOUR_USER_PASSWORD";
sau đó nhấn Ctrl + X , y và Enter để lưu file.
Truy cập địa chỉ: http://You-Raspberry-Pi-IP-Address/post-esp-data.php
sẽ có kết quả:

3. PHP Script – Hiển thị nội dung Database
Tạo một tệp PHP khác trong thư mục /var/www/html để hiển thị tất cả dữ liệu trong database lên một trang web sau đó đặt tên cho file mới là: Esp-data.php
pi@raspberrypi:~ $ nano /var/www/html/esp-data.php
Edit file vừa tạo ( esp-data.php) và coppy tập lệnh PHP sau:
<!DOCTYPE html>
<html><body>
<?php
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
$servername = "localhost";
// REPLACE with your Database name
$dbname = "REPLACE_WITH_YOUR_DATABASE_NAME";
// REPLACE with Database user
$username = "REPLACE_WITH_YOUR_USERNAME";
// REPLACE with Database user password
$password = "REPLACE_WITH_YOUR_PASSWORD";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, sensor, location, value1, value2, value3, reading_time FROM SensorData ORDER BY id DESC";
echo '<table cellspacing="5" cellpadding="5">
<tr>
<td>ID</td>
<td>Sensor</td>
<td>Location</td>
<td>Value 1</td>
<td>Value 2</td>
<td>Value 3</td>
<td>Timestamp</td>
</tr>';
if ($result = $conn->query($sql)) {
while ($row = $result->fetch_assoc()) {
$row_id = $row["id"];
$row_sensor = $row["sensor"];
$row_location = $row["location"];
$row_value1 = $row["value1"];
$row_value2 = $row["value2"];
$row_value3 = $row["value3"];
$row_reading_time = $row["reading_time"];
// Uncomment to set timezone to - 1 hour (you can change 1 to any number)
//$row_reading_time = date("Y-m-d H:i:s", strtotime("$row_reading_time - 1 hours"));
// Uncomment to set timezone to + 4 hours (you can change 4 to any number)
//$row_reading_time = date("Y-m-d H:i:s", strtotime("$row_reading_time + 4 hours"));
echo '<tr>
<td>' . $row_id . '</td>
<td>' . $row_sensor . '</td>
<td>' . $row_location . '</td>
<td>' . $row_value1 . '</td>
<td>' . $row_value2 . '</td>
<td>' . $row_value3 . '</td>
<td>' . $row_reading_time . '</td>
</tr>';
}
$result->free();
}
$conn->close();
?>
</table>
</body>
</html>
Add $dbname, $username và $password của bạn:
// Your Database name
$dbname = "esp_data";
// Your Database user
$username = "root";
// Your Database user password
$password = "YOUR_USER_PASSWORD";
Sau đó Ctrl+X, y, và Enter để save sau đó thử truy cập http://Your-Raspberry-Pi-IP-Address/esp-data.php
, bạn sẽ thấy như sau:

Trong phần tiếp theo, bạn sẽ tìm hiểu cách chèn dữ liệu từ ESP32 hoặc ESP8266 vào cơ sở dữ liệu.