How to get data from mySQL database using wifi on WeMos

Hello, i'm University student, and i want to make my final year project.
i'm making a smart locker using Raspberry Pi as main processor and database storage, WeMos D1 mini using arduino programming as locker sub processor to control solenoid, and input limit switch.

and my question is, how to make WeMos to get data from database?
ex : the database have 1 table consist of 4 row ( date, barcode, locker number*, locker status**)

  • it's used for knowing which locker should be lock or unlock
    ** it's used to control solenoid of the choosen locker, (on= unlock/off(lock))

it's like, when the database showing locker number 3 and the status is on, then WeMos will continuously check the status of locker number 3. if the status is ON the WeMos will control solenoid to unlock

oh yeah, i'm using local area network over wifi for the media, and not using internet

could you use HTTP GET protocol?

How do you do that? is that possible to receive data from mysql database?
#new in this kind of thing :slight_smile:

if you do a web search for HTTP get you will get plenty of links
have a look at topic
http://forum.arduino.cc/index.php?topic=525702.15

still dont get it :frowning:
by anychance is that Get HTTP using internet?
what i'm still confused is how to use arduino to get data from phpmyadmin/ mysql database :frowning:

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>

const char* ssid = "WS Elektro";             //!!!!!!!!!!!!!!!!!!!!! modify this
const char* password = "workshop525";                //!!!!!!!!!!!!!!!!!!!!!modify this

int ledPin = D4;          //connect led pin to d4 and ground
int intensitas = 0;
WiFiServer server(80);

void setup() {
 Serial.begin(115200);
 delay(10);


 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);

 // Connect to WiFi network
 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");

 // Start the server
 server.begin();
 Serial.println("Server started");

 // Print the IP address
 Serial.print("Use this URL : ");
 Serial.print("http://");
 Serial.print(WiFi.localIP());
 Serial.println("/");
}


void loop() {

  
  
 // Check if a client has connected
 WiFiClient client = server.available();
 if (!client) {
   return;
 }

 // Wait until the client sends some data
 Serial.println("new client");
 while(!client.available()){
   delay(1);
 }

 // Read the first line of the request
 String request = client.readStringUntil('\r');
 Serial.println(request);
 client.flush();

 // Match the request

 int value = LOW;
 if (request.indexOf("/LED=ON") != -1) { // request.indexOf = cari char atau string dalam string, -1 = tidak ditemukan
      if(intensitas >= 255)
     intensitas = 255;
     else
     intensitas+=20;
     analogWrite(ledPin, intensitas);
    
     
     value = HIGH; 
 } 
 if (request.indexOf("/LED=OFF") != -1){
   if(intensitas <= 0)
    { intensitas = 0;}
    else
     intensitas-=20;
     
     analogWrite(ledPin, intensitas);
     
     
   value = LOW;
 }



 // Return the response
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println(""); //  do not forget this one
 client.println("<!DOCTYPE HTML>");
 client.println("<?php");
 client.println("$db = mysqli_connect('localhost','root','root','coba')");
 client.print  ("or die('Error connecting to MySQL server.')");
 client.println("<html>");
 client.println("<head>");
 client.println("</head>");
 client.println("<body>");
 client.println("<h1>PHP connect to MySQL</h1>");
 client.println("<?php>");
 client.println("$query = 'SELECT * FROM test'");
 client.println("\n");
 client.println("mysqli_query($db, $query) or die('Error querying database.')");
 client.println("$result = mysqli_query($db, $query)");
 client.println("$row = mysqli_fetch_array($result)");
 client.println("while ($row = mysqli_fetch_array($result))");
 client.print  ("{echo $row['first_name'] . ' ' . $row['last_name'] . ': ' . $row['email'] . ' ' . $row['city'] .'
'}");
 client.println("mysqli_close($db)");
 client.println("?>");
 client.println("</body>");
 client.print("Led pin is now: ");

 if(value == HIGH) {
   client.print("On");  
 } else {
   client.print("Off");
 }
 client.println("

");
 client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on pin 4 ON
");
 client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 4 OFF
");
 client.println("</html>");

 delay(1);
 Serial.println("Client disconnected");
 Serial.println("");

}

this is my code from the basic wifi control LED using html code
what i was doing is writing html code into arduino. i thought writing it will make html get data from mysql database using php

. confused

what i'm still confused is how to use arduino to get data from phpmyadmin/ mysql database

Until you understand the differences by the php scripr, phpmyadmin, and the database, you probably never will get it.

One of the reasons that php and MySQL are usually installed together is that php has a bunch of functionality that makes it easy to get data from, or insert data into (a table in) a MySQL database.

So, eat the pie, and put the toys away for now.

Learn how to write a php script ON YOUR PC that can get data out of a table in your MySQL database.

Learn how to invoke that php script from a browser. A hint: the browser makes a GET request (but you don't see that happening) and reads the server response.

Now, go to another PC in the same local area network, and invoke that php script from a browser.

ONLY after you can successfully do that should you even consider how to make the NOT-AN-ARDUINO do that.

Thankyou so much Pauls,
now i understand PHP, and how to collect the data and the server things.
I apreciate your opinion in telling me to learn those first xD.

and now my mission is almost accomplished,

now i get to know how to store data from database to a string in arduino.

so im trying to encode data that i want from database in PHP code.
then i'll try to decode it in arduino so i can output it into controlling my solenoid for lock and unlock

Thanks!