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
2. **Configure the Code:**
- Open the `esp32_MachineNotify.ino` file.
- Replace the placeholder values for `WIFI_SSID`, `WIFI_PASSWORD`, and `NTFY_TOPIC` with your WiFi credentials and desired `ntfy.sh` topic.
- Copy `secrets_template.h` to a new file named `secrets.h` in the same directory.
- 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:**
- Connect your ESP32 board to your computer.

View File

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

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