Added Buzzer and RGB led drivers
This commit is contained in:
96
src/main.cpp
96
src/main.cpp
@@ -8,6 +8,8 @@
|
||||
#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>
|
||||
@@ -50,6 +52,7 @@ void loop()
|
||||
uint16_t k(0);
|
||||
uint8_t ethRetries(0);
|
||||
uint8_t sensors(0);
|
||||
bool buzzing(false);
|
||||
|
||||
//////////////// DEVICES ////////////////
|
||||
// Declared here to keep devices local to the main loop otherwise the kernel crashes //
|
||||
@@ -62,33 +65,56 @@ void loop()
|
||||
auto io = digitalIO(i2c, bus, {relayBoardAddr});
|
||||
delay(100);
|
||||
auto seneca = drivers::S50140(bus, senecaMeterAddr);
|
||||
|
||||
Network.onEvent([ð](arduino_event_id_t event, arduino_event_info_t info)
|
||||
{ eth.onEvent(event, info); });
|
||||
|
||||
while (!eth.isConnected() && ethRetries++ < 5)
|
||||
{
|
||||
LOG_WARN("Waiting for Ethernet retry", ethRetries);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
auto buzzer = drivers::Buzzer();
|
||||
auto led = drivers::Led();
|
||||
//////////////// DEVICES ////////////////
|
||||
// Initialize temperature sensors
|
||||
sensors = tmp.getNum();
|
||||
LOG_INFO("Temperature sensors connected ->", sensors);
|
||||
|
||||
// Get RTC time at startup
|
||||
time_t ntpTime;
|
||||
drivers::PCF85063::datetime_t dt;
|
||||
|
||||
// MQTT Test
|
||||
//////////////// NETWORK ////////////////
|
||||
// MQTT Test //
|
||||
NetworkClient tcp;
|
||||
PubSubClient mqtt(tcp);
|
||||
|
||||
mqtt.setServer("10.0.2.249", 1883);
|
||||
mqtt.setCallback(callback);
|
||||
mqtt.connect("esp32-client");
|
||||
mqtt.subscribe("test/esp32-in");
|
||||
xTaskCreatePinnedToCore(myTask, "mqttLoop", 4096, &mqtt, 2, NULL, 1);
|
||||
//////////////// NETWORK ////////////////
|
||||
|
||||
//////////////// NETWORK ////////////////
|
||||
/////////////// CALLBACK ////////////////
|
||||
Network.onEvent(
|
||||
[ð, &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())
|
||||
return;
|
||||
// Get RTC time at ethernet connection
|
||||
time_t ntpTime;
|
||||
uint8_t timeRetries(0);
|
||||
uint8_t mqttRetries(0);
|
||||
while (timeRetries++ < 5)
|
||||
{
|
||||
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++ < 5)
|
||||
{
|
||||
if (!mqtt.connected() && mqtt.connect("esp32-client"))
|
||||
{
|
||||
mqtt.subscribe("test/esp32-in");
|
||||
xTaskCreatePinnedToCore(myTask, "mqttLoop", 4096, &mqtt, 2, NULL, 1);
|
||||
break;
|
||||
}
|
||||
delay(100);
|
||||
}
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
///////// MAIN LOOP INSIDE LOOP ////////
|
||||
@@ -98,11 +124,9 @@ void loop()
|
||||
{
|
||||
LOG_INFO("[", k++, "] Loop");
|
||||
|
||||
eth.getNtpTime(ntpTime);
|
||||
dt = drivers::PCF85063::fromEpoch(ntpTime);
|
||||
LOG_INFO("Network Datetime", rtc.datetime2str(dt).c_str());
|
||||
LOG_INFO("Current Datetime", rtc.getTimeStr().c_str());
|
||||
mqtt.publish("test/esp32-out", ("[" + std::to_string(k) + "] -> " + rtc.getTimeStr()).c_str());
|
||||
const std::string timeStr(rtc.getTimeStr());
|
||||
LOG_INFO("Current Datetime", timeStr.c_str());
|
||||
mqtt.publish("test/esp32-out", ("[" + std::to_string(k) + "] -> " + timeStr).c_str());
|
||||
|
||||
uint8_t i(0);
|
||||
for (auto v : tmp.getTempAll())
|
||||
@@ -116,12 +140,7 @@ void loop()
|
||||
LOG_INFO("Temperature correction channel", i++, "tc", v);
|
||||
}
|
||||
|
||||
for (auto j(0); j < io.getOutNum(); j++)
|
||||
{
|
||||
LOG_INFO("Input", j, io.digitalIORead(j) ? "True" : "False");
|
||||
delay(50);
|
||||
}
|
||||
|
||||
delay(100);
|
||||
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);
|
||||
|
||||
@@ -133,6 +152,23 @@ void loop()
|
||||
LOG_INFO("Counter Status: ", countStat);
|
||||
seneca.resetPartialCounters();
|
||||
}
|
||||
delay(100);
|
||||
if (io.digitalIORead(8))
|
||||
{
|
||||
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");
|
||||
}
|
||||
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user