{"id":85922,"date":"2023-03-26T16:58:52","date_gmt":"2023-03-26T16:58:52","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=85922"},"modified":"2023-03-26T18:11:56","modified_gmt":"2023-03-26T18:11:56","slug":"esp32-esp8266-mysql-database-php","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-esp8266-mysql-database-php\/","title":{"rendered":"ESP32\/ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE"},"content":{"rendered":"\n<p>In this project, you\u2019ll build an ESP32 or ESP8266 client that makes an HTTP POST request to a PHP script to insert data (sensor readings) into a MySQL database.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/ESP32-ESP8266-MySQL-Database-PHP-f.png?resize=1024%2C576&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE\" class=\"wp-image-129610\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/ESP32-ESP8266-MySQL-Database-PHP-f.png?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/ESP32-ESP8266-MySQL-Database-PHP-f.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/ESP32-ESP8266-MySQL-Database-PHP-f.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/ESP32-ESP8266-MySQL-Database-PHP-f.png?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgray\"><em>Updated on 26 March 2023<\/em><\/p>\n\n\n\n<p>You&#8217;ll also have a web page that displays the sensor readings, timestamps, and other information from the database. You can visualize your data from anywhere in the world by accessing your own server.<\/p>\n\n\n\n<p>As an example, we&#8217;ll be using a BME280 sensor connected to an ESP32 or ESP8266 board. You can modify the code provided to send readings from a different sensor or use multiple boards.<\/p>\n\n\n\n<p>In order to create and build this project, you&#8217;ll use these technologies:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-esp32\/\" title=\"\">ESP32 <\/a>or <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-esp8266-wifi-transceiver-review\/\" title=\"\">ESP8266 <\/a>programmed with Arduino IDE<\/li>\n\n\n\n<li>Hosting server and domain name<\/li>\n\n\n\n<li>PHP script to insert data into MySQL and display it on a web page<\/li>\n\n\n\n<li>MySQL database to store readings<\/li>\n<\/ul>\n\n\n\n<p><strong>Table of Contents<\/strong><\/p>\n\n\n\n<p>The project is divided into the following main sections:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#hosting-php-application-mysql\" title=\"\">Hosting Your PHP Application and MySQL Database<\/a><\/li>\n\n\n\n<li><a href=\"#preparing-mysql-database\" title=\"\">Preparing Your MySQL Database<\/a><\/li>\n\n\n\n<li><a href=\"#PHP-Script\" title=\"\">PHP Script HTTP POST &#8211; Insert Data in MySQL Database<\/a><\/li>\n\n\n\n<li><a href=\"#php-script-display-content\" title=\"\">PHP Script &#8211; Display Database Content<\/a><\/li>\n\n\n\n<li><a href=\"#5. Setting up the ESP32 or ESP8266\" title=\"\">Setting up the ESP32 or ESP8266<\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"hosting-php-application-mysql\">1. Hosting Your PHP Application and MySQL Database<\/h2>\n\n\n\n<p>The goal of this project is to have your own domain name and hosting account that allows you to store sensor readings from the ESP32 or ESP8266. You can visualize the readings from anywhere in the world by accessing your own server domain. Here&#8217;s a high-level overview:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-MySQL-Project-Overview.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"680\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-MySQL-Project-Overview.png?resize=750%2C680&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Hosting PHP Application and MySQL Database to post ESP32 or ESP8266 Sensor Readings\" class=\"wp-image-86006\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-MySQL-Project-Overview.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-MySQL-Project-Overview.png?resize=300%2C272&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<p>I recommend using one of the following hosting services that can handle all the project requirements:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/randomnerdtutorials.com\/bluehost\" target=\"_blank\">Bluehost (user-friendly with cPanel)<\/a>: free domain name when you sign up for the 3-year plan. I recommend choosing the unlimited websites option;<\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/randomnerdtutorials.com\/digitalocean\" target=\"_blank\">Digital Ocean<\/a><span style=\"font-size: inherit; background-color: initial;\">: Linux server that you manage through a command line. I only recommended this option for advanced users.<\/span><\/li>\n<\/ul>\n\n\n\n<p>Those two services are the ones that I use and personally recommend, but you can use any other hosting service. Any hosting service that offers PHP and MySQL will work with this tutorial. If you don&#8217;t have a hosting account, I recommend <a href=\"https:\/\/randomnerdtutorials.com\/bluehost\">signing up for Bluehost<\/a>.<\/p>\n\n\n\n<p style=\"text-align:center\"><a href=\"https:\/\/randomnerdtutorials.com\/bluehost\" target=\"_blank\" class=\"button\" rel=\"noopener noreferrer\">Get Hosting and Domain Name with Bluehost \u00bb<\/a><\/p>\n\n\n\n<p>When buying a hosting account, you&#8217;ll also have to purchase a domain name. This is what makes this project interesting: you&#8217;ll be able to go to your domain name (https:\/\/example-domain.com) and see the sensor readings from the ESP32 or ESP8266.<\/p>\n\n\n\n<p>If you like our projects, you might consider signing up for one of the recommended hosting services, because you&#8217;ll be supporting our work.<\/p>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Note: <\/strong> you can also run a <a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-apache-mysql-php-lamp-server\/\">LAMP (Linux, Apache, MySQL, PHP) server<\/a> on a Raspberry Pi to <a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-raspberry-pi-lamp-server\/\">access data in your local network<\/a>. However, the purpose of this tutorial is to publish readings in your own domain name that you can access from anywhere in the world. This allows you to easily access your ESP readings without relying on a third-party IoT platform.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"preparing-mysql-database\">2. Preparing Your MySQL Database<\/h2>\n\n\n\n<p>After signing up for a <a aria-label=\"hosting and setting up a domain name (opens in a new tab)\" href=\"https:\/\/randomnerdtutorials.com\/bluehost\" target=\"_blank\" rel=\"noreferrer noopener\">hosting account and setting up a domain name<\/a>, you can login to your cPanel or similar dashboard. After that, follow the next steps to create your database, username, password, and SQL table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a database and user<\/h3>\n\n\n\n<p><strong>1.<\/strong> Type &#8220;database&#8221; in the search bar and select &#8220;MySQL Database Wizard&#8221;.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"826\" height=\"300\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/CPanel-select-MySQL-database-wizard-to-create-db.png?resize=826%2C300&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"CPanel select MySQL database wizard to create db\" class=\"wp-image-85950\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/CPanel-select-MySQL-database-wizard-to-create-db.png?w=826&amp;quality=100&amp;strip=all&amp;ssl=1 826w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/CPanel-select-MySQL-database-wizard-to-create-db.png?resize=300%2C109&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/CPanel-select-MySQL-database-wizard-to-create-db.png?resize=768%2C279&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 826px) 100vw, 826px\" \/><\/figure><\/div>\n\n\n<p><strong>2.<\/strong> Enter your desired Database name. In my case, the database name is <span class=\"rnthl rntliteral\">esp_data<\/span>. Then, press the &#8220;Next Step&#8221; button:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"609\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Create-MySQL-Database.png?resize=609%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 CPanel Create MySQL Database\" class=\"wp-image-85938\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Create-MySQL-Database.png?w=609&amp;quality=100&amp;strip=all&amp;ssl=1 609w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Create-MySQL-Database.png?resize=300%2C218&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 609px) 100vw, 609px\" \/><\/figure><\/div>\n\n\n<p><strong>Note: <\/strong> later you&#8217;ll have to use the database name with the prefix that your host gives you (my database prefix in the screenshot above is blurred). I&#8217;ll refer to it as <span class=\"rnthl rntliteral\">example_esp_data<\/span> from now on.<\/p>\n\n\n\n<p><strong>3.<\/strong> Type your Database username and set a password. You must save all those details, because you&#8217;ll need them later to establish a database connection with your PHP code.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"615\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Create-MySQL-Database-User-Password.png?resize=736%2C615&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 CPanel Create MySQL Database User and Password\" class=\"wp-image-85939\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Create-MySQL-Database-User-Password.png?w=736&amp;quality=100&amp;strip=all&amp;ssl=1 736w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Create-MySQL-Database-User-Password.png?resize=300%2C251&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/figure><\/div>\n\n\n<p>That&#8217;s it! Your new database and user were created successfully. Now, save all your details because you&#8217;ll need them later:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Database name<\/strong>: example_esp_data<\/li>\n\n\n\n<li><strong>Username<\/strong>: example_esp_board<\/li>\n\n\n\n<li><strong>Password<\/strong>: your password<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a SQL table<\/h3>\n\n\n\n<p>After creating your database and user, go back to cPanel dashboard and search for &#8220;phpMyAdmin&#8221;.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"766\" height=\"315\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Open-PHPMyAdmin.png?resize=766%2C315&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 CPanel Open PHPMyAdmin\" class=\"wp-image-85942\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Open-PHPMyAdmin.png?w=766&amp;quality=100&amp;strip=all&amp;ssl=1 766w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Open-PHPMyAdmin.png?resize=300%2C123&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 766px) 100vw, 766px\" \/><\/figure><\/div>\n\n\n<p>In the left sidebar, select your database name <span class=\"rnthl rntliteral\">example_esp_data<\/span> and open the &#8220;SQL&#8221; tab. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"797\" height=\"521\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-Open-Database.png?resize=797%2C521&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 PHPMyAdmin Open Database\" class=\"wp-image-85944\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-Open-Database.png?w=797&amp;quality=100&amp;strip=all&amp;ssl=1 797w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-Open-Database.png?resize=300%2C196&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-Open-Database.png?resize=768%2C502&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 797px) 100vw, 797px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntcorange\"><strong>Important: <\/strong> make sure you&#8217;ve opened the <span class=\"rnthl rntliteral\">example_esp_data<\/span> database. Then, click the SQL tab. If you don&#8217;t follow these exact steps and run the SQL query, you might create a table in the wrong database.<\/p>\n\n\n\n<p>Copy the SQL query in the following snippet:<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">CREATE TABLE SensorData (\n    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n    sensor VARCHAR(30) NOT NULL,\n    location VARCHAR(30) NOT NULL,\n    value1 VARCHAR(10),\n    value2 VARCHAR(10),\n    value3 VARCHAR(10),\n    reading_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\n)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-ESP8266-PHP-MySQL\/raw\/master\/code\/SensorData_Table.sql\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Paste it in the SQL query field (highlighted with a red rectangle) and press the &#8220;Go&#8221; button to create your table:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"509\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-Create-SQL-Table.png?resize=1024%2C509&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 PHPMyAdmin Create SQL Table\" class=\"wp-image-85943\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-Create-SQL-Table.png?resize=1024%2C509&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-Create-SQL-Table.png?resize=300%2C149&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-Create-SQL-Table.png?resize=768%2C381&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-Create-SQL-Table.png?w=1031&amp;quality=100&amp;strip=all&amp;ssl=1 1031w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>After that, you should see your newly created table called <span class=\"rnthl rntliteral\">SensorData<\/span> in the <span class=\"rnthl rntliteral\">example_esp_data<\/span> database as shown in the figure below:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-View-SQL-Database.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"341\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-View-SQL-Database.png?resize=1024%2C341&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 PHPMyAdmin View SQL Database\" class=\"wp-image-85945\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-View-SQL-Database.png?resize=1024%2C341&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-View-SQL-Database.png?resize=300%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-View-SQL-Database.png?resize=768%2C256&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-PHPMyAdmin-View-SQL-Database.png?w=1148&amp;quality=100&amp;strip=all&amp;ssl=1 1148w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"PHP-Script\">3. PHP Script HTTP POST &#8211; Insert Data in MySQL Database<\/h2>\n\n\n\n<p>In this section, we&#8217;re going to create a PHP script that is responsible for receiving incoming requests from the ESP32 or ESP8266 and inserting the data into a MySQL database.<\/p>\n\n\n\n<p>If you&#8217;re using a hosting provider with cPanel, you can search for &#8220;File Manager&#8221;:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"761\" height=\"340\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Open-Edit-PHP-Files.png?resize=761%2C340&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 CPanel Open Edit PHP Files\" class=\"wp-image-85941\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Open-Edit-PHP-Files.png?w=761&amp;quality=100&amp;strip=all&amp;ssl=1 761w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Open-Edit-PHP-Files.png?resize=300%2C134&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 761px) 100vw, 761px\" \/><\/figure><\/div>\n\n\n<p>Then, select the <strong>public_html<\/strong> option and press the &#8220;+ File&#8221; button to create a new <em>.php<\/em> file.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"849\" height=\"346\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Create-New-PHP-File.png?resize=849%2C346&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 CPanel Create New PHP File\" class=\"wp-image-85940\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Create-New-PHP-File.png?w=849&amp;quality=100&amp;strip=all&amp;ssl=1 849w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Create-New-PHP-File.png?resize=300%2C122&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-CPanel-Create-New-PHP-File.png?resize=768%2C313&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> if you&#8217;re following this tutorial and you&#8217;re not familiar with PHP or MySQL, I recommend creating these exact files. Otherwise, you&#8217;ll need to modify the ESP sketch provided with different URL paths. <\/p>\n\n\n\n<p>Create a new file in <strong>\/public_html<\/strong> with this exact name and extension: <em>post-esp-data.php<\/em><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"501\" height=\"246\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/PHP-Create-New-file-post-esp-data.png?resize=501%2C246&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"PHP Create New file post esp data\" class=\"wp-image-85955\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/PHP-Create-New-file-post-esp-data.png?w=501&amp;quality=100&amp;strip=all&amp;ssl=1 501w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/PHP-Create-New-file-post-esp-data.png?resize=300%2C147&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/><\/figure><\/div>\n\n\n<p>Edit the newly created file (<em>post-esp-data.php<\/em>) and copy the following snippet:<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">&lt;?php\n\n\/*\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-esp8266-mysql-database-php\/\n  \n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files.\n  \n  The above copyright notice and this permission notice shall be included in all\n  copies or substantial portions of the Software.\n*\/\n\n$servername = &quot;localhost&quot;;\n\n\/\/ REPLACE with your Database name\n$dbname = &quot;REPLACE_WITH_YOUR_DATABASE_NAME&quot;;\n\/\/ REPLACE with Database user\n$username = &quot;REPLACE_WITH_YOUR_USERNAME&quot;;\n\/\/ REPLACE with Database user password\n$password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ Keep this API Key value to be compatible with the ESP32 code provided in the project page. \n\/\/ If you change this value, the ESP32 sketch needs to match\n$api_key_value = &quot;tPmAT5Ab3j7F9&quot;;\n\n$api_key= $sensor = $location = $value1 = $value2 = $value3 = &quot;&quot;;\n\nif ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {\n    $api_key = test_input($_POST[&quot;api_key&quot;]);\n    if($api_key == $api_key_value) {\n        $sensor = test_input($_POST[&quot;sensor&quot;]);\n        $location = test_input($_POST[&quot;location&quot;]);\n        $value1 = test_input($_POST[&quot;value1&quot;]);\n        $value2 = test_input($_POST[&quot;value2&quot;]);\n        $value3 = test_input($_POST[&quot;value3&quot;]);\n        \n        \/\/ Create connection\n        $conn = new mysqli($servername, $username, $password, $dbname);\n        \/\/ Check connection\n        if ($conn-&gt;connect_error) {\n            die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);\n        } \n        \n        $sql = &quot;INSERT INTO SensorData (sensor, location, value1, value2, value3)\n        VALUES ('&quot; . $sensor . &quot;', '&quot; . $location . &quot;', '&quot; . $value1 . &quot;', '&quot; . $value2 . &quot;', '&quot; . $value3 . &quot;')&quot;;\n        \n        if ($conn-&gt;query($sql) === TRUE) {\n            echo &quot;New record created successfully&quot;;\n        } \n        else {\n            echo &quot;Error: &quot; . $sql . &quot;&lt;br&gt;&quot; . $conn-&gt;error;\n        }\n    \n        $conn-&gt;close();\n    }\n    else {\n        echo &quot;Wrong API Key provided.&quot;;\n    }\n\n}\nelse {\n    echo &quot;No data posted with HTTP POST.&quot;;\n}\n\nfunction test_input($data) {\n    $data = trim($data);\n    $data = stripslashes($data);\n    $data = htmlspecialchars($data);\n    return $data;\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-ESP8266-PHP-MySQL\/raw\/master\/code\/post-esp-data.php\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Before saving the file, you need to modify the <span class=\"rnthl rntliteral\">$dbname<\/span>, <span class=\"rnthl rntliteral\">$username<\/span> and <span class=\"rnthl rntliteral\">$password<\/span> variables with your unique details:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Your Database name\n$dbname = \"example_esp_data\";\n\/\/ Your Database user\n$username = \"example_esp_board\";\n\/\/ Your Database user password\n$password = \"YOUR_USER_PASSWORD\";<\/code><\/pre>\n\n\n\n<p>After adding the database name, username and password, save the file and continue with this tutorial. If you try to access your domain name in the next URL path, you&#8217;ll see the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;example-domain.com\/post-esp-data.php<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"271\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/no-data-posted.png?resize=739%2C271&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Test POST ESP Data PHP URL\" class=\"wp-image-129600\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/no-data-posted.png?w=739&amp;quality=100&amp;strip=all&amp;ssl=1 739w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/no-data-posted.png?resize=300%2C110&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 739px) 100vw, 739px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"php-script-display-content\">4. PHP Script &#8211; Display Database Content<\/h2>\n\n\n\n<p>Create another PHP file in the <strong>\/public_html<\/strong> directory that will display all the database content on a web page. Name your new file: <em>esp-data.php <\/em><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"501\" height=\"244\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/PHP-Create-New-file-esp-data.png?resize=501%2C244&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"PHP Create New file esp data\" class=\"wp-image-85956\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/PHP-Create-New-file-esp-data.png?w=501&amp;quality=100&amp;strip=all&amp;ssl=1 501w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/PHP-Create-New-file-esp-data.png?resize=300%2C146&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/><\/figure><\/div>\n\n\n<p>Edit the newly created file (<em>esp-data.php<\/em>) and copy the following code:<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;&lt;body&gt;\n&lt;?php\n\/*\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-esp8266-mysql-database-php\/\n  \n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files.\n  \n  The above copyright notice and this permission notice shall be included in all\n  copies or substantial portions of the Software.\n*\/\n\n$servername = &quot;localhost&quot;;\n\n\/\/ REPLACE with your Database name\n$dbname = &quot;REPLACE_WITH_YOUR_DATABASE_NAME&quot;;\n\/\/ REPLACE with Database user\n$username = &quot;REPLACE_WITH_YOUR_USERNAME&quot;;\n\/\/ REPLACE with Database user password\n$password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ Create connection\n$conn = new mysqli($servername, $username, $password, $dbname);\n\/\/ Check connection\nif ($conn-&gt;connect_error) {\n    die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);\n} \n\n$sql = &quot;SELECT id, sensor, location, value1, value2, value3, reading_time FROM SensorData ORDER BY id DESC&quot;;\n\necho '&lt;table cellspacing=&quot;5&quot; cellpadding=&quot;5&quot;&gt;\n      &lt;tr&gt; \n        &lt;td&gt;ID&lt;\/td&gt; \n        &lt;td&gt;Sensor&lt;\/td&gt; \n        &lt;td&gt;Location&lt;\/td&gt; \n        &lt;td&gt;Value 1&lt;\/td&gt; \n        &lt;td&gt;Value 2&lt;\/td&gt;\n        &lt;td&gt;Value 3&lt;\/td&gt; \n        &lt;td&gt;Timestamp&lt;\/td&gt; \n      &lt;\/tr&gt;';\n \nif ($result = $conn-&gt;query($sql)) {\n    while ($row = $result-&gt;fetch_assoc()) {\n        $row_id = $row[&quot;id&quot;];\n        $row_sensor = $row[&quot;sensor&quot;];\n        $row_location = $row[&quot;location&quot;];\n        $row_value1 = $row[&quot;value1&quot;];\n        $row_value2 = $row[&quot;value2&quot;]; \n        $row_value3 = $row[&quot;value3&quot;]; \n        $row_reading_time = $row[&quot;reading_time&quot;];\n        \/\/ Uncomment to set timezone to - 1 hour (you can change 1 to any number)\n        \/\/$row_reading_time = date(&quot;Y-m-d H:i:s&quot;, strtotime(&quot;$row_reading_time - 1 hours&quot;));\n      \n        \/\/ Uncomment to set timezone to + 4 hours (you can change 4 to any number)\n        \/\/$row_reading_time = date(&quot;Y-m-d H:i:s&quot;, strtotime(&quot;$row_reading_time + 4 hours&quot;));\n      \n        echo '&lt;tr&gt; \n                &lt;td&gt;' . $row_id . '&lt;\/td&gt; \n                &lt;td&gt;' . $row_sensor . '&lt;\/td&gt; \n                &lt;td&gt;' . $row_location . '&lt;\/td&gt; \n                &lt;td&gt;' . $row_value1 . '&lt;\/td&gt; \n                &lt;td&gt;' . $row_value2 . '&lt;\/td&gt;\n                &lt;td&gt;' . $row_value3 . '&lt;\/td&gt; \n                &lt;td&gt;' . $row_reading_time . '&lt;\/td&gt; \n              &lt;\/tr&gt;';\n    }\n    $result-&gt;free();\n}\n\n$conn-&gt;close();\n?&gt; \n&lt;\/table&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-ESP8266-PHP-MySQL\/raw\/master\/code\/esp-data.php\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>After adding the <span class=\"rnthl rntliteral\">$dbname<\/span>, <span class=\"rnthl rntliteral\">$username,<\/span> and <span class=\"rnthl rntliteral\">$password<\/span> save the file and continue with this project. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Your Database name\n$dbname = \"example_esp_data\";\n\/\/ Your Database user\n$username = \"example_esp_board\";\n\/\/ Your Database user password\n$password = \"YOUR_USER_PASSWORD\";<\/code><\/pre>\n\n\n\n<p>If you try to access your domain name in the following URL path, you&#8217;ll see the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;example-domain.com\/esp-data.php<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"292\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-Test-ESP-Data-URL.png?resize=682%2C292&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Test ESP Data PHP URL\" class=\"wp-image-85946\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-Test-ESP-Data-URL.png?w=682&amp;quality=100&amp;strip=all&amp;ssl=1 682w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-Test-ESP-Data-URL.png?resize=300%2C128&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/figure><\/div>\n\n\n<p>That&#8217;s it! If you see that empty table printed in your browser, it means that everything is ready. In the next section, you&#8217;ll learn how to insert data from your ESP32 or ESP8266 into the database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"set-up-esp32-esp8266\">5. Setting up the ESP32 or ESP8266<\/h2>\n\n\n\n<p>This project is compatible with both the ESP32 and ESP8266 boards. You just need to assemble a simple circuit and upload the sketch provided to insert temperature, humidity, pressure, and more into your database every 30 seconds. The sketch is slightly different for each board.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>For this example, we&#8217;ll get sensor readings from the BME280 sensor. Here&#8217;s a list of parts you need to build the circuit for this project:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32 board<\/a>&nbsp;(read <a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\">Best ESP32 dev boards<\/a>)<\/li>\n\n\n\n<li>Alternative &#8211; <a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a> board (read <a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\">Best ESP8266 dev boards<\/a>)<\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\">BME280 sensor<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematics<\/h3>\n\n\n\n<p>The BME280 sensor module we\u2019re using communicates via I2C communication protocol, so you need to connect it to the ESP32 or ESP8266 I2C pins.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">BME280 wiring to ESP32<\/h4>\n\n\n\n<p>The ESP32 I2C pins are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>GPIO 22:<\/strong>&nbsp;SCL (SCK)<\/li>\n\n\n\n<li><strong>GPIO 21:<\/strong>&nbsp;SDA (SDI)<\/li>\n<\/ul>\n\n\n\n<p>So, assemble your circuit as shown in the next schematic diagram (<a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/\">read complete Guide for ESP32 with BME280<\/a>).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"652\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/03\/ESP32-bme280_bb.png?resize=750%2C652&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 wiring to ESP32\" class=\"wp-image-56623\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/03\/ESP32-bme280_bb.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/03\/ESP32-bme280_bb.png?resize=300%2C261&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference Guide<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">BME280 wiring to ESP8266<\/h4>\n\n\n\n<p>The ESP8266 I2C pins are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>GPIO 5&nbsp;<\/strong>(D1): SCL (SCK)<\/li>\n\n\n\n<li><strong>GPIO 4&nbsp;<\/strong>(D2): SDA (SDI)<\/li>\n<\/ul>\n\n\n\n<p>Assemble your circuit as in the next schematic diagram if you&#8217;re using an ESP8266 board (<a href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/\">read complete Guide for ESP8266 with BME280<\/a>).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"705\" height=\"532\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP8266-BME280-Arduino-IDE.png?resize=705%2C532&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"BME280 wiring to ESP8266\" class=\"wp-image-85980\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP8266-BME280-Arduino-IDE.png?w=705&amp;quality=100&amp;strip=all&amp;ssl=1 705w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP8266-BME280-Arduino-IDE.png?resize=300%2C226&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 705px) 100vw, 705px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 Pinout Reference Guide<\/a> <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Libraries<\/h3>\n\n\n\n<p>We\u2019ll program the ESP32\/ESP8266 using Arduino IDE, so you must have the ESP32\/ESP8266 add-on installed in your Arduino IDE. Follow one of the next tutorials depending on the board you&#8217;re using:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Install the ESP32 Board in Arduino IDE<\/a> &#8211; you also need to <a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/\">install the BME280 Library and Adafruit_Sensor library<\/a><\/li>\n\n\n\n<li><a style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\">Install the ESP8266 Board in Arduino IDE<\/a> &#8211; you also need to <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/\">install the BME280 Library and Adafruit_Sensor library<\/a> <\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"esp32-code\">ESP32 Code<\/h3>\n\n\n\n<p>Follow this section if you&#8217;re using an ESP32. <a href=\"#ESP8266-Code\" title=\"\">For an ESP8266 click here<\/a>.<\/p>\n\n\n\n<p>After installing the necessary board add-ons and libraries, copy the following code to your Arduino IDE, but don\u2019t upload it yet. You need to make some changes to make it work for you.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\r\n  Rui Santos\r\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-esp8266-mysql-database-php\/\r\n  \r\n  Permission is hereby granted, free of charge, to any person obtaining a copy\r\n  of this software and associated documentation files.\r\n  \r\n  The above copyright notice and this permission notice shall be included in all\r\n  copies or substantial portions of the Software.\r\n\r\n*\/\r\n\r\n#include &lt;WiFi.h&gt;\r\n#include &lt;WiFiClientSecure.h&gt;\r\n#include &lt;HTTPClient.h&gt;\r\n#include &lt;Wire.h&gt;\r\n#include &lt;Adafruit_Sensor.h&gt;\r\n#include &lt;Adafruit_BME280.h&gt;\r\n\r\n\/\/ Replace with your network credentials\r\nconst char* ssid     = &quot;REPLACE_WITH_YOUR_SSID&quot;;\r\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\r\n\r\n\/\/ REPLACE with your Domain name and URL path or IP address with path\r\nconst char* serverName = &quot;https:\/\/example.com\/post-esp-data.php&quot;;\r\n\r\n\/\/ Keep this API Key value to be compatible with the PHP code provided in the project page. \r\n\/\/ If you change the apiKeyValue value, the PHP file \/post-esp-data.php also needs to have the same key \r\nString apiKeyValue = &quot;tPmAT5Ab3j7F9&quot;;\r\n\r\nString sensorName = &quot;BME280&quot;;\r\nString sensorLocation = &quot;Office&quot;;\r\n\r\n\/*#include &lt;SPI.h&gt;\r\n#define BME_SCK 18\r\n#define BME_MISO 19\r\n#define BME_MOSI 23\r\n#define BME_CS 5*\/\r\n\r\n#define SEALEVELPRESSURE_HPA (1013.25)\r\n\r\nAdafruit_BME280 bme;  \/\/ I2C\r\n\/\/Adafruit_BME280 bme(BME_CS);  \/\/ hardware SPI\r\n\/\/Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);  \/\/ software SPI\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n  \r\n  WiFi.begin(ssid, password);\r\n  Serial.println(&quot;Connecting&quot;);\r\n  while(WiFi.status() != WL_CONNECTED) { \r\n    delay(500);\r\n    Serial.print(&quot;.&quot;);\r\n  }\r\n  Serial.println(&quot;&quot;);\r\n  Serial.print(&quot;Connected to WiFi network with IP Address: &quot;);\r\n  Serial.println(WiFi.localIP());\r\n\r\n  \/\/ (you can also pass in a Wire library object like &amp;Wire2)\r\n  bool status = bme.begin(0x76);\r\n  if (!status) {\r\n    Serial.println(&quot;Could not find a valid BME280 sensor, check wiring or change I2C address!&quot;);\r\n    while (1);\r\n  }\r\n}\r\n\r\nvoid loop() {\r\n  \/\/Check WiFi connection status\r\n  if(WiFi.status()== WL_CONNECTED){\r\n    WiFiClientSecure *client = new WiFiClientSecure;\r\n    client-&gt;setInsecure(); \/\/don't use SSL certificate\r\n    HTTPClient https;\r\n    \r\n    \/\/ Your Domain name with URL path or IP address with path\r\n    https.begin(*client, serverName);\r\n    \r\n    \/\/ Specify content-type header\r\n    https.addHeader(&quot;Content-Type&quot;, &quot;application\/x-www-form-urlencoded&quot;);\r\n    \r\n    \/\/ Prepare your HTTP POST request data\r\n    String httpRequestData = &quot;api_key=&quot; + apiKeyValue + &quot;&amp;sensor=&quot; + sensorName\r\n                          + &quot;&amp;location=&quot; + sensorLocation + &quot;&amp;value1=&quot; + String(bme.readTemperature())\r\n                          + &quot;&amp;value2=&quot; + String(bme.readHumidity()) + &quot;&amp;value3=&quot; + String(bme.readPressure()\/100.0F) + &quot;&quot;;\r\n    Serial.print(&quot;httpRequestData: &quot;);\r\n    Serial.println(httpRequestData);\r\n    \r\n    \/\/ You can comment the httpRequestData variable above\r\n    \/\/ then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)\r\n    \/\/String httpRequestData = &quot;api_key=tPmAT5Ab3j7F9&amp;sensor=BME280&amp;location=Office&amp;value1=24.75&amp;value2=49.54&amp;value3=1005.14&quot;;\r\n\r\n    \/\/ Send HTTP POST request\r\n    int httpResponseCode = https.POST(httpRequestData);\r\n     \r\n    \/\/ If you need an HTTP request with a content type: text\/plain\r\n    \/\/https.addHeader(&quot;Content-Type&quot;, &quot;text\/plain&quot;);\r\n    \/\/int httpResponseCode = https.POST(&quot;Hello, World!&quot;);\r\n    \r\n    \/\/ If you need an HTTP request with a content type: application\/json, use the following:\r\n    \/\/https.addHeader(&quot;Content-Type&quot;, &quot;application\/json&quot;);\r\n    \/\/int httpResponseCode = https.POST(&quot;{\\&quot;value1\\&quot;:\\&quot;19\\&quot;,\\&quot;value2\\&quot;:\\&quot;67\\&quot;,\\&quot;value3\\&quot;:\\&quot;78\\&quot;}&quot;);\r\n    \r\n    if (httpResponseCode&gt;0) {\r\n      Serial.print(&quot;HTTP Response code: &quot;);\r\n      Serial.println(httpResponseCode);\r\n    }\r\n    else {\r\n      Serial.print(&quot;Error code: &quot;);\r\n      Serial.println(httpResponseCode);\r\n    }\r\n    \/\/ Free resources\r\n    https.end();\r\n  }\r\n  else {\r\n    Serial.println(&quot;WiFi Disconnected&quot;);\r\n  }\r\n  \/\/Send an HTTP POST request every 30 seconds\r\n  delay(30000);  \r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-ESP8266-PHP-MySQL\/raw\/master\/code\/HTTPS_ESP32_MySQL_Database_PHP.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Setting your network credentials<\/h4>\n\n\n\n<p>You need to modify the following lines with your network credentials: SSID and password. The code is well commented on where you should make the changes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Replace with your network credentials\nconst char* ssid     = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting your serverName<\/h4>\n\n\n\n<p>You also need to type your domain name, so the ESP publishes the readings to your own server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* serverName = \"https:\/\/example-domain.com\/post-esp-data.php\";<\/code><\/pre>\n\n\n\n<p>Now, you can upload the code to your board.<\/p>\n\n\n\n<p><strong>Note: <\/strong> Most servers require you to make HTTPS requests. The code above makes HTTPS requests to be compliant with the requirements of most cloud servers nowadays.<\/p>\n\n\n\n<p class=\"rntbox rntclgray\">Your server doesn&#8217;t support HTTPS? <a href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-ESP8266-PHP-MySQL\/blob\/master\/code\/ESP32_ESP8266_MySQL_Database_PHP.ino\" target=\"_blank\" rel=\"noopener\" title=\"\">Use this code instead<\/a>.<\/p>\n\n\n\n<p>If you want to learn how the code works read the following section.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How the code works<\/h4>\n\n\n\n<p>This project is already quite long, so we won&#8217;t cover in detail how the code works, but here&#8217;s a quick summary:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Import all the libraries to make it work;<\/li>\n\n\n\n<li>Set variables that you might want to change (<span class=\"rnthl rntliteral\">apiKeyValue<\/span>, <span class=\"rnthl rntliteral\">sensorName<\/span>, <span class=\"rnthl rntliteral\">sensorLocation<\/span>);<\/li>\n\n\n\n<li>The <span class=\"rnthl rntliteral\">apiKeyValue<\/span> is just a random string that you can modify. It&#8217;s used for security reasons, so only anyone that knows your API key can publish data to your database;<\/li>\n\n\n\n<li>Initialize the serial communication for debugging purposes;<\/li>\n\n\n\n<li>Establish a Wi-Fi connection with your router;<\/li>\n\n\n\n<li>Initialize the BME280 to get readings;<\/li>\n\n\n\n<li>Initialize a secure WiFi client.<\/li>\n<\/ul>\n\n\n\n<p>Then, in the <span class=\"rnthl rntliteral\">loop()<\/span> is where you actually make the HTTP POST request every 30 seconds with the latest BME280 readings:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Your Domain name with URL path or IP address with path\nhttp.begin(client, serverName);\n\n\/\/ Specify content-type header\nhttp.addHeader(\"Content-Type\", \"application\/x-www-form-urlencoded\");\n\n\/\/ Prepare your HTTP POST request data\nString httpRequestData = \"api_key=\" + apiKeyValue + \"&amp;sensor=\" + sensorName                      + \"&amp;location=\" + sensorLocation + \"&amp;value1=\" + String(bme.readTemperature())                      + \"&amp;value2=\" + String(bme.readHumidity()) + \"&amp;value3=\" + String(bme.readPressure()\/100.0F) + \"\";\n\nint httpResponseCode = http.POST(httpRequestData);<\/code><\/pre>\n\n\n\n<p>You can comment the <span class=\"rnthl rntliteral\">httpRequestData<\/span> variable above that concatenates all the BME280 readings and use the <span class=\"rnthl rntliteral\">httpRequestData<\/span> variable below for testing purposes:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String httpRequestData = \"api_key=tPmAT5Ab3j7F9&amp;sensor=BME280&amp;location=Office&amp;value1=24.75&amp;value2=49.54&amp;value3=1005.14\";<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about HTTPS Requests with the ESP32: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-https-requests\/\">ESP32 HTTPS Requests (Arduino IDE)<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ESP8266-Code\">ESP8266 Code<\/h3>\n\n\n\n<p>Follow this section if you&#8217;re using an ESP8266. <a href=\"#esp32-code\" title=\"\">For an ESP32 check the section above<\/a>.<\/p>\n\n\n\n<p>After installing the necessary board add-ons and libraries, copy the following code to your Arduino IDE, but don\u2019t upload it yet. You need to make some changes to make it work for you.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\r\n  Rui Santos\r\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-esp8266-mysql-database-php\/\r\n  \r\n  Permission is hereby granted, free of charge, to any person obtaining a copy\r\n  of this software and associated documentation files.\r\n  \r\n  The above copyright notice and this permission notice shall be included in all\r\n  copies or substantial portions of the Software.\r\n\r\n*\/\r\n\r\n#include &lt;ESP8266WiFi.h&gt;\r\n#include &lt;ESP8266HTTPClient.h&gt;\r\n#include &lt;WiFiClientSecureBearSSL.h&gt;\r\n#include &lt;Wire.h&gt;\r\n#include &lt;Adafruit_Sensor.h&gt;\r\n#include &lt;Adafruit_BME280.h&gt;\r\n\r\n\/\/ Replace with your network credentials\r\nconst char* ssid     = &quot;REPLACE_WITH_YOUR_SSID&quot;;\r\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\r\n\r\n\/\/ REPLACE with your Domain name and URL path or IP address with path\r\nconst char* serverName = &quot;https:\/\/example.com\/post-esp-data.php&quot;;\r\n\r\n\/\/ Keep this API Key value to be compatible with the PHP code provided in the project page. \r\n\/\/ If you change the apiKeyValue value, the PHP file \/post-esp-data.php also needs to have the same key \r\nString apiKeyValue = &quot;tPmAT5Ab3j7F9&quot;;\r\n\r\nString sensorName = &quot;BME280&quot;;\r\nString sensorLocation = &quot;Office&quot;;\r\n\r\n\/*#include &lt;SPI.h&gt;\r\n#define BME_SCK 18\r\n#define BME_MISO 19\r\n#define BME_MOSI 23\r\n#define BME_CS 5*\/\r\n\r\n#define SEALEVELPRESSURE_HPA (1013.25)\r\n\r\nAdafruit_BME280 bme;  \/\/ I2C\r\n\/\/Adafruit_BME280 bme(BME_CS);  \/\/ hardware SPI\r\n\/\/Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);  \/\/ software SPI\r\n\r\nvoid setup() {\r\n  Serial.begin(115200);\r\n  \r\n  WiFi.begin(ssid, password);\r\n  Serial.println(&quot;Connecting&quot;);\r\n  while(WiFi.status() != WL_CONNECTED) { \r\n    delay(500);\r\n    Serial.print(&quot;.&quot;);\r\n  }\r\n  Serial.println(&quot;&quot;);\r\n  Serial.print(&quot;Connected to WiFi network with IP Address: &quot;);\r\n  Serial.println(WiFi.localIP());\r\n\r\n  \/\/ (you can also pass in a Wire library object like &amp;Wire2)\r\n  bool status = bme.begin(0x76);\r\n  if (!status) {\r\n    Serial.println(&quot;Could not find a valid BME280 sensor, check wiring or change I2C address!&quot;);\r\n    while (1);\r\n  }\r\n}\r\n\r\nvoid loop() {\r\n  \/\/Check WiFi connection status\r\n  if(WiFi.status()== WL_CONNECTED){\r\n\r\n    std::unique_ptr&lt;BearSSL::WiFiClientSecure&gt;client(new BearSSL::WiFiClientSecure);\r\n\r\n    \/\/ Ignore SSL certificate validation\r\n    client-&gt;setInsecure();\r\n    \r\n    \/\/create an HTTPClient instance\r\n    HTTPClient https;\r\n    \r\n    \/\/ Your Domain name with URL path or IP address with path\r\n    https.begin(*client, serverName);\r\n    \r\n    \/\/ Specify content-type header\r\n    https.addHeader(&quot;Content-Type&quot;, &quot;application\/x-www-form-urlencoded&quot;);\r\n    \r\n    \/\/ Prepare your HTTP POST request data\r\n    String httpRequestData = &quot;api_key=&quot; + apiKeyValue + &quot;&amp;sensor=&quot; + sensorName\r\n                          + &quot;&amp;location=&quot; + sensorLocation + &quot;&amp;value1=&quot; + String(bme.readTemperature())\r\n                          + &quot;&amp;value2=&quot; + String(bme.readHumidity()) + &quot;&amp;value3=&quot; + String(bme.readPressure()\/100.0F) + &quot;&quot;;\r\n    Serial.print(&quot;httpRequestData: &quot;);\r\n    Serial.println(httpRequestData);\r\n    \r\n    \/\/ You can comment the httpRequestData variable above\r\n    \/\/ then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)\r\n    \/\/String httpRequestData = &quot;api_key=tPmAT5Ab3j7F9&amp;sensor=BME280&amp;location=Office&amp;value1=24.75&amp;value2=49.54&amp;value3=1005.14&quot;;\r\n\r\n    \/\/ Send HTTP POST request\r\n    int httpResponseCode = https.POST(httpRequestData);\r\n     \r\n    \/\/ If you need an HTTP request with a content type: text\/plain\r\n    \/\/http.addHeader(&quot;Content-Type&quot;, &quot;text\/plain&quot;);\r\n    \/\/int httpResponseCode = https.POST(&quot;Hello, World!&quot;);\r\n    \r\n    \/\/ If you need an HTTP request with a content type: application\/json, use the following:\r\n    \/\/http.addHeader(&quot;Content-Type&quot;, &quot;application\/json&quot;);\r\n    \/\/int httpResponseCode = https.POST(&quot;{\\&quot;value1\\&quot;:\\&quot;19\\&quot;,\\&quot;value2\\&quot;:\\&quot;67\\&quot;,\\&quot;value3\\&quot;:\\&quot;78\\&quot;}&quot;);\r\n        \r\n    if (httpResponseCode&gt;0) {\r\n      Serial.print(&quot;HTTP Response code: &quot;);\r\n      Serial.println(httpResponseCode);\r\n    }\r\n    else {\r\n      Serial.print(&quot;Error code: &quot;);\r\n      Serial.println(httpResponseCode);\r\n    }\r\n    \/\/ Free resources\r\n    https.end();\r\n  }\r\n  else {\r\n    Serial.println(&quot;WiFi Disconnected&quot;);\r\n  }\r\n  \/\/Send an HTTP POST request every 30 seconds\r\n  delay(30000);  \r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-ESP8266-PHP-MySQL\/raw\/master\/code\/HTTPS_ESP8266_MySQL_Database_PHP.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Setting your network credentials<\/h4>\n\n\n\n<p>You need to modify the following lines with your network credentials: SSID and password. The code is well commented on where you should make the changes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Replace with your network credentials\nconst char* ssid     = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting your serverName<\/h4>\n\n\n\n<p>You also need to type your domain name, so the ESP publishes the readings to your own server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* serverName = \"https:\/\/example-domain.com\/post-esp-data.php\";<\/code><\/pre>\n\n\n\n<p>Now, you can upload the code to your board.<\/p>\n\n\n\n<p><strong>Note: <\/strong> Most servers require you to make HTTPS requests. The code above makes HTTPS requests to be compliant with the requirements of most cloud servers nowadays.<\/p>\n\n\n\n<p class=\"rntbox rntclgray\">Your server doesn&#8217;t support HTTPS? <a href=\"https:\/\/github.com\/RuiSantosdotme\/ESP32-ESP8266-PHP-MySQL\/blob\/master\/code\/ESP32_ESP8266_MySQL_Database_PHP.ino\" target=\"_blank\" rel=\"noopener\" title=\"\">Use this code instead<\/a>.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about HTTPS Requests with the ESP8266: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-https-requests\/\" title=\"\">ESP8266 NodeMCU HTTPS Requests (Arduino IDE)<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After completing all the steps, let your ESP board collect some readings and publish them to your server. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-BME280-Arduino-IDE-MySQL.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 BME280 Arduino IDE MySQL\" class=\"wp-image-86000\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-BME280-Arduino-IDE-MySQL.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-BME280-Arduino-IDE-MySQL.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>If everything is correct, this is what you should see in your Arduino IDE Serial Monitor:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"493\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-BME280-Readings-Arduino-IDE-Serial-Monitor.png?resize=859%2C493&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 BME280 Readings Arduino IDE Serial Monitor\" class=\"wp-image-85983\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-BME280-Readings-Arduino-IDE-Serial-Monitor.png?w=859&amp;quality=100&amp;strip=all&amp;ssl=1 859w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-BME280-Readings-Arduino-IDE-Serial-Monitor.png?resize=300%2C172&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/ESP32-ESP8266-BME280-Readings-Arduino-IDE-Serial-Monitor.png?resize=768%2C441&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 859px) 100vw, 859px\" \/><\/figure><\/div>\n\n\n<p>If you open your domain name in this URL path:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https:&#47;&#47;example-domain.com\/esp-data.php<\/code><\/pre>\n\n\n\n<p>You should see all the readings stored in your database. Refresh the web page to see the latest readings:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"754\" height=\"561\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp-mysql-database-demo.png?resize=754%2C561&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 View Sensor Readings Webpage PHP\" class=\"wp-image-129590\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp-mysql-database-demo.png?w=754&amp;quality=100&amp;strip=all&amp;ssl=1 754w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp-mysql-database-demo.png?resize=300%2C223&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 754px) 100vw, 754px\" \/><\/figure><\/div>\n\n\n<p>You can also go to phpMyAdmin to manage the data stored in your <span class=\"rnthl rntliteral\">SensorData<\/span> table. You can delete it, edit, etc&#8230;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"693\" height=\"707\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp8266-mysql-database-phpmyadmin-arduino-demonstration.png?resize=693%2C707&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 View Sensor Readings PHPMyAdmin SQL Database\" class=\"wp-image-129591\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp8266-mysql-database-phpmyadmin-arduino-demonstration.png?w=693&amp;quality=100&amp;strip=all&amp;ssl=1 693w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp8266-mysql-database-phpmyadmin-arduino-demonstration.png?resize=294%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 294w\" sizes=\"(max-width: 693px) 100vw, 693px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial you&#8217;ve learned how to publish sensor data into a database in your own server domain that you can access from anywhere in the world. This requires that you have your own <a rel=\"noreferrer noopener\" aria-label=\"server and domain name (opens in a new tab)\" href=\"https:\/\/randomnerdtutorials.com\/bluehost\" target=\"_blank\">server and domain name<\/a> (you can use a <a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-raspberry-pi-lamp-server\/\">Raspberry Pi for local access<\/a>). <\/p>\n\n\n\n<p>The example provided is as simple as possible so that you can understand how everything works. After understanding this example, you may change the appearance of the table, publish different sensor readings, publish from multiple ESP boards, and much more.<\/p>\n\n\n\n<p><strong>You might also like reading:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/visualize-esp32-esp8266-sensor-readings-from-anywhere\/\" title=\"\">Visualize Your Sensor Readings from Anywhere in the World (ESP32\/ESP8266 + MySQL + PHP)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/cloud-weather-station-esp32-esp8266\/\">DIY Cloud Weather Station with ESP32\/ESP8266 (MySQL Database and PHP)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-publish-sensor-readings-to-google-sheets\/\">ESP32 Publish Sensor Readings to Google Sheets (ESP8266 Compatible)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/firebase-esp32-esp8266-ebook\/\">Firebase Web App with ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/smart-home-ebook\/\" title=\"\">SMART HOME with Raspberry Pi, ESP32, and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">Free ESP32 Projects and Tutorials\u2026<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thank you for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this project, you\u2019ll build an ESP32 or ESP8266 client that makes an HTTP POST request to a PHP script to insert data (sensor readings) into a MySQL database. Updated &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32\/ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-mysql-database-php\/#more-85922\" aria-label=\"Read more about ESP32\/ESP8266 Insert Data into MySQL Database using PHP and Arduino IDE\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":129610,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,299,265,264],"tags":[],"class_list":["post-85922","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-0-esp32","category-esp8266-project","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/03\/ESP32-ESP8266-MySQL-Database-PHP-f.png?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/85922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=85922"}],"version-history":[{"count":12,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/85922\/revisions"}],"predecessor-version":[{"id":129632,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/85922\/revisions\/129632"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/129610"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=85922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=85922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=85922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}