ESP8266 - MySQL
This tutorial instructs you how to use ESP8266 to insert/update data to MySQL database, or read the data from MySQL database
Hardware Preparation
1 | × | ESP8266 NodeMCU | |
1 | × | Micro USB Cable | |
1 | × | Breadboard | |
1 | × | Jumper Wires | |
1 | × | (Optional) DC Power Jack | |
1 | × | (Optional) Screw Terminal Expansion Board for ESP8266 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
ESP8266 - MySQL
There are two terms in MySQL that beginners usually get confused: MySQL Database and MySQL server. They are different. Howerver, if you are a beginner, you can assume that they are the same. Later, You will find the differencesin your learning process.
ESP8266 can interact with the MySQL database in two ways:
- Directly: ESP8266 connects directly to MySQL server and interacts with MySQL server using MySQL protocol
- Indirectly: ESP8266 connects indirectly to MySQL server via a web server using HTTP/HTTPS protocol.
Which one is the best for ESP8266? Let's find out!
ESP8266 interacts directly to MySQL Server
Interacting with MySQL directly seems to be simple but there are a lot of drawbacks:
- We have to grant remote access permissions to a MySQL user account ⇒ This is risky in the security aspect, even if we grant a limited privileges the user account.
- ESP8266 MUST store and send MySQL queries to MySQL server ⇒ Need to write much ESP8266 code, and also exhaust ESP8266 resources (Memory and CPU usage).
- ESP8266 MUST process a complex MySQL response (very big size of data in some cases) ⇒ This can make ESP8266 run out of memory
- MySQL server must process the raw data ⇒ increases the complexity of MySQL script.
- Most of the MySQL libraries for ESP8266 do not support SSL/TLS ⇒ The data and username/password is sent without encryption ⇒ another security issue.
ESP8266 interacts indirectly to MySQL Server via HTTP/HTTPS
Interacting with MySQL indirectly via HTTP/HTTPS solves all problems of the direct access.
How it works
- Step 1: ESP8266 includes the data to HTTP/HTTPS request and send the request to the Web server
- Step 2: Web server runs a PHP script that handles the request from ESP8266
- Step 3: PHP script extracts the data from the HTTP request, processes the data, and then interacts with MySQL database.
- Step 4: PHP script processes the result and returns only the necessary result to ESP8266 via HTTP response
We are going to install MySQL server, Web server, and PHP on the PC. In the fact, we can install it on a dedicated server or cloud service such as AWS EC2.
The below is how the indirect way solve the problems of the direct way.
- We can install HTTP server and the MySQL server in the same physical server, We can give a limit access to a MySQL user account(e.g. localhost access ONLY) ⇒ secure
- The username/password of the MySQL account is stored on the server ⇒ secure.
- Data is processed by a PHP script ⇒ This reduces the load and complexity for ESP8266 and MySQL server.
- PHP script can process the data much easier than the ESP8266 code and MySQL script ⇒ Simplify ESP8266 code and MySQL script
- PHP script can process the data and send only necessary data to ESP8266 (Step 4) to prevent ESP8266 from running out of memory.
- ESP8266 can make HTTPS request easily ⇒ the data is encrypted.
Note that the authentication between ESP8266 and Web Server should be independent with MySQL authentication. For example, the HTTP username/password should be different from the MySQL username/password.
Because of those advantages, This tutorial will use the indirect way.
ESP8266 To MySQL via HTTP/HTTPS
The below are steps that we need to do:
- Installing XAMPP package that includes MySQL server, Web server, and PHP on your PC
- Creating a MySQL user account
- Creating a MySQL database
- Creating a MySQL table
- Writing one or more PHP script files
- Writing ESP8266 code
1. Installing MySQL server, Web server, and PHP on your PC
- Download and install the XAMPP from this link. After installation, check the C:\xampp\htdocs folder on your PC. This is where you put PHP code (see later).
- Open XAMPP Control Panel
- Click Start buttons to enable MySQL and Web server (as the below image)
2. Creating a MySQL User account
We will create a MySQL account with localhost access permissions only.
- Even if attackers know the username/password, they cannot access your MySQL database unless they take control of your PC.
- This username/password will be used by PHP to connect to the MySQL database.
This tutorial creates a MySQL user account with username and password are ESP8266 and newbiely.com, respectively:
- Open the Command Prompt on your PC. Do not close it until the end of the tutorial.
- Type the following command on Command Prompt:
- By default, MySQL has THE root account without password. It is highly recommend to set the password for the root account (e.g. YOUR_ROOT_PASSWORD) for the security reason. Type the below command on the Command Prompt:
- Type the below command on Command Prompt to login to MySQL server:
- Type YOUR_ROOT_PASSWORD and press Enter
- Let's create a MySQL user account with username and passowrd are ESP8266 and newbiely.com, respectively by coping the below commands and paste on Command Prompt:
You created and MySQL user account successfully. Write down the username/password. It will be used in PHP script.
3. Creating a MySQL database
Create a database db_esp8266 by typing the following command on Command Prompt:
4. Creating a MySQL table
Create a table tbl_temp by coping the below commands and paste on Command Prompt:
6. Write PHP script files
Create a insert_temp.php by using any text editor (e.g. Notepad/Notepad++). We will write the script in this file to extract the temperature value from HTTP Request and inserts the temperature value into the database.
- Put this file inside C:\xampp\htdocs folder
- Get your PC's IP address. If you do not know how to, google it.
- Test PHP code by open a web browser (e.g. Chrome) and access this link: http://192.168.0.19/insert_temp.php?temperature=26.2. Note that you need to replace the above IP address with your PC address.
- The web browser shows as below:
- Check if data is stored in database by typing the following command on Command Prompt:
As you can see, the temperature of 26.2 is stored in the database. The next step is to write ESP8266 code that makes a HTTP Request to the web server on your PC.
7. Write ESP8266 code
The below ESP8266 code makes HTTP to your PC to insert a temperature of 30.5°C into the database
Detailed Instructions
- If this is the first time you use ESP8266, see how to setup environment for ESP8266 on Arduino IDE.
- Do the wiring as above image.
- Connect the ESP8266 board to your PC via a micro USB cable
- Change IP address on the code by your PC's IP address
- Compile and upload code to ESP8266
- Open Serial Monitor on Arduino IDE
- The result on Serial Monitor
- Check if data is stored in database by using the following command on Command Prompt:
As you can see, the temperature 30.5 is stored in database.
How ESP8266 insert, update or get data to/from MySQL database
The above example shows how to insert data into the MySQL database. It is similar for updating and reading data from the database. You just need to modify MySQL query on the PHP code. You can learn more on W3Schools.com