368d6fafea
Code backup
282 lines
8.0 KiB
C++
282 lines
8.0 KiB
C++
#include <Arduino.h>
|
|
#include <WiFi.h>
|
|
#include <WiFiClient.h>
|
|
#include <WebServer.h>
|
|
#include <HTTPClient.h>
|
|
#include <Update.h>
|
|
#include <ArduinoJson.h>
|
|
#include <Adafruit_Sensor.h>
|
|
#include <Adafruit_BME280.h>
|
|
|
|
#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", "<form name='loginForm'>"
|
|
"<table width='20%' bgcolor='A09F9F' align='center'>"
|
|
"<tr>"
|
|
"<td colspan=2>"
|
|
"<center><font size=4><b>ESP32 Login Page</b></font></center>"
|
|
"<br>"
|
|
"</td>"
|
|
"<br>"
|
|
"<br>"
|
|
"</tr>"
|
|
"<td>Username:</td>"
|
|
"<td><input type='text' size=25 name='userid'><br></td>"
|
|
"</tr>"
|
|
"<br>"
|
|
"<br>"
|
|
"<tr>"
|
|
"<td>Password:</td>"
|
|
"<td><input type='Password' size=25 name='pwd'><br></td>"
|
|
"<br>"
|
|
"<br>"
|
|
"</tr>"
|
|
"<tr>"
|
|
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
|
|
"</tr>"
|
|
"</table>"
|
|
"</form>"
|
|
"<script>"
|
|
"function check(form)"
|
|
"{"
|
|
"if(form.userid.value=='Fin3' && form.pwd.value==''passwordDaImpostarePerSicurezza)"
|
|
"{"
|
|
"window.open('/serverIndex')"
|
|
"}"
|
|
"else"
|
|
"{"
|
|
" alert('Error Password or Username')/*displays error message*/"
|
|
"}"
|
|
"}"
|
|
"</script>");
|
|
}
|
|
|
|
void handleOtaServerIndex() {
|
|
otaServer.sendHeader("Connection", "close");
|
|
otaServer.send(200, "text/html", "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
|
|
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
|
|
"<input type='file' name='update'>"
|
|
"<input type='submit' value='Update'>"
|
|
"</form>"
|
|
"<div id='prg'>progress: 0%</div>"
|
|
"<script>"
|
|
"$('form').submit(function(e){"
|
|
"e.preventDefault();"
|
|
"var form = $('#upload_form')[0];"
|
|
"var data = new FormData(form);"
|
|
" $.ajax({"
|
|
"url: '/update',"
|
|
"type: 'POST',"
|
|
"data: data,"
|
|
"contentType: false,"
|
|
"processData:false,"
|
|
"xhr: function() {"
|
|
"var xhr = new window.XMLHttpRequest();"
|
|
"xhr.upload.addEventListener('progress', function(evt) {"
|
|
"if (evt.lengthComputable) {"
|
|
"var per = evt.loaded / evt.total;"
|
|
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
|
|
"}"
|
|
"}, false);"
|
|
"return xhr;"
|
|
"},"
|
|
"success:function(d, s) {"
|
|
"console.log('success!')"
|
|
"},"
|
|
"error: function (a, b, c) {"
|
|
"}"
|
|
"});"
|
|
"});"
|
|
"</script>");
|
|
}
|
|
|
|
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
|
|
|