Arduino is able to connect to server but not POST temperature data to php

I'm working on a project that gets arduino sensor data and sends it to my apache webserver. Right now I'm just trying to test some code as a proof of concept to see if I can send something to the server, so I ripped some code from this guy (source code in the description) with some minor modifications. The code in the arduino is as follows:


#include <WiFiNINA.h>

#define sensorPin A5

char ssid[] = "--WIFI--";
char pass[] = "";
boolean connected = false;
int status = WL_IDLE_STATUS;

char server[] = "--ADDRESS--";

String postData;
String postVariable = "temp=";

WiFiClient client;

void setup() {

  Serial.begin(9600);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  IPAddress gateway = WiFi.gatewayIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
  int reading = analogRead(sensorPin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float temperatureC = (voltage - 0.5) * 100 ;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

  postData = postVariable + temperatureF;

  if (client.connect(server, 80)) {
    if (connected == false)
    {
      connected = true;
      Serial.print("Connected\n");
    }
    //Serial.print("Connected");
    client.println("POST /test/post.php HTTP/1.1");
    client.println("Host: --ADDRESS--");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(postData.length());
    client.println();
    client.print(postData);
  }
  else if (connected == true)
    {
      connected = false;
      Serial.print("Not Connected\n");
    }

  if (client.connected()) {
    client.stop();
  }
  Serial.println(postData);

  delay(3000);
}

Then I also have post.php here:

<?php
$time = time();
$tempF = $_POST["temp"];
$file = 'temp.html';
$data = $time."  -  ".$tempF;
file_put_contents($file, $data);
?>

I also created a temp.html file which is empty. The issue that I'm having is that when running the arduino code, it's able to display the temperature data and says it has connected to the server in the command prompt, but when I go back to check the server at /test/temp.html it displays nothing. Is there something that I'm missing?

I also tried another script called time.php which I was able to get to display the UTC time correctly, and I do have php installed on my Debian server. At this point, I at least know that php is working and that the arduino is connecting to the server somehow.

<!doctype html>
<html lang="en">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>

<body>

<p>Date/Time: MM/DD/YYYY hh:mm<br>
<?php echo(strftime("%m/%d/%Y %H:%M")); ?></p>

<p>Date/Time: DD.MM.YYYY hh:mm (German)<br>
<?php echo(strftime("%d.%m.%Y %H:%M")); ?></p>

</body>

</html>

I'll try this later today when I have my arduino with me. I've also tried testing file_put_contents() but only for the time variable. Issue is, this still isn't doing anything. I checked that I had the up to date php.

$ php -v
PHP 8.0.12 (cli) (built: Oct 22 2021 12:38:28) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.12, Copyright (c) Zend Technologies

$ whereis php
php: /usr/bin/php7.4 /usr/bin/php /usr/bin/php8.0 /usr/lib/php /etc/php /usr/share/php7.4-readline /usr/share/php7.4-common /usr/share/php8.0-common /usr/share/php7.4-json /usr/share/php7.4-opcache /usr/share/php8.0-readline /usr/share/php8.0-opcache /usr/share/man/man1/php.1.gz

Any idea why this might be happening.

Tried that solution, nothing is showing up still.

Maybe try using GET instead - that's how I send temperature data to a web server. It's nice because then you can test the page with a browser before trying the Arduino.

Not too experience with php, how do I use GET?

Can't help you there - all my receiving pages are Perl based. The PHP solution is a popular one though, I expect your search engine can find plenty of examples.

As this is linux, could it be a permissions problem on the folder where you are trying to write the file?

Coming from a Windows background, this sort of thing drove me nuts, so, although not advisable in the real world, I granted everybody read/write permissions on the folder I was using whilst showing the concept.

I changed the permissions of post.php and temp.html and it seems like something is working now at least, but when I run the php code I get something like this:

PHP Warning:  Undefined array key "temp" in /var/www/html/test/post.php on line 2

And the temp.html file just shows "(the time) - " without the time or temperature data.