PHP code (not yet using database, instead storing in a text file on the server):
<?php
function StoreInDatabase($dev, $tmp, $hum)
{
$file = '../tempdata.txt'; //No database yet, just use txt file
$timestamp = date('Y-m-d H:i:s');
$current = $timestamp . "\t" . $dev . "\t" . $tmp . "\t" . $hum . "\n";
file_put_contents($file, $current, FILE_APPEND | LOCK_EX);
}
$source = $_GET['source'];
$temp = $_GET['temp'];
$humid = $_GET['humid'];
if (!isset($source))
{
echo "
Failure: missing source
";
exit;
}
if (!isset($temp))
{
echo "
Failure: missing temp
";
exit;
}
if (!isset($humid))
{
echo "
Failure: missing humid
";
exit;
}
StoreInDatabase($source, $temp, $humid);
echo "OK\n";
?>
Function SendData that sends the DHT values to the webserver php script:
/******************************************************
* This function sends measured data to a PHP script on
* the webserver where it is stored with a timestamp to
* a MySQL database or into a text file.
*******************************************************/
void SendData(int sensor, float temp, float humid)
{
HTTPClient http;
//Build call string:
String dataline = "http://<serverurl>/php/tempreport.php";
dataline += "?source=" + String(sensor);
dataline += "&temp=" + String(temp, 1); //2 decimal places
dataline += "&humid=" + String(humid, 1);
bool httpResult = http.begin(dataline);
if(!httpResult)
{
SerialDebug.println("Invalid HTTP request:");
SerialDebug.println(dataline);
}
else
{
int httpCode = http.GET();
if (httpCode > 0)
{ // Request has been made
SerialDebug.printf("HTTP status: %d Message: ", httpCode);
String payload = http.getString();
SerialDebug.println(payload);
}
else
{ // Request could not be made
SerialDebug.printf("HTTP request failed. Error: %s\r\n", http.errorToString(httpCode).c_str());
}
}
http.end();
}
My loop function:
void loop()
{
//Check if a new DHT reading is requested (once every interval ms):
unsigned long tickcount = millis();
if ((interval > 0) && ((unsigned long)(tickcount - lasttickcount) >= interval))
HandleDHTsensors(tickcount);
//Next follows other unrelated tasks not shown here
}
Not showing HandleDHTsensors() because of its size.
But basically it reads the sensors and for each successful read it makes one call like this for each sensor read:
if (dhtcnt > 0) SendData(1, temp1, hum1);
HTH