Connect Arduino UNO to a remote database

Hi everybody.

I'm going to use Ethernet Shield for Arduino in order to connect my Arduino Mega 2560 to a database hosted on a remote domain.

I'm not an expert at networks or at databases, so I'm not sure what's the correct way to connect my sketch to the database for downloading some data.

I know that the Ethernet.h library permits me to send a HTTP request to an IP, and that I can also select the port to communicate into (for accesing my own database on a databases server, right?). But I don't know if it's possible to access to a DB only using a HTTP request.

Can anybody write me a simple example of how to connect to a remote database and download some data of a table?

Excuse my ignorance. I use the forum hardly ever, always prefer to search on Google, but in this case I don't know enough about DB for understanding the most of the websites.

Thank you!

Many years ago when I did this same thing, having an Arduino Mega with onboard Ethernet send data to a database at my hosting site at the time.

What I did was to send data via HTTP 'GET' method to my hosting site where a PHP script would then take the data and open the MySQL database and insert the data accordingly.

I also had another PHP script when a HTTP client connected to retrieve the data from the database to be displayed in near real time on a web page.

I recall the PHP scripts were not complex.

You need to know what your web hosting provider provides you in terms of these features, though I think it's all pretty common these days.

There are plenty of examples of Arduino code to use HTTP 'GET' and 'POST' data.

Paul - VK7KPA

Great! That's the kind of answer I was looking for.

I have pretty knowledges about programming on HTML5, CSS and Javascript. Also I know perfectly C code. But I have NO IDEA about PHP (in fact, until yesterday I haven't know what it is). That's the reason because I didn't know that it is the bridge between Arduino/Ethernet Shield and my html webpage.

Thanks for answering. I will look for some examples on the Internet in order to getting started on my project. I'll post some questions when I got stucked.

My best regards.

Hi there again!

I’ve implemented my database with phpMyAdmin, hosted at Hostinger (free acount), and used php for bridging my database with Arduino, but It seems not to work properly.

When I tip on my web browser, the php script returns me correctly the string ‘userid=;’.

But when I try to get this code throught Arduino I don’t get any response. This is my code, hope you can help me!

/* Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };    //Dirección MAC del equipo
char server[] = "";              //Host del servidor (usando DNS)

// Dirección estática en caso de que falle el DHCP
IPAddress ip(192, 168, 0, 177);

// Inicializar la librería Ethernet
EthernetClient client;

//Otras variables
String codigo;    //Almacenará la respuesta del servidor
char c;
char userID;      //Almacenará el userID extraído de la bbdd

void setup() {

  // Conexión ethernet
  if (Ethernet.begin(mac) == 0) {
    //Si no es posible conectarse por DHCP
    Serial.println("Error al configurar Ethernet usando DHCP");
    //Probar con la dirección estática
    Ethernet.begin(mac, ip);

  // Si se produce conexión...
  if (client.connect(server,80)) {

    Serial.println("Enviando petición...");
    //Realizar una petición HTTP:
    client.print("GET /login.php?user=user&pass=pass");
    client.println(" HTTP/1.0");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");

    Serial.println("Petición enviada!");
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");


void loop()
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c =;
    codigo += c;

  if(c == ';'){
    int longitud = codigo.length();
    int pos = codigo.indexOf("userid=");
    if(pos >= 0 && longitud == 9){
      userID = codigo[7];
      Serial.print("Tu User ID es:");
      Serial.println("Error al recibir User ID");

    codigo = "";
    c = 5;

In the future I will add users to the database so the variable userID could be read correctly. But, at the moment, I’m not receiving any string from the server. What I shall do?

Thanks for your help!

My best regards,

bivalo, you need to take the HTTP code from your setup section and place it in the main loop section.
You will find many examples here in the forum for Arduino code to send data via a GET method over HTTP, please have a look.
User SuferTim often provides very good examples of this in the past.

Paul - VK7KPA

Hello, rockwallaby.

It's correct to use port 80, right? It's reserved for http requests as I read.

Thank you!

Yes, port 80 is fine, and probably the only port you can use if your hosting site is provided by a commercial company.
Hosting your own, then you can select a different port, as long as all components are configured accordingly.

So, yes, use port 80.

Paul - VK7KPA