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>