Peter Smith
Published © GPL3+

Human Presence Detection Through Changes in Air Pressure

Turn on a closet light when the door is opened with a pressure sensor that can be placed anywhere in the room.

IntermediateFull instructions provided5 hours2,639
Human Presence Detection Through Changes in Air Pressure

Things used in this project

Hardware components

DPS310
Infineon DPS310
×1
Texas Instruments TI beLight cc2540
×1
Microsoft Windows Phone
Any Windows 10 device will work just fine
×1

Software apps and online services

Microsoft Best Calculator, IOT Edition
The app is actually my own IOT programming app. It's on the Windows app store.

Story

Read more

Code

Detect door opening using pressure changes

Plain text
The language here is "Best Calculator BASIC", a variant of BASIC that I use to program IOT devices. It's fully documented at https://bestcalculator.wordpress.com/
REM
REM Turn on a light when the door opens
REM

CLS GREEN
PRINT "PRESSURE READING"

REM
REM All of the Pressure data.  Arrays in
REM BASIC start at 1.
REM
DIM Pressure()
PressureIndex = 1
TotalPressureValues = 0

REM
REM All of the Light variables.  We just drive 
REM the first beLight the we see.
REM
allLights  = Bluetooth.DevicesName("beLight*")
oneLight = allLights[1]
Light = oneLight.As("beLight")
REM 0=off otherwise is time.AsTimeInSeconds
LightTime = 0

REM
REM Let the user pick the OPressure Sensor.
REM
bt = Bluetooth.PickDevicesRfcommName (“IFX_NANOHUB”)
REM Functions are altitude, pressure, temperature
dps310 = bt.As (“DPS310”, "", “OnPressureChanged”, "")

REM
REM The FOREVER statement will loop forever.,
REM
FOREVER

REM 
REM The Pressure-change callback.  Will update
REM the Pressure array (etc), call the function
REM that says if the door is opened, and
REM will call the light update function.
REM
FUNCTION OnPressureChanged (dps310, value)
    Screen.ClearLine (4)
    PRINT "PRESSURE=",value

    GLOBAL TotalPressureValues
    GLOBAL Pressure
    GLOBAL PressureIndex


    Pressure[PressureIndex] = value
    PressureIndex = PressureIndex + 1
    IF (PressureIndex > 16) THEN PressureIndex = 1
    TotalPressureValues = TotalPressureValues + 1

    REM Handle the startup edge case.  If there
    REM aren't enough data points, just
    REM accept it and don't try to do the 
    REM calculations.
    IF (TotalPressureValues < 16) THEN RETURN

   open = CalculateIsDoorOpen (Pressure, value)
   Screen.ClearLine (5)
   PRINT "OPEN=", open
   HandleDoor(open)
END

FUNCTION CalculateIsDoorOpen(data, latest)
    mean  = data.Mean
    delta = Math.Abs (mean - latest)
    retval = 0
    IF (delta > 0.05) THEN retval = 1
    RETURN retval
END

REM does one of two actions
REM if the light is off, and should be on, turn it on
REM if the light is on, and should be off, turn it off.
FUNCTION HandleDoor(door)
    GLOBAL LightTime
    GLOBAL Light

    now = DateTime.GetNow()
    unix = now.AsTotalSeconds
    delta = unix - LightTime

    Screen.ClearLine (6)
    PRINT "Door", door, LightTime, delta

    IF (door = 1 AND LightTime = 0)
        LightTime = unix
        Light.SetColor (0, 0, 255, 50)
        Screen.ClearLine(7)
        PRINT "ON"
    END IF

    IF ((door = 0 AND LightTime <> 0) AND delta > 5)
        LightTime = 0
        Light.SetColor (0, 0, 0, 0)

        Screen.ClearLine(7)
        PRINT "OFF"
    END IF
END

Credits

Peter Smith

Peter Smith

2 projects • 3 followers
C# Programmer for many years, and a lover of all things IOT and Bluetooth.

Comments