Implemented config file and save to memory using ffat
This commit is contained in:
42
src/main.cpp
42
src/main.cpp
@@ -2,9 +2,11 @@
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <DebugLogEnable.h>
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <PCF85063_Driver.h>
|
||||
#include <R4DCB08_Driver.h>
|
||||
#include <S50140_Driver.h>
|
||||
@@ -36,7 +38,6 @@ void myTask(void *mqtt)
|
||||
vTaskDelete(NULL); // delete the current task
|
||||
};
|
||||
|
||||
/////////////// GLOBALS ///////////////
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
@@ -45,12 +46,11 @@ void setup()
|
||||
|
||||
void loop()
|
||||
{
|
||||
const uint8_t tempBoardAddr(0xAA);
|
||||
const uint8_t relayBoardAddr(0x01);
|
||||
const uint8_t senecaMeterAddr(0xBB);
|
||||
/////////////// GLOBALS ///////////////
|
||||
Config conf = Config();
|
||||
/////////////// GLOBALS ///////////////
|
||||
const uint8_t baseRegister(0x00);
|
||||
uint16_t k(0);
|
||||
uint8_t ethRetries(0);
|
||||
uint8_t sensors(0);
|
||||
bool buzzing(false);
|
||||
|
||||
@@ -59,12 +59,12 @@ void loop()
|
||||
auto i2c = drivers::I2C();
|
||||
auto bus = drivers::MODBUS(9600, SERIAL_8N1);
|
||||
auto rtc = drivers::PCF85063(i2c, PCF85063_ADDRESS);
|
||||
auto eth = drivers::Ethernet("waveshare-test");
|
||||
auto tmp = drivers::R4DCB08(bus, tempBoardAddr);
|
||||
auto eth = drivers::Ethernet(conf.m_ethHostname);
|
||||
auto tmp = drivers::R4DCB08(bus, conf.m_modbusTemperatureAddr);
|
||||
delay(100);
|
||||
auto io = digitalIO(i2c, bus, {relayBoardAddr});
|
||||
auto io = digitalIO(i2c, bus, {conf.m_modbusRelayAddr});
|
||||
delay(100);
|
||||
auto seneca = drivers::S50140(bus, senecaMeterAddr);
|
||||
auto seneca = drivers::S50140(bus, conf.m_modbusSenecaAddr);
|
||||
auto buzzer = drivers::Buzzer();
|
||||
auto led = drivers::Led();
|
||||
//////////////// DEVICES ////////////////
|
||||
@@ -76,14 +76,14 @@ void loop()
|
||||
// MQTT Test //
|
||||
NetworkClient tcp;
|
||||
PubSubClient mqtt(tcp);
|
||||
mqtt.setServer("10.0.2.249", 1883);
|
||||
mqtt.setServer(conf.m_mqttHost.c_str(), conf.m_mqttPort);
|
||||
mqtt.setCallback(callback);
|
||||
//////////////// NETWORK ////////////////
|
||||
|
||||
//////////////// NETWORK ////////////////
|
||||
/////////////// CALLBACK ////////////////
|
||||
Network.onEvent(
|
||||
[ð, &rtc, &mqtt, &buzzer, &led](arduino_event_id_t event, arduino_event_info_t info) -> void
|
||||
[&conf, ð, &rtc, &mqtt, &buzzer, &led](arduino_event_id_t event, arduino_event_info_t info) -> void
|
||||
{
|
||||
eth.onEvent(event, info); // Arduino Ethernet event handler
|
||||
if (!eth.isConnected())
|
||||
@@ -92,7 +92,7 @@ void loop()
|
||||
time_t ntpTime;
|
||||
uint8_t timeRetries(0);
|
||||
uint8_t mqttRetries(0);
|
||||
while (timeRetries++ < 5)
|
||||
while (timeRetries++ < conf.m_ntpRetries)
|
||||
{
|
||||
if (eth.getNtpTime(ntpTime) && rtc.setDatetime(drivers::PCF85063::fromEpoch(ntpTime)))
|
||||
{
|
||||
@@ -104,9 +104,9 @@ void loop()
|
||||
}
|
||||
break;
|
||||
}
|
||||
while (mqttRetries++ < 5)
|
||||
while (mqttRetries++ < conf.m_mqttRetries)
|
||||
{
|
||||
if (!mqtt.connected() && mqtt.connect("esp32-client"))
|
||||
if (!mqtt.connected() && mqtt.connect(conf.m_mqttClientName.c_str()))
|
||||
{
|
||||
mqtt.subscribe("test/esp32-in");
|
||||
xTaskCreatePinnedToCore(myTask, "mqttLoop", 4096, &mqtt, 2, NULL, 1);
|
||||
@@ -144,7 +144,7 @@ void loop()
|
||||
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);
|
||||
|
||||
if (io.digitalIORead(0))
|
||||
if (io.digitalIORead(0)) // rosso
|
||||
{
|
||||
uint8_t regset(seneca.getRegset());
|
||||
uint16_t countStat(seneca.getCounterStatus());
|
||||
@@ -153,7 +153,7 @@ void loop()
|
||||
seneca.resetPartialCounters();
|
||||
}
|
||||
delay(100);
|
||||
if (io.digitalIORead(8))
|
||||
if (io.digitalIORead(8)) // blu
|
||||
{
|
||||
if (!buzzing)
|
||||
{
|
||||
@@ -170,7 +170,15 @@ void loop()
|
||||
LOG_INFO("Buzzing -> ", buzzing ? "True" : "False");
|
||||
}
|
||||
|
||||
delay(2000);
|
||||
if(io.digitalIORead(9)) { // verde
|
||||
conf.resetConfig();
|
||||
}
|
||||
|
||||
if(io.digitalIORead(10)) { // giallo
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
delay(conf.m_globalLoopDelay);
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user