ESP32 S3 UNO - MySQL

This ESP32 S3 Uno MySQL tutorial shows you how to save sensor data from the ESP32 S3 Uno form board into a MySQL database over WiFi. The board sends a simple HTTP request, and a small PHP script on your PC writes the value into the MySQL server (MariaDB). You can use the same idea to update or read database rows.

In this tutorial, you will:

  1. Install a MySQL server, Apache web server and PHP on your PC with XAMPP
  2. Create a MySQL user account, a database and a table
  3. Write a PHP script that inserts a temperature value into the database
  4. Program the ESP32 S3 Uno to send the value with an HTTP GET request
ESP32 S3 Uno MySQL

Hardware Preparation

1×ESP32 S3 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Optionally, DC Power Jack
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

ESP32 S3 Uno Pinout

This project only needs the board's built-in WiFi, so you do not have to wire anything. The pinout image below is still handy when you later add a real sensor to the Uno headers (D2, A0, SDA…) and need its GPIO number.

ESP32 S3 Uno pinout diagram

ESP32 S3 Uno - MySQL

Two words often confuse beginners: MySQL Database and MySQL server. The server is the program that runs, and the database is the data it keeps. When you are just starting, you can treat them as the same thing. The difference will become clear as you build more projects.

There are two ways for the ESP32 S3 Uno to talk to MySQL. In the direct way, the board connects to the MySQL server and speaks the MySQL protocol itself. In the indirect way, the board sends an HTTP/HTTPS request to a web server, and the web server talks to MySQL for it. The two parts below compare them, so you can see why this tutorial picks the second one.

ESP32 S3 Uno Interacts Directly with MySQL Server

ESP32 S3 Uno directly to MySQL

The direct way looks simple, but it brings real problems.

Security risk

You must let a MySQL user log in from another device on the network. Even with limited rights, this opens a door that an attacker could use.

Heavy work for the board

The ESP32 S3 Uno has to build raw SQL queries and decode MySQL reply packets. This uses more memory and CPU time than a plain HTTP call, and the code gets complex.

Out-of-memory crashes

A query can return a very large result. The board may run out of RAM while it reads the reply.

No encryption

Most MySQL libraries for microcontrollers do not support SSL/TLS. The username, password and data travel over the network as plain text.

ESP32 S3 Uno Interacts Indirectly with MySQL Server via HTTP/HTTPS

Putting a PHP script between the board and the database fixes all the problems above. The board only does what it is good at — sending a short web request — and the server does the rest.

How It Works

  1. The ESP32 S3 Uno puts the sensor value into an HTTP/HTTPS request and sends it to the web server.
  2. The web server runs a PHP script to handle the request.
  3. The PHP script reads the value, builds the SQL query and talks to the MySQL database.
  4. The PHP script sends back only a short result to the ESP32 S3 Uno in the HTTP response.
ESP32 S3 Uno MySQL HTTP

In this tutorial, XAMPP installs MySQL, Apache and PHP on your PC. Later, you can move the same setup to a real server or a cloud service such as AWS EC2.

Why the indirect way is better

Apache and MySQL run on the same machine, so the MySQL user can be limited to localhost. Its username and password stay on the server and never sit inside the ESP32 S3 Uno code. PHP does the data handling, which keeps the board's sketch short and light on memory. Because PHP returns only the data the board needs, the board does not run out of RAM. Finally, the ESP32 S3 Uno can make HTTPS requests, so you can encrypt the whole link.

※ NOTE THAT:

If you protect the web server with a login, use a username and password that are different from the MySQL account. The two logins should never share the same values.

ESP32 S3 Uno to MySQL via HTTP/HTTPS

The setup has six small parts. Do them in order, and test each part before you move on. This way, if something goes wrong, you know exactly where to look.

  1. Install XAMPP (MySQL server, Apache web server and PHP) on your PC
  2. Create a MySQL user account
  3. Create a MySQL database
  4. Create a MySQL table
  5. Write a PHP script file
  6. Write the ESP32 S3 Uno code

1. Installing MySQL Server, Web Server, and PHP on Your PC

XAMPP puts everything you need into one installer, and you only install it once.

  1. Download XAMPP from this link and install it.
  2. Check that the folder C:\xampp\htdocs now exists on your PC. Your PHP files will go here.
  3. Open the XAMPP Control Panel.
  4. Click the Start button next to MySQL and next to Apache to run both services, as in the image below.
