split into features

This commit is contained in:
Dejvino 2026-02-12 19:55:45 +01:00
parent f388365609
commit daa3cff439

View File

@ -1,22 +1,34 @@
#include <Wire.h>
// Feature Configuration - Comment out to disable
#define ENABLE_SENSORS
#define ENABLE_DISPLAY
#define ENABLE_WIFI
#define ENABLE_NOTIFICATIONS
#ifdef ENABLE_WIFI
#include <WiFi.h> #include <WiFi.h>
#include <HTTPClient.h> #include <HTTPClient.h>
#include <Wire.h> #endif
#ifdef ENABLE_SENSORS
#include <BH1750.h> #include <BH1750.h>
#endif
#ifdef ENABLE_DISPLAY
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#endif
// WiFi credentials // WiFi credentials
#ifdef ENABLE_WIFI
const char* WIFI_SSID = "YOUR_WIFI_SSID"; const char* WIFI_SSID = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"; const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
#endif
// ntfy.sh topic // ntfy.sh topic
const char* NTFY_TOPIC = "YOUR_NTFY_TOPIC"; const char* NTFY_TOPIC = "YOUR_NTFY_TOPIC";
// Notification queue
String queuedNotification = "";
String queuedTitle = "";
String queuedPriority = "";
// Pin definitions // Pin definitions
const int VIBRATION_PIN = 4; const int VIBRATION_PIN = 4;
@ -25,10 +37,14 @@ const int VIBRATION_PIN = 4;
#define SCREEN_HEIGHT 64 #define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C #define SCREEN_ADDRESS 0x3C
#ifdef ENABLE_DISPLAY
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#endif
// Light sensor // Light sensor
#ifdef ENABLE_SENSORS
BH1750 lightMeter; BH1750 lightMeter;
#endif
// Thresholds // Thresholds
const float LIGHT_ACTIVATION_THRESHOLD = 200.0; const float LIGHT_ACTIVATION_THRESHOLD = 200.0;
@ -47,26 +63,37 @@ unsigned long lightHighStartTime = 0;
unsigned long lightLowStartTime = 0; unsigned long lightLowStartTime = 0;
// OLED display data // OLED display data
#ifdef ENABLE_DISPLAY
String displayData[4]; String displayData[4];
int currentDisplayLine = 0; int currentDisplayLine = 0;
unsigned long lastDisplayScrollTime = 0; unsigned long lastDisplayScrollTime = 0;
const unsigned long DISPLAY_SCROLL_INTERVAL = 2000; // 2 seconds const unsigned long DISPLAY_SCROLL_INTERVAL = 1000; // 1000 = 1 second
#endif
// Notification queue
String queuedNotification = "";
String queuedTitle = "";
String queuedPriority = "";
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
#if defined(ENABLE_SENSORS) || defined(ENABLE_DISPLAY)
Wire.begin();
#endif
#ifdef ENABLE_SENSORS
// Initialize vibration sensor pin // Initialize vibration sensor pin
pinMode(VIBRATION_PIN, INPUT); pinMode(VIBRATION_PIN, INPUT);
// Initialize I2C bus
Wire.begin();
// Initialize light sensor // Initialize light sensor
if (!lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) { if (!lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F("Could not find a valid BH1750 sensor, check wiring!")); Serial.println(F("Could not find a valid BH1750 sensor, check wiring!"));
while (1) { } while (1) { }
} }
#endif
#ifdef ENABLE_DISPLAY
// Initialize OLED display // Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed")); Serial.println(F("SSD1306 allocation failed"));
@ -78,7 +105,9 @@ void setup() {
display.setCursor(0, 0); display.setCursor(0, 0);
display.println(F("Initializing...")); display.println(F("Initializing..."));
display.display(); display.display();
#endif
#ifdef ENABLE_WIFI
// Connect to WiFi // Connect to WiFi
Serial.print("Connecting to "); Serial.print("Connecting to ");
Serial.println(WIFI_SSID); Serial.println(WIFI_SSID);
@ -91,18 +120,22 @@ void setup() {
Serial.println("WiFi connected"); Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
#endif
#if defined(ENABLE_DISPLAY) && defined(ENABLE_WIFI)
display.clearDisplay(); display.clearDisplay();
display.setCursor(0,0); display.setCursor(0,0);
display.println("WiFi Connected"); display.println("WiFi Connected");
display.display(); display.display();
delay(1000); delay(1000);
#endif
} }
bool sendNotification(String message, String title, String priority); bool sendNotification(String message, String title, String priority);
void updateDisplay(); void updateDisplay();
void loop() { void loop() {
#ifdef ENABLE_NOTIFICATIONS
// Try to send any queued notification // Try to send any queued notification
if (queuedNotification != "") { if (queuedNotification != "") {
if (sendNotification(queuedNotification, queuedTitle, queuedPriority)) { if (sendNotification(queuedNotification, queuedTitle, queuedPriority)) {
@ -114,12 +147,18 @@ void loop() {
// Failed, will retry on next loop // Failed, will retry on next loop
} }
} }
#endif
unsigned long currentTime = millis(); unsigned long currentTime = millis();
// Sensor readings // Sensor readings
#ifdef ENABLE_SENSORS
bool currentVibrationState = (digitalRead(VIBRATION_PIN) == LOW); bool currentVibrationState = (digitalRead(VIBRATION_PIN) == LOW);
float currentLightLevel = lightMeter.readLightLevel(); float currentLightLevel = lightMeter.readLightLevel();
#else
bool currentVibrationState = false;
float currentLightLevel = 0.0;
#endif
// Vibration state change logic // Vibration state change logic
if (currentVibrationState && !isVibrationActive) { // Vibration is active (LOW) if (currentVibrationState && !isVibrationActive) { // Vibration is active (LOW)
@ -186,13 +225,18 @@ void loop() {
priority = "high"; priority = "high";
} }
#ifdef ENABLE_NOTIFICATIONS
if (!sendNotification(message, title, priority)) { if (!sendNotification(message, title, priority)) {
queuedNotification = message; // Failed to send, queue it (overwrites any older one) queuedNotification = message; // Failed to send, queue it (overwrites any older one)
queuedTitle = title; queuedTitle = title;
queuedPriority = priority; queuedPriority = priority;
} }
#else
Serial.println("Notification (Disabled): " + title + " - " + message);
#endif
} }
#ifdef ENABLE_DISPLAY
// Update display data // Update display data
displayData[0] = "Vibration: " + String(isVibrationActive ? "ON" : "OFF"); displayData[0] = "Vibration: " + String(isVibrationActive ? "ON" : "OFF");
displayData[1] = "Light: " + String(currentLightLevel) + " lx"; displayData[1] = "Light: " + String(currentLightLevel) + " lx";
@ -201,9 +245,11 @@ void loop() {
// Update display // Update display
updateDisplay(); updateDisplay();
#endif
} }
bool sendNotification(String message, String title, String priority) { bool sendNotification(String message, String title, String priority) {
#ifdef ENABLE_WIFI
if (WiFi.status() == WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) {
HTTPClient http; HTTPClient http;
String url = "http://ntfy.sh/" + String(NTFY_TOPIC); String url = "http://ntfy.sh/" + String(NTFY_TOPIC);
@ -228,9 +274,14 @@ bool sendNotification(String message, String title, String priority) {
Serial.println("WiFi Disconnected. Cannot send notification."); Serial.println("WiFi Disconnected. Cannot send notification.");
return false; return false;
} }
#else
Serial.println("WiFi Disabled. Notification skipped.");
return true;
#endif
} }
void updateDisplay() { void updateDisplay() {
#ifdef ENABLE_DISPLAY
unsigned long currentTime = millis(); unsigned long currentTime = millis();
// Scroll logic // Scroll logic
@ -250,4 +301,5 @@ void updateDisplay() {
} }
display.display(); display.display();
#endif
} }