just some reformatting - no code changes

This commit is contained in:
Joe Tretter
2022-07-05 14:18:19 -05:00
parent c372dcd95d
commit 22783a6e58

View File

@@ -1,13 +1,13 @@
#include <Arduino.h> #include <Arduino.h>
#include <ArduinoJson.h>
#include <ESPmDNS.h>
#include <PubSubClient.h>
#include <SPIFFS.h>
#include <Update.h>
#include <WebServer.h>
#include <WiFi.h> #include <WiFi.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
#include <SPIFFS.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
const char* host = "esp32gragedoor"; const char* host = "esp32gragedoor";
const char* ssid = "JoNelsDarlings"; const char* ssid = "JoNelsDarlings";
@@ -75,8 +75,11 @@ const char* loginIndex =
*/ */
const char* serverIndex = const char* serverIndex =
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>" "<script "
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>" "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='file' name='update'>"
"<input type='submit' value='Update'>" "<input type='submit' value='Update'>"
"</form>" "</form>"
@@ -111,7 +114,6 @@ const char* serverIndex =
"});" "});"
"</script>"; "</script>";
void send_notification(String subject) { void send_notification(String subject) {
// return; // return;
Serial.println("Sending Notification."); Serial.println("Sending Notification.");
@@ -127,12 +129,9 @@ void send_notification(String subject){
String url = "/api/v1?topic=" + subject + "&secs=0&email=none"; String url = "/api/v1?topic=" + subject + "&secs=0&email=none";
Serial.print("Requesting URL: "); Serial.print("Requesting URL: ");
Serial.println(url); Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" + client.print(String("POST ") + url + " HTTP/1.1\r\n" + "Host: " + host +
"Host: " + host + "\r\n" + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: 14\r\n\r\n" + "platform=esp32\r\n");
"Content-Length: 14\r\n\r\n" +
"platform=esp32\r\n");
} }
void send_mqtt(String value) { void send_mqtt(String value) {
@@ -172,7 +171,6 @@ void reconnect() {
} }
} }
/* /*
* setup function * setup function
*/ */
@@ -200,7 +198,8 @@ void setup(void) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
if (wifiConnAttements++ > 120) { if (wifiConnAttements++ > 120) {
Serial.println("<Wifi not connecting after 120 attmepts (1 min.) - rebooting>"); Serial.println(
"<Wifi not connecting after 120 attmepts (1 min.) - rebooting>");
ESP.restart(); ESP.restart();
} }
} }
@@ -252,7 +251,8 @@ void setup(void) {
server.on("/status", HTTP_GET, []() { server.on("/status", HTTP_GET, []() {
server.sendHeader("Connection", "close"); server.sendHeader("Connection", "close");
boolean isOpen = digitalRead(buttonPin) == HIGH; boolean isOpen = digitalRead(buttonPin) == HIGH;
server.send(200, "application/json", "{ \"isOpen\": " + String(isOpen?"true":"false") + " }"); server.send(200, "application/json",
"{ \"isOpen\": " + String(isOpen ? "true" : "false") + " }");
// send_mqtt("statusquery"); // send_mqtt("statusquery");
}); });
@@ -267,25 +267,32 @@ void setup(void) {
server.send(200, "text/html", serverIndex); server.send(200, "text/html", serverIndex);
}); });
/*handling uploading firmware file */ /*handling uploading firmware file */
server.on("/update", HTTP_POST, []() { server.on(
"/update", HTTP_POST,
[]() {
server.sendHeader("Connection", "close"); server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart(); ESP.restart();
}, []() { },
[]() {
HTTPUpload& upload = server.upload(); HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) { if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n", upload.filename.c_str()); Serial.printf("Update: %s\n", upload.filename.c_str());
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size if (!Update.begin(
UPDATE_SIZE_UNKNOWN)) { // start with max available size
Update.printError(Serial); Update.printError(Serial);
} }
} else if (upload.status == UPLOAD_FILE_WRITE) { } else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware to ESP*/ /* flashing firmware to ESP*/
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { if (Update.write(upload.buf, upload.currentSize) !=
upload.currentSize) {
Update.printError(Serial); Update.printError(Serial);
} }
} else if (upload.status == UPLOAD_FILE_END) { } else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress if (Update.end(
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); true)) { // true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n",
upload.totalSize);
} else { } else {
Update.printError(Serial); Update.printError(Serial);
} }