Retrieve data from a database using esp8266 AT commands

Hi everyone,

I am working on a payment system using an Arduino Mega, and RFID cards. The program allows people to buy items and refill their accounts. I have now managed to post the money in people's account to a database using esp8266 and AT commands.

Now I want to retrieve the data in the database every time the Arduino is turned on, but I am not sure how to do this.

Should I use a GET request and how might this look like??

Thank you in advance :),
Alex

Payment_system_without_credentials.ino (15.7 KB)

It looks like you are talking from your ESP8266 to a web server running PHP.

You appear to be using command like :

data = "Money=" + String(account[i]) + "&change=" + String(change) + "&id=" + String(i) + "&purchase=" + purchase;

wrapped up in an HTTP POST transaction to send data into the database.

You have to write a parameterised PHP command (similar to accounts.php) on the web server which returns the needed data from the database in the background. You can use HTTP Post or get to call the command, then parse the results.

Maybe you also want to remove your credentials from the published code.

Thank you for your answer! :slight_smile:

How would you get the results from the PHP file?

I can't help much more because I use the Arduino core software directly on ESP8266 as an alternative to using the AT command interpreter.

You'll have to find an ESP8266 Web Client example/tutorial somewhere which uses AT commands and parses the results.

I guess it will be similar to what you have already done when you issue a get or post request and parse the results. This, since you are already using esp.find() to look at data sent back from the web server, so you are effectively parsing data in serial stream to find the information you require.

As a start, you must understand the page format that the webserver returns, when you issue a request.

Thank you so much for your answer :slight_smile: :), but how might this code look like?.

All the other tutorials online use the ESP8266Wifi library, so is it possible to simply do it using the AT commands??

Kind regards,
Alex

Have you already coded the new command (URL) to retrieve information from the database ?

Start by attempting to send the new command in the same way as you have already done to put information in the database, then see what format the results are delivered in by printing them out. This is based on what you've already done:

. . .
. . .
// build your new command  as before then send it

String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

esp.print(sendCmd);

esp.println(postRequest.length() );

delay(500);

// print out the data returned by the command . . .

while (esp.available()) {

String tmpResp = esp.readString();

Serial.println(tmpResp);

}

// close the connection

esp.println("AT+CIPCLOSE");

Once you see that , then it will become clearer how you parse the returned results to pick out the required data.

Ahhhhhh, now it makes sense!

So, basically, I just send a POST request and then see what the server answers???

Thank you so much :slight_smile: :slight_smile: :), you made my day!

Kind regards,
Alex