Files
PlatformIO_GarageDoorMonitor/src/main.cpp

233 lines
6.2 KiB
C++
Raw Normal View History

2022-06-16 17:18:09 -05:00
#include <Arduino.h>
2022-06-16 17:41:25 -05:00
#include <WiFi.h>
#include <WiFiClient.h>
2022-06-16 20:14:15 -05:00
#include <WiFiClientSecure.h>
2022-06-16 17:41:25 -05:00
#include <WebServer.h>
#include <ESPmDNS.h>
#include <Update.h>
const char* host = "esp32gragedoor";
const char* ssid = "JoNelsDarlings";
const char* password = "Paris2016";
2022-06-16 17:18:09 -05:00
const int buttonPin = 4; // Reed switch - don't forget the pull down resistor.
const int ledPin = 2; // Internal LED
2022-06-16 17:41:25 -05:00
int previousState;
WebServer server(80);
/*
* Login page
*/
const char* loginIndex =
"<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=='admin' && form.pwd.value=='admin')"
"{"
"window.open('/serverIndex')"
"}"
"else"
"{"
" alert('Error Password or Username')/*displays error message*/"
"}"
"}"
"</script>";
/*
* Server Index Page
*/
const char* serverIndex =
"<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>";
2022-06-17 16:43:55 -05:00
void send_notification(String subject){
//return;
Serial.println("Sending Notification.");
WiFiClientSecure client;
client.setInsecure(); // NOT RECOMMENDED
const char* host="deadswitch.kawomi.com";
const int port=443;
if (!client.connect(host,port)) {
Serial.println("connection failed");
return;
}
String url = "/api/v1?topic=" + subject + "&secs=0&email=none";
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 14\r\n\r\n" +
"platform=esp32\r\n");
}
2022-06-16 17:41:25 -05:00
/*
* setup function
*/
void setup(void) {
2022-06-16 17:18:09 -05:00
Serial.begin(9600);
2022-06-16 17:41:25 -05:00
delay(500);
Serial.println(">>>>>>>>>> Garage Door Monitor <<<<<<<<<<<<<<");
2022-06-16 17:18:09 -05:00
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
2022-06-16 17:41:25 -05:00
// if we stat up we need to see what the status is...
2022-06-16 20:14:15 -05:00
digitalWrite(ledPin, previousState= digitalRead(buttonPin));
2022-06-16 17:41:25 -05:00
Serial.printf("Initial State: %d",previousState);
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.println("");
2022-06-17 23:11:49 -05:00
// Wait for connection or reboot if it doesn't connect for too long
uint32_t wifiConnAttements = 0;
2022-06-16 17:41:25 -05:00
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
2022-06-17 23:11:49 -05:00
if (wifiConnAttements++ > 120) {
Serial.println("<Wifi not connecting after 120 attmepts (1 min.) - rebooting>");
ESP.restart();
}
2022-06-16 17:41:25 -05:00
}
2022-06-17 23:11:49 -05:00
2022-06-16 17:41:25 -05:00
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
/*use mdns for host name resolution*/
if (!MDNS.begin(host)) { //http://esp32ota.local
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
2022-06-17 16:43:55 -05:00
send_notification("garagedoor.rebooted.livorno.address");
2022-06-16 17:41:25 -05:00
/*return index page which is stored in serverIndex */
server.on("/", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", loginIndex);
});
server.on("/serverIndex", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
});
/*handling uploading firmware file */
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, []() {
HTTPUpload& upload = server.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);
}
}
});
server.begin();
2022-06-16 17:18:09 -05:00
}
2022-06-16 20:14:15 -05:00
2022-06-16 17:41:25 -05:00
void loop(void) {
server.handleClient();
delay(1000);
2022-06-16 17:18:09 -05:00
2022-06-16 17:41:25 -05:00
int buttonState = digitalRead(buttonPin);
if (buttonState!=previousState){
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println("Garage Opened");
2022-06-17 16:43:55 -05:00
send_notification("garagedoor.open.livorno.address");
2022-06-16 17:41:25 -05:00
} else {
digitalWrite(ledPin, LOW);
Serial.println("Garage Closed");
2022-06-17 16:43:55 -05:00
send_notification("garagedoor.closed.livorno.address");
2022-06-16 17:41:25 -05:00
}
previousState=buttonState;
2022-06-16 17:18:09 -05:00
}
2022-06-16 17:41:25 -05:00
}