Community discussions

MikroTik App
 
nielajoubert
just joined
Topic Author
Posts: 6
Joined: Mon Apr 15, 2024 12:32 pm

Mikrotik Knot BLE Scripting

Mon Apr 15, 2024 12:41 pm

Good day I have the following code that works perfectly on a MIkrotik KNOT for switching relays by toggling pin 4 and 6, when BLE tags are nearby, the problem that I am facing is that I can only add one MAC address to the :local addressRegex and I need multiple tags to be picked up by the knot, I already tried pipes (|) and (||) and 'or' logic nothing workings I even tried ";" and "," but no luck. I also tried to only add the first 3 MAC ID's like so :local addressRegex "DC:2C:6E" , still no tags gets picked up. Any help will be appreciate.

Regards Niel

### User params

:local missingInterval 5000
:local addressRegex "DC:2C:6E:7C:C5:04"

###Time
:global EpochTime do={
   :local ds [/system clock get date];
   :local months;
   :if ((([:pick $ds 9 11]-1)/4) != (([:pick $ds 9 11])/4)) do={
      :set months {"an"=0;"eb"=31;"ar"=60;"pr"=91;"ay"=121;"un"=152;"ul"=182;"ug"=213;"ep"=244;"ct"=274;"ov"=305;"ec"=335};
   } else={
      :set months {"an"=0;"eb"=31;"ar"=59;"pr"=90;"ay"=120;"un"=151;"ul"=181;"ug"=212;"ep"=243;"ct"=273;"ov"=304;"dec"=334};
   }
   :set ds (([:pick $ds 9 11]*365)+(([:pick $ds 9 11]-1)/4)+($months->[:pick $ds 1 3])+[:pick $ds 4 6]);
   :local ts [/system clock get time];
   :set ts (([:pick $ts 0 2]*60*60)+([:pick $ts 3 5]*60)+[:pick $ts 6 8]);
   :return ($ds*24*60*60 + $ts + 946684800 - [/system clock get gmt-offset]);
}

:local Epomili
:set Epomili ([$EpochTime]."000")

###Bluetooth
:local adver
:local adver2
:local ts
:local clockmin

:set adver [/iot bluetooth scanners advertisements print detail as-value where address ~ $addressRegex]
:set adver2 ($adver->0)
:set ts ($adver2 -> "epoch")
:set clockmin ($Epomili-$missingInterval)

:local counterIN
:local counterOUT

:if ($ts<=$clockmin) do={
    :set $counterIN 0;
    :if ($counterOUT=0 or $counterOUT=null) do={
        :set $counterOUT 0;
        /iot gpio digital set pin6 output=1;
       /iot gpio digital set pin4 output=0;
        :set counterOUT ($counterOUT+1);
    } else={
        :log info "Tag is OUT of range! Notification was already sent before!"
    }
} else={
    :set $counterOUT 0;
    :if ($counterIN=0 or $counterIN=null) do={
        :set $counterIN 0;
       /iot gpio digital set pin6 output=0;
       /iot gpio digital set pin4 output=1;
        :set $counterIN ($counterIN+1);
    } else={
        :log info "Tag is WITHIN the range! Notification was already sent before!"
    }
}
 
nielajoubert
just joined
Topic Author
Posts: 6
Joined: Mon Apr 15, 2024 12:32 pm

Re: Mikrotik Knot BLE Scripting

Mon Apr 15, 2024 3:57 pm

I was able to fix the issue where it does not want to use the 3 ID like so : :local addressRegex "DC:2C:6E" , the problem was that I had to clear the advertisments first as follow before the last } of the code : /iot/bluetooth/scanners/advertisements clear

Who is online

Users browsing this forum: No registered users and 12 guests