Skip to content

Commit

Permalink
iot2050-event-record: Make sensors' threshold configurable
Browse files Browse the repository at this point in the history
Currently it is impossible to get the precise threshold values for
tilting and uncovering threshold, make sensors' threshold values
configurable.

Signed-off-by: Li Hua Qian <[email protected]>
  • Loading branch information
huaqianli authored and BaochengSu committed Mar 8, 2024
1 parent 83cd499 commit b5963ab
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions recipes-app/iot2050-event-record/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ as follows:
```sh
cp /usr/lib/iot2050/event/iot2050-event-record.conf /etc/systemd/system/iot2050-event-record.service.d/
```
If changing the tilting threshold and uncovering threshold is expected, please
refer to the ``iot2050-event-record.conf`` for the details.

### Watchdog events

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# In IoT2050-SM, there are sensors for tilted and uncovered detection. If you
# want to enable logging of sensor events, i.e. tilted and uncovered events.
# Please copy this file to /etc/systemd/system/iot2050-event-record.service.d/.
#
# RECORD_SENSOR_EVENTS: to start sensor event recording.
# ACCEL_CRITICAL_VALUE: to define the tilting threshold.
# LUX_CRITICAL_VALUE: to define the uncovering threshold.

[Service]
Environment="RECORD_SENSOR_EVENTS=True"
Environment="ACCEL_CRITICAL_VALUE=1000"
Environment="LUX_CRITICAL_VALUE=100"
14 changes: 7 additions & 7 deletions recipes-app/iot2050-event-record/files/iot2050-event-record.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ def record_eio_events():

IIO_IMU_PATH = "/sys/devices/platform/bus@100000/2030000.i2c/i2c-5/5-006a/"
IIO_PRO_PATH = "/sys/devices/platform/bus@100000/2030000.i2c/i2c-5/5-0044/"
ACCEL_CRITICAL_VALUE = 1000
LUX_CRITICAL_VALUE = 300
def record_sensor_events():
accel_x_raw = "{}/in_accel_x_raw"
accel_y_raw = "{}/in_accel_y_raw"
Expand All @@ -154,6 +152,8 @@ def record_sensor_events():
open(accel_z_raw, 'r') as z, \
open(pro_raw, 'r') as l:
is_uncovered = False
accel_critical_value = int(os.getenv('ACCEL_CRITICAL_VALUE'))
lux_critical_value = int(os.getenv('LUX_CRITICAL_VALUE'))
while True:
# Detect tilt sensor event
x.seek(0)
Expand All @@ -168,22 +168,22 @@ def record_sensor_events():
x.seek(0)
y.seek(0)
z.seek(0)
if abs(int(x.read()) - old_x) > ACCEL_CRITICAL_VALUE or \
abs(int(y.read()) - old_y) > ACCEL_CRITICAL_VALUE or \
abs(int(z.read()) - old_z) > ACCEL_CRITICAL_VALUE:
if abs(int(x.read()) - old_x) > accel_critical_value or \
abs(int(y.read()) - old_y) > accel_critical_value or \
abs(int(z.read()) - old_z) > accel_critical_value:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
tilted_event = EVENT_STRINGS["tilt"].format(now)
write_event(EVENT_TYPES["tilt"], tilted_event)

# Detect tamper sensor event
l.seek(0)
lux = int(l.read())
if lux < LUX_CRITICAL_VALUE and not is_uncovered:
if lux < lux_critical_value and not is_uncovered:
is_uncovered = True
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
uncover_event = EVENT_STRINGS["uncover"].format(now)
write_event(EVENT_TYPES["uncover"], uncover_event)
elif lux > LUX_CRITICAL_VALUE and is_uncovered:
elif lux > lux_critical_value and is_uncovered:
is_uncovered = False

def event_record():
Expand Down

0 comments on commit b5963ab

Please sign in to comment.