Uploading/Downloading data to website hosted MySQL database via Ethernet Shield

Hello everyone,

I'm pretty sure this question has been asked before but I've only seen answers related to localhosted databases or with php files that are stored on the computer itself. My question is pretty simple: how can I run a php script that's hosted on a website of mine via arduino code and give it some input parameters (the php script obviously is already set to receive them and store the values in my database). And how to receive some parameters from a php script (download data, again, with the php script being on my website)? Thank you for your time.

Not sure the question is clear.

You send a request over TCP-IP, if you know how to reach the local computer it’s the same code for a remote server, you just need to ensure the routing works to reach that remote IP.

I’m having some trouble with the code that I found. I try to establish the connection near the end of the void loop (see code below) but the code simply freezes at that point. It doesn’t continue looping, nor enters any of the if/else statements which would display the connection result.

#include <SPI.h>



//---------ELECTRONOOBS----------//
//--------BT app control---------//


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

//Definicoes de IP, mascara de rede e gateway
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,99);
char server[] = "https://chatocn.000webhostapp.com/"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")


int light = 8;
char R;
String Received;
int light_state = 2;
int sensor;
int c1 = 0, c2 = 0, c3 = 0;
int c1c = 1, c2c = 1, c3c = 1;
int msg;
int timer = 0, del = 500, contres = 0;
#define DHTPIN 10
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE); 
EthernetClient client;
void setup() {

  Serial.begin(9600);

  pinMode(light, OUTPUT);
  dht.begin();
  Ethernet.begin(mac, ip);


}

void loop() {


  while (Serial.available() > 0)
  {
    R = Serial.read();
    Received += R;
  }

  delay(500);

  sensor = analogRead(A0);

  Serial.print(sensor);
  Serial.print(" ");
  Serial.print(getTemp("c"));
  Serial.print(" ");
  Serial.print(getTemp("hic"));
  Serial.print(" ");
  Serial.println(getTemp("h"));
  Serial.println(Received);
  Received = "";

  if (client.connect(server, 80)) {
    Serial.println("--> connection successful\n");
    client.print("GET /arduino.php?"); // This
    client.print("temperature="); // This
    client.print(getTemp("c")); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("https://chatocn.000webhostapp.com/"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
    client.stop();    // Closing connection to server

  }

  else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }

}

float getTemp(String req)
{

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // Compute heat index in Kelvin 
  float k = t + 273.15;
  if(req =="c"){
    return t;//return Cilsus
  }else if(req =="f"){
    return f;// return Fahrenheit
  }else if(req =="h"){
    return h;// return humidity
  }else if(req =="hif"){
    return hif;// return heat index in Fahrenheit
  }else if(req =="hic"){
    return hic;// return heat index in Cilsus
  }else if(req =="k"){
    return k;// return temprature in Kelvin
  }else{
    return 0.000;// if no reqest found, retun 0.000
  }
 
}




///////////////////////////////////////////

When I take out that segment trying to connect to the database the code works fine and prints the values of the sensors every loop. With this code I’m getting stuck at the first iteration with only the values of the first loop and no connection error/success message. Any ideas?

client.connect() will return different values based on status so

  if (client.connect(server, 80)) {

does not mean much as you don't get a 0 answer in case of an issue so you will still try to do something.. You should test == 1 for success

you should capture the value it returns and see if it's one of those:

SUCCESS 1
TIMED_OUT -1
INVALID_SERVER -2
TRUNCATED -3
INVALID_RESPONSE -4

Actually, I ran the code and it indeed returned 0. I’m using a way simpler code now, that only gets data from 1 sensor. Note that I’ve put a line to print the result of the connection before the if/else.

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

//Definicoes de IP, mascara de rede e gateway
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,99);
char server[] = "chatocn.000webhostapp.com"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")


int light = 8;
int light_state = 2;
int sensor;

EthernetClient client;
void setup() {

  Serial.begin(9600);

  pinMode(light, OUTPUT);
  Ethernet.begin(mac, ip);


}

void loop() {

  delay(5000);

  sensor = analogRead(A0);

  Serial.println(sensor);
  Serial.println(client.connect(server,80));
  if (client.connect(server, 80)) {
  Serial.println("--> connection successful\n");
  client.print("GET /arduino.php?"); // This
  client.print("temperature="); // This    
  client.print(sensor); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
  client.println(" HTTP/1.1"); // Part of the GET request
  client.println("https://chatocn.000webhostapp.com/"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
  client.println(); // Empty line
  client.println(); // Empty line
  client.stop();    // Closing connection to server

  }

  else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }

}

I’m guessing this has to do with the fact that the website that’s hosting my base is using https and not http. But I don’t know how to work around this. By the way, I’m really grateful you’re taking your time to answer these questions.

sure https is a different game and nothing to do with your initial question… what Arduino do you have ??

Arduino UNO with a W5100 Ethernet Shield. All I'm trying to do is connecting it to my website to run the php script. I'm sorry, as you can see I'm really new to this and I had no idea that going from http to https would cause all this trouble.

no luck.. won't work with https

This will probably be the dumbest question you've ever heard, but I tried accessing the website using http instead of https and it works, it just shows connection not secure. Would this solve the problem or should I just abandon this project?

well - at it says, the communication is not crypted, so whatever you send to your server is in plain text. if you have password or confidential data etc going through that, then it’s a problem…

if this is a log of the temperature of your aquarium and you don’t mind being possibly hacked then you are possibly OK

For now not having encryption is fine. But even trying to access it as http, I'm getting the same error in the arduino code. Probably has to do with these two lines.

char server[] = "http://chatocn.000webhostapp.com"; // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")

I've tried both with and without the http:// in the code above. And

  client.println("http://chatocn.000webhostapp.com/"); //

Inside the if when trying to access the website. In both tries I'm still getting a response of 0 from the connect.

your server is chatocn.000webhostapp.com

HTTP is a protocol

and what you want to perform is a GET request probably.

you will get an answer to that request, in the form of a HTTP header, a blank line and then whatever the URL you triggered returned

look at the examples or use google translate to read this tutorial for example. (I'm sure there are tons in english too)

Thank you so much. I speak portuguese so I could actually make my way through the french and managed to get it to work. Again, I'm really grateful for you help.

Perfect - well done