Insert Data into MySQl database using ESP8266

Project Overview:
In this project we will be using ESP8266 Microcontroller as a Client to post values to the MySQL database. To demonstrate this, here we use LDR to measure light intensity 
*Link to the code is provided at the end of this page*


Inorder to build this project, We'll use the following technologies;
  • ESP8266 Programmed with Arduino IDE
  • XAMPP Server
Hosting your MySQL Database
  • Install XAMPP Server (This tutorial is intended for Windows Users Only)  https://www.apachefriends.org/index.html
  • Open the XAMPP Control Panel
    XAMPP Control Panel
  • Start Apache and MySQL Modules, then click on Admin (highlighted in the screenshot)
  • The column on the left displays the list of databases. You could create one by clicking the new option, Specifying the Table Name, No of columns in the table and the datatype of the entries (INT in this case)
    phpMyAdmin page
  • Thats all about creating a new database.
  • In this example  ( Database Name: trial0 , Table Name: status , Column 1; Timestamp , Column 2: VALUE )
  • You will have to modify the code based on your choice of names
  • Now Navigate to C:\xampp\htdocs\trial0 (if not available, create a new folder inside the htdocs folder)
  • Create a PHP file ( the simplest way is to type PHP scrips on Notepas and save it with .php extension) "sensor.php in my case" Link to the code is provided at the end of this page
Preparing Your ESP8266:

Circuit:
  • Connect one end of LDR to Analog pin A0
  • Other end to 3v supply in ESP8288
  • Power up the module using Micro usb cable (connect it to your laptop)

Setting your Network credentials

You will need to modify the following lines with your network credentials

Arduino code
The code is well commented on where you should make the changes.

(link to the code will be provided at the end of this page)

Open the serial monitor to check the status of execution

If everything works fine, You will be able to see the values getting updated in your database

Wrapping Up,

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.

Comments