diff --git a/platformio.ini b/platformio.ini index c4b493e..8d6d90d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,14 +1,17 @@ -; PlatformIO Project Configuration File -; -; Build options: build flags, source filter -; Upload options: custom upload port, speed and extra flags -; Library options: dependencies, extra library storages -; Advanced options: extra scripting -; -; Please visit documentation for the other options and examples -; https://docs.platformio.org/page/projectconf.html - -[env:esp32doit-devkit-v1] -platform = espressif32 -board = esp32doit-devkit-v1 -framework = arduino +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32doit-devkit-v1] +platform = espressif32 +board = esp32doit-devkit-v1 +framework = arduino +lib_deps = + knolleary/PubSubClient@^2.8 + bblanchon/ArduinoJson@^6.19.4 diff --git a/src/main.cpp b/src/main.cpp index 9409b06..c25393a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,16 +6,24 @@ #include #include #include +#include +#include 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 doc; + + // create an object + JsonObject object = doc.to(); + 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); - } + Serial.println("An Error has occurred while mounting SPIFFS"); } + //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; }