Skip to content

Commit

Permalink
Merge pull request #119 from EdJoPaTo/rename-variables
Browse files Browse the repository at this point in the history
Improve variable names
  • Loading branch information
Patrick Lapointe authored Jan 15, 2023
2 parents 9337b22 + 631e5cb commit 080c73b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 41 deletions.
76 changes: 37 additions & 39 deletions src/EspMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ EspMQTTClient::EspMQTTClient(
_wifiConnected = false;
_connectingToWifi = false;
_nextWifiConnectionAttemptMillis = 500;
_lastWifiConnectiomAttemptMillis = 0;
_lastWifiConnectionAttemptMillis = 0;
_wifiReconnectionAttemptDelay = 60 * 1000;

// MQTT client
Expand All @@ -84,7 +84,7 @@ EspMQTTClient::EspMQTTClient(
_enableOTA = false;

// other
_enableSerialLogs = false;
_enableDebugMessages = false;
_drasticResetOnConnectionFailures = false;
_connectionEstablishedCallback = onConnectionEstablished;
_connectionEstablishedCount = 0;
Expand All @@ -103,20 +103,20 @@ EspMQTTClient::~EspMQTTClient()

void EspMQTTClient::enableDebuggingMessages(const bool enabled)
{
_enableSerialLogs = enabled;
_enableDebugMessages = enabled;
}

void EspMQTTClient::enableHTTPWebUpdater(const char* username, const char* password, const char* address)
{
if (_httpServer == NULL)
{
_httpServer = new WebServer(80);
_httpUpdater = new ESPHTTPUpdateServer(_enableSerialLogs);
_httpUpdater = new ESPHTTPUpdateServer(_enableDebugMessages);
_updateServerUsername = (char*)username;
_updateServerPassword = (char*)password;
_updateServerAddress = (char*)address;
}
else if (_enableSerialLogs)
else if (_enableDebugMessages)
Serial.print("SYS! You can't call enableHTTPWebUpdater() more than once !\n");
}

Expand Down Expand Up @@ -161,7 +161,6 @@ void EspMQTTClient::enableLastWillMessage(const char* topic, const char* message

void EspMQTTClient::loop()
{
// WIFI handling
bool wifiStateChanged = handleWiFi();

// If there is a change in the wifi connection state, don't handle the mqtt connection state right away.
Expand All @@ -174,8 +173,7 @@ void EspMQTTClient::loop()
if(mqttStateChanged)
return;

// Procewss the delayed execution commands
processDelayedExecutionRequests();
processDelayedExecutionRequests();
}

bool EspMQTTClient::handleWiFi()
Expand Down Expand Up @@ -203,7 +201,7 @@ bool EspMQTTClient::handleWiFi()
_connectingToWifi = false;

// At least 500 miliseconds of waiting before an mqtt connection attempt.
// Some people have reported instabilities when trying to connect to
// Some people have reported instabilities when trying to connect to
// the mqtt broker right after being connected to wifi.
// This delay prevent these instabilities.
_nextMqttConnectionAttemptMillis = millis() + 500;
Expand All @@ -212,11 +210,11 @@ bool EspMQTTClient::handleWiFi()
// Connection in progress
else if(_connectingToWifi)
{
if(WiFi.status() == WL_CONNECT_FAILED || millis() - _lastWifiConnectiomAttemptMillis >= _wifiReconnectionAttemptDelay)
if(WiFi.status() == WL_CONNECT_FAILED || millis() - _lastWifiConnectionAttemptMillis >= _wifiReconnectionAttemptDelay)
{
if(_enableSerialLogs)
if(_enableDebugMessages)
Serial.printf("WiFi! Connection attempt failed, delay expired. (%fs). \n", millis()/1000.0);

WiFi.disconnect(true);
MDNS.end();

Expand Down Expand Up @@ -257,7 +255,7 @@ bool EspMQTTClient::handleWiFi()
connectToWifi();
_nextWifiConnectionAttemptMillis = 0;
_connectingToWifi = true;
_lastWifiConnectiomAttemptMillis = millis();
_lastWifiConnectionAttemptMillis = millis();
}

/**** Detect and return if there was a change in the WiFi state ****/
Expand All @@ -279,7 +277,7 @@ bool EspMQTTClient::handleMQTT()

// Get the current connextion status
bool isMqttConnected = (isWifiConnected() && _mqttClient.connected());


/***** Detect and handle the current MQTT handling state *****/

Expand All @@ -297,7 +295,7 @@ bool EspMQTTClient::handleMQTT()
_nextMqttConnectionAttemptMillis = millis() + _mqttReconnectionAttemptDelay;
}

// It's time to connect to the MQTT broker
// It's time to connect to the MQTT broker
else if (isWifiConnected() && _nextMqttConnectionAttemptMillis > 0 && millis() >= _nextMqttConnectionAttemptMillis)
{
// Connect to MQTT broker
Expand All @@ -313,13 +311,13 @@ bool EspMQTTClient::handleMQTT()
_mqttClient.disconnect();
_failedMQTTConnectionAttemptCount++;

if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.printf("MQTT!: Failed MQTT connection count: %i \n", _failedMQTTConnectionAttemptCount);

// When there is too many failed attempt, sometimes it help to reset the WiFi connection or to restart the board.
if(_handleWiFi && _failedMQTTConnectionAttemptCount == 8)
{
if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.println("MQTT!: Can't connect to broker after too many attempt, resetting WiFi ...");

WiFi.disconnect(true);
Expand All @@ -331,7 +329,7 @@ bool EspMQTTClient::handleMQTT()
}
else if(_drasticResetOnConnectionFailures && _failedMQTTConnectionAttemptCount == 12) // Will reset after 12 failed attempt (3 minutes of retry)
{
if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.println("MQTT!: Can't connect to broker after too many attempt, resetting board ...");

#ifdef ESP8266
Expand All @@ -358,7 +356,7 @@ bool EspMQTTClient::handleMQTT()

void EspMQTTClient::onWiFiConnectionEstablished()
{
if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.printf("WiFi: Connected (%fs), ip : %s \n", millis()/1000.0, WiFi.localIP().toString().c_str());

// Config of web updater
Expand All @@ -369,7 +367,7 @@ void EspMQTTClient::onWiFiConnectionEstablished()
_httpServer->begin();
MDNS.addService("http", "tcp", 80);

if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.printf("WEB: Updater ready, open http://%s.local in your browser and login with username '%s' and password '%s'.\n", _mqttClientName, _updateServerUsername, _updateServerPassword);
}

Expand All @@ -379,7 +377,7 @@ void EspMQTTClient::onWiFiConnectionEstablished()

void EspMQTTClient::onWiFiConnectionLost()
{
if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.printf("WiFi! Lost connection (%fs). \n", millis()/1000.0);

// If we handle wifi, we force disconnection to clear the last connection
Expand All @@ -398,7 +396,7 @@ void EspMQTTClient::onMQTTConnectionEstablished()

void EspMQTTClient::onMQTTConnectionLost()
{
if (_enableSerialLogs)
if (_enableDebugMessages)
{
Serial.printf("MQTT! Lost connection (%fs). \n", millis()/1000.0);
Serial.printf("MQTT: Retrying to connect in %i seconds. \n", _mqttReconnectionAttemptDelay / 1000);
Expand All @@ -414,7 +412,7 @@ bool EspMQTTClient::setMaxPacketSize(const uint16_t size)

bool success = _mqttClient.setBufferSize(size);

if(!success && _enableSerialLogs)
if(!success && _enableDebugMessages)
Serial.println("MQTT! failed to set the max packet size.");

return success;
Expand All @@ -425,15 +423,15 @@ bool EspMQTTClient::publish(const char* topic, const uint8_t* payload, unsigned
// Do not try to publish if MQTT is not connected.
if(!isConnected())
{
if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.println("MQTT! Trying to publish when disconnected, skipping.");

return false;
}

bool success = _mqttClient.publish(topic, payload, plength, retain);

if (_enableSerialLogs)
if (_enableDebugMessages)
{
if(success)
Serial.printf("MQTT << [%s] %s\n", topic, payload);
Expand All @@ -455,7 +453,7 @@ bool EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messa
// Do not try to subscribe if MQTT is not connected.
if(!isConnected())
{
if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.println("MQTT! Trying to subscribe when disconnected, skipping.");

return false;
Expand All @@ -473,8 +471,8 @@ bool EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messa
if(!found)
_topicSubscriptionList.push_back({ topic, messageReceivedCallback, NULL });
}
if (_enableSerialLogs)

if (_enableDebugMessages)
{
if(success)
Serial.printf("MQTT: Subscribed to [%s]\n", topic.c_str());
Expand All @@ -500,7 +498,7 @@ bool EspMQTTClient::unsubscribe(const String &topic)
// Do not try to unsubscribe if MQTT is not connected.
if(!isConnected())
{
if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.println("MQTT! Trying to unsubscribe when disconnected, skipping.");

return false;
Expand All @@ -515,12 +513,12 @@ bool EspMQTTClient::unsubscribe(const String &topic)
_topicSubscriptionList.erase(_topicSubscriptionList.begin() + i);
i--;

if(_enableSerialLogs)
if(_enableDebugMessages)
Serial.printf("MQTT: Unsubscribed from %s\n", topic.c_str());
}
else
{
if(_enableSerialLogs)
if(_enableDebugMessages)
Serial.println("MQTT! unsubscribe failed");

return false;
Expand Down Expand Up @@ -566,7 +564,7 @@ void EspMQTTClient::connectToWifi()
#endif
WiFi.begin(_wifiSsid, _wifiPassword);

if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.printf("\nWiFi: Connecting to %s ... (%fs) \n", _wifiSsid, millis()/1000.0);
}

Expand All @@ -577,7 +575,7 @@ bool EspMQTTClient::connectToMqttBroker()

if (_mqttServerIp != nullptr && strlen(_mqttServerIp) > 0)
{
if (_enableSerialLogs)
if (_enableDebugMessages)
{
if (_mqttUsername)
Serial.printf("MQTT: Connecting to broker \"%s\" with client name \"%s\" and username \"%s\" ... (%fs)", _mqttServerIp, _mqttClientName, _mqttUsername, millis()/1000.0);
Expand All @@ -591,14 +589,14 @@ bool EspMQTTClient::connectToMqttBroker()
}
else
{
if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.printf("MQTT: Broker server ip is not set, not connecting (%fs)\n", millis()/1000.0);
success = false;
}

if (_enableSerialLogs)
if (_enableDebugMessages)
{
if (success)
if (success)
Serial.printf(" - ok. (%fs) \n", millis()/1000.0);
else
{
Expand Down Expand Up @@ -642,7 +640,7 @@ bool EspMQTTClient::connectToMqttBroker()
return success;
}

// Delayed execution handling.
// Delayed execution handling.
// Check if there is delayed execution requests to process and execute them if needed.
void EspMQTTClient::processDelayedExecutionRequests()
{
Expand Down Expand Up @@ -728,7 +726,7 @@ void EspMQTTClient::mqttMessageReceivedCallback(char* topic, uint8_t* payload, u
{
strTerminationPos = length - 1;

if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.print("MQTT! Your message may be truncated, please set setMaxPacketSize() to a higher value.\n");
}
else
Expand All @@ -740,7 +738,7 @@ void EspMQTTClient::mqttMessageReceivedCallback(char* topic, uint8_t* payload, u
String topicStr(topic);

// Logging
if (_enableSerialLogs)
if (_enableDebugMessages)
Serial.printf("MQTT >> [%s] %s\n", topic, payloadStr.c_str());

// Send the message to subscribers
Expand Down
4 changes: 2 additions & 2 deletions src/EspMQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class EspMQTTClient
bool _handleWiFi;
bool _wifiConnected;
bool _connectingToWifi;
unsigned long _lastWifiConnectiomAttemptMillis;
unsigned long _lastWifiConnectionAttemptMillis;
unsigned long _nextWifiConnectionAttemptMillis;
unsigned int _wifiReconnectionAttemptDelay;
const char* _wifiSsid;
Expand Down Expand Up @@ -91,7 +91,7 @@ class EspMQTTClient

// General behaviour related
ConnectionEstablishedCallback _connectionEstablishedCallback;
bool _enableSerialLogs;
bool _enableDebugMessages;
bool _drasticResetOnConnectionFailures;
unsigned int _connectionEstablishedCount; // Incremented before each _connectionEstablishedCallback call

Expand Down

0 comments on commit 080c73b

Please sign in to comment.