feature: Including MQTT client
This commit is contained in:
@@ -174,7 +174,7 @@ std::string NvsApi::getValue(const std::string &key) {
|
||||
auto value = opt.value();
|
||||
return value;
|
||||
} catch (const std::bad_optional_access &e) {
|
||||
ESP_LOGE(TAG, "%s", "Value not defined");
|
||||
ESP_LOGE(TAG, "Value '%s' not defined", key.c_str());
|
||||
} catch (const std::exception &e) {
|
||||
ESP_LOGE(TAG, "%s", e.what());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
idf_component_register(
|
||||
SRC_DIRS "src"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES esp_netif lwip project_bases freeRtosApi
|
||||
REQUIRES esp_netif lwip project_bases freeRtosApi mqtt
|
||||
)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* @file mqttClient.hpp
|
||||
* @author Gustice
|
||||
* @brief Wrapper class for TCP/IP based MQTT APIs
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "esp_event.h"
|
||||
#include "esp_system.h"
|
||||
#include "mqtt_client.h"
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
/**
|
||||
* @brief MqttClient wrapper (Singleton)
|
||||
*/
|
||||
class MqttClient {
|
||||
public:
|
||||
/// @brief Init and mount file system
|
||||
/// @param broker URI to web MQTT broker server
|
||||
static MqttClient &init(std::string broker, std::string prefix);
|
||||
void addSubscription(std::string topic, std::function<void(std::string)> call);
|
||||
void publish(std::string topic, std::string data);
|
||||
void start();
|
||||
bool isRunning() { return _state != State::Init; }
|
||||
|
||||
/// @brief Get instance, ensure initialization
|
||||
/// @param broker URI to web MQTT broker server
|
||||
/// @returnb
|
||||
static MqttClient &getInstance();
|
||||
|
||||
// Disable move and assignment
|
||||
MqttClient(MqttClient &other) = delete;
|
||||
void operator=(const MqttClient &) = delete;
|
||||
|
||||
// protected:
|
||||
MqttClient(std::string broker, std::string pefix);
|
||||
~MqttClient();
|
||||
|
||||
private:
|
||||
enum class State {
|
||||
Init,
|
||||
Started,
|
||||
Disconnected,
|
||||
Connected
|
||||
};
|
||||
struct Subscription {
|
||||
std::string topic;
|
||||
std::function<void(std::string)> callback;
|
||||
bool active{};
|
||||
int id{};
|
||||
};
|
||||
|
||||
std::string _broker;
|
||||
std::string _prefix;
|
||||
State _state{};
|
||||
static MqttClient *_instance;
|
||||
esp_mqtt_client_handle_t _client;
|
||||
std::vector<Subscription> _subscriptions;
|
||||
|
||||
static void eventHandler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
|
||||
};
|
||||
@@ -269,6 +269,9 @@ class Syslog {
|
||||
if (_Socket == nullptr) {
|
||||
return;
|
||||
}
|
||||
// if (severity > _severityThresh) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
char buffer[512];
|
||||
// V T H A P M D Msg
|
||||
@@ -286,6 +289,7 @@ class Syslog {
|
||||
if (_Socket) {
|
||||
return;
|
||||
}
|
||||
ESP_LOGI("UDP", "Starting syslog session to %s", target.getIp().c_str());
|
||||
_Facility = Facility::Syslog;
|
||||
_HostName = "ESP";
|
||||
_AppName = "App";
|
||||
@@ -301,4 +305,5 @@ class Syslog {
|
||||
static std::string _AppName;
|
||||
static UdpSocket *_Socket;
|
||||
static IpAdr _Target;
|
||||
static Severity _severityThresh;
|
||||
};
|
||||
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* @file mqttClient.cpp
|
||||
* @author Gustice
|
||||
* @brief Wrapper class for TCP/IP based MQTT APIs
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*/
|
||||
|
||||
#include "mqttClient.hpp"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "mqtt_client.h"
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
MqttClient *MqttClient::_instance = nullptr;
|
||||
const char *TAG = "Mqtt";
|
||||
|
||||
static void log_error_if_nonzero(const char *message, int error_code) {
|
||||
if (error_code != 0) {
|
||||
ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code);
|
||||
}
|
||||
}
|
||||
|
||||
void MqttClient::eventHandler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
|
||||
ESP_LOGI(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id);
|
||||
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
|
||||
esp_mqtt_client_handle_t client = event->client;
|
||||
MqttClient &instance = *((MqttClient *)handler_args);
|
||||
|
||||
int msg_id;
|
||||
switch ((esp_mqtt_event_id_t)event_id) {
|
||||
case MQTT_EVENT_CONNECTED:
|
||||
instance._state = State::Connected;
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
||||
for (auto &s : instance._subscriptions) {
|
||||
msg_id = esp_mqtt_client_subscribe(client, s.topic.c_str(), 1);
|
||||
ESP_LOGI(TAG, "sent subscribe successful: topic='%s', msg_id=%d", s.topic.c_str(), msg_id);
|
||||
}
|
||||
break;
|
||||
case MQTT_EVENT_DISCONNECTED:
|
||||
instance._state = State::Disconnected;
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBSCRIBED:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
|
||||
break;
|
||||
case MQTT_EVENT_UNSUBSCRIBED:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
|
||||
break;
|
||||
case MQTT_EVENT_PUBLISHED:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
|
||||
break;
|
||||
case MQTT_EVENT_DATA: {
|
||||
bool found = false;
|
||||
std::string topic(event->topic, event->topic_len);
|
||||
for (auto &s : instance._subscriptions) {
|
||||
if (s.topic == topic) {
|
||||
found = true;
|
||||
s.callback(std::string(event->data, event->data_len));
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
ESP_LOGW(TAG, "Unrecognized Topic");
|
||||
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
|
||||
printf("DATA=%.*s\r\n", event->data_len, event->data);
|
||||
}
|
||||
} break;
|
||||
case MQTT_EVENT_ERROR:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
|
||||
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
|
||||
log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
|
||||
log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
|
||||
log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno);
|
||||
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
MqttClient::MqttClient(std::string broker, std::string prefix)
|
||||
: _broker("mqtt://" + broker), _prefix(prefix) {
|
||||
// esp_log_level_set("*", ESP_LOG_INFO);
|
||||
// esp_log_level_set(TAG, ESP_LOG_VERBOSE);
|
||||
// esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE);
|
||||
// esp_log_level_set("transport_base", ESP_LOG_VERBOSE);
|
||||
// esp_log_level_set("esp-tls", ESP_LOG_VERBOSE);
|
||||
// esp_log_level_set("transport", ESP_LOG_VERBOSE);
|
||||
// esp_log_level_set("outbox", ESP_LOG_VERBOSE);
|
||||
|
||||
esp_mqtt_client_config_t mqtt_cfg{};
|
||||
mqtt_cfg.broker.address.uri = _broker.c_str();
|
||||
|
||||
_client = esp_mqtt_client_init(&mqtt_cfg);
|
||||
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
|
||||
esp_mqtt_client_register_event(_client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, eventHandler, this);
|
||||
}
|
||||
|
||||
MqttClient::~MqttClient() {
|
||||
for (auto &s : _subscriptions) {
|
||||
auto msg_id = esp_mqtt_client_unsubscribe(_client, s.topic.c_str());
|
||||
ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
|
||||
}
|
||||
esp_mqtt_client_disconnect(_client);
|
||||
}
|
||||
|
||||
MqttClient &MqttClient::getInstance() {
|
||||
if (_instance == nullptr) {
|
||||
throw "No instance created";
|
||||
}
|
||||
return *_instance;
|
||||
}
|
||||
|
||||
MqttClient &MqttClient::init(std::string broker, std::string prefix) {
|
||||
if (_instance != nullptr) {
|
||||
return *_instance;
|
||||
}
|
||||
|
||||
_instance = new MqttClient(broker, prefix);
|
||||
return *_instance;
|
||||
}
|
||||
|
||||
void MqttClient::addSubscription(std::string topic, std::function<void(std::string)> call) {
|
||||
std::string fullTopic = _prefix + "/" + topic;
|
||||
_subscriptions.emplace_back(fullTopic, call, false, 0);
|
||||
// ToDo ensure each topic is only present once
|
||||
}
|
||||
|
||||
void MqttClient::start() {
|
||||
ESP_LOGI(TAG, "Starting MQTT client");
|
||||
_state = State::Started;
|
||||
esp_mqtt_client_start(_client);
|
||||
}
|
||||
|
||||
void MqttClient::publish(std::string topic, std::string data) {
|
||||
if (_state != State::Connected) {
|
||||
ESP_LOGW(TAG, "not connected, cannot publish s=%i", (int)_state);
|
||||
return;
|
||||
}
|
||||
std::string fullTopic = _prefix + "/" + topic;
|
||||
int msg_id = esp_mqtt_client_publish(_client, fullTopic.c_str(), data.c_str(), data.size(), 1, 0);
|
||||
ESP_LOGI(TAG, "sent publish of topic '%s' successful, msg_id=%d", fullTopic.c_str(), msg_id);
|
||||
}
|
||||
@@ -13,6 +13,7 @@ Syslog::Facility Syslog::_Facility;
|
||||
std::string Syslog::_HostName;
|
||||
std::string Syslog::_AppName;
|
||||
IpAdr Syslog::_Target(INADDR_ANY, 0);
|
||||
Syslog::Severity Syslog::_severityThresh = Syslog::Severity::Informational;
|
||||
|
||||
void Syslog::SyslogTask() {
|
||||
ESP_LOGI("SysLog", "Starting Syslog Task");
|
||||
|
||||
+157
-70
@@ -6,6 +6,25 @@
|
||||
* @copyright Copyright (c) 2024
|
||||
*/
|
||||
|
||||
// Conig-Key:
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
struct {
|
||||
const char *syslogDest = "syslogDst"; // example: "172.16.16.10"
|
||||
const char *mqttBroker = "mqttBroker"; // example: "172.16.17.62"
|
||||
const char *mqttPrefix = "mqttPrefix"; // example: "SomeTag"
|
||||
} ConfigKeys;
|
||||
|
||||
// MQTT Topics
|
||||
// Subscriptions:
|
||||
// <mqttPrefix>/watch/MoveTo
|
||||
// <mqttPrefix>/watch/Shuffle
|
||||
// Publishes:
|
||||
// <mqttPrefix>/watch/inPosition
|
||||
// <mqttPrefix>/watch/changingPosition
|
||||
|
||||
#include "watchMain.hpp"
|
||||
|
||||
extern "C" { // This switch allows the ROS C-implementation to find this main
|
||||
@@ -13,23 +32,24 @@ void app_main(void);
|
||||
}
|
||||
|
||||
struct DeviceState {
|
||||
int jumps;
|
||||
int pictogram;
|
||||
int profile;
|
||||
uint8_t minJumps;
|
||||
uint8_t maxJumps;
|
||||
uint8_t profile;
|
||||
uint8_t restTime;
|
||||
float speed = 1.0; // Rad/sec
|
||||
float accel = 1.0; // Rad/sec^2
|
||||
Color color{16, 16, 16};
|
||||
uint8_t pictogram;
|
||||
};
|
||||
|
||||
enum class MoveType {
|
||||
Step,
|
||||
StepTo,
|
||||
Shuffle,
|
||||
Demo
|
||||
};
|
||||
struct MoveCommand {
|
||||
MoveType type;
|
||||
uint8_t deltaPic;
|
||||
uint8_t profile;
|
||||
Color color;
|
||||
uint8_t pos;
|
||||
};
|
||||
static fos::Queue<MoveCommand> commandQueue(8);
|
||||
|
||||
@@ -37,40 +57,62 @@ DeviceState activeState;
|
||||
static const char *TAG = "Watch";
|
||||
static bool EthernetIsConnected = false;
|
||||
|
||||
std::unique_ptr<Cli> shellPtr;
|
||||
std::unique_ptr<Cli> pShell;
|
||||
|
||||
void onCommand(std::string name, Cli::CArgs args) {
|
||||
ESP_LOGI(TAG, "Invoking: %s", name.c_str());
|
||||
}
|
||||
|
||||
void startUpdate(Cli::CArgs args) {
|
||||
|
||||
ESP_LOGI(TAG, "Invoking Update");
|
||||
if (args.size() != 1) {
|
||||
shellPtr->write("Need URL to application'\n");
|
||||
pShell->write("Need URL to application'\n");
|
||||
return;
|
||||
}
|
||||
// @ToDo: Start Update here
|
||||
}
|
||||
|
||||
void setLogServerConfig(Cli::CArgs args) {
|
||||
ESP_LOGI(TAG, "on showLogServerConfig:");
|
||||
void getConfig(Cli::CArgs args) {
|
||||
if (args.size() != 1) {
|
||||
shellPtr->write("Need IP address to syslog server'\n");
|
||||
// auto &store = NvsApi::getInstance();
|
||||
// auto logger = store.getValue("syslogDst");
|
||||
// shellPtr->write(std::string{"syslog destination = '"} + logger + "'\n");
|
||||
pShell->write("Needs string key for query'\n");
|
||||
return;
|
||||
}
|
||||
// auto &store = NvsApi::getInstance();
|
||||
// shellPtr->write(std::string{"setting syslog destination to = '"} + args[0] + "'\n");
|
||||
// store.setValue("syslogDst", args[0]);
|
||||
auto key = args[0];
|
||||
auto &store = NvsApi::getInstance();
|
||||
auto val = store.getValue(key);
|
||||
pShell->write(key + ": " + val + "\n");
|
||||
}
|
||||
|
||||
void setConfig(Cli::CArgs args) {
|
||||
if (args.size() != 2) {
|
||||
pShell->write("Needs string key and value'\n");
|
||||
return;
|
||||
}
|
||||
auto key = args[0];
|
||||
auto val = args[1];
|
||||
|
||||
auto &store = NvsApi::getInstance();
|
||||
pShell->write(std::string{"Storing value = '"} + val + "'\n");
|
||||
store.setValue(key, val);
|
||||
}
|
||||
|
||||
void setLogServerConfig(Cli::CArgs args) {
|
||||
ESP_LOGI(TAG, "on showLogServerConfig:");
|
||||
auto &store = NvsApi::getInstance();
|
||||
if (args.size() != 1) {
|
||||
auto logger = store.getValue("syslogDst");
|
||||
pShell->write(std::string{"syslog destination = '"} + logger + "'\n");
|
||||
pShell->write("provide argument to change syslog server'\n");
|
||||
return;
|
||||
}
|
||||
pShell->write(std::string{"setting syslog destination to = '"} + args[0] + "'\n");
|
||||
store.setValue("syslogDst", args[0]);
|
||||
}
|
||||
|
||||
void setDynamics(Cli::CArgs args) {
|
||||
ESP_LOGI(TAG, "on setDynamics:");
|
||||
if (args.size() != 2) {
|
||||
shellPtr->write("need float values for velocity and acceleration'\n");
|
||||
pShell->write("need float values for velocity and acceleration'\n");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -86,17 +128,15 @@ void setDynamics(Cli::CArgs args) {
|
||||
|
||||
void triggerMovement(Cli::CArgs args) {
|
||||
ESP_LOGI(TAG, "on triggerMovement:");
|
||||
if (args.size() != 2) {
|
||||
shellPtr->write("need profile index and steps'\n");
|
||||
if (args.size() != 1) {
|
||||
pShell->write("need target position'\n");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
int p = std::stoi(args[0]);
|
||||
int j = std::stoi(args[1]);
|
||||
activeState.profile = p;
|
||||
activeState.jumps = j;
|
||||
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::Step, j, p, activeState.color));
|
||||
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::StepTo, p));
|
||||
} catch (const std::exception &e) {
|
||||
ESP_LOGI(TAG, "Failed to parse %s and %s", args[0].c_str(), args[1].c_str());
|
||||
}
|
||||
@@ -119,22 +159,30 @@ static void telnetSocketTask(int addrFamily) {
|
||||
TelnetServer telnet(addrFamily);
|
||||
telnet.run(
|
||||
[](TcpSession &session) {
|
||||
shellPtr = std::make_unique<Cli>(session, [](std::string n, Cli::CArgs a) { onCommand(n, a); });
|
||||
shellPtr->addBinding(
|
||||
pShell = std::make_unique<Cli>(session, [](std::string n, Cli::CArgs a) { onCommand(n, a); });
|
||||
pShell->addBinding(
|
||||
{"update", "Start OTA-Update-Process",
|
||||
[](Cli::CArgs a) { startUpdate(a); }});
|
||||
shellPtr->addBinding(
|
||||
pShell->addBinding(
|
||||
{"logserver_set", "Set log server config ip",
|
||||
[](Cli::CArgs a) { setLogServerConfig(a); }});
|
||||
shellPtr->addBinding(
|
||||
|
||||
pShell->addBinding(
|
||||
{"getcfg", "Get configuration value for key",
|
||||
[](Cli::CArgs a) { getConfig(a); }});
|
||||
pShell->addBinding(
|
||||
{"setcfg", "Set configuration value by key and value",
|
||||
[](Cli::CArgs a) { setConfig(a); }});
|
||||
|
||||
pShell->addBinding(
|
||||
{"dyn_set", "set dynamics of movement (velocity,accelerations) as floats",
|
||||
[](Cli::CArgs a) { setDynamics(a); }});
|
||||
shellPtr->addBinding(
|
||||
pShell->addBinding(
|
||||
{"move", "start movement (profile-idx,steps) as integers ",
|
||||
[](Cli::CArgs a) { triggerMovement(a); }});
|
||||
},
|
||||
[](TcpSession &session) {
|
||||
shellPtr->process();
|
||||
pShell->process();
|
||||
Syslog::setupTask(session.getOther());
|
||||
});
|
||||
} catch (const std::exception &e) {
|
||||
@@ -160,8 +208,10 @@ static void UniverseCallback(const uint8_t *data, const uint16_t size) {
|
||||
int len = std::min(static_cast<int>(size), DmxChannelCount);
|
||||
ESP_LOGD(TAG, "Got DMX");
|
||||
ESP_LOG_BUFFER_HEXDUMP("DMX", data, len, ESP_LOG_DEBUG);
|
||||
Syslog::logv(Syslog::Severity::Debug, "Got DMX: r%0x g%0x b%0x i%0x trig%i j%i p%i a%i w%i",
|
||||
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8]);
|
||||
Syslog::logv(Syslog::Severity::Debug, "Got DMX: r%0x g%0x b%0x i%0x m%i M%i p%i a%i w%i r%i",
|
||||
/* RGB*/ data[0], data[1], data[2], /*Intens*/ data[3],
|
||||
/* MinMaxJumps */ data[4], data[5], /*profile*/ data[6], /*accel/speed*/ data[7], data[8],
|
||||
/*RestTime*/ data[9]);
|
||||
|
||||
float intens = (float)data[3] / UINT8_MAX;
|
||||
activeState.color = Color{
|
||||
@@ -170,23 +220,44 @@ static void UniverseCallback(const uint8_t *data, const uint16_t size) {
|
||||
scaledRgbChannel(data[2], intens),
|
||||
0};
|
||||
|
||||
static uint8_t lastTrig = 0;
|
||||
uint8_t thisTrig = data[4];
|
||||
if (thisTrig == lastTrig) {
|
||||
return;
|
||||
}
|
||||
lastTrig = thisTrig;
|
||||
uint8_t jumps = data[5];
|
||||
activeState.jumps = jumps;
|
||||
uint8_t profile = data[6];
|
||||
activeState.minJumps = data[4];
|
||||
activeState.maxJumps = data[5];
|
||||
activeState.profile = data[6];
|
||||
|
||||
uint8_t accel = data[7];
|
||||
uint8_t velo = data[8];
|
||||
activeState.accel = (float)accel / 100 + 0.5; // min .5 / max 3
|
||||
activeState.speed = (float)velo / 100 + 0.5; // min .5 / max 3
|
||||
activeState.profile = profile;
|
||||
|
||||
Syslog::logv(Syslog::Severity::Notice, "Command detected step=%i prof=%i accel=%.1f velo=%.1f", jumps, profile, activeState.accel, activeState.speed);
|
||||
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::Step, jumps, profile, activeState.color));
|
||||
activeState.restTime = data[9];
|
||||
}
|
||||
|
||||
void moveToSubs(std::string data) {
|
||||
// Syslog::logv(Syslog::Severity::Notice,
|
||||
// "Running Stepper by %i steps with prof=%i a=%.2f w=%.2f",
|
||||
// delta, profile, activeState.accel, activeState.speed);
|
||||
|
||||
ESP_LOGI(TAG, "MoveTo command '%s'", data.c_str());
|
||||
try {
|
||||
int p = std::stoi(data);
|
||||
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::StepTo, p));
|
||||
auto &client = MqttClient::getInstance();
|
||||
client.publish("watch/inPosition", data + "Picto");
|
||||
} catch (const std::exception &e) {
|
||||
ESP_LOGE(TAG, "Error to process data %s, with ex: %s", data.c_str(), e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void startShuffle(std::string data) {
|
||||
ESP_LOGI(TAG, "StartShuffle command %s", data.c_str());
|
||||
|
||||
try {
|
||||
commandQueue.enqueue(std::make_unique<MoveCommand>(MoveType::Shuffle, 0));
|
||||
auto &client = MqttClient::getInstance();
|
||||
client.publish("watch/changingPosition", "....");
|
||||
} catch (const std::exception &e) {
|
||||
ESP_LOGE(TAG, "Error to process data %s, with ex: %s", data.c_str(), e.what());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,19 +301,11 @@ static void dmxSocketTask(int addrFamily) {
|
||||
ArtnetReceiver artnet(socket);
|
||||
artnet.subscribe(DmxUniverse, UniverseCallback);
|
||||
artnet.parse();
|
||||
// auto msg = std::make_unique<Display::Message>(Display::Message::Type::UpdateInfo, "DMX-IP-Mode");
|
||||
// displayEvents.enqueue(std::move(msg));
|
||||
// deviceState.setNewState(DeviceState::Mode::Remote);
|
||||
// xTaskCreate(valueRefresherTask, "valueRefresher", 4096, nullptr, 5, NULL);
|
||||
// static TaskT<UdpSocket &> dmxSender(dmxSocketSenderTask, socket, "dmxSender", 4096);
|
||||
|
||||
// parse artnet as long socket is active, then restart socket
|
||||
while (socket.isActive()) {
|
||||
ESP_LOGD(TAG, "Waiting for data");
|
||||
artnet.parse();
|
||||
// socket.write(data);
|
||||
}
|
||||
// deviceState.fallbackToLast();
|
||||
}
|
||||
|
||||
} catch (const std::exception &e) {
|
||||
@@ -250,12 +313,7 @@ static void dmxSocketTask(int addrFamily) {
|
||||
}
|
||||
}
|
||||
|
||||
void startServerTasks() {
|
||||
static int ipVersion = AF_INET;
|
||||
static fos::TaskT<int> telnetSocket(telnetSocketTask, ipVersion, "tcpServer", 4096);
|
||||
static fos::TaskT<int> dmxNode(dmxSocketTask, ipVersion, "dmxNode", 4096);
|
||||
// static Task otaUpdate(SystemFunctions::OtaTaskEntry, "otaTask", 8192);
|
||||
}
|
||||
fos::Semaphore etherActive;
|
||||
|
||||
/** Event handler for IP_EVENT_ETH_GOT_IP */
|
||||
static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
|
||||
@@ -269,7 +327,7 @@ static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
|
||||
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
|
||||
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
|
||||
ESP_LOGI(TAG, "~~~~~~~~~~~");
|
||||
startServerTasks();
|
||||
etherActive.give();
|
||||
}
|
||||
|
||||
// Event handler for Ethernet
|
||||
@@ -390,7 +448,6 @@ void LightingTask() {
|
||||
// processors[3].setEffect(Effect::macDemoPulse, &colorSource, 64);
|
||||
// processors[4].setEffect(Effect::macStartFlicker, &colorSource, 80);
|
||||
|
||||
|
||||
while (true) {
|
||||
values[0].fill(0);
|
||||
values[1].fill(0);
|
||||
@@ -409,7 +466,6 @@ void LightingTask() {
|
||||
ledDriver2.sendImage(values[1]);
|
||||
fos::Task::delayMs(40);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void MovementTask() {
|
||||
@@ -442,19 +498,17 @@ void MovementTask() {
|
||||
while (true) {
|
||||
ESP_LOGD(TAG, "Waiting for command");
|
||||
auto cmd = commandQueue.dequeue();
|
||||
if (cmd->type == MoveType::Step) {
|
||||
if (cmd->type == MoveType::StepTo) {
|
||||
if (stepper.isIdle()) {
|
||||
auto delta = cmd->deltaPic;
|
||||
auto profile = cmd->profile;
|
||||
|
||||
// activeState.pictogram = ledPos;
|
||||
// ledRing.send(ledStrip);
|
||||
delta = std::min<uint8_t>(delta, 128);
|
||||
// activeState.pictogram
|
||||
auto delta = std::min<uint8_t>(cmd->pos, 128);
|
||||
Syslog::logv(Syslog::Severity::Notice,
|
||||
"Running Stepper by %i steps with prof=%i a=%.2f w=%.2f",
|
||||
delta, profile, activeState.accel, activeState.speed);
|
||||
delta, activeState.profile, activeState.accel, activeState.speed);
|
||||
stepper.moveBy(delta * std::numbers::pi / 3,
|
||||
profile, activeState.accel, activeState.speed);
|
||||
activeState.profile, activeState.accel, activeState.speed);
|
||||
} else {
|
||||
fos::Task::delayMs(100);
|
||||
}
|
||||
@@ -502,7 +556,40 @@ void app_main(void) {
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
initialize_ethernet();
|
||||
NvsApi::init();
|
||||
// static fos::Task lightingTask(LightingTask, "Lights", 0x4000uL);
|
||||
static fos::Task movementTask(MovementTask, "Motor", 0x4000uL);
|
||||
// static fos::Task movementTask(MovementTask, "Motor", 0x4000uL);
|
||||
// static fos::Task ioTask(IoTest, "IO", 0x4000uL);
|
||||
|
||||
etherActive.take();
|
||||
ESP_LOGI(TAG, "Start net services");
|
||||
|
||||
static int ipVersion = AF_INET;
|
||||
static fos::TaskT<int> telnetSocket(telnetSocketTask, ipVersion, "tcpServer", 4096);
|
||||
static fos::TaskT<int> dmxNode(dmxSocketTask, ipVersion, "dmxNode", 4096);
|
||||
// static Task otaUpdate(SystemFunctions::OtaTaskEntry, "otaTask", 8192);
|
||||
|
||||
auto &store = NvsApi::getInstance();
|
||||
auto logger = store.getValue(ConfigKeys.syslogDest);
|
||||
if (logger != "") {
|
||||
Syslog::setupTask(IpAdr{logger, 0});
|
||||
}
|
||||
|
||||
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("watch/moveTo", [](std::string d) { moveToSubs(d); });
|
||||
client.addSubscription("watch/shuffle", [](std::string d) { startShuffle(d); });
|
||||
client.start();
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
fos::Task::delayMs(5000);
|
||||
// ToDo send
|
||||
Syslog::logv(Syslog::Severity::Notice, "Health state");
|
||||
}
|
||||
}
|
||||
|
||||
+3
-19
@@ -34,36 +34,20 @@
|
||||
#include "Task.hpp"
|
||||
#include "Artnet.h"
|
||||
#include "Processor.hpp"
|
||||
|
||||
#include "nvsFunctions.hpp"
|
||||
#include "mqttClient.hpp"
|
||||
|
||||
// #include "esp_chip_info.h"
|
||||
// #include "esp_flash.h"
|
||||
// #include "esp_log.h"
|
||||
// #include "esp_netif.h"
|
||||
// #include "esp_ota_ops.h"
|
||||
// #include "esp_partition.h"
|
||||
// #include "esp_random.h"
|
||||
// #include "esp_system.h"
|
||||
// #include "esp_vfs_fat.h"
|
||||
// #include "esp_vfs_semihost.h"
|
||||
// #include "esp_wifi.h"
|
||||
// #include "fileSystem.hpp"
|
||||
// #include "freertos/FreeRTOS.h"
|
||||
// #include "freertos/task.h"
|
||||
// #include "gpTimer.hpp"
|
||||
// #include "gpioPort.hpp"
|
||||
// #include "lwip/apps/netbiosns.h"
|
||||
// #include "mdns.h"
|
||||
// #include "mqttClient.hpp"
|
||||
// #include "nlohmann/json.hpp"
|
||||
// #include "nvsFunctions.hpp"
|
||||
// #include "pwmExpanderPca9685.hpp"
|
||||
// #include "rmtPort.hpp"
|
||||
// #include "soc/soc_caps.h"
|
||||
// #include "systemFunctions.hpp"
|
||||
// #include <inttypes.h>
|
||||
// #include <cstring>
|
||||
// #include <sys/param.h>
|
||||
// #include <unistd.h>
|
||||
// #include <cmath>
|
||||
|
||||
// #include <unistd.h>
|
||||
Reference in New Issue
Block a user