Monday , 27 May 2019
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

Assalam-o-Alaikum!

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.

Problem:

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.

Solution:

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 inc.inc. 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

https://tympanus.net/Tutorials/CSS3ButtonSwitches/index3.html

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.

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 www.facebook.com/tolamangali

7 comments

  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)
    -dht11
    then i will have an idea of the temperature/humidity and the status of each door/window.
    My network is 192.168.0.0/24. Router is on 192.168.0.1. The Raspberry that has the LAMP server is on 192.168.0.2. The ESP8266 nodemcu will in 192.168.0.3 and the other ESP8266 in 192.168.0.4

    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 ?

    “localhost/testdht/get_status.php?device_name=home_sensor
    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,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Ad Test