Monday , 22 July 2019
Breaking News
Home > Tutorials > Arduino Tutorials > How to read data from database with PHP for WeMos EPS8266

How to read data from database with PHP for WeMos EPS8266

Assalam-o-Alaikum!

Dear friends today we will learn more interesting thing. We are going to pull out data from Database using PHP for WeMos. It is very important step for creating project for accessing devices status online.

Problem:

Let’s suppose you are going to design a project that have several electronics instruments and you need a web based software that will set the status of each instrument ON or OFF and then from database you pull data and get status of each instrument and then perform action on each instrument based on pulled data with WeMos of Arduino.

Solution:

Here is the solution.

You need a Server, Database and some PHP code that will do the magic. First design a database and then write code in PHP and then from WeMos send GET request to server, it will send you status of instrument and you just do something in WeMos.

For simplicity I will work on only two records. Let’s suppose I have installed two lights, one in my Guest Room and second in my Office for this database will be like this:

Now you have database and now you need PHP code to handle the request. Copy and paste below code and save as get_status.php into server. Here is PHP code:-

Code is very simple you just receive GET request from anywhere and then look value into database based on request and then simple echo status. On the other hand, in WeMos, you send the request to get_status.php with a parameter and then receive reply from server and store it into variable and then perform action based on that reply.

Now lets see what happened in WeMos. First see the code:-

As you can see I wrote a function for getting status of device and then in loop I called the function and provide parameters. The purpose of the function is that, you do not need to write complete code for every device, just call function in loop and then set parameters.

Now start the server and upload the code to WeMos and open Serial Monitor. You will see status from database.

Now change the values from 0 to 1 or 1 to 0 in database and see the result on serial monitor.

Hope you enjoy the tutorial. Sorry for broken English.

Have a nice day and Happy Coding. See you in next tutorial.

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

One comment

  1. Thanks for the code sample.
    I wonder whether the approach will work if I move ‘WiFiServer server(80);’ into ‘get_device_status’ so the server won’t be global object any more.

Leave a Reply

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

*

Ad Test