mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-09 09:19:51 +03:00
standalone w/ web server for testing
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
#include "emsesp.h"
|
||||
#include "version.h"
|
||||
|
||||
#ifdef EMSESP_DEBUG
|
||||
#ifdef EMSESP_STANDALONE
|
||||
#include "test/test.h"
|
||||
#endif
|
||||
|
||||
@@ -285,7 +285,7 @@ void Console::enter_custom_context(Shell & shell, unsigned int context) {
|
||||
|
||||
// each custom context has the common commands like log, help, exit, su etc
|
||||
void Console::load_standard_commands(unsigned int context) {
|
||||
#ifdef EMSESP_DEBUG
|
||||
#ifdef EMSESP_STANDALONE
|
||||
EMSESPShell::commands->add_command(context,
|
||||
CommandFlags::ADMIN,
|
||||
flash_string_vector{F_(test)},
|
||||
|
||||
@@ -51,10 +51,6 @@ using uuid::log::Level;
|
||||
// clang-format on
|
||||
|
||||
// common words
|
||||
#ifdef EMSESP_DEBUG
|
||||
MAKE_PSTR_WORD(test)
|
||||
#endif
|
||||
|
||||
MAKE_PSTR_WORD(exit)
|
||||
MAKE_PSTR_WORD(help)
|
||||
MAKE_PSTR_WORD(settings)
|
||||
|
||||
@@ -135,10 +135,6 @@ void Solar::publish_values() {
|
||||
doc["energytotal"] = (float)energyTotal_ / 10;
|
||||
}
|
||||
|
||||
#ifdef EMSESP_DEBUG
|
||||
LOG_DEBUG(F("[DEBUG] Performing a solar module publish"));
|
||||
#endif
|
||||
|
||||
Mqtt::publish("sm_data", doc);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,15 +30,17 @@ AsyncWebServer webServer(80);
|
||||
#if defined(ESP32)
|
||||
ESP8266React EMSESP::esp8266React(&webServer, &SPIFFS);
|
||||
EMSESPSettingsService EMSESP::emsespSettingsService = EMSESPSettingsService(&webServer, &SPIFFS, EMSESP::esp8266React.getSecurityManager());
|
||||
#else
|
||||
#elif defined(ESP8266)
|
||||
ESP8266React EMSESP::esp8266React(&webServer, &LittleFS);
|
||||
EMSESPSettingsService EMSESP::emsespSettingsService = EMSESPSettingsService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
|
||||
#elif defined(EMSESP_STANDALONE)
|
||||
FS dummyFS;
|
||||
ESP8266React EMSESP::esp8266React(&webServer, &dummyFS);
|
||||
EMSESPSettingsService EMSESP::emsespSettingsService = EMSESPSettingsService(&webServer, &dummyFS, EMSESP::esp8266React.getSecurityManager());
|
||||
#endif
|
||||
|
||||
EMSESPStatusService EMSESP::emsespStatusService = EMSESPStatusService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
|
||||
EMSESPDevicesService EMSESP::emsespDevicesService = EMSESPDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
|
||||
EMSESPStatusService EMSESP::emsespStatusService = EMSESPStatusService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
EMSESPDevicesService EMSESP::emsespDevicesService = EMSESPDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
EMSESPScanDevicesService EMSESP::emsespScanDevicesService = EMSESPScanDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
|
||||
std::vector<std::unique_ptr<EMSdevice>> EMSESP::emsdevices; // array of all the detected EMS devices
|
||||
@@ -622,7 +624,7 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) {
|
||||
Roomctrl::check((data[1] ^ 0x80 ^ rxservice_.ems_mask()), data);
|
||||
#ifdef EMSESP_DEBUG
|
||||
// get_uptime is only updated once per loop, does not give the right time
|
||||
LOG_DEBUG(F("[DEBUG] Echo after %d ms: %s"), ::millis() - rx_time_, Helpers::data_to_hex(data, length).c_str());
|
||||
LOG_TRACE(F("[DEBUG] Echo after %d ms: %s"), ::millis() - rx_time_, Helpers::data_to_hex(data, length).c_str());
|
||||
#endif
|
||||
return; // it's an echo
|
||||
}
|
||||
@@ -720,7 +722,7 @@ void EMSESP::start() {
|
||||
#endif
|
||||
|
||||
esp8266React.begin(); // starts wifi, ap, ota, security, mqtt services
|
||||
emsespSettingsService.begin(); // load settings
|
||||
emsespSettingsService.begin(); // load EMS-ESP specific settings from LittleFS
|
||||
console_.start(); // telnet and serial console
|
||||
system_.start(); // starts syslog, uart, sets version, initializes LED. Requires pre-loaded settings.
|
||||
mqtt_.start(EMSESP::esp8266React.getMqttClient()); // mqtt init
|
||||
@@ -732,17 +734,14 @@ void EMSESP::start() {
|
||||
|
||||
// loop de loop
|
||||
void EMSESP::loop() {
|
||||
#ifndef EMSESP_STANDALONE
|
||||
esp8266React.loop();
|
||||
#endif
|
||||
|
||||
system_.loop(); // does LED and checks system health, and syslog service
|
||||
mqtt_.loop(); // starts mqtt, and sends out anything in the queue
|
||||
rxservice_.loop(); // process what ever is in the rx queue
|
||||
txservice_.loop(); // check that the Tx is all ok
|
||||
shower_.loop(); // check for shower on/off
|
||||
sensors_.loop(); // this will also send out via MQTT
|
||||
console_.loop(); // telnet/serial console
|
||||
esp8266React.loop(); // web
|
||||
system_.loop(); // does LED and checks system health, and syslog service
|
||||
mqtt_.loop(); // starts mqtt, and sends out anything in the queue
|
||||
rxservice_.loop(); // process what ever is in the rx queue
|
||||
txservice_.loop(); // check that the Tx is all ok
|
||||
shower_.loop(); // check for shower on/off
|
||||
sensors_.loop(); // this will also send out via MQTT
|
||||
console_.loop(); // telnet/serial console
|
||||
|
||||
// force a query on the EMS devices to fetch latest data at a set interval (1 min)
|
||||
if ((uuid::get_uptime() - last_fetch_ > EMS_FETCH_FREQUENCY)) {
|
||||
@@ -753,7 +752,6 @@ void EMSESP::loop() {
|
||||
#if defined(ESP32)
|
||||
delay(1);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -64,7 +64,7 @@ class EMSESP {
|
||||
|
||||
static void publish_all_values();
|
||||
|
||||
#ifdef EMSESP_DEBUG
|
||||
#ifdef EMSESP_STANDALONE
|
||||
static void run_test(uuid::console::Shell & shell, const std::string & command); // only for testing
|
||||
static void dummy_mqtt_commands(const char * message);
|
||||
static void rx_telegram(const std::vector<uint8_t> & data);
|
||||
|
||||
29
src/mqtt.cpp
29
src/mqtt.cpp
@@ -27,9 +27,7 @@ MAKE_PSTR(logger_name, "mqtt")
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
AsyncMqttClient * Mqtt::mqttClient_;
|
||||
#endif
|
||||
|
||||
// static parameters we make global
|
||||
std::string Mqtt::hostname_;
|
||||
@@ -90,10 +88,6 @@ void Mqtt::subscribe(const uint8_t device_id, const std::string & topic, mqtt_fu
|
||||
|
||||
if (!exists) {
|
||||
mqtt_functions_.emplace_back(device_id, std::move(full_topic), cb); // register a call back function for a specific telegram type
|
||||
} else {
|
||||
#ifdef EMSESP_DEBUG
|
||||
LOG_DEBUG(F("[DEBUG] Resubscribing to topic %s"), full_topic);
|
||||
#endif
|
||||
}
|
||||
|
||||
queue_subscribe_message(topic); // add subscription to queue
|
||||
@@ -120,11 +114,7 @@ void Mqtt::subscribe(const std::string & topic, mqtt_function_p cb) {
|
||||
// sends out top item on publish queue
|
||||
void Mqtt::loop() {
|
||||
// exit if MQTT is not enabled or if there is no WIFI
|
||||
#ifndef EMSESP_STANDALONE
|
||||
if (!connected()) {
|
||||
#else
|
||||
if (false) {
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -201,10 +191,7 @@ void Mqtt::on_message(char * topic, char * payload, size_t len) {
|
||||
// convert payload to a null-terminated char string
|
||||
char message[len + 2];
|
||||
strlcpy(message, payload, len + 1);
|
||||
|
||||
#ifdef EMSESP_DEBUG
|
||||
LOG_DEBUG(F("[DEBUG] Received %s => %s (length %d)"), topic, message, len);
|
||||
#endif
|
||||
|
||||
/*
|
||||
// strip out everything until the last /
|
||||
@@ -296,14 +283,13 @@ void Mqtt::start(AsyncMqttClient * mqttClient) {
|
||||
mqtt_qos_ = mqttSettings.mqtt_qos;
|
||||
});
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
|
||||
mqttClient_->onConnect([this](bool sessionPresent) { on_connect(); });
|
||||
mqttClient_->setWill(make_topic(will_topic_, "status"), 1, true, "offline"); // with qos 1, retain true
|
||||
mqttClient_->onMessage([this](char * topic, char * payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
on_message(topic, payload, len);
|
||||
mqttClient_->onPublish([this](uint16_t packetId) { on_publish(packetId); });
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
void Mqtt::set_publish_time(uint16_t publish_time) {
|
||||
@@ -320,7 +306,9 @@ void Mqtt::on_connect() {
|
||||
StaticJsonDocument<90> doc;
|
||||
doc["event"] = "start";
|
||||
doc["version"] = EMSESP_APP_VERSION;
|
||||
doc["ip"] = WiFi.localIP().toString();
|
||||
#ifndef EMSESP_STANDALONE
|
||||
doc["ip"] = WiFi.localIP().toString();
|
||||
#endif
|
||||
publish("info", doc, false); // send with retain off
|
||||
|
||||
publish("status", "online", true); // say we're alive to the Last Will topic, with retain on
|
||||
@@ -416,11 +404,7 @@ void Mqtt::process_queue() {
|
||||
// if we're subscribing...
|
||||
if (message->operation == Operation::SUBSCRIBE) {
|
||||
LOG_DEBUG(F("Subscribing to topic: %s"), full_topic);
|
||||
#ifndef EMSESP_STANDALONE
|
||||
uint16_t packet_id = mqttClient_->subscribe(full_topic, mqtt_qos_);
|
||||
#else
|
||||
uint16_t packet_id = 1;
|
||||
#endif
|
||||
if (!packet_id) {
|
||||
LOG_DEBUG(F("Error subscribing to %s, error %d"), full_topic, packet_id);
|
||||
}
|
||||
@@ -437,12 +421,7 @@ void Mqtt::process_queue() {
|
||||
}
|
||||
|
||||
// else try and publish it
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
uint16_t packet_id = mqttClient_->publish(full_topic, mqtt_qos_, message->retain, message->payload.c_str(), message->payload.size(), false, mqtt_message.id_);
|
||||
#else
|
||||
uint16_t packet_id = 1;
|
||||
#endif
|
||||
LOG_DEBUG(F("Publishing topic %s (#%02d, attempt #%d, pid %d)"), full_topic, mqtt_message.id_, mqtt_message.retry_count_ + 1, packet_id);
|
||||
|
||||
if (packet_id == 0) {
|
||||
|
||||
12
src/mqtt.h
12
src/mqtt.h
@@ -27,9 +27,7 @@
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
#include <AsyncMqttClient.h>
|
||||
#endif
|
||||
|
||||
#include "helpers.h"
|
||||
#include "system.h"
|
||||
@@ -86,21 +84,13 @@ class Mqtt {
|
||||
static void on_connect();
|
||||
|
||||
void disconnect() {
|
||||
#ifdef EMSESP_STANDALONE
|
||||
return;
|
||||
#else
|
||||
mqttClient_->disconnect();
|
||||
#endif
|
||||
}
|
||||
|
||||
void incoming(char * topic, char * payload); // for testing
|
||||
|
||||
static bool connected() {
|
||||
#ifdef EMSESP_STANDALONE
|
||||
return true;
|
||||
#else
|
||||
return mqttClient_->connected();
|
||||
#endif
|
||||
}
|
||||
|
||||
static uint32_t publish_fails() {
|
||||
@@ -126,9 +116,7 @@ class Mqtt {
|
||||
};
|
||||
static std::deque<QueuedMqttMessage> mqtt_messages_;
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
static AsyncMqttClient * mqttClient_;
|
||||
#endif
|
||||
|
||||
static constexpr size_t MAX_MQTT_MESSAGES = 50;
|
||||
static size_t maximum_mqtt_messages_;
|
||||
|
||||
@@ -75,7 +75,6 @@ void System::mqtt_commands(const char * message) {
|
||||
#endif
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
|
||||
if (doc["D0"] != nullptr) {
|
||||
const int8_t set = doc["D0"];
|
||||
pinMode(d0_, OUTPUT);
|
||||
@@ -119,7 +118,6 @@ void System::mqtt_commands(const char * message) {
|
||||
}
|
||||
LOG_INFO(F("Port D3 set to %d"), set);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
const char * command = doc["cmd"];
|
||||
@@ -155,11 +153,10 @@ void System::restart() {
|
||||
// format fs
|
||||
// format the FS. Wipes everything.
|
||||
void System::format(uuid::console::Shell & shell) {
|
||||
auto msg = F("Resetting all settings to defaults");
|
||||
auto msg = F("Formatting file system. This will also reset all settings to their defaults");
|
||||
shell.logger().warning(msg);
|
||||
shell.flush();
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
EMSuart::stop();
|
||||
|
||||
#if defined(ESP8266)
|
||||
@@ -169,8 +166,6 @@ void System::format(uuid::console::Shell & shell) {
|
||||
#endif
|
||||
|
||||
System::restart();
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
// return free heap mem as a percentage
|
||||
@@ -226,10 +221,9 @@ void System::start() {
|
||||
|
||||
// fetch settings
|
||||
EMSESP::emsespSettingsService.read([&](EMSESPSettings & settings) { tx_mode_ = settings.tx_mode; });
|
||||
|
||||
EMSESP::esp8266React.getMqttSettingsService()->read([&](MqttSettings & settings) { system_heartbeat_ = settings.system_heartbeat; });
|
||||
|
||||
syslog_init();
|
||||
syslog_init(); // init SysLog
|
||||
|
||||
#if defined(ESP32)
|
||||
LOG_INFO(F("System booted (EMS-ESP version %s ESP32)"), EMSESP_APP_VERSION);
|
||||
|
||||
@@ -34,6 +34,8 @@
|
||||
#include "mqtt.h"
|
||||
#include "emsesp.h"
|
||||
|
||||
MAKE_PSTR_WORD(test)
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
class Test {
|
||||
|
||||
Reference in New Issue
Block a user