Wiznet makers

gavinchang

Published February 02, 2023 ©

53 UCC

25 WCC

60 VAR

0 Contests

3 Followers

0 Following

Original Link

Arduino - MySQL

Arduino can collect sensor data and store it on the MySQL database. Arduino can also get data (command) from MySQL database and control LED,

COMPONENTS
PROJECT DESCRIPTION

In this tutorial, we are going to learn:

 
The best way for Arduino to interact with MySQL database
 
How Arduino insert data to MySQL database
 
How Arduino update data to MySQL database
 
How Arduino get data from MySQL database

arduino MySQL

Hardware Required

1×Arduino UNO or Genuino UNO 
1×USB 2.0 cable type A/B 
1×Arduino Ethernet Shield 2 
1×Ethernet Cable 
1×(Optional) 9V Power Adapter for Arduino 
1×(Optional) Screw Terminal Block Shield for Arduino 
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

Arduino - MySQL

If you do not have much knowledge of the system architecture, two terms: MySQL Database and MySQL Server can be understood as the same. You will find the differences later when you know a lot about the system architecture.

There are two ways for Arduino to interact with the MySQL database:

 
Arduino interacts directly to MySQL Server via MySQL connection (called direct way)
 
Arduino interacts indirectly to MySQL Server via HTTP connection (called indirect way)

Let's find the best one.

 

Arduino interacts directly to MySQL Server

arduino directly to MySQL

This sounds simpler but there are many disadvantages:

 
This allows a MySQL User account to remotely access MySQL database ⇒ This is dangerous from the security point of view, even if limited privileges were granted to the user account.
 
The data MUST be processed in Arduino and/or MySQL server ⇒ This increases the complexity of Arduino code and MySQL script. Especially, it consumes a lot of Arduino resources (Memory and CPU usage).
 
MySQL server may return a very big amount of data to Arduino in some cases ⇒ This can make Arduino run out of memory.
 
Most of the available MySQL libraries do not support SSL/TLS. The data including username/password will be sent in plain text ⇒ another security issue.
 

Arduino interacts indirectly to MySQL Server via HTTP/HTTPS

This indirect way solves all problems that the direct way has. Before seeing how the indirect way overcomes the disadvantages of the direct way, let's see how it works first

How it works

 
Step 1: Arduino makes HTTP Request to Web Server
 
Step 2: Web Server runs PHP script
 
Step 3: PHP script gets data from HTTP Request, processes the data, and then interacts with MySQL database.
 
Step 4: PHP script processes the result and returns the result to Arduino via HTTP Response
Arduino MySQL HTTP

In this tutorial, the Web server, MySQL server will be installed on PC.

It looks complicated but not. Now Let's see how the indirect way overcomes the disadvantages of the direct way.

 
By installing the MySQL server and HTTP server in the same physical server, We can limit a MySQL User account to access localhost ONLY. Moreover, the username/password of the MySQL account is stored on Server (step 3), this makes the system more secure.
 
Data is processed by a PHP script(step 3 and step 4). This reduces the works and complexity for Arduino and MySQL servers. Processing data using PHP code is much easier than the Arduino code and MySQL script.
 
PHP script can process the data and send only necessary data to Arduino (Step 4) to prevent Arduino from running out of memory.
 
Most of Ethernet/WiFi libraries supports TLS/SSL that allows us to make HTTPS request. By using HTTPS, the data is encrypted and securely exchanged over the Internet.

In step 1, we can use another username/password to do authentication between Arduino and Web Server. Please note that the HTTP username/password should be different from the MySQL username/password for security reasons.

 

With those advantages, the rest of this tutorial will present how to use Arduino with MySQL via indirect way.

Arduino - MySQL via HTTP/HTTPS

We need to do the following step:

 
Install MySQL server, Web server, and PHP on your PC
 
Enable MySQL and Web server
 
Create a MySQL User account
 
Create a MySQL database
 
Create a MySQL table
 
Write one or more PHP script files
 
Write Arduino code

Now let's do it step-by-step.

1. Install MySQL server, Web server, and PHP on your PC

Fortunately, the XAMPP package includes all of these. We just need to install one time

 
Download XAMPP from this link
 
Install it.

After installing, you will see C:\xampp\htdocs folder on your PC. This is where you put PHP code (see later).

 

2. Enable MySQL and Web server

 
Open XAMPP Control Panel
 
Click Start button to enable MySQL and Web server (See the below image)
Arduino XAMPP

3. Create a MySQL User account

We will create a MySQL account that can connect to the MySQL database from localhost only.

 
Even if username/password are revealed, the attackers still cannot access your MySQL database unless they take control of your PC.
 
Because PHP and MySQL are installed on the same PC, PHP can use this username/password to connect to the MySQL database.

Let's create a MySQL user account with username is Arduino and password is ArduinoGetStarted.com:

 
Open Command Prompt on your PC. Do not close it until the end of the tutorial.
 
Type the following command on Command Prompt:
cd C:\xampp\mysql\bin
Command Prompt
C:\Users\youruser>cd C:\xampp\mysql\bin C:\xampp\mysql\bin>
 