ESP32 S3 Uno XAMPP

2. Creating a MySQL User Account

You will make a MySQL account that can only log in from the local PC. Even if someone learns its password, they cannot reach your database from another machine. PHP runs on the same PC, so it can still use this account.

This tutorial uses the username esp32_s3_uno and the password newbiely.com.

  1. Open the Command Prompt on your PC. Keep it open until you finish the tutorial.
  2. Go to the MySQL folder with this command:
cd C:\xampp\mysql\bin
Command Prompt
C:\Users\YOUR_USER>cd C:\xampp\mysql\bin C:\xampp\mysql\bin>
  1. The MySQL root account has no password by default. Give it one (for example YOUR_ROOT_PASSWORD) to keep your server safe:
mysqladmin -u root password YOUR_ROOT_PASSWORD
Command Prompt
C:\xampp\mysql\bin>mysqladmin -u root password YOUR_ROOT_PASSWORD C:\xampp\mysql\bin>
  1. Log in to the MySQL server as root:
mysql.exe -u root -p
  1. Type your root password and press Enter.
Command Prompt
C:\xampp\mysql\bin>mysql.exe -u root -p Enter password: ****************** Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 10.4.6-MariaDB mariadb.org binary distribution Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
  1. Copy the commands below and paste them into the Command Prompt to create the user account:
CREATE USER 'esp32_s3_uno'@'localhost' IDENTIFIED BY 'newbiely.com'; GRANT ALL PRIVILEGES ON *.* TO 'esp32_s3_uno'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES;
Command Prompt
MariaDB [(none)]> CREATE USER 'esp32_s3_uno'@'localhost' IDENTIFIED BY 'newbiely.com'; Query OK, 0 rows affected (0.005 sec) MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'esp32_s3_uno'@'localhost' WITH GRANT OPTION; Query OK, 0 rows affected (0.005 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]>

The user account is ready. Write down the username and password, because the PHP script needs them.

3. Creating a MySQL Database

Next, make a database named db_esp32_s3_uno. Run this command in the Command Prompt:

CREATE DATABASE db_esp32_s3_uno CHARACTER SET = 'utf8' COLLATE = 'utf8_general_ci';
Command Prompt
MariaDB [(none)]> CREATE DATABASE db_esp32_s3_uno CHARACTER SET = 'utf8' COLLATE = 'utf8_general_ci'; Query OK, 1 row affected (0.003 sec) MariaDB [(none)]>

4. Creating a MySQL Table

The table holds the temperature values. Each row gets an ID number that MySQL counts up for you. Copy the commands below and paste them into the Command Prompt to create the table tbl_temp:

USE db_esp32_s3_uno; CREATE TABLE tbl_temp ( temp_id INT UNSIGNED NOT NULL AUTO_INCREMENT, temp_value FLOAT DEFAULT 0.00, PRIMARY KEY (temp_id) );
Command Prompt
MariaDB [(none)]> USE db_esp32_s3_uno; Database changed MariaDB [db_esp32_s3_uno]> MariaDB [db_esp32_s3_uno]> CREATE TABLE tbl_temp ( -> temp_id INT UNSIGNED NOT NULL AUTO_INCREMENT, -> temp_value FLOAT DEFAULT 0.00, -> PRIMARY KEY (temp_id) -> ); Query OK, 0 rows affected (0.044 sec) MariaDB [db_esp32_s3_uno]>

5. Writing PHP Script Files

The PHP script is the bridge between the board and the database. It reads the temperature value from the HTTP GET request and inserts it as a new row. Create a file named insert_temp.php with any text editor, such as Notepad or Notepad++.

