Skip to content

brakid/HomeAlerts

Repository files navigation

HomeAlerts

Managing sensors to create alarms

Idea

Manage several sensors, monitors (that read sensor events, and create new events) and actors (read events and send alerts, change system values etc)

Architecture

  • use Apache Kafka as Event Streaming Platform that receives all events generated by the distributed system (Sensors, Monitors and Actors are running on different machines)
    • Sensors (e.g Temperature Sensors, Webcameras) write messages into topics
    • Monitors read events from the topics, compare values with each other (e.g temperature changes over time, detecting motion in webcam images), create new events in different topics (e.g a temperature drop alert)
    • Actors read events and act upon them (e.g turn on the heating, send an alert)
  • the central place for events allows an easy creation of new events, receiver can subscribe to new events. Compared to messaging queues, Kafka delivers all events to all subscribers - replicating these in Message Queues is harder (e.g having to maintain multiple queues, or explicit fan-out steps (as in RabbitMQ) - see: Fan Controller Project
  • decoupling Sensors and Monitors: Sensors must not be aware of any Monitors observing them, Monitors can observe multiple Sensors (via subscribing to multiple topics), they keep their own logic for keeping track of values over time

Questions:

  • Kafka limitations (How large can messages be? Can one send whole images or is it better to create a central storage and send file references only?)
    • A: up to 1MB, for the image case: reduce the image size (640 x 480), encode as JPEG
  • starting monitors up: how to ensure they receive recent data from the topics - especially important for for infrequently generated data, e.g. from other monitors)
    • A: change consumer setting to receive all messages in the topic (auto_offset_reset='earliest', see: KafkaConsumer), by default the Kafka Docker keeps data for the Confluent Kafka Docker image is set to 1 day (Confluent, Retention)

Implemented Sensors, Monitors and Actors:

Sensors

  • Temperature Sensor: read the current temperature (Raspberry Pi & DB18B20 Temperature Sensor) -> send to a temperaturetopic
  • Webcam Sensor: read an image from a webcam every Xseconds, convert to a base64 encoded string -> send to a webcamtopic

Monitors

  • Temperature Monitor: Temperature < THRESHOLDor Temperature > THRESHOLD-> send an alert to an alert topic
  • Motion Detector: compare 2 webcam images, if the difference exceed a threshold -> send an alert
def detect_motion(img1: np.ndarray, img2: np.ndarray) -> bool:
    diff = cv2.absdiff(img1, img2)
    eroded_diff = cv2.erode(diff, KERNEL, cv2.BORDER_REFLECT)
    motion = np.mean(eroded_diff) > MOTION_THRESHOLD

    return motion

Actors

Monitor Ideas:

  • Geofencing & Security Webcam -> leave the house -> enable motion detection, disable when coming back
  • object tracking: identfy objects in the webcam image and track them over time (detect objects (Yolo), identify them across frames SIFT)
  • object recognition: run face recognition to check for known faces

Links: