# ESP32 Dual Machine Activity Notifier This project transforms an ESP32 into a smart monitoring device. It uses two light sensors to track the activity of machines like a washer and dryer, sending notifications to your phone or desktop when a cycle starts and finishes. A local OLED display provides real-time status at a glance. ## Features - **Dual Light Sensing:** Monitors two separate devices using two BH1750 light sensors. - **Real-time Status Display:** Shows current sensor data and machine status on a 128x64 SSD1306 OLED display. - **Push Notifications:** Sends instant start and stop notifications via a self-hostable `ntfy.sh` topic. - **WiFi Connectivity:** Connects to your local WiFi network. - **Non-Blocking Logic:** Uses `millis()` for timing, ensuring the device remains responsive. ## Hardware Requirements - ESP32 development board - 2x BH1750 light sensor modules - SSD1306 128x64 OLED display (I2C) - Breadboard and jumper wires ## Wiring and Connections All components use the I2C communication protocol, which conveniently allows them to share the same data (SDA) and clock (SCL) lines. ### I2C Address Configuration To use two BH1750 sensors on the same I2C bus, they **must** have different addresses. This is a critical step. - **Sensor 1 (Address `0x23`):** Connect the `ADDR` pin to `GND`. This sets the address to the default `0x23`. - **Sensor 2 (Address `0x5C`):** Connect the `ADDR` pin to `3.3V`. This changes its I2C address to `0x5C`. ### Pinout Table | Component | Pin on ESP32 | Notes | | ------------------- | ------------ | ----- | | I2C SCL (OLED, Sensors)| GPIO 22 | Shared I2C Clock Line | | I2C SDA (OLED, Sensors)| GPIO 21 | Shared I2C Data Line | ### Wiring Diagram ``` +-----------------+ +-----------------+ +-----------------+ | ESP32 Dev Board | | BH1750 Sensors | | OLED Display | +-----------------+ +-----------------+ +-----------------+ | 3.3V |---------->| VCC |----->| VCC | | GND |---------->| GND |----->| GND | | | | | | | | GPIO 22 (SCL) |---------->| SCL |----->| SCL | | GPIO 21 (SDA) |---------->| SDA |----->| SDA | +-----------------+ +-----------------+ +-----------------+ ``` **Note:** For Sensor 1, connect the ADDR pin to GND (or leave floating if default is low). For Sensor 2, connect the ADDR pin to 3.3V to change its I2C address to 0x5C. ## Setup & Usage 1. **Install Libraries:** - Open the Arduino IDE and go to `Sketch` > `Include Library` > `Manage Libraries...`. - Install the following libraries: - `Adafruit SSD1306` by Adafruit - `Adafruit GFX Library` by Adafruit - `BH1750` by Christopher Laws 2. **Configure the Code:** - 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. - Select the correct board and port in the Arduino IDE. - Click the upload button. 4. **Deploy the Device:** - Make the connections according to the wiring diagram, which is quite simple. All 3 peripheral devices communicate over I2C, which lets them share the same 4 wires. - Place Sensor 1 & 2 on the respective machines such that they only detect light when the device is running. This is specific to each monitored device. The "Start/Stop" button LED is a great candidate for this. - Make sure to block off any ambient light that might interfere with whatever you're monitoring (e.g. by using a tape). - Tip: wire the sensors using connectors, so that you can later disconnect the main unit (e.g. for a firmware update) without having to remove the sensors from the monitored machines. - Tip: use a power socket with a switch to enable/disable the monitoring as needed ## Code Explanation The code is structured as follows: - It continuously reads the values from both light sensors. - It implements a non-blocking logic using `millis()` to check if the sensors have been in a certain state (light level high) for a continuous period. - It updates the overall device states (`isDevice1Active`, `isDevice2Active`) based on the individual sensor states. - If the device state changes, it calls `sendNotification()`. - It calls `updateDisplay()` to show the latest data. - **`sendNotification()`:** Sends a POST request to the configured `ntfy.sh` topic with a message. - **`updateDisplay()`:** Manages the OLED display. It clears the screen and prints the current status of the sensors and the device. It also implements a scrolling feature to cycle through the displayed information every 2 seconds.