feat: Switching to mqtt

This commit is contained in:
2025-11-10 20:52:14 +01:00
parent de9ff6f807
commit a18ddf2cb1
15 changed files with 127 additions and 161 deletions
+1 -1
Submodule core updated: 02db420163...8af1c2f555
+1 -8
View File
@@ -1,17 +1,10 @@
dependencies: dependencies:
ethernet_init:
dependencies: []
source:
path: /home/gustice/esp/v5.4.1/esp-idf/examples/ethernet/basic/components/ethernet_init
type: local
version: '*'
idf: idf:
source: source:
type: idf type: idf
version: 5.4.1 version: 5.4.1
direct_dependencies: direct_dependencies:
- ethernet_init
- idf - idf
manifest_hash: dfcb516d4bd1043fe7dbc63ade7e95173fcf2993c59d7108e7b96cb5af8f7ebd manifest_hash: 00d8efc8e59d9f2cc6a576ada310175e19e6f7506dd0b59211849f065dadccae
target: esp32c6 target: esp32c6
version: 2.0.0 version: 2.0.0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+4 -4
View File
@@ -1,25 +1,25 @@
menu "Example Configuration" menu "Example Configuration"
config EXAMPLE_WIFI_SSID config DEVICE_WIFI_SSID
string "Wi-Fi SSID" string "Wi-Fi SSID"
default "eth2ap" default "eth2ap"
help help
Set the SSID of Wi-Fi ap interface. Set the SSID of Wi-Fi ap interface.
config EXAMPLE_WIFI_PASSWORD config DEVICE_WIFI_PASSWORD
string "Wi-Fi Password" string "Wi-Fi Password"
default "12345678" default "12345678"
help help
Set the password of Wi-Fi ap interface. Set the password of Wi-Fi ap interface.
config EXAMPLE_WIFI_CHANNEL config DEVICE_WIFI_CHANNEL
int "WiFi channel" int "WiFi channel"
range 1 13 range 1 13
default 1 default 1
help help
Set the channel of Wi-Fi ap. Set the channel of Wi-Fi ap.
config EXAMPLE_MAX_STA_CONN config DEVICE_MAX_STA_CONN
int "Maximum STA connections" int "Maximum STA connections"
default 4 default 4
help help
-3
View File
@@ -1,3 +0,0 @@
dependencies:
ethernet_init:
path: ${IDF_PATH}/examples/ethernet/basic/components/ethernet_init
+47
View File
@@ -0,0 +1,47 @@
#pragma once
#include <cstdint>
struct {
const char *syslogDest = "syslogDst"; // example: "172.16.16.10"
const char *mqttBroker = "mqttBroker"; // example: "172.16.16.10"
const char *mqttPrefix = "mqttPrefix"; // example: "SomeTag"
const char *spitServer = "spitServer"; // example: "172.16.16.10"
} ConfigKeys;
// MQTT Topics
struct {
// Subscriptions:
const char *moveTo = /* <mqttPrefix>/ */ "watch/moveTo";
const char *startShuffle = /* <mqttPrefix>/ */ "watch/startShuffle";
// Publishes:
const char *inPosition = /* <mqttPrefix>/ */ "watch/inPosition";
const char *changingPosition = /* <mqttPrefix>/ */ "watch/changingPosition";
} MqttTopics;
struct {
const char *expectedServer = "";
struct {
const char *group = "CatasWatchMovement";
const char *startShuffle = "ActionStartShuffle";
const char *moveToX[6] = {
"ActionMoveTo1",
"ActionMoveTo2",
"ActionMoveTo3",
"ActionMoveTo4",
"ActionMoveTo5",
"ActionMoveTo6",
};
} Provided;
struct {
const char *changingPos = "ChangingPositionScene";
const char *inPosX[6] = {
"InPosition1Scene",
"InPosition2Scene",
"InPosition3Scene",
"InPosition4Scene",
"InPosition5Scene",
"InPosition6Scene",
};
} Expected;
} SpitTypes;
+38 -103
View File
@@ -2,8 +2,6 @@
* @file watchMain.cpp * @file watchMain.cpp
* @author Gustice * @author Gustice
* @brief ChaosWatch Application * @brief ChaosWatch Application
*
* @copyright Copyright (c) 2024
*/ */
/* Watchface /* Watchface
@@ -13,58 +11,8 @@
3 3
*/ */
struct {
const char *syslogDest = "syslogDst"; // example: "172.16.16.10"
const char *mqttBroker = "mqttBroker"; // example: "172.16.16.10"
const char *mqttPrefix = "mqttPrefix"; // example: "SomeTag"
const char *spitServer = "spitServer"; // example: "172.16.16.10"
} ConfigKeys;
// MQTT Topics
struct {
// Subscriptions:
const char *moveTo = /* <mqttPrefix>/ */ "watch/moveTo";
const char *startShuffle = /* <mqttPrefix>/ */ "watch/startShuffle";
// Publishes:
const char *inPosition = /* <mqttPrefix>/ */ "watch/inPosition";
const char *changingPosition = /* <mqttPrefix>/ */ "watch/changingPosition";
} MqttTopics;
struct {
const char *expectedServer = "";
struct {
const char *group = "CatasWatchMovement";
const char *startShuffle = "ActionStartShuffle";
const char *moveToX[6] = {
"ActionMoveTo1",
"ActionMoveTo2",
"ActionMoveTo3",
"ActionMoveTo4",
"ActionMoveTo5",
"ActionMoveTo6",
};
} Provided;
struct {
const char *changingPos = "ChangingPositionScene";
const char *inPosX[6] = {
"InPosition1Scene",
"InPosition2Scene",
"InPosition3Scene",
"InPosition4Scene",
"InPosition5Scene",
"InPosition6Scene",
};
} Expected;
} SpitTypes;
// ToDo:
// - MQTT Integration
// - OTA
// - Syslog heartbeat
// - Streamlined DMX
// -
#include "watchMain.hpp" #include "watchMain.hpp"
#include "interconConfig.hpp"
string ClientName = "CatasWatchDev"; string ClientName = "CatasWatchDev";
string ProjectName = "CatastrphicProj"; string ProjectName = "CatastrphicProj";
@@ -112,7 +60,7 @@ static fos::Queue<PictoIllumCommand> pictoIllumQue(2);
DeviceState activeState; DeviceState activeState;
static const char *TAG = "Watch"; static const char *TAG = "Watch";
static bool EthernetIsConnected = false; bool EthernetIsConnected = false;
std::unique_ptr<Cli> pShell; std::unique_ptr<Cli> pShell;
@@ -312,8 +260,6 @@ void moveToSubs(std::string data) {
int p = std::stoi(data); int p = std::stoi(data);
Syslog::logv(Syslog::Severity::Informational, "got MoveTo-Cmd->%i", p); Syslog::logv(Syslog::Severity::Informational, "got MoveTo-Cmd->%i", p);
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::StepTo, p)); commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::StepTo, p));
auto &client = MqttClient::getInstance();
client.publish(MqttTopics.inPosition, data + "Picto");
} catch (const std::exception &e) { } catch (const std::exception &e) {
ESP_LOGE(TAG, "Error to process data %s, with ex: %s", data.c_str(), e.what()); ESP_LOGE(TAG, "Error to process data %s, with ex: %s", data.c_str(), e.what());
} }
@@ -321,12 +267,9 @@ void moveToSubs(std::string data) {
void startShuffle(std::string data) { void startShuffle(std::string data) {
ESP_LOGI(TAG, "StartShuffle command %s", data.c_str()); ESP_LOGI(TAG, "StartShuffle command %s", data.c_str());
try { try {
Syslog::logv(Syslog::Severity::Informational, "got Shuffle-Command"); Syslog::logv(Syslog::Severity::Informational, "got Shuffle-Command");
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::Shuffle, 0)); commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::Shuffle, 0));
auto &client = MqttClient::getInstance();
client.publish(MqttTopics.changingPosition, "....");
} catch (const std::exception &e) { } catch (const std::exception &e) {
ESP_LOGE(TAG, "Error to process data %s, with ex: %s", data.c_str(), e.what()); ESP_LOGE(TAG, "Error to process data %s, with ex: %s", data.c_str(), e.what());
} }
@@ -360,7 +303,6 @@ static void dmxSocketSenderTask(UdpSocket &socket) {
* @param args ignored task argument * @param args ignored task argument
*/ */
static void dmxSocketTask(int addrFamily) { static void dmxSocketTask(int addrFamily) {
ESP_LOGI(TAG, "Starting ArtNet task"); ESP_LOGI(TAG, "Starting ArtNet task");
constexpr uint16_t ArtNetPort = arx::artnet::DEFAULT_PORT; constexpr uint16_t ArtNetPort = arx::artnet::DEFAULT_PORT;
IpAdr address = addrFamily == AF_INET ? IpAdr(INADDR_ANY, ArtNetPort) : IpAdr(ArtNetPort); IpAdr address = addrFamily == AF_INET ? IpAdr(INADDR_ANY, ArtNetPort) : IpAdr(ArtNetPort);
@@ -389,7 +331,7 @@ fos::Semaphore etherActive;
fos::Semaphore spitProjectFinish; fos::Semaphore spitProjectFinish;
/** Event handler for IP_EVENT_ETH_GOT_IP */ /** Event handler for IP_EVENT_ETH_GOT_IP */
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, void got_ip_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) { int32_t event_id, void *event_data) {
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
const esp_netif_ip_info_t *ip_info = &event->ip_info; const esp_netif_ip_info_t *ip_info = &event->ip_info;
@@ -403,37 +345,10 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
etherActive.give(); etherActive.give();
} }
// Event handler for Ethernet
static void eth_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
uint8_t mac_addr[6] = {0};
switch (event_id) {
case ETHERNET_EVENT_CONNECTED:
ESP_LOGI(TAG, "Ethernet Link Up");
EthernetIsConnected = true;
ESP_LOGI(TAG, "Ethernet Link Up");
ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
break;
case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "Ethernet Link Down");
EthernetIsConnected = false;
break;
case ETHERNET_EVENT_START:
ESP_LOGI(TAG, "Ethernet Started");
break;
case ETHERNET_EVENT_STOP:
ESP_LOGI(TAG, "Ethernet Stopped");
break;
default:
break;
}
}
static void initialize_ethernet(void) { static void initialize_ethernet(void) {
uint8_t eth_port_cnt = 0; uint8_t eth_port_cnt = 0;
esp_eth_handle_t *eth_handles; esp_eth_handle_t *eth_handles;
ESP_ERROR_CHECK(example_eth_init(&eth_handles, &eth_port_cnt)); ESP_ERROR_CHECK(eth_init(&eth_handles, &eth_port_cnt));
if (eth_port_cnt > 1) { if (eth_port_cnt > 1) {
ESP_LOGW(TAG, "multiple Ethernet devices detected, the first initialized is to be used!"); ESP_LOGW(TAG, "multiple Ethernet devices detected, the first initialized is to be used!");
} }
@@ -656,6 +571,7 @@ void MovementTask() {
} }
} }
} }
struct LocalSpitObj { struct LocalSpitObj {
string iD; string iD;
string groupName; string groupName;
@@ -851,7 +767,6 @@ void onPlay(string p_PlayerID, string p_PlayerName, char p_PlayerType, bool p_Pl
*/ */
void onPlaySpitType(string p_PlayerID, string spitTypeId, bool p_PlayerIsRunning, bool p_Started, bool p_Ended, double p_Value) { void onPlaySpitType(string p_PlayerID, string spitTypeId, bool p_PlayerIsRunning, bool p_Started, bool p_Ended, double p_Value) {
// double doubleValue = ((double)p_PlayObject.getValue() / 1000000) * ((double)p_PlayObject.getValueFactor() / 1000000); // double doubleValue = ((double)p_PlayObject.getValue() / 1000000) * ((double)p_PlayObject.getValueFactor() / 1000000);
ESP_LOGD(TAG, "eventPlayObject Player: %s is running: %d Object Started: %d Stopped: %d", p_PlayerID.data(), p_PlayerIsRunning, p_Started, p_Ended); ESP_LOGD(TAG, "eventPlayObject Player: %s is running: %d Object Started: %d Stopped: %d", p_PlayerID.data(), p_PlayerIsRunning, p_Started, p_Ended);
// ESP_LOGD(TAG, " SPITType: %s SPITObject: %s Position: %lld Value: %f\n", spitTypeId.data(), p_SPITObject.getName().data(), p_PlayObject.getFrameInside(), doubleValue); // ESP_LOGD(TAG, " SPITType: %s SPITObject: %s Position: %lld Value: %f\n", spitTypeId.data(), p_SPITObject.getName().data(), p_PlayObject.getFrameInside(), doubleValue);
ESP_LOGD(TAG, " SPITType: %s Value: %f\n", spitTypeId.data(), p_Value); ESP_LOGD(TAG, " SPITType: %s Value: %f\n", spitTypeId.data(), p_Value);
@@ -938,6 +853,32 @@ void SpitClientApp() {
} }
} }
void MqttThread() {
ESP_LOGI(TAG, "Starting MQTT task");
auto &store = NvsApi::getInstance();
auto broker = store.getValue(ConfigKeys.mqttBroker);
if (broker != "") {
ESP_LOGI(TAG, "Initializing MQTT");
auto prefix = store.getValue(ConfigKeys.mqttPrefix);
auto &client = MqttClient::init(broker, prefix);
if (!client.isRunning()) {
client.addSubscription(MqttTopics.moveTo, [](std::string d) { moveToSubs(d); });
client.addSubscription(MqttTopics.startShuffle, [](std::string d) { startShuffle(d); });
client.start();
}
while (true) {
auto res = responseQueue.dequeue();
if (res->type == ResponseType::StartMoving) {
client.publish(MqttTopics.changingPosition, "");
} else if (res->type == ResponseType::StepAt) {
client.publish(MqttTopics.inPosition, std::to_string(res->pos) );
}
}
}
}
void app_main(void) { void app_main(void) {
// esp_err_t ret = nvs_flash_init(); // esp_err_t ret = nvs_flash_init();
// if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { // if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
@@ -953,14 +894,14 @@ void app_main(void) {
etherActive.take(); etherActive.take();
fos::Task::delayMs(10000); fos::Task::delayMs(10000);
ESP_LOGI(TAG, "Start net services"); ESP_LOGI(TAG, "Start net services");
// esp_log_level_set("SPIT", esp_log_level_t::ESP_LOG_DEBUG); // esp_log_level_set("SPIT", esp_log_level_t::ESP_LOG_DEBUG);
static int ipVersion = AF_INET; static int ipVersion = AF_INET;
static fos::TaskT<int> telnetSocket(telnetSocketTask, ipVersion, "tcpServer", 4096); static fos::TaskT<int> telnetSocket(telnetSocketTask, ipVersion, "tcpServer", 4096);
static fos::TaskT<int> dmxNode(dmxSocketTask, ipVersion, "dmxNode", 4096); static fos::TaskT<int> dmxNode(dmxSocketTask, ipVersion, "dmxNode", 4096);
// static fos::Task spit(SpitClientApp, "SpitClient");
static fos::Task mqtt(MqttThread, "MqttEndpoint");
// static Task otaUpdate(SystemFunctions::OtaTaskEntry, "otaTask", 8192); // static Task otaUpdate(SystemFunctions::OtaTaskEntry, "otaTask", 8192);
static fos::Task spit(SpitClientApp, "SpitClient");
auto &store = NvsApi::getInstance(); auto &store = NvsApi::getInstance();
auto logger = store.getValue(ConfigKeys.syslogDest); auto logger = store.getValue(ConfigKeys.syslogDest);
@@ -968,17 +909,11 @@ void app_main(void) {
Syslog::setupTask(IpAdr{logger, 0}); Syslog::setupTask(IpAdr{logger, 0});
} }
// auto broker = store.getValue(ConfigKeys.mqttBroker); ESP_LOGI(TAG, "Configuration");
// if (broker != "") { ESP_LOGI(TAG, "%s: %s", ConfigKeys.syslogDest, store.getValue(ConfigKeys.syslogDest).c_str());
// ESP_LOGI(TAG, "Initializing MQTT"); ESP_LOGI(TAG, "%s: %s", ConfigKeys.mqttBroker, store.getValue(ConfigKeys.mqttBroker).c_str());
// auto prefix = store.getValue(ConfigKeys.mqttPrefix); ESP_LOGI(TAG, "%s: %s", ConfigKeys.mqttPrefix, store.getValue(ConfigKeys.mqttPrefix).c_str());
// auto &client = MqttClient::init(broker, prefix); ESP_LOGI(TAG, "%s: %s", ConfigKeys.spitServer, store.getValue(ConfigKeys.spitServer).c_str());
// if (!client.isRunning()) {
// client.addSubscription(MqttTopics.moveTo, [](std::string d) { moveToSubs(d); });
// client.addSubscription(MqttTopics.startShuffle, [](std::string d) { startShuffle(d); });
// client.start();
// }
// }
while (true) { while (true) {
fos::Task::delayMs(5000); fos::Task::delayMs(5000);
+19 -25
View File
@@ -582,10 +582,10 @@ CONFIG_PARTITION_TABLE_MD5=y
# #
# Example Configuration # Example Configuration
# #
CONFIG_EXAMPLE_WIFI_SSID="eth2ap" CONFIG_DEVICE_WIFI_SSID="eth2ap"
CONFIG_EXAMPLE_WIFI_PASSWORD="12345678" CONFIG_DEVICE_WIFI_PASSWORD="12345678"
CONFIG_EXAMPLE_WIFI_CHANNEL=1 CONFIG_DEVICE_WIFI_CHANNEL=1
CONFIG_EXAMPLE_MAX_STA_CONN=4 CONFIG_DEVICE_MAX_STA_CONN=4
# end of Example Configuration # end of Example Configuration
# #
@@ -595,27 +595,21 @@ CONFIG_ENV_GPIO_RANGE_MIN=0
CONFIG_ENV_GPIO_RANGE_MAX=30 CONFIG_ENV_GPIO_RANGE_MAX=30
CONFIG_ENV_GPIO_IN_RANGE_MAX=30 CONFIG_ENV_GPIO_IN_RANGE_MAX=30
CONFIG_ENV_GPIO_OUT_RANGE_MAX=30 CONFIG_ENV_GPIO_OUT_RANGE_MAX=30
# CONFIG_USE_SPI_ETHERNET is not set CONFIG_USE_SPI_ETHERNET=y
# end of Example Ethernet Configuration CONFIG_SPI_ETHERNETS_NUM=1
# CONFIG_USE_DM9051 is not set
# # CONFIG_USE_KSZ8851SNL is not set
# Example Ethernet Configuration CONFIG_USE_W5500=y
# CONFIG_ETH_SPI_HOST=1
CONFIG_EXAMPLE_USE_SPI_ETHERNET=y CONFIG_ETH_SPI_SCLK_GPIO=6
CONFIG_EXAMPLE_SPI_ETHERNETS_NUM=1 CONFIG_ETH_SPI_MOSI_GPIO=7
# CONFIG_EXAMPLE_USE_DM9051 is not set CONFIG_ETH_SPI_MISO_GPIO=2
# CONFIG_EXAMPLE_USE_KSZ8851SNL is not set CONFIG_ETH_SPI_CLOCK_MHZ=16
CONFIG_EXAMPLE_USE_W5500=y CONFIG_ETH_SPI_CS0_GPIO=3
CONFIG_EXAMPLE_ETH_SPI_HOST=1 CONFIG_ETH_SPI_INT0_GPIO=4
CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO=6 CONFIG_ETH_SPI_POLLING0_MS=0
CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO=7 CONFIG_ETH_SPI_PHY_RST0_GPIO=-1
CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO=2 CONFIG_ETH_SPI_PHY_ADDR0=1
CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ=16
CONFIG_EXAMPLE_ETH_SPI_CS0_GPIO=3
CONFIG_EXAMPLE_ETH_SPI_INT0_GPIO=4
CONFIG_EXAMPLE_ETH_SPI_POLLING0_MS=0
CONFIG_EXAMPLE_ETH_SPI_PHY_RST0_GPIO=-1
CONFIG_EXAMPLE_ETH_SPI_PHY_ADDR0=1
# end of Example Ethernet Configuration # end of Example Ethernet Configuration
# #
+17 -17
View File
@@ -5,24 +5,24 @@ CONFIG_COMPILER_CXX_EXCEPTIONS=y
CONFIG_LWIP_LOCAL_HOSTNAME="ChaosWatch" CONFIG_LWIP_LOCAL_HOSTNAME="ChaosWatch"
# #
# Example Ethernet Configuration # Ethernet Configuration
# #
CONFIG_EXAMPLE_USE_SPI_ETHERNET=y CONFIG_USE_SPI_ETHERNET=y
CONFIG_EXAMPLE_SPI_ETHERNETS_NUM=1 CONFIG_SPI_ETHERNETS_NUM=1
# CONFIG_EXAMPLE_USE_DM9051 is not set # CONFIG_USE_DM9051 is not set
# CONFIG_EXAMPLE_USE_KSZ8851SNL is not set # CONFIG_USE_KSZ8851SNL is not set
CONFIG_EXAMPLE_USE_W5500=y CONFIG_USE_W5500=y
CONFIG_EXAMPLE_ETH_SPI_HOST=1 CONFIG_ETH_SPI_HOST=1
CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO=6 CONFIG_ETH_SPI_SCLK_GPIO=6
CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO=7 CONFIG_ETH_SPI_MOSI_GPIO=7
CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO=2 CONFIG_ETH_SPI_MISO_GPIO=2
CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ=16 CONFIG_ETH_SPI_CLOCK_MHZ=16
CONFIG_EXAMPLE_ETH_SPI_CS0_GPIO=3 CONFIG_ETH_SPI_CS0_GPIO=3
CONFIG_EXAMPLE_ETH_SPI_INT0_GPIO=4 CONFIG_ETH_SPI_INT0_GPIO=4
CONFIG_EXAMPLE_ETH_SPI_POLLING0_MS=0 CONFIG_ETH_SPI_POLLING0_MS=0
CONFIG_EXAMPLE_ETH_SPI_PHY_RST0_GPIO=-1 CONFIG_ETH_SPI_PHY_RST0_GPIO=-1
CONFIG_EXAMPLE_ETH_SPI_PHY_ADDR0=1 CONFIG_ETH_SPI_PHY_ADDR0=1
# end of Example Ethernet Configuration # end of Ethernet Configuration
# #
# Ethernet # Ethernet