Monday , 25 September 2023
Breaking News
Home > Tutorials > Arduino Tutorials > How to control DHT11 sensor data with PHP and MySQL

How to control DHT11 sensor data with PHP and MySQL


Today’s topic is very interesting because today we will work on PHP and MySQL to control DHT11 data. We will write a code that get status from MySQL and based on that information you can stop receiving data or start receiving data.


Here is a problem. Let’s suppose you need a system that stores Sensors On/Off status into Database and you have a frontend that will show the current status of that sensor’s ON/OFF. If sensor’s receiving data status is ON then you can OFF it or if it is OFF you can ON it. If sensor status is ON then data will start to add into database otherwise will stop.


You have to perform 3 steps to complete this process.

  1. Create a database
  2. Write PHP/HTML/CSS code
  3. Write code for Sensor on WeMos or Arduino

And that’s it. Now let’s begin with step No.1.

I assume that you setup a server (may be XAMPP or WAMPP) and now create database (I created temperature) and add some data:

These are 2 tables one for controlling device status and 2nd is for storing sensor data (Temperature and Humidity)

Now let’s move to our send part. We will write some PHP code along with some CSS. First let’s see what is in PHP code:

You need three PHP pages and one inc page, one inc page for storing MySQL connection, one PHP page for getting device status and second PHP page for storing data into database and last one is for showing you visual and functional button for sensor status . First of all see connection code. It is .inc file.

Change values as per your server and save it as Now let’s see PHP code that will get status from Database. Copy an paste below code and save it as “get_status.php” without quotes:

This will get current status of Sensor status and echo it out. On the other side WeMos will catch this response as 1 or 0 and then perform action. Our next page is “add_data.php”. It will store data into database received from WeMos. Code is here:

Very simple code. Just receiving $_GET[] request from somewhere and then stores it into database. Now let’s see last page, very interesting, copy and paste this code and save file as “status_page.php”:

One thing that I want to tell you that visual button, generated with CSS is not my code I downloaded it from here

However, PHP and some major changes in mine. For this page you also need a CSS code. Copy and paste below code create a folder “css” and save in this folder and name it as “style.css”. But remember this is not mine:

Now let’s come to WeMos code:

Here is little explanation for this code.

First you connect with WiFi then you get sensor data and then you get status of sensor from MySQL via calling URL and then you add this data into Database via calling URL.

Now open serial monitor and upload this code, I assume that you setup Sensor on WeMos like this:

  1. Plus wire into 3 or 5 volt
  2. Minus wire into G
  3. Data wire into D4 or as you wish.

Browse for “status_page.php” to ON or OFF the sensor data. If status is OFF then your data should not be added into database if ON your sensor data should be added into database. Try toggle the button and check both status by watching serial monitor.

I hope you understand what I explained. However, you are free to comment and ask for anything.

Complete source code is here for download.


See you in next tutorial. Happy coding.


About Muhammad Faryad

Muhammad Faryad is a professional web application developer. He has been working for 5 years in ICE786 Technologies Pvt. Ltd as a Senior Programmer and Developer. You can contact him on facebook at


  1. brother, thanks a lot for sharing this code.

  2. Dear Sir. Really nice tutorial. I am looking how i could alter it to suit my needs. i have a remote site(VPN to that) that i need to have an ESP8266 nodemcu to send me data from:
    -3 magnetic door swithes (open/close)
    then i will have an idea of the temperature/humidity and the status of each door/window.
    My network is Router is on The Raspberry that has the LAMP server is on The ESP8266 nodemcu will in and the other ESP8266 in

    Would you be so kind to help me get this working, please?

  3. hello, and thank you for the guide.
    i tried to implement it but is giving me errors in the arduino code.
    i am using wemos mini d1r1 and it says “no matching function for call to ‘dht::dht()'”

  4. how can i send at the same time the data to thingspeak? i cant see how to

  5. hi all im getting omn the serial comm. is this value ?

    Device Status is going OFF”

    is there something wrong ?

  6. Hello..i am so grateful if u can share on how to setup the dht11 and esp8266 based on the scripting u made on wemos or arduino..can u please share it?

  7. thank you for shared,

  8. Hi!

    First, thanks for sharing.

    I have done all as you described, but it cant connect to localhost or something and cant add or get data.

    This is error line:

    Device Status is going OFF

    I would be grateful if you share sollution for this problem.



  9. please, sir, help me to understand these two lines-

    String get_status_url = “”;
    String add_data_url = “”;

    here what the IP address meant here? which IP address do you use? Is this IP from ESP8266 or Server IP?

  10. Dear, I was compiling and presented the following error:

    code: 36: 26: error: expected primary-expression before ‘.’ token

    get_temperature = DHT.temperature;


    code: 37: 23: error: expected primary-expression before ‘.’ token

    get_humidity = DHT.humidity;


    would anyone know how to solve ??

    The problem is in the functions: (dht_pin);
    get_temperature = DHT.temperature;
    get_humidity = DHT.humidity;

Leave a Reply

Ad Test

%d bloggers like this: