Skip to content

Commit

Permalink
IotWebConf update and MQTT lib replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Rüttgers committed Oct 5, 2022
1 parent 2d72520 commit d888efc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 71 deletions.
6 changes: 3 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ lib_deps =
EspSoftwareSerial
MicroDebug
IotWebConf@3.2.0
ESPAsyncTCP
git+https://github.com/philbowles/PangolinMQTT.git#v1.0.0
ESP AsyncTCP
AsyncMqttClient
jled

env_default = d1_mini
Expand Down Expand Up @@ -47,7 +47,7 @@ board = d1_mini
framework = arduino
lib_deps = ${common.lib_deps}
lib_ldf_mode = ${common.lib_ldf_mode}
build_flags = ${common.build_flags} -DSERIAL_DEBUG=true -DSERIAL_DEBUG_VERBOSE=true
build_flags = ${common.build_flags} -DSERIAL_DEBUG=true -DSERIAL_DEBUG_VERBOSE=false
upload_port = /dev/ttyUSB0
monitor_port = /dev/ttyUSB0
monitor_speed = 115200
89 changes: 26 additions & 63 deletions src/MqttPublisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include "config.h"
#include "debug.h"
#include <Ticker.h>
#include <PangolinMQTT.h>

#include <AsyncMqttClient.h>
#include <string.h>
#include <sml/sml_file.h>
#include <Ticker.h>

#define MQTT_RECONNECT_DELAY 5
#define MQTT_LWT_TOPIC "LWT"
Expand Down Expand Up @@ -35,18 +35,25 @@ class MqttPublisher
config = _config;
uint8_t lastCharOfTopic = strlen(config.topic) - 1;
baseTopic = String(config.topic) + (lastCharOfTopic >= 0 && config.topic[lastCharOfTopic] == '/' ? "" : "/");

lastWillTopic = String(baseTopic + MQTT_LWT_TOPIC);

DEBUG(F("MQTT: Setting up..."));
DEBUG(F("MQTT: Server: %s"),config.server);
DEBUG(F("MQTT: Port: %d"),atoi(config.port));
DEBUG(F("MQTT: Username: %s"),config.username);
DEBUG(F("MQTT: Password: <hidden>"));
DEBUG(F("MQTT: Topic: %s"), baseTopic.c_str());

client.setServer(const_cast<const char *>(config.server), atoi(config.port));
if (strlen(config.username) > 0 || strlen(config.password) > 0)
{
client.setCredentials(config.username, config.password);
}

client.setCleanSession(true);
client.setWill(String(baseTopic + MQTT_LWT_TOPIC).c_str(), MQTT_LWT_QOS, MQTT_LWT_RETAIN, MQTT_LWT_PAYLOAD_OFFLINE);
client.setWill(lastWillTopic.c_str(), MQTT_LWT_QOS, MQTT_LWT_RETAIN, MQTT_LWT_PAYLOAD_OFFLINE);
client.setKeepAlive(MQTT_RECONNECT_DELAY * 3);

this->registerHandlers();

}

void debug(const char *message)
Expand Down Expand Up @@ -128,6 +135,7 @@ class MqttPublisher
DEBUG(F("MQTT: Connecting to broker..."));
client.connect();
}

void disconnect()
{
if (!this->connected)
Expand All @@ -137,15 +145,16 @@ class MqttPublisher
}
DEBUG(F("MQTT: Disconnecting from broker..."));
client.disconnect();
this->reconnectTimer.detach();
}

private:
bool connected = false;
MqttConfig config;
WiFiClient net;
PangolinMQTT client;
AsyncMqttClient client;
Ticker reconnectTimer;
String baseTopic;
String lastWillTopic;

void publish(const String &topic, const String &payload, uint8_t qos=0, bool retain=false)
{
Expand All @@ -167,77 +176,31 @@ class MqttPublisher
{
DEBUG(F("MQTT: Publishing to %s:"), topic);
DEBUG(F("%s\n"), payload);
client.publish(topic, payload, strlen(payload), qos, retain);
client.publish(topic, qos, retain, payload, strlen(payload));
}
}

