add remote messages via deadswitch

This commit is contained in:
Joe Tretter
2022-06-16 20:14:15 -05:00
parent 318df373c1
commit 66864ba2bf

View File

@@ -1,6 +1,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <WiFi.h> #include <WiFi.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WebServer.h> #include <WebServer.h>
#include <ESPmDNS.h> #include <ESPmDNS.h>
#include <Update.h> #include <Update.h>
@@ -113,7 +114,7 @@ void setup(void) {
pinMode(ledPin, OUTPUT); pinMode(ledPin, OUTPUT);
// if we stat up we need to see what the status is... // if we stat up we need to see what the status is...
previousState= digitalRead(buttonPin); digitalWrite(ledPin, previousState= digitalRead(buttonPin));
Serial.printf("Initial State: %d",previousState); Serial.printf("Initial State: %d",previousState);
// Connect to WiFi network // Connect to WiFi network
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
@@ -175,6 +176,31 @@ void setup(void) {
server.begin(); server.begin();
} }
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=joerg.tretter@gmail.com";
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");
}
void loop(void) { void loop(void) {
server.handleClient(); server.handleClient();
delay(1000); delay(1000);
@@ -185,9 +211,11 @@ void loop(void) {
if (buttonState == HIGH) { if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); digitalWrite(ledPin, HIGH);
Serial.println("Garage Opened"); Serial.println("Garage Opened");
send_notification("garagedooropen.livorno.address");
} else { } else {
digitalWrite(ledPin, LOW); digitalWrite(ledPin, LOW);
Serial.println("Garage Closed"); Serial.println("Garage Closed");
send_notification("garagedoorclosed.livorno.address");
} }
previousState=buttonState; previousState=buttonState;
} }