Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "HomeSpan.h"
- #define LD2410_PIN 4 // GPIO pin connected to the sensor's OUT pin
- ////////////////////////////////////////////
- // Occupancy Sensor Service Definition
- ////////////////////////////////////////////
- struct OccupancySensor : Service::OccupancySensor {
- SpanCharacteristic *occupancy; // 0 = Not Occupied, 1 = Occupied
- // Constructor
- OccupancySensor() : Service::OccupancySensor() {
- occupancy = new Characteristic::OccupancyDetected(0);
- pinMode(LD2410_PIN, INPUT); // or INPUT_PULLUP if needed
- }
- // Continuously called by HomeSpan
- void loop() {
- bool isPresent = digitalRead(LD2410_PIN);
- occupancy->setVal(isPresent ? 1 : 0);
- }
- };
- ////////////////////////////////////////////
- // Setup and Main Loop
- ////////////////////////////////////////////
- void setup() {
- Serial.begin(115200);
- // Initialize HomeSpan
- homeSpan.begin(Category::Sensors, "LD2410 Occupancy Sensor");
- // Create an Accessory with Identify + Occupancy
- new SpanAccessory();
- new Service::AccessoryInformation();
- new Characteristic::Identify();
- new OccupancySensor(); // Attach our custom OccupancySensor
- }
- void loop() {
- homeSpan.poll();
- }
Advertisement
Add Comment
Please, Sign In to add comment