I have Arduino Uno Wifi Rev2 and DHT11 sensor. I want to send the values of the sensor to a server and store it to a html file for now using WIFI , not Ethernet. So.. when I run the code I can see the POST in the access.log (Linux) , but It doesn't write any html files. I gave permissions to the files - still no progress. If I click for example post.php manually - It creates the file without problem. Any suggestions?
#include <WiFiNINA.h>
#include "DHT.h"
#define DHTPIN 3 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
char ssid[] = "";
char pass[] = "";
int status = WL_IDLE_STATUS;
char server[] = "mywebsite.com";
String postData;
String postVariable = "temp=";
WiFiClient client;
void setup() {
Serial.begin(9600);
dht.begin();
while (status != WL_CONNECTED) {
Serial.print("Trying to connect with: ");
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() {
// Wait a few seconds between measurements.
delay(2000);
// 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);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// 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);
Serial.print(F(" Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("C "));
Serial.print(f);
Serial.print(F("F Heat index: "));
Serial.print(hic);
Serial.print(F("C "));
Serial.print(hif);
Serial.println(F("F"));
postData = postVariable + t ; //only temperature
if (client.connect(server, 80)) {
client.println("POST /arduino/post.php HTTP/1.1");
client.println("Host: mywebsite.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postData.length());
client.println();
client.print(postData);
}
if (client.connected()) {
client.stop();
}
Serial.println(postData);
delay(3000);
}
My post.php code
<?php
$file = fopen("temp.html", "w") or die("Unable to open file!");
$temp = $_POST['temp'];
fwrite($file, $temp);
?>