<?php if(isset($_GET["temperature"])) { $temperature = $_GET["temperature"]; // get temperature value from HTTP GET $servername = "localhost"; $username = "esp32_s3_uno"; $password = "newbiely.com"; $database_name = "db_esp32_s3_uno"; // Create MySQL connection from PHP to MySQL server $connection = new mysqli($servername, $username, $password, $database_name); // Check connection if ($connection->connect_error) { die("MySQL connection failed: " . $connection->connect_error); } $sql = "INSERT INTO tbl_temp (temp_value) VALUES ($temperature)"; if ($connection->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . " => " . $connection->error; } $connection->close(); } else { echo "temperature is not set in the HTTP request"; } ?>
  1. Save the file inside the C:\xampp\htdocs folder.
  2. Find the IP address of your PC (search online if you are not sure how).
  3. Test the script in a web browser by opening http://192.168.0.19/insert_temp.php?temperature=26.2. Use your own PC's IP address instead of 192.168.0.19.
  4. The browser should show a success message like the one below:
ESP32 S3 Uno MySQL test
  1. Check that the row is really in the database with this command:
SELECT * from tbl_temp;
Command Prompt
MariaDB [db_esp32_s3_uno]> SELECT * from tbl_temp; +---------+------------+ | temp_id | temp_value | +---------+------------+ | 1 | 26.2 | +---------+------------+ 1 row in set (0.001 sec) MariaDB [db_esp32_s3_uno]>

The value 26.2 is now saved. The server side works, so the last step is to make the ESP32 S3 Uno send the same kind of request.

6. Writing ESP32 S3 Uno Code

The sketch below connects the ESP32 S3 Uno to your WiFi network and sends one HTTP GET request to insert_temp.php. It passes a fixed temperature of 30.5°C, then prints the server's reply on the Serial Monitor. It uses the built-in WiFi.h and HTTPClient.h libraries of the ESP32 core, so you do not need to install any extra library.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-mysql */ #include <WiFi.h> #include <HTTPClient.h> const char WIFI_SSID[] = "YOUR_WIFI_SSID"; // change to your WiFi network name const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // change to your WiFi password String HOST_NAME = "http://192.168.0.19"; // change to your PC's IP address String PATH_NAME = "/insert_temp.php"; String queryString = "?temperature=30.5"; void setup() { Serial.begin(9600); // connect the ESP32 S3 Uno to the WiFi network WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.println("Connecting"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to WiFi network with IP Address: "); Serial.println(WiFi.localIP()); HTTPClient http; http.begin(HOST_NAME + PATH_NAME + queryString); // HTTP int httpCode = http.GET(); // httpCode will be negative on error if (httpCode > 0) { // file found at server if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } else { // HTTP header has been sent and server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } void loop() { }

Detailed Instructions

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Set up the server — finish the XAMPP, MySQL and PHP steps above, and keep Apache and MySQL running.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
How to upload ESP32 S3 Uno code on Arduino IDE
  1. Copy the code above and paste it into the Arduino IDE.
  2. Edit the WiFi details — replace YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD with your network name and password.
  3. Set the server address — change 192.168.0.19 in HOST_NAME to your PC's IP address.
  4. Upload — click the Upload button in the Arduino IDE.
  5. Open the Serial Monitor and set the baud rate to 9600.
  6. Tip: The sketch sends the request only once, inside setup(). Press the RESET button on the board to send it again and add another row.

Serial Monitor Output

When the board joins your WiFi and the PHP script saves the value, the Serial Monitor shows something like this:

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
Connecting ....... Connected to WiFi network with IP Address: 192.168.0.27 New record created successfully
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Now look at the table again to confirm the new row:

SELECT * from tbl_temp;
Command Prompt
MariaDB [db_esp32_s3_uno]> SELECT * from tbl_temp; +---------+------------+ | temp_id | temp_value | +---------+------------+ | 1 | 26.2 | | 2 | 30.5 | +---------+------------+ 2 rows in set (0.000 sec) MariaDB [db_esp32_s3_uno]>

The 30.5°C value from the ESP32 S3 Uno sits right below the test value you added from the browser.

How ESP32 S3 Uno Inserts, Updates, or Reads Data from MySQL

The example above only inserts a row. Updating or reading data works the same way: the ESP32 S3 Uno sends a request, and you only change the SQL query inside the PHP script (an UPDATE or a SELECT instead of an INSERT). To learn more about PHP and MySQL, see W3Schools.com.

※ NOTE THAT:

This guide keeps things simple for beginners. A real product needs more protection, such as blocking SQL injection, using HTTPS, adding a login between the board and the web server, and sending data as JSON through a REST API. You can add these step by step after you finish this tutorial.

Application and Project Ideas

Once the ESP32 S3 Uno can write to MySQL, you can keep a long history of anything it measures. Here are some ideas to try.

  1. Environment logger: Save temperature, humidity or air-quality readings every few minutes and study the trends over weeks.
  2. Smart watering: Log soil moisture values, then use the average from the database to decide when to run a water pump.
  3. Door access log: Store each RFID card or keypad entry with its ID, time and result, so you can check who came in.
  4. Energy monitor: Read a current sensor, calculate the power use and save daily totals to follow your energy bill.
  5. Multi-room network: Place several ESP32 S3 Uno boards in different rooms, all sending data to one central database.
  6. Alert history: Save every alarm event (high temperature, gas leak, motion) and show the list on a web page.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

Challenge Yourself

When your board is writing rows to MySQL, try these challenges to go further. They start easy and get harder.

  1. Beginner: Change the PHP script to return the new row ID, and print it on the Serial Monitor.
  2. Beginner: Read a real sensor, such as a DHT22 or a DS18B20, and send its value instead of the fixed 30.5°C.
  3. Beginner: Move the request into loop() and send a new value every 10 seconds.
  4. Intermediate: Write a get_temp.php script that returns the last ten rows as JSON, and print them on the ESP32 S3 Uno.
  5. Intermediate: Write an update_temp.php script that changes the value of a row by its ID.
  6. Advanced: Switch from HTTP to HTTPS with WiFiClientSecure and a certificate on the server.

FAQ

Do I need a WiFi module to send data to MySQL from the ESP32 S3 Uno?

No. The ESP32 S3 Uno has WiFi built into the chip. You only need the board, a USB cable and a PC (or server) that runs MySQL, Apache and PHP.

Can I use the UNO R4 WiFi MySQL code on the ESP32 S3 Uno?

Not as it is. The UNO R4 WiFi code uses the WiFiS3.h library, which only works on that board. The ESP32 S3 Uno uses WiFi.h and HTTPClient.h from the ESP32 core, as in the code above. The PHP script and the MySQL setup stay the same.

Why does the ESP32 S3 Uno not connect to MySQL directly?

A direct link needs a heavy MySQL library, remote access to your database and often sends the password without encryption. Sending a short HTTP request to a PHP script is lighter, safer and easier to change later.

Why does my browser test work but the ESP32 S3 Uno gets an error?

The browser runs on the same PC as XAMPP, so it always reaches the server. The board must reach the PC over the network. Make sure both are on the same WiFi network, the IP address in HOST_NAME is the PC's local IP, and the Windows firewall allows Apache on port 80. Also note that the ESP32 S3 Uno only connects to 2.4 GHz WiFi, not 5 GHz.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs: GPIO15 and GPIO16 (I/O, PWM, analog), GPIO45 and GPIO35 to GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers to them and write the GPIO number in your code. For a data logger, connect analog sensors to GPIO15/16 and digital sensors to GPIO35 to GPIO42. Do not use GPIO47/48 for sensor inputs. Be careful with two things. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Can I use an online database instead of my PC?

Yes. Put the PHP script on any web host that has PHP and MySQL, then change HOST_NAME to the host's address. For a public server, use HTTPS and add a login so that strangers cannot write to your table.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE USB driver missing or charge-only cable Install the CP210x or CH340 driver, use a data USB cable, or hold BOOT while pressing RESET
Board stays at "Connecting" with many dots Wrong WiFi name or password, or a 5 GHz network Check the SSID and password, and use a 2.4 GHz WiFi network
"[HTTP] GET... failed, error - connection refused" Apache not running, wrong IP address or firewall blocks port 80 Start Apache in XAMPP, use the PC's local IP and allow Apache in the Windows firewall
"MySQL connection failed" in the reply MySQL not running or wrong user, password or database name in PHP Start MySQL in XAMPP and check the values in insert_temp.php
"temperature is not set in the HTTP request" The query string is missing or misspelled Make sure the URL ends with ?temperature=VALUE
"[HTTP] GET... code - 404" insert_temp.php is not in the htdocs folder or the path is wrong Save the file in the xampp htdocs folder and check PATH_NAME in the code
Board resets or acts strangely Weak USB power or a bad cable Use a good USB Type-C cable and a powered USB port

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!