#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG #include #include #include #include #include #include #include #include #include #include #include #include #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); conf.init(); // read the configuration from internal flash } void loop() { uint16_t k(0); uint8_t sensors(0); bool buzzing(false); //////////////// DEVICES //////////////// // Declared here to keep devices local to the main loop otherwise the kernel crashes // auto i2c = drivers::I2C(); auto bus = drivers::MODBUS(9600, SERIAL_8N1); auto rtc = drivers::PCF85063(i2c, PCF85063_ADDRESS); auto eth = drivers::Ethernet(conf.m_ethHostname); auto tmp = drivers::R4DCB08(bus, conf.m_modbusTemperatureAddr); auto seneca = drivers::S50140(bus, conf.m_modbusSenecaAddr); auto buzzer = drivers::Buzzer(); auto led = drivers::Led(); delay(500); auto io = digitalIO(i2c, bus, {conf.m_modbusRelayAddr}); // Initialize temperature sensors sensors = tmp.getNum(); LOG_INFO("Temperature sensors connected ->", sensors); //////////////// DEVICES //////////////// //////////////// NETWORK //////////////// auto mqtt = MQTTwrapper(); //////////////// NETWORK //////////////// std::function mycallback = [&io](const ArduinoJson::JsonDocument &doc) { std::vector v1 = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}; std::vector v2 = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}; std::vector 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); }; //////////////// NETWORK //////////////// /////////////// CALLBACK //////////////// Network.onEvent( [ð, &rtc, &mqtt, &buzzer, &led, &mycallback](arduino_event_id_t event, arduino_event_info_t info) -> void { eth.onEvent(event, info); // Arduino Ethernet event handler if (!eth.isConnected()) return; // Get RTC time at ethernet connection time_t ntpTime; uint8_t timeRetries(0); uint8_t mqttRetries(0); while (timeRetries++ < conf.m_ntpRetries) { if (eth.getNtpTime(ntpTime) && rtc.setDatetime(drivers::PCF85063::fromEpoch(ntpTime))) { // buzzer.beep(250, NOTE_F); led.setColor({255, 255, 0}); const drivers::PCF85063::datetime_t dt(drivers::PCF85063::fromEpoch(ntpTime)); LOG_INFO("NTP Time: ", drivers::PCF85063::datetime2str(dt).c_str()); delay(100); } break; } while (mqttRetries++ < conf.m_mqttRetries) { if (mqtt.connect()) { mqtt.subscribe("test/esp32-in", testAction); mqtt.subscribe("test/esp32-functional", mycallback); break; } delay(100); } }); //////////////////////////////////////// ///////// MAIN LOOP INSIDE 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); uint8_t i(0); for (auto v : tmp.getTempAll()) { LOG_INFO("Temperature channel", i++, "->", v); } LOG_INFO("Read Red"); if (io.digitalInRead(0)) // rosso { std::vector v1 = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}; std::vector v2 = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}; std::vector 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 conf.resetConfig(); } LOG_INFO("Read Yellow"); if (io.digitalInRead(10)) { // giallo 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); } //////////////////////////////////////// ///////// MAIN LOOP INSIDE LOOP //////// //////////////////////////////////////// }