Application develop start

This commit is contained in:
Emanuele Trabattoni
2025-07-24 22:46:31 +02:00
parent bea42c9a36
commit bb0832ad4f
12 changed files with 237 additions and 123 deletions

View File

@@ -1,36 +1,23 @@
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
#include <Arduino.h>
#include <DebugLog.h>
#include <DebugLogEnable.h>
#include <config.h>
#include <PCF85063_Driver.h>
#include <R4DCB08_Driver.h>
#include <S50140_Driver.h>
#include <BUZZER_Driver.h>
#include <LED_Driver.h>
#include <ETH_Driver.h>
#include <digitalIO.h>
#include <commands.h>
#include <mqtt.h>
#include <devices.h>
#include "utils.h"
/////////////// GLOBALS ///////////////
Config &conf = Config::getInstance();
/////////////// GLOBALS ///////////////
void testAction(const ArduinoJson::JsonDocument &doc)
{
std::string message;
ArduinoJson::serializeJsonPretty(doc, message);
LOG_INFO("Received on testAction\n", message.c_str());
}
void setup()
{
Serial.begin(9600);
LOG_ATTACH_SERIAL(Serial);
LOG_SET_LEVEL(DebugLogLevel::LVL_INFO);
conf.init(); // read the configuration from internal flash
}
@@ -54,44 +41,54 @@ void loop()
auto io = digitalIO(i2c, bus, {conf.m_modbusRelayAddr});
// Initialize temperature sensors
sensors = tmp.getNum();
tmp.setCorrection(conf.m_tempCorrectionValues);
LOG_INFO("Temperature sensors connected ->", sensors);
// Create device structure to pass all devices in the callbacks as needed
devices_t devices(rtc, tmp, seneca, buzzer, led, io);
//////////////// DEVICES ////////////////
//////////////// NETWORK ////////////////
auto mqtt = MQTTwrapper();
//////////////// NETWORK ////////////////
std::function<void(const ArduinoJson::JsonDocument &)> mycallback =
[&io](const ArduinoJson::JsonDocument &doc)
//////////////// MQTT ////////////////
/////////////// CALLBACK //////////////
std::function<void(const ArduinoJson::JsonDocument &)> commandsCallback =
[&mqtt, &devices](const ArduinoJson::JsonDocument &doc)
{
std::vector<bool> v1 = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
std::vector<bool> v2 = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
std::vector<bool> v0(io.getOutNum(), 0);
LOG_INFO("SET Digital Outputs V1: ", printBoolVec(v1).c_str());
io.digitalOutWritePort(v1);
delay(100);
LOG_INFO("GET Digital Outputs V1: ", printBoolVec(io.digitalOutReadPort()).c_str());
delay(2000);
LOG_INFO("SET Digital Outputs V2: ", printBoolVec(v2).c_str());
io.digitalOutWritePort(v2);
delay(100);
LOG_INFO("GET Digital Outputs V2: ", printBoolVec(io.digitalOutReadPort()).c_str());
delay(2000);
LOG_INFO("GET Digital Inputs: ", printBoolVec(io.digitalInReadPort()).c_str());
io.digitalOutWritePort(v0);
if (!doc["cmd"].is<std::string>() || !doc["params"].is<ArduinoJson::JsonObject>())
{
LOG_ERROR("Invalid Json Command");
return;
}
const std::string cmd = doc["cmd"].as<std::string>();
ArduinoJson::JsonDocument params = doc["params"];
if (commands::commandMap.contains(cmd))
{ // call command from command map in this same thread (the MQTT thread)
LOG_INFO("Executing command", cmd.c_str());
auto answer = std::move(commands::commandMap.at(cmd)(devices, params));
if (answer.isNull())
return;
mqtt.publish(conf.m_mqttPublish["answers"], answer);
}
else
{
LOG_ERROR("Unknown command", cmd.c_str());
}
};
//////////////// NETWORK ////////////////
/////////////// CALLBACK ////////////////
Network.onEvent(
[&eth, &rtc, &mqtt, &buzzer, &led, &mycallback](arduino_event_id_t event, arduino_event_info_t info) -> void
[&](arduino_event_id_t event, arduino_event_info_t info) -> void
{
eth.onEvent(event, info); // Arduino Ethernet event handler
if (!eth.isConnected())
{
led.setColor(led.COLOR_RED);
return;
}
// Get RTC time at ethernet connection
time_t ntpTime;
uint8_t timeRetries(0);
@@ -100,20 +97,20 @@ void loop()
{
if (eth.getNtpTime(ntpTime) && rtc.setDatetime(drivers::PCF85063::fromEpoch(ntpTime)))
{
// buzzer.beep(250, NOTE_F);
led.setColor({255, 255, 0});
buzzer.beep(250, NOTE_A);
led.setColor(led.COLOR_ORANGE);
const drivers::PCF85063::datetime_t dt(drivers::PCF85063::fromEpoch(ntpTime));
LOG_INFO("NTP Time: ", drivers::PCF85063::datetime2str(dt).c_str());
delay(100);
LOG_INFO("NTP Time Update: ", drivers::PCF85063::datetime2str(dt).c_str());
break;
}
break;
delay(100);
}
while (mqttRetries++ < conf.m_mqttRetries)
{
if (mqtt.connect())
{
mqtt.subscribe("test/esp32-in", testAction);
mqtt.subscribe("test/esp32-functional", mycallback);
led.setColor(led.COLOR_GREEN);
mqtt.subscribe(conf.m_mqttSubscribe["commands"], commandsCallback);
break;
}
delay(100);
@@ -126,81 +123,57 @@ void loop()
while (true)
{
LOG_INFO("[", k++, "] Loop");
const std::string timeStr(rtc.getTimeStr());
LOG_INFO("Current Datetime", timeStr.c_str());
ArduinoJson::JsonDocument ts;
ts["loopIterator"] = k;
ts["currentTime"] = timeStr;
mqtt.publish("test/esp32-out", ts);
LOG_INFO("[", k++, "] Loop - Current Datetime", timeStr.c_str());
uint8_t i(0);
for (auto v : tmp.getTempAll())
{
LOG_INFO("Temperature channel", i++, "->", v);
ArduinoJson::JsonDocument poll;
poll["cmd"] = "POLL";
auto params = poll["params"].to<ArduinoJson::JsonObject>();
params["time"] = timeStr;
params["number"] = k;
mqtt.publish(conf.m_mqttPublish["answers"], poll);
};
{
ArduinoJson::JsonDocument ti;
auto tempinfo = tmp.getTempAll();
ti["solar"] = tempinfo.at(0);
ti["acs"] = tempinfo.at(0);
ti["heating"] = tempinfo.at(0);
mqtt.publish(conf.m_mqttPublish["temperatures"], ti);
};
{
ArduinoJson::JsonDocument pi;
auto powerinfo = seneca.getAll();
pi["power"] = powerinfo.pAct;
pi["current"] = powerinfo.a;
pi["energy"] = powerinfo.whPar;
pi["voltage"] = powerinfo.v;
mqtt.publish(conf.m_mqttPublish["heatpump"], pi);
}
LOG_INFO("Read Red");
if (io.digitalInRead(0)) // rosso
if (io.digitalInRead(0)) // ROSSO - Config Reset
{
std::vector<bool> v1 = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
std::vector<bool> v2 = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
std::vector<bool> v0(16, 0);
LOG_INFO("SET Digital Outputs V1: ", printBoolVec(v1).c_str());
io.digitalOutWritePort(v1);
LOG_INFO("GET Digital Outputs V1: ", printBoolVec(io.digitalOutReadPort()).c_str());
delay(2000);
LOG_INFO("SET Digital Outputs V2: ", printBoolVec(v2).c_str());
io.digitalOutWritePort(v2);
LOG_INFO("GET Digital Outputs V2: ", printBoolVec(io.digitalOutReadPort()).c_str());
delay(2000);
LOG_INFO("GET Digital Inputs: ", printBoolVec(io.digitalInReadPort()).c_str());
delay(2000);
io.digitalOutWritePort(v0);
delay(2000);
}
LOG_INFO("Read Blue");
if (io.digitalInRead(8)) // blu
{
if (!buzzing)
{
buzzing = true;
buzzer.beepRepeat(100, 1000, NOTE_C);
led.blinkColor(100, 500, {255, 0, 255});
}
else
{
buzzer.beepStop();
led.blinkAlternate(500, 500, {255, 255, 0}, {0, 255, 255});
buzzing = false;
}
LOG_INFO("Buzzing -> ", buzzing ? "True" : "False");
}
LOG_INFO("Read Green");
if (io.digitalInRead(9))
{ // verde
LOG_WARN("Config RESET!");
buzzer.beep(450, NOTE_E);
delay(500);
conf.resetConfig();
}
LOG_INFO("Read Yellow");
if (io.digitalInRead(10))
{ // giallo
if (io.digitalInRead(1)) // GIALLO - Restart
{
LOG_WARN("RESTART!");
buzzer.beep(450, NOTE_D);
delay(100);
esp_restart();
}
drivers::S50140::powerinfo_t pinfo = seneca.getAll();
LOG_INFO("Power Info ==> V:", pinfo.v, "- A:", pinfo.a, "- W:", pinfo.pAct, "- F:", pinfo.f, "- Wh_t:", pinfo.whTot, "- Wh_p:", pinfo.whPar);
delay(conf.m_globalLoopDelay);
delay(conf.m_globalLoopDelay); // to avoid too fast loop
}
////////////////////////////////////////
///////// MAIN LOOP INSIDE LOOP ////////
////////////////////////////////////////