Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.idea
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
/Tools
src/Globals.cpp
src/Globals.cpp
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ default_envs = ulanzi
[env]
framework = arduino
board_build.f_cpu = 240000000L
upload_speed = 921600
upload_speed = 115200
monitor_speed = 115200
monitor_filters = esp32_exception_decoder
lib_deps =
Expand Down
1 change: 1 addition & 0 deletions src/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ uint16_t MQTT_PORT = 1883;
String MQTT_USER;
String MQTT_PASS;
String MQTT_PREFIX;
bool MQTT_TLS = false;
bool IO_BROKER = false;
bool NET_STATIC = false;
bool SHOW_TIME = true;
Expand Down
1 change: 1 addition & 0 deletions src/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern uint16_t MQTT_PORT;
extern String MQTT_USER;
extern String MQTT_PASS;
extern String MQTT_PREFIX;
extern bool MQTT_TLS;
extern bool IO_BROKER;
extern bool NET_STATIC;
extern bool SHOW_TIME;
Expand Down
31 changes: 26 additions & 5 deletions src/MQTTManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ServerManager.h"
#include <ArduinoHA.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include "Dictionary.h"
#include "PeripheryManager.h"
Expand All @@ -12,9 +13,12 @@

const uint16_t PORT = 1883;

WiFiClient espClient;
WiFiClientSecure secureClient;
WiFiClient plainClient;
HADevice device;
HAMqtt mqtt(espClient, device, 26);
HAMqtt *mqttPtr = nullptr;

#define mqtt (*mqttPtr)

HALight *Matrix, *Indikator1, *Indikator2, *Indikator3 = nullptr;
HASelect *BriMode, *transEffect = nullptr;
Expand Down Expand Up @@ -463,7 +467,6 @@ bool MQTTManager_::isConnected()

void connect()
{

mqtt.onMessage(onMqttMessage);
mqtt.onConnected(onMqttConnected);

Expand Down Expand Up @@ -537,6 +540,15 @@ void MQTTManager_::sendStats()

void MQTTManager_::setup()
{
if (MQTT_TLS)
{
secureClient.setInsecure();
mqttPtr = new HAMqtt(secureClient, device, 26);
}
else
{
mqttPtr = new HAMqtt(plainClient, device, 26);
}

if (HA_DISCOVERY)
{
Expand Down Expand Up @@ -741,7 +753,7 @@ void MQTTManager_::setup()

void MQTTManager_::tick()
{
if (MQTT_HOST != "")
if (mqttPtr != nullptr && MQTT_HOST != "")
{
mqtt.loop();
}
Expand All @@ -755,6 +767,9 @@ void MQTTManager_::tick()

void MQTTManager_::publish(const char *topic, const char *payload)
{
if (mqttPtr == nullptr)
return;

char result[100];
strcpy(result, MQTT_PREFIX.c_str());
strcat(result, "/");
Expand All @@ -768,7 +783,7 @@ void MQTTManager_::publish(const char *topic, const char *payload)

void MQTTManager_::rawPublish(const char *prefix, const char *topic, const char *payload)
{
if (!mqtt.isConnected())
if (mqttPtr == nullptr || !mqtt.isConnected())
return;
char result[100];
strcpy(result, prefix);
Expand Down Expand Up @@ -864,15 +879,21 @@ void MQTTManager_::setIndicatorState(uint8_t indicator, bool state, uint32_t col

void MQTTManager_::beginPublish(const char *topic, unsigned int plength, boolean retained)
{
if (mqttPtr == nullptr)
return;
mqtt.beginPublish(topic, plength, retained);
}

void MQTTManager_::writePayload(const char *data, const uint16_t length)
{
if (mqttPtr == nullptr)
return;
mqtt.writePayload(data, length);
}

void MQTTManager_::endPublish()
{
if (mqttPtr == nullptr)
return;
mqtt.endPublish();
}
2 changes: 2 additions & 0 deletions src/ServerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ void ServerManager_::setup()
mws.addOption("Username", MQTT_USER);
mws.addOption("Password", MQTT_PASS);
mws.addOption("Prefix", MQTT_PREFIX);
mws.addOption("TLS", MQTT_TLS);
mws.addOption("Homeassistant Discovery", HA_DISCOVERY);
mws.addOptionBox("Time");
mws.addOption("NTP Server", NTP_SERVER);
Expand Down Expand Up @@ -366,6 +367,7 @@ void ServerManager_::loadSettings()
MQTT_PASS = doc["Password"].as<String>();
MQTT_PREFIX = doc["Prefix"].as<String>();
MQTT_PREFIX.trim();
MQTT_TLS = doc.containsKey("TLS") ? doc["TLS"].as<bool>() : false;
NET_STATIC = doc["Static IP"];
HA_DISCOVERY = doc["Homeassistant Discovery"];
NET_IP = doc["Local IP"].as<String>();
Expand Down