void registerHandlers()
{

client.onConnect([this](bool sessionPresent) {
this->connected = true;
reconnectTimer.detach();
DEBUG(F("MQTT client connection established."));
this->reconnectTimer.detach();
DEBUG(F("MQTT: Connection established."));
char message[64];
snprintf(message, 64, "Hello from %08X, running SMLReader version %s.", ESP.getChipId(), VERSION);
info(message);
publish(baseTopic + MQTT_LWT_TOPIC, MQTT_LWT_PAYLOAD_ONLINE, MQTT_LWT_QOS, MQTT_LWT_RETAIN);
});
client.onDisconnect([this](int8_t reason) {
client.onDisconnect([this](AsyncMqttClientDisconnectReason reason) {
this->connected = false;
DEBUG("MQTT client disconnected with reason=%d", reason);
DEBUG(F("MQTT: Disconnected. Reason: %d."), reason);
reconnectTimer.attach(MQTT_RECONNECT_DELAY, [this]() {
this->connect();
if (WiFi.isConnected()) {
this->connect();
}
});
});
client.onError([this](uint8_t e, uint32_t info) {
switch (e)
{
case TCP_DISCONNECTED:
// usually because your structure is wrong and you called a function before onMqttConnect
DEBUG(F("MQTT: NOT CONNECTED info=%d"), info);
break;
case MQTT_SERVER_UNAVAILABLE:
// server has gone away - network problem? server crash?
DEBUG(F("MQTT: MQTT_SERVER_UNAVAILABLE info=%d"), info);
break;
case UNRECOVERABLE_CONNECT_FAIL:
// there is something wrong with your connection parameters? IP:port incorrect? user credentials typo'd?
DEBUG(F("MQTT: UNRECOVERABLE_CONNECT_FAIL info=%d"), info);
break;
case TLS_BAD_FINGERPRINT:
DEBUG(F("MQTT: TLS_BAD_FINGERPRINT info=%d"), info);
break;
case SUBSCRIBE_FAIL:
// you tried to subscribe to an invalid topic
DEBUG(F("MQTT: SUBSCRIBE_FAIL info=%d"), info);
break;
case INBOUND_QOS_ACK_FAIL:
DEBUG(F("MQTT: OUTBOUND_QOS_ACK_FAIL id=%d"), info);
break;
case OUTBOUND_QOS_ACK_FAIL:
DEBUG(F("MQTT: OUTBOUND_QOS_ACK_FAIL id=%d"), info);
break;
case INBOUND_PUB_TOO_BIG:
// someone sent you a p[acket that this MCU does not have enough FLASH to handle
DEBUG(F("MQTT: INBOUND_PUB_TOO_BIG size=%d Max=%d"), info, client.getMaxPayloadSize());
break;
case OUTBOUND_PUB_TOO_BIG:
// you tried to send a packet that this MCU does not have enough FLASH to handle
DEBUG(F("MQTT: OUTBOUND_PUB_TOO_BIG size=%d Max=%d"), info, client.getMaxPayloadSize());
break;
case BOGUS_PACKET: // Your server sent a control packet type unknown to MQTT 3.1.1
// 99.99% unlikely to ever happen, but this message is better than a crash, non?
DEBUG(F("MQTT: BOGUS_PACKET info=%02x"), info);
break;
case X_INVALID_LENGTH: // An x function rcvd a msg with an unexpected length: probale data corruption or malicious msg
// 99.99% unlikely to ever happen, but this message is better than a crash, non?
DEBUG(F("MQTT: X_INVALID_LENGTH info=%02x"), info);
break;
default:
DEBUG(F("MQTT: UNKNOWN ERROR: %u extra info %d"), e, info);
break;
}
});
}
};

Expand Down
14 changes: 9 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ MqttPublisher publisher;
IotWebConf iotWebConf(WIFI_AP_SSID, &dnsServer, &server, WIFI_AP_DEFAULT_PASSWORD, CONFIG_VERSION);

iotwebconf::TextParameter mqttServerParam = iotwebconf::TextParameter("MQTT server", "mqttServer", mqttConfig.server, sizeof(mqttConfig.server), nullptr, mqttConfig.server);
iotwebconf::TextParameter mqttPortParam = iotwebconf::TextParameter("MQTT port", "mqttPort", mqttConfig.port, sizeof(mqttConfig.port), nullptr, mqttConfig.port);
iotwebconf::NumberParameter mqttPortParam = iotwebconf::NumberParameter("MQTT port", "mqttPort", mqttConfig.port, sizeof(mqttConfig.port), nullptr, mqttConfig.port);
iotwebconf::TextParameter mqttUsernameParam = iotwebconf::TextParameter("MQTT username", "mqttUsername", mqttConfig.username, sizeof(mqttConfig.username), nullptr, mqttConfig.username);
iotwebconf::PasswordParameter mqttPasswordParam = iotwebconf::PasswordParameter("MQTT password", "mqttPassword", mqttConfig.password, sizeof(mqttConfig.password), nullptr, mqttConfig.password);
iotwebconf::TextParameter mqttTopicParam = iotwebconf::TextParameter("MQTT topic", "mqttTopic", mqttConfig.topic, sizeof(mqttConfig.topic), nullptr, mqttConfig.topic);
iotwebconf::ParameterGroup paramGroup = iotwebconf::ParameterGroup("group1", "");
iotwebconf::ParameterGroup paramGroup = iotwebconf::ParameterGroup("MQTT Settings", "");

boolean needReset = false;
boolean connected = false;

void process_message(byte *buffer, size_t len, Sensor *sensor)
{
Expand Down Expand Up @@ -83,6 +82,11 @@ void setup()
iotWebConf.setConfigSavedCallback(&configSaved);
iotWebConf.setWifiConnectionCallback(&wifiConnected);


WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) {
publisher.disconnect();
});

// -- Define how to handle updateServer calls.
iotWebConf.setupUpdateServer(
[](const char *updatePath)
Expand All @@ -101,7 +105,8 @@ void setup()
publisher.setup(mqttConfig);
}

server.on("/", [] { iotWebConf.handleConfig(); });
server.on("/", []() { iotWebConf.handleConfig(); });
server.on("/reset", []() { needReset = true; });
server.onNotFound([]() { iotWebConf.handleNotFound(); });

DEBUG("Setup done.");
Expand Down Expand Up @@ -134,6 +139,5 @@ void configSaved()
void wifiConnected()
{
DEBUG("WiFi connection established.");
connected = true;
publisher.connect();
}

0 comments on commit d888efc

Please sign in to comment.