Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Try sending a test email to make sure that it is set up correctly.

System>Scripts

...

Option (a)

Navigate to System>Scripts menu and add a new script there.

...

You should make sure that the e-mail notification was received after manually initiating the script.

Monitoring multiple triggers

For monitoring multiple triggers (those are freefalling, tilt, and movement against X, Y, Z, XY, XZ, XYZ, and YZ axis), use the script:

When script (a) is initiated, RouterOS is going to check every received/stored Bluetooth payload, and if any trigger is found in the payload's list → an e-mail message is structured and sent. This e-mail message will simply indicate which triggers were detected in the list with no additional information.

Option (b) 

# Input MAC address of the tag within ""
:

    #User configuration:

    #input the tag's mac address that you wish to monitor within the ""

    :local addressRegex "2C:C8:1B:4BE2:BB15:0A29"

    #enter trigger value, for example "04" for freefalling trigger and "02" for tilt trigger detection
    :local triggervalueF "04"
    :local triggervalueT "02"
    :local triggervalueX "08"
    :local triggervalueXY "18"
    :local triggervalueXZ "28"
    :local triggervalueXYZ "38"
    :local triggervalueY "10"
    :local triggervalueYZ "30"
    :local triggervalueZ "20"

    #name the trigger accordingly
    :local triggernameF "freefalling"
    :local triggernameT "tilt"
    :local triggernameX "Xmovement"
    :local triggernameXY "XYmovement"
    :local triggernameXZ "XZmovement"
    :local triggernameXYZ "XYZmovement"
    :local triggernameY "Ymovement"
    :local triggernameYZ "YZmovement"
    :local triggernameZ "Zmovement"    #enter the message for the email body that you wish to receive within the ""
    :local emailmessageF "Our tag fell down!"
    :local emailmessageT "Our tag tilted!"
    :local emailmessageX "Our tag moved against X axis!"
    :local emailmessageXY "Our tag moved against X and Y axis!"
    :local emailmessageXZ "Our tag moved against X and Z axis!"
    :local emailmessageXYZ "Our tag moved against X,Y and Z axis!"
    :local emailmessageY "Our tag moved against Y axis!"
    :local emailmessageYZ "Our tag moved against Y and Z axis!"
    :local emailmessageZ "Our tag moved against Z axis!"

# POSIX regex for filtering Bluetooth advertisements based on their data. Same
# usage as with 'addressRegex'.
:local advertisingDataRegex ""

# Signal strength filter. E.g. -40 would only include Bluetooth advertisements
# whose signal strength is stronger than -40dBm.
# To disable this filter, set it to ""
:local rssiThreshold ""

#enter the subject for the email within the ""
:local emailsubject "RouterOS report!"

#enter the email address wthin the ""
:local emailaddress "YOUR_GMAIL@gmail.com"

#Script:
################################## Bluetooth ##################################
:local flagStr do={
    :local str ""

    :if ($1 & 0x01) do={ :set $str " switch" }
    :if ($1 & 0x02) do={ :set $str "$str tilt" }
    :if ($1 & 0x04) do={ :set $str "$str free_fall" }
    :if ($1 & 0x08) do={ :set $str "$str impact_x" }
    :if ($1 & 0x10) do={ :set $str "$str impact_y" }
    :if ($1 & 0x20) do={ :set $str "$str impact_z" }

    :if ([:len $str] = 0) do={ :return "" }

    :return [:pick $str 1 [:len $str]]
}

:local triggernameF "free_fall"
:local triggernameT "tilt"
:local triggernameX "impact_x"
:local triggernameY "impact_y"
:local triggernameZ "impact_z"
:local triggernameS "switch"

# Find fresh Bluetooth advertisements
:global btOldestAdvertisementTimestamp
:if ([:typeof $btOldestAdvertisementTimestamp] = "nothing") do={
    # First time this script has been run since booting, need to initialize
    # persistent variables
    :set $btOldestAdvertisementTimestamp 0
}
:local advertisements [/iot bluetooth scanners advertisements print detail \
    as-value where \
        epoch > $btOldestAdvertisementTimestamp and \
        address ~ $addressRegex and \
        data ~ $advertisingDataRegex and \
        rssi > $rssiThreshold
]
:local advCount 0
:local lastAdvTimestamp 0
:local triggerlist ""

:local triggerF
:local triggerT
:local triggerX
:local triggerY
:local triggerZ
:local triggerS

:local messageF ""
:local messageT ""
:local messageX ""
:local messageY ""
:local messageZ ""
:local messageS ""

:local 21st ""

