/**
* @file ToggableLedDevice.ino
* @brief Example sketch demonstrating the Modest IoT Nano-framework (C++ Edition).
*
* Implements an interrupt-driven LED toggle using the ToggableLedDevice class, showcasing the framework’s
* event-driven and CQRS-inspired design. Requires the framework from
* https://github.com/avelasquezn/Modest-IoT-Nano-framework-Cpp.
*
* @author Angel Velasquez
* @date March 23, 2025
* @version 0.1
*/
/*
* This file is part of the connected-toggable-led-device-cpp project.
* Copyright (c) 2025 Angel Velasquez
*
* Licensed under the Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0).
* You may use, copy, and distribute this software in its original, unmodified form, provided
* you give appropriate credit to the original author (Angel Velasquez) and include this notice.
* Modifications, adaptations, or derivative works are not permitted.
*
* Full license text: https://creativecommons.org/licenses/by-nd/4.0/legalcode
*/
#include "ModestIoT.h"
#include "ConnectedToggableLedDevice.h"
#include <time.h>
#define LED_INITIAL_STATE false
#define DEBOUNCE_DELAY_MS 200
#define HEALTH_INTERVAL_US 60000000 // 60s in microseconds
WiFiDriver wifi("your-ssid", "your-password");
RESTClient rest("https://connected-toggable-led-edge-server.free.beeceptor.com", &wifi);
ConnectedToggableLedEdgeServerFacade facade(rest);
ConnectedToggableLedDevice device(facade, wifi.getMacAddress());
hw_timer_t* healthTimer = nullptr;
String getTimestamp() {
time_t now;
time(&now);
char buf[25];
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
return String(buf);
}
void IRAM_ATTR triggerButtonPressedEvent() {
static unsigned long lastPress = 0;
unsigned long now = millis();
if (now - lastPress > DEBOUNCE_DELAY_MS) {
device.triggerButtonEvent(Button::BUTTON_PRESSED_EVENT);
lastPress = now;
}
}
void IRAM_ATTR triggerHealthReport() {
device.handle(ConnectedToggableLedDevice::SEND_HEALTH_RECORD_COMMAND);
}
void setup() {
Serial.begin(115200);
wifi.setup();
if (wifi.connect()) {
Serial.println("WiFi connected, MAC: " + wifi.getMacAddress());
} else {
Serial.println("WiFi connection failed");
}
configTime(0, 0, "pool.ntp.org");
setenv("TZ", "UTC0", 1);
tzset();
Serial.printf("LED initial state: %d\n", device.getLed().getState());
attachInterrupt(digitalPinToInterrupt(ConnectedToggableLedDevice::BUTTON_PIN), triggerButtonPressedEvent, FALLING);
healthTimer = timerBegin(80); // Prescaler 80 (1 tick = 1us)
timerAttachInterrupt(healthTimer, &triggerHealthReport);
timerAlarm(healthTimer, HEALTH_INTERVAL_US, true, 0);
// timerAlarmEnable(healthTimer);
}
void loop() {
// Empty: Interrupts handle events and commands
}