#include #include #include #include #include #include #include #include #include #pragma region BASIC #define SEALEVELPRESSURE_HPA (1013.25) Adafruit_BME280 bme; IPAddress localIp(192, 168, 0, 24); // IP dell' ESP32 const char* description = "ESP32 Temperature server"; // Description #pragma endregion #pragma region GLOBAL VARIABLES hw_timer_t *wdt_temperature_zone = NULL; // Timer for single solenoid zone int mustReset = 0; static long wdt_survey_timeout = 600000; // Time in ms to trigger the watchdog (ten minutes) for solenoid zones static bool wdt_survey_triggered = false; // Trigger for a single solenoid zone timer static const char* ssid = "Melafonino di Claudio"; static const char* password = "1234567890"; static const IPAddress gateway(192, 168, 0, 1); //IP del gateway static const IPAddress subnet(255, 255, 255, 0); // Subnet Mask static const IPAddress primaryDNS(192, 168, 0, 1); // DNS WebServer server(80); WebServer otaServer(8080); String macAddress = ""; HTTPClient http; #pragma endregion #pragma region WIFI void handle_WiFi(){ if ( WiFi.status() != WL_CONNECTED ) { WiFi.begin(ssid, password); int WLcount = 0; while (WiFi.status() != WL_CONNECTED && WLcount < 200 ) { delay(100); ++WLcount; } mustReset++; if(WiFi.status() != WL_CONNECTED && mustReset > 4) { ESP.restart(); } } else { mustReset = 0; server.handleClient(); otaServer.handleClient(); } } #pragma endregion #pragma region WEB SERVER OTA void handleOtaRoot() { otaServer.sendHeader("Connection", "close"); otaServer.send(200, "text/html", "
" "" "" "" "
" "
" "" "" "" "" "
" "
" "" "" "" "
" "
" "" "" "" "" "
" "
ESP32 Login Page
" "
" "
Username:
Password:
" "
" ""); } void handleOtaServerIndex() { otaServer.sendHeader("Connection", "close"); otaServer.send(200, "text/html", "" "
" "" "" "
" "
progress: 0%
" ""); } void handleOtaUpdate() { otaServer.sendHeader("Connection", "close"); otaServer.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); ESP.restart(); } void otaDoUpdate() { HTTPUpload& upload = otaServer.upload(); if (upload.status == UPLOAD_FILE_START) { Serial.printf("Update: %s\n", upload.filename.c_str()); if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size Update.printError(Serial); } } else if (upload.status == UPLOAD_FILE_WRITE) { /* flashing firmware to ESP*/ if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { Update.printError(Serial); } } else if (upload.status == UPLOAD_FILE_END) { if (Update.end(true)) { //true to set the size to the current progress Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); } else { Update.printError(Serial); } } } #pragma endregion void handle_SurveyLoop(){ // Handling temperature zone timer trigger if(wdt_survey_triggered == true) { // execute code when timer trigger // put your code here } } void handle_GetLights() { /*DynamicJsonDocument doc(400); JsonObject light1obj = doc.createNestedObject(); light1obj["LightId"] = light1_Id; light1obj["Enabled"] = light1_enabled; JsonObject light2obj = doc.createNestedObject(); light2obj["LightId"] = light2_Id; light2obj["Enabled"] = light2_enabled; JsonObject light3obj = doc.createNestedObject(); light3obj["LightId"] = light3_Id; light3obj["Enabled"] = light3_enabled; String jsonData = ""; serializeJson(doc, jsonData); server.send(200, "text/plain", jsonData);*/ server.send(200, "text/plain", "Funziona!!!!"); } void setup(void) { Serial.begin(115200); if (!WiFi.config(localIp, gateway, subnet, primaryDNS)) { Serial.println("STA Failed to configure"); } WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); wdt_temperature_zone = timerBegin(0, 8000, true); //timer 0, div 80 //timerAttachInterrupt(wdt_temperature_zone, &onWdTimerElapsed_Zone, true); timerAlarmWrite(wdt_temperature_zone, wdt_survey_timeout * 10, true); timerAlarmEnable(wdt_temperature_zone); Serial.println(""); Serial.print("ESP Board MAC Address: "); macAddress = WiFi.macAddress(); Serial.println(macAddress); Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); //server.on("/", handleRoot); //server.on("/setTemperature", HTTP_POST, handle_SetTemperature); //server.on("/getTemperature", HTTP_GET, handle_GetTemperature); //server.on("/setLights", HTTP_POST, handle_SetLights); server.on("/getLights", HTTP_GET, handle_GetLights); //server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP 80 server started"); otaServer.on("/", HTTP_GET, handleOtaRoot); otaServer.on("/serverIndex", HTTP_GET, handleOtaServerIndex); otaServer.on("/update", HTTP_POST, handleOtaUpdate, otaDoUpdate); otaServer.begin(); Serial.println("HTTP 8080 server started"); Serial.println(localIp); } void loop(void) { handle_WiFi(); handle_SurveyLoop(); } #pragma region Timer Functions void IRAM_ATTR onWdTimerElapsed_Zone() { wdt_survey_triggered = true; } #pragma endregion