From 5472b4dbcf867f1abc4f944737d0e7b71287b6bb Mon Sep 17 00:00:00 2001 From: Matteo Giaccone Date: Tue, 22 Feb 2022 21:31:07 +0100 Subject: [PATCH] Extract MQTT message processing --- platformio.ini | 2 +- src/config.esp | 7 +------ src/main.cpp | 1 + src/mqtt.esp | 35 +++++++++++++++++++++-------------- src/wifi.esp | 10 +++------- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/platformio.ini b/platformio.ini index b315791f..87ea9fd9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ lib_deps = ESPAsyncUDP ESP Async WebServer ;https://github.com/me-no-dev/ESPAsyncWebServer#e4950444c41f082c1e040aca97034c3f53c8562c - AsyncMqttClient@0.8.2 + AsyncMqttClient@0.9.0 https://github.com/miguelbalboa/rfid#ea7ee3f3daafd46d0c5b8438ba41147c384a1f0d ;https://github.com/monkeyboard/Wiegand-Protocol-Library-for-Arduino.git https://github.com/beikeland/Wiegand-Protocol-Library-for-Arduino.git diff --git a/src/config.esp b/src/config.esp index 7ed71fd0..98d9a74e 100644 --- a/src/config.esp +++ b/src/config.esp @@ -234,13 +234,8 @@ bool ICACHE_FLASH_ATTR loadConfiguration() #ifdef DEBUG Serial.println("[ INFO ] Trying to setup NTP Server"); #endif - /* - IPAddress timeserverip; - WiFi.hostByName(ntpserver, timeserverip); - String ip = printIP(timeserverip); - writeEvent("INFO", "ntp", "Connecting NTP Server", ip); - */ NTP.Ntp(ntpserver, timeZone, ntpinter * 60); + if (mqtt["enabled"] == 1) mqttEnabled = true; else diff --git a/src/main.cpp b/src/main.cpp index d2cd6aab..4de3cdff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -477,5 +477,6 @@ void ICACHE_RAM_ATTR loop() #endif } } + processMqttMessage(); } } diff --git a/src/mqtt.esp b/src/mqtt.esp index 36243948..4bfb5577 100644 --- a/src/mqtt.esp +++ b/src/mqtt.esp @@ -1,4 +1,6 @@ char mqttBuffer[512]; +StaticJsonDocument<512> incomingMqttMessage; +bool mqttMessageToProcess = false; void connectToMqtt() { @@ -388,8 +390,7 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties Serial.println(mqttBuffer); #endif - StaticJsonDocument<512> root; - auto error = deserializeJson(root, mqttBuffer); + auto error = deserializeJson(incomingMqttMessage, mqttBuffer); if (error) { #ifdef DEBUG @@ -403,9 +404,9 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties { // Check if IP was send with command because we only // accept commands for this where sent IP is equal to device IP - if (root.containsKey("doorip")) + if (incomingMqttMessage.containsKey("doorip")) { - const char *ipadr = root["doorip"]; + const char *ipadr = incomingMqttMessage["doorip"]; String espIp = WiFi.localIP().toString(); if (!((strcmp(ipadr, espIp.c_str()) == 0) && (ipadr != NULL))) { @@ -424,15 +425,21 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties } } - //////////////////////////////////////////////////////////// - // CASE FOR MQTT - //////////////////////////////////////////////////////////// - const char *command = root["cmd"]; - // Check whatever the command is and act accordingly + mqttMessageToProcess = true; + return; +} + +void processMqttMessage() { + if(!mqttMessageToProcess) + { + return; + } + mqttMessageToProcess = false; + const char *command = incomingMqttMessage["cmd"]; if (strcmp(command, "getuser") == 0) { #ifdef DEBUG - Serial.println("[ MARE ] Get User List"); + Serial.println("[ INFO ] Get User List"); #endif getUserList(0); return; @@ -473,7 +480,7 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties #ifdef DEBUG Serial.println("[ INFO ] Delete a single user by uid"); #endif - const char *uid = root["uid"]; + const char *uid = incomingMqttMessage["uid"]; DeleteUserID(uid); return; } @@ -483,18 +490,18 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties #ifdef DEBUG Serial.print("[ INFO ] Add Users: "); - const char *name = root["user"]; + const char *name = incomingMqttMessage["user"]; Serial.println(name); #endif - const char *uid = root["uid"]; + const char *uid = incomingMqttMessage["uid"]; String filename = "/P/"; filename += uid; File f = SPIFFS.open(filename, "w+"); // Check if we created the file if (f) { - serializeJson(root, f); + serializeJson(incomingMqttMessage, f); } f.close(); return; diff --git a/src/wifi.esp b/src/wifi.esp index b8177cad..ddb6a402 100644 --- a/src/wifi.esp +++ b/src/wifi.esp @@ -39,8 +39,9 @@ void onWifiGotIP(const WiFiEventStationModeGotIP &event) #ifdef DEBUG Serial.println(F("\n[ INFO ] WiFi IP Connected")); #endif - connectToMqtt(); - //writeEvent("INFO", "wifi", "Connected with IP", "onWifiGotIP"); + if (mqttEnabled) { + connectToMqtt(); + } } bool ICACHE_FLASH_ATTR startAP(IPAddress apip, IPAddress apsubnet, int hid, const char *ssid, const char *password = NULL) @@ -165,11 +166,6 @@ bool ICACHE_FLASH_ATTR connectSTA(const char *ssid, const char *password, byte b if (WiFi.status() == WL_CONNECTED) { // Assume time is out first and check -#ifdef DEBUG - //Serial.println(); - Serial.print(F("[ INFO ] Client IP address: ")); - Serial.println(WiFi.localIP()); -#endif isWifiConnected = true; String data = ssid; data += " " + WiFi.localIP().toString();