change garage door to send MQTT

This commit is contained in:
Joe Tretter
2022-06-25 15:49:47 -05:00
parent d9d92e1310
commit ed2e6ba398
2 changed files with 91 additions and 23 deletions

View File

@@ -12,3 +12,6 @@
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
lib_deps =
knolleary/PubSubClient@^2.8
bblanchon/ArduinoJson@^6.19.4

View File

@@ -6,16 +6,24 @@
#include <ESPmDNS.h>
#include <Update.h>
#include <SPIFFS.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
const char* host = "esp32gragedoor";
const char* ssid = "JoNelsDarlings";
const char* password = "Paris2016";
const char* mqtt_server = "mqtt.ddns.kawomi.com";
const int buttonPin = 4; // Reed switch - don't forget the pull down resistor.
const int ledPin = 2; // Internal LED
boolean bForceResend=false;
int previousState;
WebServer server(80);
WiFiClient espClient;
PubSubClient mqttClient(espClient);
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
/*
* Login page
@@ -127,6 +135,44 @@ void send_notification(String subject){
}
void send_mqtt(String value){
const size_t CAPACITY = JSON_OBJECT_SIZE(1);
StaticJsonDocument<CAPACITY> doc;
// create an object
JsonObject object = doc.to<JsonObject>();
object["status"] = value.c_str();
// serialize the object and send the result to Serial
static char sensorStatus[150];
serializeJson(doc, sensorStatus);
Serial.println(sensorStatus);
mqttClient.publish("Address/Livorno/Garagedoor", sensorStatus);
}
void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (mqttClient.connect(clientId.c_str())) {
Serial.println("MQTT connected");
// Once connected, publish an announcement...
} else {
Serial.print("MQTT failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
/*
* setup function
*/
@@ -141,6 +187,9 @@ void setup(void) {
// if we stat up we need to see what the status is...
digitalWrite(ledPin, previousState= digitalRead(buttonPin));
Serial.printf("Initial State: %d",previousState);
randomSeed(micros());
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.println("");
@@ -171,13 +220,14 @@ void setup(void) {
}
Serial.println("mDNS responder started");
mqttClient.setServer(mqtt_server, 1883);
Serial.println("mqtt server set.");
if(!SPIFFS.begin(true)){
while (1) {
Serial.println("An Error has occurred while mounting SPIFFS");
delay(1000);
}
}
//send_mqtt("rebooted");
send_notification("garagedoor.rebooted.livorno.address");
/*return index page which is stored in serverIndex */
@@ -203,7 +253,15 @@ void setup(void) {
server.sendHeader("Connection", "close");
boolean isOpen=digitalRead(buttonPin)==HIGH;
server.send(200, "application/json", "{ \"isOpen\": " + String(isOpen?"true":"false") + " }");
send_mqtt("statusquery");
});
server.on("/sendmqtt", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", "mqtt resend.");
bForceResend=true;
});
server.on("/serverIndex", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
@@ -236,23 +294,30 @@ void setup(void) {
server.begin();
}
void loop(void) {
server.handleClient();
delay(1000);
if (!mqttClient.connected()) {
reconnect();
send_mqtt("(re)connected");
}
mqttClient.loop();
int buttonState = digitalRead(buttonPin);
if (buttonState!=previousState){
if (buttonState!=previousState || bForceResend ){
bForceResend=false;
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println("Garage Opened");
send_notification("garagedoor.opened.livorno.address");
//send_notification("garagedoor.opened.livorno.address");
send_mqtt("opened");
} else {
digitalWrite(ledPin, LOW);
Serial.println("Garage Closed");
send_notification("garagedoor.closed.livorno.address");
//send_notification("garagedoor.closed.livorno.address");
send_mqtt("cosed");
}
previousState=buttonState;
}