Hi,
I need to monitor the temp / humidity inside a box (cctv), so have written a webserver and dht11 with littlefs. However the majority of the time the webserver is timing out, so I tried to use tasks using xTaskCreate, it still times out, so I tried setting the priority, then taskYIELD(), no joy!So I tried using xTaskCreatePinnedToCore(), but the core dumps (constantly reboots) if I try to put the server on core 0.
Any ideas / solutions please?
// FILE SYSTEM
#include <Arduino.h>
#include "FS.h"
#include <LittleFS.h>
#define FORMAT_LITTLEFS_IF_FAILED true // used to install the FS the first time if need be...
// TEMPERATURE SENSOR
#include <DHT.h>
#define DHT11_PIN 21 // ESP32 pin GPIO21 connected to DHT11 sensor
DHT dht11(DHT11_PIN, DHT11);
// WIFI
#include <WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";
// WEB SERVER
WiFiServer server(80);
String header;
// TIMER STUFF
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 10000;
float last_humi = 0;
float last_tempC = 0;
unsigned long dht11_timer = timeoutTime;
unsigned long diff_time = 0;
TaskHandle_t Task1Handle = NULL;
TaskHandle_t Task2Handle = NULL;
int readFile(fs::FS &fs, const char *path, int print_serial=0, WiFiClient client=0) {
// print_serial: 0=serial, 1=both, 2=just web
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if (!file || file.isDirectory()) {
Serial.println("- failed to open file for reading");
return 1;
}
Serial.println("- read from file:");
while (file.available()) {
if(print_serial<2){
Serial.write(file.read());
}
if(print_serial>0){
client.println(file.readString());
}
}
file.close();
return 0;
}
void writeFile(fs::FS &fs, const char *path, const char *message) {
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("- failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("- file written");
} else {
Serial.println("- write failed");
}
file.close();
}
void appendFile(fs::FS &fs, const char *path, const char *message) {
Serial.printf("Appending to file: %s\r\n", path);
File file = fs.open(path, FILE_APPEND);
if (!file) {
Serial.println("- failed to open file for appending");
return;
}
if (file.print(message)) {
Serial.println("- message appended");
} else {
Serial.println("- append failed");
}
file.close();
}
void deleteFile(fs::FS &fs, const char * path){
Serial.printf("Deleting file: %s\r\n", path);
if(fs.remove(path)){
Serial.println("- file deleted");
} else {
Serial.println("- delete failed");
}
}
//void dht11_reader()
void dht11_reader(void *parameter)
{
for (;;) {
//*
//xTaskYield();
taskYIELD();
// read humidity
float humi = dht11.readHumidity();
//vTaskDelay(pdMS_TO_TICKS(200));
//xTaskYield();
taskYIELD();
// read temperature in Celsius
float tempC = dht11.readTemperature();
//vTaskDelay(pdMS_TO_TICKS(200));
//xTaskYield();
taskYIELD();
// read temperature in Fahrenheit
//float tempF = dht11.readTemperature(true);
// check whether the reading is successful or not
//if ( isnan(tempC) || isnan(tempF) || isnan(humi)) {
if ( isnan(tempC) || isnan(humi)) {
Serial.println("Failed to read from DHT11 sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(humi);
Serial.print("%");
Serial.print(" | ");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println("°C ~ ");
//Serial.print(tempF);
//Serial.println("°F");
char buffer[64];
int ret = snprintf(buffer, sizeof buffer, "%.2f,%.2f\n", tempC, humi);
//appendFile(LittleFS, "/log.txt", "World!\r\n");
appendFile(LittleFS, "/log.txt", buffer);
//appendFile(LittleFS, "/log.txt", "\n");
last_humi = humi;
last_tempC = tempC;
}
//*/
//vTaskDelay(10000 / portTICK_PERIOD_MS);
vTaskDelay(pdMS_TO_TICKS(30000));
}
}
void webserve(void *parameter)
{
for (;;) {
//*
//Serial.println("Check Client.");
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client.");
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
//client.println("Connection: close");
client.println();
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<body><h1>ESP32 Web Server</h1>");
client.println("Temperature: ");
client.println(last_tempC);
client.println(" C<br />");
client.println("Humidity: ");
client.println(last_humi);
client.println(" %<br />");
client.println("<pre>");
readFile(LittleFS, "/log.txt", 1, client);
client.println("</pre><br />");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
//*/
//vTaskDelay(pdMS_TO_TICKS(200));
taskYIELD();
}
}
void setup() {
// put your setup code here, to run once:
// SERIAL
Serial.begin(115200);
while (!Serial)
{
; // SERIAL: wait USB port
}
Serial.println("\n\nINIT\n");
// FILE SYSTEM
if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
Serial.println("LittleFS Mount Failed");
return;
}
//writeFile(LittleFS, "/log.txt", "Hello ");
//appendFile(LittleFS, "/log.txt", "World!\r\n");
//readFile(LittleFS, "/log.txt");
deleteFile(LittleFS, "/log.txt");
if (readFile(LittleFS, "/log.txt")){
Serial.println("Log not exist!");
writeFile(LittleFS, "/log.txt", "INIT\n");
}
// RESET
//writeFile(LittleFS, "/log.txt", "INIT\n");
// WIFI
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.setTxPower(WIFI_POWER_8_5dBm); // ??? on an ESP32C3 Super Mini, kept getting: _onStaArduinoEvent(): Reason: 2 - AUTH_EXPIRE
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
// TEMPERATURE
dht11.begin(); // initialize the DHT11 sensor
delay(200);
xTaskCreate(dht11_reader, "Blink1", 2048*5, NULL, 1, NULL);
xTaskCreate(webserve, "Blink2", 2048, NULL, 1, NULL);
/*
xTaskCreatePinnedToCore(
dht11_reader, // Task function
"Task1", // Task name
10000, // Stack size (bytes)
NULL, // Parameters
1, // Priority
&Task1Handle, // Task handle
1 // Core 1
);
xTaskCreatePinnedToCore(
webserve, // Task function
"Task2", // Task name
10000, // Stack size (bytes)
NULL, // Parameters
2, // Priority
&Task2Handle, // Task handle
1 // Core 0
);
*/
}
void loop() {
// put your main code here, to run repeatedly:
}