:

    #enter the subject for the email within the ""
    :local emailsubject "RouterOS report!"

    #enter the email address wthin the ""
    :local emailaddress "YOUR_EMAIL@gmail.com"

    ###Bluetooth scanner
    :local triggerF
    :local triggerT
    :local triggerX
    :local triggerXY
    :local triggerXZ
    :local triggerXYZ
    :local triggerY
    :local triggerYZ
    :local triggerZ

    :put ("[*] Gathering Bluetooth info...")
    :global btOldestAdvertisementTimestamp
    :if ([:typeof $btOldestAdvertisementTimestamp] = "nothing") do={:set $btOldestAdvertisementTimestamp 0}
    :local btProcessingStart [/system clock get time]
    :local advertisements [/iot bluetooth scanners advertisements print detail \
        as-value where \
            epoch > $btOldestAdvertisementTimestamp and \
            address ~ $addressRegex]
    :local advCount 0
    :local lastAdvTimestamp 0

    :foreach adv in=$advertisements do={
            :local address ($adv->"address")
            :local ad rssi ($adv->"datarssi")
            :local ts epoch ($adv->"epoch")
            :local trig [:pick rtime ($adv->"datatime") 40 42]
           :set $advCount ($advCount + 1:local ad ($adv->"data")
           :set $lastAdvTimestamp $ts
        :local flags [:tonum "0x$[:pick $ad 40 42]"]
    :local fflags "$[$flagStr $flags]"
    :if ($trig$fflags="$triggervalueF") do={:set triggerF "$triggernameF$fflags "none - periodically broadcasted payload"}   

       :if ($trig="$triggervalueT") do={:set triggerT "$triggernameT"}
:local obj "# $advCount:
                :if ($trig="$triggervalueX") do={:set triggerX "$triggernameX"}
            :if ($trig="$triggervalueXY") do={:set triggerXY "$triggernameXY"} time: $rtime
            :if ($trig="$triggervalueXZ") do={:set triggerXZ "$triggernameXZ"}
                 trigger: $fflags

"
    :set $advCount ($advCount + 1)
    :set $lastAdvTimestamp $epoch
    :set $triggerlist "$triggerlist$obj"

:set triggerF [:pick $triggerlist ([find $triggerlist "free_fall"]-0) ([find $triggerlist "free_fall"]+9)]
:if ($triggerF=$triggernameF:if ($trig="$triggervalueXYZ") do={:set triggerXYZ "$triggernameXYZ"}
           :if ($trig="$triggervalueY") do={:set triggerY "$triggernameY"}
           :if ($trig="$triggervalueYZ") do={:set triggerYZ "$triggernameYZ"}
           :if ($trig="$triggervalueZ") do={:set triggerZ "$triggernameZ"}           :if ($advCount > 0messageF "$triggernameF";:set 21st "not00";}
:set triggerT [:pick $triggerlist ([find $triggerlist "tilt"]-0) ([find $triggerlist "tilt"]+4)]
:if ($triggerT=$triggernameT) do={:set $btOldestAdvertisementTimestamp $lastAdvTimestamp}}

    :put ("[*] Found $advCount new advertisements \
        (processing time: $[([/system clock get time] - $btProcessingStart)])")

messageT "$triggernameT";:set 21st "not00";}
:set triggerX [:pick $triggerlist ([find $triggerlist "impact_x"]-0) ([find $triggerlist "impact_x"]+8)]
:if ($triggerX=$triggernameX) do={:set messageX "$triggernameX";:set 21st "not00";}
:set triggerY [:pick $triggerlist ([find $triggerlist "impact_y"]-0) ([find $triggerlist "impact_y"]+8)]
:if ($triggerY=$triggernameY) do={:set messageY "$triggernameY";:set 21st "not00";}
:set triggerZ [:pick $triggerlist ([find $triggerlist "impact_z"]-0) ([find $triggerlist "impact_z"]+8)]
:if ($triggerZ=$triggernameZ) do={:set messageZ "$triggernameZ";:set 21st "not00";}
:set triggerS [:pick $triggerlist ([find $triggerlist "switch"]-0) ([find $triggerlist "switch"]+6)]
:if ($triggerS=$triggernameS) do={:set messageS "$triggernameS";:set 21st "not00";}

}
:if ($advCount > 0) do={
    :set $btOldestAdvertisementTimestamp $lastAdvTimestamp
}

:if ($21st="not00    ###Result
    :if ($triggerF="$triggernameF") do={/tool e-mail send to=$emailaddress subject="$emailsubject" body="$emailmessageF"}
    :if ($triggerT="$triggernameT") do={/tool e-mail send to=$emailaddress subject="$emailsubject" body="$emailmessageT"}
    :if ($triggerX="$triggernameX") do={/tool e-mail send to=$emailaddress subject="$emailsubject" body="$emailmessageX"}
    :if ($triggerXY="$triggernameXY") do={/tool e-mail send to=$emailaddress subject="$emailsubject" body="$emailmessageXY"}
    :if ($triggerXZ="$triggernameXZ") do={/tool e-mail send to=$emailaddress subject="$emailsubject" body="$emailmessageXZ"}
    :if ($triggerXYZ="$triggernameXYZ") do={/tool e-mail send to=$emailaddress subject="$emailsubject" body="$emailmessageXYZ"}
    :if ($triggerY="$triggernameY") do={/tool e-mail send to=$emailaddress subject="$emailsubject" body="$emailmessageY$triggerlist"}
    :if ($triggerYZ="$triggernameYZ") do={/tool e-mail send to=$emailaddress subject="$emailsubject" body="$emailmessageYZ"}
    :if ($triggerZ="$triggernameZ") do={/tool e-mail send to=$emailaddress subject="$emailsubject" body="$emailmessageZ"}

...

titlenote

...

else={:put "21st octet does not have triggers detected!"}

When script (b) is initiated, RouterOS is going to check every received/stored Bluetooth payload, and if any trigger is found in the payload's list → an e-mail message is structured and sent. This e-mail message will have a bit more detailed information, compared to script (a). The message, in this case, will indicate each payload's reception time.

System>Scheduler

To automate the process add a new scheduler under the System>Scheduler tab:

...

Click on Apply and OK.

Result verification

a1) Confirm that the tag broadcasts advertising reports and that the tag's triggers get detected:

b2) Confirm that your email server is set up correctly and that you are able to send emails from the KNOT (RouterOS).

c3) After you manually run the script or apply a scheduler to the script, an email notification should go through, like so:

     Script (a):

Image Added

     Script (b):

Image AddedImage Removed