Skip to content

Commit

Permalink
MQTT merged complement
Browse files Browse the repository at this point in the history
  • Loading branch information
couin3 committed Apr 27, 2020
1 parent 3514704 commit ef58202
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
25 changes: 9 additions & 16 deletions RFLink/6_WiFi_MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@
WiFiClient WIFIClient;
PubSubClient MQTTClient; // MQTTClient(WIFIClient);

void callback(char *, byte *, unsigned int);

#ifndef AUTOCONNECT_ENABLED
static String WIFI_PWR = String(WIFI_PWR_0);
#endif

void callback(char *, byte *, unsigned int);

void setup_WIFI()
{
Expand Down Expand Up @@ -109,18 +108,18 @@ void reconnect()
// Once connected, resubscribe
MQTTClient.subscribe(MQTT_TOPIC_IN.c_str());
Serial.print(F("MQTT connection Established : "));
Serial.println(clientId.c_str());
Serial.println(MQTT_ID.c_str());
MQTTClient.subscribe(MQTT_TOPIC_IN.c_str());
}
else
{
Serial.print(F("Connection mqttserver : "));
Serial.print(F("Connection MQTT_Server : "));
Serial.println(MQTT_SERVER.c_str());
Serial.print(F("Connection Mqtt_ID : "));
Serial.print(F("Connection MQTT_ID : "));
Serial.println(MQTT_ID.c_str());
Serial.print(F("Connection Mqtt_Username : "));
Serial.print(F("Connection MQTT_Username : "));
Serial.println(MQTT_USER.c_str());
Serial.print(F("Connection Mqtt_Password : ********"));
Serial.print(F("Connection MQTT_Password : ********"));
Serial.print(F("Connection failed : "));
Serial.println(MQTTClient.state());
if (!--retry)
Expand All @@ -135,18 +134,15 @@ void reconnect()
{
// Loop until we're reconnected
// delay(1);
uint8_t retry = 3;

Serial.print(F("test"));
while (!MQTTClient.connected())
{
Serial.print(F("Attempting MQTT connection..."));
// Attempt to connect
if (MQTTClient.connect(ac_MQTT_ID.c_str(), ac_MQTT_USER.c_str(), ac_MQTT_PSWD.c_str()))
if (MQTTClient.connect(MQTT_ID.c_str(), MQTT_USER.c_str(), MQTT_PSWD.c_str()))
{
Serial.println(F("Connected"));
// Once connected, resubscribe
// MQTTClient.subscribe(ac_MQTT_TOPIC_IN.c_str());
MQTTClient.subscribe(MQTT_TOPIC_IN.c_str());
}
else
{
Expand All @@ -156,9 +152,6 @@ void reconnect()
// Wait 5 seconds before retrying
for (byte i = 0; i < 10; i++)
delay(500); // delay(5000) may cause hang
if (!--retry)
break;
delay(500);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions RFLink/9_AutoConnect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ String saveParams(AutoConnectAux &aux, PageArgument &args)
echo.value += Adv_Power;
echo.value += F("<br>");

#ifdef MQTT_ENABLED
setup_MQTT(); // Reload settings
#endif

return String("");
}
Expand Down
6 changes: 3 additions & 3 deletions RFLink/RFLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@
// MQTT messages
#define SERIAL_ENABLED // Send RFLink messages over Serial
#if (defined(ESP32) || defined(ESP8266))
#define MQTT_ENABLED // Send RFLink messages over MQTT
#define MQTT_LOOP_MS 1000 // MQTTClient.loop(); call period (in mSec)
#define MQTT_RETAINED false // Retained option
#define MQTT_ENABLED // Send RFLink messages over MQTT
#define MQTT_LOOP_MS 1000 // MQTTClient.loop(); call period (in mSec)
#define MQTT_RETAINED_0 false // Retained option
#endif

// Debug default
Expand Down
22 changes: 13 additions & 9 deletions RFLink/RFLink.ino
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,23 @@ void loop()
{
#ifdef AUTOCONNECT_ENABLED
loop_AutoConnect();
#endif
#ifdef MQTT_ENABLED
if (WiFi.status() == WL_CONNECTED)
{
checkMQTTloop();
sendMsg();
#endif

if (CheckSerial())
#ifdef MQTT_ENABLED
checkMQTTloop();
sendMsg();
#endif

if (ScanEvent())
sendMsg();
if (CheckSerial())
sendMsg();

if (ScanEvent())
sendMsg();

#ifdef AUTOCONNECT_ENABLED
}
#endif
}

void sendMsg()
Expand All @@ -135,7 +139,7 @@ void sendMsg()
publishMsg();
#endif
#ifdef AUTOCONNECT_ENABLED
LastMsg = pbuffer;
LastMsg = pbuffer;
#endif
#ifdef OLED_ENABLED
print_OLED();
Expand Down

0 comments on commit ef58202

Please sign in to comment.