Skip to content

Commit

Permalink
Merge pull request #6 from FablabTorino/fix-mqtt
Browse files Browse the repository at this point in the history
Extract MQTT message processing
  • Loading branch information
matjack1 committed Feb 22, 2022
2 parents 5b31dae + 5472b4d commit 95fa9ba
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions src/config.esp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,6 @@ void ICACHE_RAM_ATTR loop()
#endif
}
}
processMqttMessage();
}
}
35 changes: 21 additions & 14 deletions src/mqtt.esp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
char mqttBuffer[512];
StaticJsonDocument<512> incomingMqttMessage;
bool mqttMessageToProcess = false;

void connectToMqtt()
{
Expand Down Expand Up @@ -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
Expand All @@ -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)))
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
10 changes: 3 additions & 7 deletions src/wifi.esp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 95fa9ba

Please sign in to comment.