extract secrets out

This commit is contained in:
Dejvino 2026-02-12 22:13:17 +01:00
parent ac08a2aa2b
commit f0714e352f
4 changed files with 112 additions and 41 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
secrets.h

View File

@ -51,8 +51,9 @@ This project turns an ESP32 into a smart device that monitors a washing machine'
- `BH1750` by Christopher Laws - `BH1750` by Christopher Laws
2. **Configure the Code:** 2. **Configure the Code:**
- Open the `esp32_MachineNotify.ino` file. - Copy `secrets_template.h` to a new file named `secrets.h` in the same directory.
- Replace the placeholder values for `WIFI_SSID`, `WIFI_PASSWORD`, and `NTFY_TOPIC` with your WiFi credentials and desired `ntfy.sh` topic. - Open `secrets.h` and populate `SECRET_WIFI_SSID`, `SECRET_WIFI_PASSWORD`, and `SECRET_NTFY_TOPIC` with your WiFi credentials and desired `ntfy.sh` topic.
- The `secrets.h` file is ignored by git to keep your credentials safe.
3. **Upload the Code:** 3. **Upload the Code:**
- Connect your ESP32 board to your computer. - Connect your ESP32 board to your computer.

View File

@ -1,4 +1,5 @@
#include <Wire.h> #include <Wire.h>
#include "secrets.h"
// Feature Configuration - Comment out to disable // Feature Configuration - Comment out to disable
#define ENABLE_SENSORS #define ENABLE_SENSORS
@ -22,12 +23,16 @@
// WiFi credentials // WiFi credentials
#ifdef ENABLE_WIFI #ifdef ENABLE_WIFI
const char* WIFI_SSID = "YOUR_WIFI_SSID"; const char* WIFI_SSID = SECRET_WIFI_SSID;
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD"; const char* WIFI_PASSWORD = SECRET_WIFI_PASSWORD;
#endif #endif
// ntfy.sh topic // ntfy.sh topic
const char* NTFY_TOPIC = "YOUR_NTFY_TOPIC"; const char* NTFY_TOPIC = SECRET_NTFY_TOPIC;
// Device Names
const char* DEVICE1_NAME = "Dryer"; // Monitored by Vibration
const char* DEVICE2_NAME = "Washer"; // Monitored by Light
// Pin definitions // Pin definitions
const int VIBRATION_PIN = 4; const int VIBRATION_PIN = 4;
@ -56,7 +61,6 @@ const int VIBRATION_WINDOW_SIZE = 4; // X * 5s seconds window
// Device state // Device state
bool isVibrationActive = false; bool isVibrationActive = false;
bool isLightActive = false; bool isLightActive = false;
bool isDeviceActive = false;
// Time tracking for sensor states // Time tracking for sensor states
unsigned long lastVibrationCheckTime = 0; unsigned long lastVibrationCheckTime = 0;
@ -74,9 +78,13 @@ int displayDataLines = displayTextLines;
#endif #endif
// Notification queue // Notification queue
String queuedNotification = ""; String queuedMessage1 = "";
String queuedTitle = ""; String queuedTitle1 = "";
String queuedPriority = ""; String queuedPriority1 = "";
String queuedMessage2 = "";
String queuedTitle2 = "";
String queuedPriority2 = "";
#ifdef ENABLE_SENSORS #ifdef ENABLE_SENSORS
unsigned long vibrationHistory[VIBRATION_WINDOW_SIZE]; unsigned long vibrationHistory[VIBRATION_WINDOW_SIZE];
@ -173,14 +181,20 @@ void updateDisplay();
void loop() { void loop() {
#ifdef ENABLE_NOTIFICATIONS #ifdef ENABLE_NOTIFICATIONS
// Try to send any queued notification // Try to send any queued notification
if (queuedNotification != "") { if (queuedMessage1 != "") {
if (sendNotification(queuedNotification, queuedTitle, queuedPriority)) { if (sendNotification(queuedMessage1, queuedTitle1, queuedPriority1)) {
Serial.println("Successfully sent queued notification."); Serial.println("Successfully sent queued notification 1.");
queuedNotification = ""; // Clear queue queuedMessage1 = ""; // Clear queue
queuedTitle = ""; queuedTitle1 = "";
queuedPriority = ""; queuedPriority1 = "";
} else { }
// Failed, will retry on next loop }
if (queuedMessage2 != "") {
if (sendNotification(queuedMessage2, queuedTitle2, queuedPriority2)) {
Serial.println("Successfully sent queued notification 2.");
queuedMessage2 = ""; // Clear queue
queuedTitle2 = "";
queuedPriority2 = "";
} }
} }
#endif #endif
@ -194,6 +208,9 @@ void loop() {
float currentLightLevel = 0.0; float currentLightLevel = 0.0;
#endif #endif
bool previousVibrationState = isVibrationActive;
bool previousLightState = isLightActive;
// Vibration state change logic // Vibration state change logic
#ifdef ENABLE_SENSORS #ifdef ENABLE_SENSORS
if (currentTime - lastVibrationCheckTime >= VIBRATION_STATE_CHANGE_INTERVAL) { if (currentTime - lastVibrationCheckTime >= VIBRATION_STATE_CHANGE_INTERVAL) {
@ -251,30 +268,52 @@ void loop() {
lightHighStartTime = 0; lightHighStartTime = 0;
} }
// Device 1 (Vibration) Notifications
// Overall device state if (isVibrationActive != previousVibrationState) {
bool previousDeviceState = isDeviceActive;
isDeviceActive = isVibrationActive || isLightActive;
if (isDeviceActive != previousDeviceState) {
String message; String message;
String title; String title;
String priority; String priority;
if (isDeviceActive) { if (isVibrationActive) {
message = "Washing machine cycle started."; message = String(DEVICE1_NAME) + " started.";
title = "Machine START"; title = String(DEVICE1_NAME) + " START";
priority = "default"; priority = "default";
} else { } else {
message = "Washing machine cycle finished."; message = String(DEVICE1_NAME) + " finished.";
title = "Machine END"; title = String(DEVICE1_NAME) + " END";
priority = "high"; priority = "high";
} }
#ifdef ENABLE_NOTIFICATIONS #ifdef ENABLE_NOTIFICATIONS
if (!sendNotification(message, title, priority)) { if (!sendNotification(message, title, priority)) {
queuedNotification = message; // Failed to send, queue it (overwrites any older one) queuedMessage1 = message;
queuedTitle = title; queuedTitle1 = title;
queuedPriority = priority; queuedPriority1 = priority;
}
#else
Serial.println("Notification (Disabled): " + title + " - " + message);
#endif
}
// Device 2 (Light) Notifications
if (isLightActive != previousLightState) {
String message;
String title;
String priority;
if (isLightActive) {
message = String(DEVICE2_NAME) + " active.";
title = String(DEVICE2_NAME) + " ACTIVE";
priority = "default";
} else {
message = String(DEVICE2_NAME) + " inactive.";
title = String(DEVICE2_NAME) + " INACTIVE";
priority = "high";
}
#ifdef ENABLE_NOTIFICATIONS
if (!sendNotification(message, title, priority)) {
queuedMessage2 = message;
queuedTitle2 = title;
queuedPriority2 = priority;
} }
#else #else
Serial.println("Notification (Disabled): " + title + " - " + message); Serial.println("Notification (Disabled): " + title + " - " + message);
@ -283,10 +322,10 @@ void loop() {
#ifdef ENABLE_DISPLAY #ifdef ENABLE_DISPLAY
// Update display data // Update display data
displayData[0] = "Vibra: " + String(vibrationWindowTotal); displayData[0] = String(DEVICE1_NAME) + ": " + String(vibrationWindowTotal);
displayData[1] = " : " + String(isVibrationActive ? "ON" : "OFF"); displayData[1] = "Vibra: " + String(isVibrationActive ? "ON" : "OFF");
displayData[2] = "Light: " + String(currentLightLevel) + " lx"; displayData[2] = String(DEVICE2_NAME) + ": " + String(currentLightLevel, 0);
displayData[3] = " : " + String(isLightActive ? "ON" : "OFF"); displayData[3] = "Light: " + String(isLightActive ? "ON" : "OFF");
displayDataLines = 4; displayDataLines = 4;
// Update display // Update display
@ -342,9 +381,11 @@ void updateDisplay() {
// Draw active status indicator // Draw active status indicator
int rectWidth = 10; int rectWidth = 10;
int rectHeight = SCREEN_HEIGHT; int rectHeight = (SCREEN_HEIGHT / 2) - 2;
int rectX = 2; int rectX = 2;
int rectY = (SCREEN_HEIGHT - rectHeight) / 2; int rectY1 = 1;
int rectY2 = (SCREEN_HEIGHT / 2) + 1;
int textX = rectX + rectWidth + 4; int textX = rectX + rectWidth + 4;
for (int i = 0; i < displayDataLines; i++) { for (int i = 0; i < displayDataLines; i++) {
@ -353,12 +394,20 @@ void updateDisplay() {
display.println(displayData[lineIndex]); display.println(displayData[lineIndex]);
} }
display.drawRect(rectX, rectY, rectWidth, rectHeight, WHITE); // Draw Device 1 Indicator (Top)
display.drawRect(rectX, rectY1, rectWidth, rectHeight, WHITE);
if (isDeviceActive) { if (isVibrationActive) {
int innerWidth = rectWidth - 4; int innerWidth = rectWidth - 4;
int step = (millis() / 150) % (innerWidth + 1); int step = (millis() / 150) % (innerWidth + 1);
display.fillRect(rectX + 2, rectY + 2, step, rectHeight - 4, WHITE); display.fillRect(rectX + 2, rectY1 + 2, step, rectHeight - 4, WHITE);
}
// Draw Device 2 Indicator (Bottom)
display.drawRect(rectX, rectY2, rectWidth, rectHeight, WHITE);
if (isLightActive) {
int innerWidth = rectWidth - 4;
int step = (millis() / 150) % (innerWidth + 1);
display.fillRect(rectX + 2, rectY2 + 2, step, rectHeight - 4, WHITE);
} }
display.display(); display.display();

20
secrets_template.h Normal file
View File

@ -0,0 +1,20 @@
/*
* secrets_template.h
*
* INSTRUCTIONS:
* 1. Copy this file to a new file named "secrets.h" in the same directory.
* 2. Open "secrets.h" and populate the values below with your actual configuration.
* 3. "secrets.h" is excluded from version control to protect your sensitive data.
*/
#ifndef SECRETS_H
#define SECRETS_H
// WiFi credentials
#define SECRET_WIFI_SSID "YOUR_WIFI_SSID"
#define SECRET_WIFI_PASSWORD "YOUR_WIFI_PASSWORD"
// ntfy.sh topic
#define SECRET_NTFY_TOPIC "YOUR_NTFY_TOPIC"
#endif