The project examples are excellent for one station and one table. Plenty of tinkering available.
However in a more complex database with several tables (recording several stations) simplistically with say sensors in one table and readings in another
How to set up PHP to receive the data and send it to the correct table?
I have tried permutations of your wx stn code and transactions but just cant get it to work.
Any assistance appreciated
Hello, I hope you are doing well.
This is a though question to answer, because it really depends on your project requirements. In summary:
- First, you need to start by learning a bit about SQL and learn how to setup a table with all the fields that you want to store in your database.
- Then, you need to write a PHP script that will receive the data and insert it into your database
- Your ESP will send the data to your PHP script based on the defined parameters
I hope this helps!
Rui
Hullo Rui and thank you for the time to respond.
I am fine setting up the tables and structure in SQL – it is getting the information into them that i struggle with.
I am sure it cant be too hard but havent found the right website to get me there yet.
As a simple example let us say table 1 has three variables with a times stamp and table 2 has another three variables with a time stamp.
var1, var2, var3 for table 1
var4, var5, var6 for table 2.
var’s 1-6 are read from the one device.
how do i export those variables from the device (one long string?) and get them in to the correct table thru PHP.
Any pointers appreciated.
It’s a bit hard to explain in a few sentences, learning PHP is a full course on itself, but I highly recommend this website:
I have this sample project about PHP and MySQL: ESP32/ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE
This script receives the readings from the ESP and inserts them in the database.
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 . "')";
You could receive it like this:
$var1= test_input($_POST["var1"]);
$var2= test_input($_POST["var2"]);
$var3= test_input($_POST["var3"]);
$sql = "INSERT INTO table1 (var1, var2, var3) VALUES ('" . $var1 . "', '" . $var2 . "', '" . $var3 . "')";
It would be something like that… Then your ESP would make an HTTP POST request with var1, var2, var3 variables in the request.
Or you can learn more about HTTP: ESP32 HTTP GET and HTTP POST with Arduino IDE (JSON, URL Encoded, Text) https://randomnerdtutorials.com/esp32-http-get-post-arduino/
Dear Rui
Thank you for your patience – I have followed that example previously with many experimental permutations (it works fine for one table). I have tried modifying it by the addition of another “insert” into a different table with / without an additional loop but I cannot get that to work. Some investigation suggests that it wont and I need to do a transaction using BEGIN…INSERT… COMMIT I have tried this as well without success. Other investigation suggest that just good PHP programming will do it without the need for a transaction.?? I do understand the concepts but struggle with the programming for what i understand should be a relatively common event.
Kind regards