// ESP32 data loger to file on your server and acces to file from
// web browser or mobile phone.
// In this example logging data from temp. and humi. sensor and
// NTP time.
// monitor images:
// http://girus.5xs.sk/esp32/2024-03-19%2015.28.13.png
// http://girus.5xs.sk/esp32/2024-03-19%2015.22.26.png
#include <WiFi.h>
#include <HTTPClient.h>
#include <time.h>
#define NTP_SERVER "europe.pool.ntp.org"
#define UTC_OFFSET 3600
#define UTC_OFFSET_DST 0
#define LED 2
#include "Wire.h"
#include "SHT31.h"
#define SHT31_ADDRESS 0x44
SHT31 sht;
int ledState = 0;
int reebot = 0;
uint32_t xx = 10000 + millis();
String message = ".";
String out = ".";
char cas[30];
void printLocalTime() {
time_t rawtime;
struct tm ti;
if (!getLocalTime(&ti)) {
Serial.println("Connection Error");
return;
}
static const char wday_name[][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char mon_name[][4] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
//Serial.println(&ti, "%H:%M:%S %d.%m.%Y %Z");
sprintf(cas, "%s %i.%s %.2i:%.2i", wday_name[ti.tm_wday], ti.tm_mday, mon_name[ti.tm_mon], ti.tm_hour, ti.tm_min);
Serial.println(&ti);
}
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
Serial.println("Connecting to WiFi ");
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
Serial.print("#");
delay(250);
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
Wire.begin(26, 27, 100000);
sht.begin();
uint16_t stat = sht.readStatus();
Serial.printf("SHT31 Status: %X\n", stat);
}
void loop() {
ledState = !ledState;
digitalWrite(LED, ledState);
printLocalTime();
sht.read(false); // default = true/fast slow = false
float temp = sht.getTemperature();
float humi = sht.getHumidity();
Serial.printf("temp:%.2f\thumi:%.2f\r\n", temp, humi);
out = "time: " + String(cas) + "\ttemp: " + String(temp) + "\thumi: " + String(humi);
log();
delay(60000);
}
void log() {
if (reebot == 0) {
message = "message=---ESP32 TempHumi (POWERON_RESET)\n";
reebot = 1;
} else {
message = "message=[ESP32 WOKWI] > " + String(out) + ";\n";
}
HTTPClient http;
http.begin("http://your.server.php/esp32/upload.php"); // Nahraďte URL adresou Vášho servera
//http.addHeader("Content-Type", "text/plain");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(message);
if (httpResponseCode == 200) {
Serial.printf("httpResponseCode: %d\n", httpResponseCode);
Serial.println(message);
} else {
Serial.printf("httpResponseCode: %d\n", httpResponseCode);
}
http.end();
}
/* ---------- upload.php -------------
<?php
// Define the expected format of the data from ESP32
$data_key = "message"; // Replace with the key used in your ESP32 code to send data
$file = "esp.txt";
// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if the data key is present in the request body
if (isset($_POST[$data_key])) {
$received_data = $_POST[$data_key];
// file_put_contents($file, $received_data);
if (File_Exists ($file)) {
$fp = FOpen ($file, "r");
$data = FRead ($fp, FileSize($file));
FClose($fp);
}
$fp = FOpen ($file, "w");
$fileSize = filesize($file);
//FWrite ($fp, $fileSize);
FWrite ($fp, "<b>" . $received_data . "</b>" . $data);
FClose ($fp);
echo 'Data received successfully!<br><br>';
}
else {
echo 'Error: Missing data key in request.';
}
if (isset($_POST["cls"])) {
$myfile = fopen($file, "w") or die("Unable to open file!");
fclose($myfile);
}
}
else {
echo "Error: This script only accepts POST requests.<br>\n";
}
?>
-------------------------------------------------- */