By default, MySQL has root account without password. You should add password

(e.g. your-root-password) for root account by typing the following command on Command Prompt:

mysqladmin -u root password your-root-password
 
Type the following command on Command Prompt:
mysql.exe -u root -p
 
Type your-root-password and press Enter
 
 
Create a MySQL user account with username is Arduino and password is ArduinoGetStarted.com by coping the below commands and paste on Command Prompt:
CREATE USER 'Arduino'@'localhost' IDENTIFIED BY 'ArduinoGetStarted.com'; GRANT ALL PRIVILEGES ON *.* TO 'Arduino'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES;
Now you successfully created and MySQL user account. Memorize the username/password, It will be used in PHP script.
 

4. Create a MySQL database

Let's create a database named db_arduino by typing the following command on Command Prompt:

CREATE DATABASE db_arduino CHARACTER SET = 'utf8' COLLATE = 'utf8_general_ci';

5. Create a MySQL table

Let's create a table named tbl_temp by coping the below commands and paste on Command Prompt:

USE db_arduino;
CREATE TABLE tbl_temp (
temp_id    INT UNSIGNED NOT NULL AUTO_INCREMENT,
temp_value FLOAT DEFAULT 0.00,
PRIMARY KEY (temp_id)
);

6. Write one or more PHP files

Create a PHP file named insert_temp.php that gets temperature from HTTP Request and inserts it into the database.

<?php
if(isset($_GET["temperature"])) {
  $temperature = $_GET["temperature"]; // get temperature value from HTTP GET
  $servername = "localhost";
  $username = "Arduino";
  $password = "ArduinoGetStarted.com";
  $dbname = "db_arduino";
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
  }
  $sql = "INSERT INTO tbl_temp (temp_value) VALUES ($temperature)";
  if ($conn->query($sql) === TRUE) {
     echo "New record created successfully";
  } else {
     echo "Error: " . $sql . " => " . $conn->error;
  }
  $conn->close();
} else {
  echo "temperature is not set";
}
?>
 
Place 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.26/insert_temp.php?temperature=27.5 . Note that you need to replace the above IP address with your PC address.
 
The output on the web browser
MySQL Test
 
Check whether data is stored in database by typing the following command on Command Prompt:
SELECT * from tbl_temp;
As you can see, the temperature of 27.5 is stored in the database. The next step is to write Arduino that makes a similar HTTP Request to your PC.
 

7. Write Arduino code

We will use Arduino Uno and Ethernet Shield for the test

The below Arduino code makes HTTP to your PC to insert a temperature of 29.1°C into the database

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-mysql
*/
#include <SPI.h>
#include <Ethernet.h>
// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
int    HTTP_PORT   = 80;
String HTTP_METHOD = "GET";
char   HOST_NAME[] = "192.168.0.26"; // change to your PC's IP address
String PATH_NAME   = "/insert_temp.php";
String queryString = "?temperature=29.1";
void setup() {
 Serial.begin(9600);
 // initialize the Ethernet shield using DHCP:
 if (Ethernet.begin(mac) == 0) {
   Serial.println("Failed to obtaining an IP address using DHCP");
   while(true);
 }
 // connect to web server on port 80:
 if(client.connect(HOST_NAME, HTTP_PORT)) {
   // if connected:
   Serial.println("Connected to server");
   // make a HTTP request:
   // send HTTP header
   client.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
   client.println("Host: " + String(HOST_NAME));
   client.println("Connection: close");
   client.println(); // end HTTP header
   while(client.connected()) {
     if(client.available()){
       // read an incoming byte from the server and print it to serial monitor:
       char c = client.read();
       Serial.print(c);
     }
   }
   // the server's disconnected, stop the client:
   client.stop();
   Serial.println();
   Serial.println("disconnected");
 } else {// if not connected:
   Serial.println("connection failed");
 }
}
void loop() {
}

Quick Steps

 
Stack Ethernet Shield on Arduino Uno
 
Connect Ethernet Cable to Ethernet Shield
 
Connect Arduino Uno to PC via USB cable
 
Change IP address on the code by your PC's IP address
 
Compile and upload code to Arduino
 
Check whether data is stored in database by typing the following command on Command Prompt:
SELECT * from tbl_temp;

As you can see, the temperature 29.1 is stored in database.

 

How Arduino insert, update or get data to/from MySQL database

In the above example, we have learned how to insert data into the MySQL database. For updating and getting data from the database, it is similar. You only need to change MySQL queries on the PHP script. You can learn more from W3Schools

Advanced Usage

To increase the security

 
You can change Arduino code to make HTTPS instead of HTTP. See Arduino - HTTPS
 
You can use a username/password to do authentication between Arduino and the Web server. See Basic access authentication

※ NOTE THAT:

To make a complete system with the highest security level, we need to do more (such as MySQL injection prevention, making HTTPS become REST API, using Json format for data ...). Howerver, this tutorial is dedicated for beginners to learn Arduino. We made it as simple as possible. After learning this tutorial, users can expand it.

Documents
Comments Write