LAMPP execute php from arduino via ethernet no result in database

Hi there

I have installed LAMPP recently on my Ubuntu 22.04 and Arduino IDE 2.03. I am using UNO rev3 and a internet shield W5500. To simplify my problem, I want to store 2 default float values to a database. I have defined a PHP script (actually 2) which stores both values to the data base when using the following syntax in my browser (BRAVE)

http://localhost/ethernet/store_p1_data.php?current_usage=1200.12&current_delivery=0.12
This updates the database correctly.

I check the internet connection via the following code in the arduino UNO

void save_record(float current_usage, float current_delivery) {
  if (eth_client.connect(serv, 80)) { 
    Serial.println("connected");

The internet connection is ok ("Connected"). I also checked the server, dns, ip addresses which are correct as well.

    eth_client.print("GET ethernet/store_p1_data.php?");  //Connecting and Sending values to database
    eth_client.print("&current_usage=");
    eth_client.print(current_usage);
    eth_client.print("&current_delivery=");
    eth_client.print(current_delivery);

Somehow above code has no effect at all that is, the database does not get updated at all. I checked the log files, but I could not discover error messages in the access_log nor in the error_log even the php_error_log does not have any messages at all. I also checked /var/log/syslog but the same result.

A message related to my project is in the access_log

"GET /ethernet/store_p1_data.php?current_usage=1200.12&current_delivery=0.12 HTTP/1.1" 200 4

Are there any other loggings which I can check?

Please help me, for me it is impossible to discover the problem when there are no error logs.

By the way I managed in the past to read a power meter and store the data in the same database actually the same way with PHP script, which runs now for half a year without interruption. The code I use now is more or less a copy of that.

If I need to copy the code, please let me know how to add the code to this topic in a arduino formatted way. I could not find any and just copy paste will create baggercode.

Looks like there are errors here

store_p1_data.php

My guess is:
localhost == 127.0.0.1 == "this device"

collect the string to pass in full: "http://xxxxx"

connection.php

<?php
$username = "root";
$pass = "-------------";
$host = "localhost";
$db_name = "arduino";
$con = mysqli_connect ($host, $username, $pass);
$db = mysqli_select_db ( $con, $db_name );
?>

load_p1_data.php

<?php
include ('connection.php');
$sql_insert = "INSERT INTO power_p1_meter (current_usage, current_delivery) 
              VALUES 
                ('".$_GET["current_usage"]."',
                 '".$_GET["current_delivery"]."'

					  )";
if(mysqli_query($con,$sql_insert))
{
echo "Done";
mysqli_close($con);
}
else
{
echo "error is ".mysqli_error($con );
}
?>

Be aware the call via the browser does the job updates the database and ends with "Done". This means I think that the code is correct.

I did some investigation and remembered something about error logging. It seems there are 2 files php.ini one for production and one for development. It seems I only have the production one,

error_reporting = E_ALL | E_STRICT
log_errors = On
error_log = /opt/lampp/logs/php_error_log

In my case error_reporting = 22527, no clue could not find that code.

I changed the error_reporting to E_ALL, but after restarting the server the code 22527 is the same. If I do a locate of php.ini I have 2 files php.ini and php.ini-pre1.7.2, the latter has only comments.

I also change the if statement in file load_p1_data.php but also in that case no error message and php_error_log is still empty.

Is something wrong with the code in Arduino, so it isn;t calling the php script at all and if so how can I examine that?

To begin with, display the entire line transmitted to the server in the port monitor

 eth_client.print("GET http://localhost/ethernet/store_p1_data.php?");  //Connecting and Sending values to database
    eth_client.print("&current_usage=");
    eth_client.print(current_usage);
    eth_client.print("&current_delivery=");
    eth_client.print(current_delivery);

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

// Ethernet.begin(mac, ip, DNS, Gateway)

byte mac[] = { };
byte ip[] = { };   //Enter the IP of ethernet shield
byte serv[] = { };  //Enter the IPv4 address, of the PC where the PHP pages exists.
byte gateway[] = { }; My modem
byte dns[] = { }; My modem
// Ethernet.begin (mac, ip, dns, gateway, subnet), when all parameters are defined.
EthernetClient eth_client;  

float current_usage = 345;
float current_delivery = 1000;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Start session serial");
  Ethernet.begin(mac, ip, dns, gateway);   

  delay(1000);
  Serial.println("Start session");

  Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
  Serial.print("IP Address        : ");
  Serial.println(Ethernet.localIP());
  Serial.print("Subnet Mask       : ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("Default Gateway IP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DNS Server IP     : ");
  Serial.println(Ethernet.dnsServerIP()); 
  Serial.print("HW status         : "); 
  Serial.println(Ethernet.hardwareStatus());
  Serial.print("Link status       : "); 
  Serial.println(Ethernet.linkStatus());  

    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.");
  }
  else if (Ethernet.hardwareStatus() == EthernetW5100) {
    Serial.println("W5100 Ethernet controller detected.");
  }
  else if (Ethernet.hardwareStatus() == EthernetW5200) {
    Serial.println("W5200 Ethernet controller detected.");
  }
  else if (Ethernet.hardwareStatus() == EthernetW5500) {
    Serial.println("W5500 Ethernet controller detected.");
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  save_record(current_usage, current_delivery);
}


void save_record(float current_usage, float current_delivery) {
  if (eth_client.connect(serv, 80)) {  //Connecting at the IP address and port we saved before
    Serial.println("connected");
    eth_client.print("GET /ethernet/store_p1_data.php?");  //Connecting and Sending values to database
    eth_client.print("&current_usage=");
    eth_client.print(current_usage);
    eth_client.print("&current_delivery=");
    eth_client.print(current_delivery);

    eth_client.stop();  //Closing the connection
    delay(6000);
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
//    delay(DELAY_SESSION);
//    flash_times(LED_ERROR, 250, 15);
  }
}

I removed the bytes of the TCP/IP addresses, but they are ok. The setup contains a lot of testing data you can skip that, the program starts with the call of the function.

I also created a test script containing a PHP error, now the PHP_error_log contains errors. So it must be the call from arduino who does not work.

You probably need to finish the request header...

Please try to add this before the eth_client.stop()

eth_client.println(" HTTP/1.0");
eth_client.println("");
delay(1000);

Instead of the delay() it is a good idea to download the response from the server.

what if you try this?

  Serial.println("connected");
    eth_client.print("GET http://localhost/ethernet/store_p1_data.php?");  //Connecting and Sending values to databas

You don't want to use the server as a proxy. Its also most likely blocked.

I make a request like this:

 client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  client.stop();
  delay(10);

host == localhost

Hi Rintin,

Great, it is updating the database now, can you explain this? I mean the following changes, before tje eth.client.stop():

    eth_client.println(" HTTP/1.0");
    eth_client.println("");
    delay(1000);
    eth_client.stop();  //Closing the connection

I am still wondering why my first project which did not have the header part either, runs without any problems. The only thing now I can think of is that the program of the first project is running under PHP verion 8.1.6 and the program I am testing now is running on 8.2.0. Kind regards all for your answers which has helped me a lot. I will close this now as solved.

so you need to tell the server who reported and that the report is over :wink:
PS the server also needs to be told that the session is closed
PPS mark problem solved

HTTP is line based and each line is finished with a line break. A request starts with the request line followed by additional header lines. An empty line closes the request.

The request line is:

GET /ethernet/store_p1_data.php?current_usage=1200.12&current_delivery=0.12 HTTP/1.0

You missed the HTTP version at the end. Also the empty line to finish the request was missing.

The delay() is only a lazy way to give the server some time to process the request. The proper way here is to read the response from the server.

For more details take a look at RFC1945.

Thanks very much and understood. I will check the response.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.