Example: Activate scene on LogicMachine based on Mikrotik WiFi registration table events

Task

How to detect WiFi client registration on Mikrotik router and automatically activate a scene on LogicMachine? This example provides possibility to run “I am home” scene when you arrive at home (and WiFi on your phone is enabled).

Check your WiFi device MAC

Check your phone or your family member’s phone MAC addresses. In Android you can see it in Settings –> About phone –> Status. In iOS you can see it in Settings –> General –> About

Enable Remote service on LogicMachine

Go to System config –> Services –> Remote services

WiFi client detection script on Mikrotik RouterOS

Go to System –> Scheduler and add the following script. It will check the existence of two MAC addresses and activate the group address 1/1/1 on LogicMachine with IP 192.168.1.13 (in this example login=remote, password = 11111111). In case one of WiFi devices connects to Mikrotik router, 1 will be send to 1/1/1. If both are disconnected – 0 is sent to 1/1/1. You can adjust also the Scheduler interval, how often you want the script to check the WiFi registration table.

Source code    
  1. {
  2. :local exists [/int wire reg find mac-address="78:02:F8:7E:96:01"];
  3. :local exists2 [/int wire reg find mac-address="B0:E2:35:CF:46:48"];
  4. :if ($exists!="" or $exists2!="") do={
  5. /tool fetch url="http://remote:11111111@192.168.1.13/scada-remote" http-data="m=json&r=grp&fn=write&alias=1/1/1&value=1" http-method=post;
  6. } else={
  7. /tool fetch url="http://remote:11111111@192.168.1.13/scada-remote" http-data="m=json&r=grp&fn=write&alias=1/1/1&value=0" http-method=post;
  8. }
  9. }

Event-based script for scene group address

Add event-based script for I am home grp address 1/1/1.
On each received telegram to this group address, the script will active grp 1/1/2 and 1/1/3 if both of the following conditions are true:
– if previous value for 1/1/1 was 0 (the owner was away and now has connected)
– AND for 5h there was no disconnect with value 0 on 1/1/1 (if the owner is walking around the house and connects to other access points, we still assume his presence. Presence detectors might be used here as well to detect this)

Source code    
  1. value = event.getvalue()
  2. prev_value = storage.get('prev_value')
  3.  
  4. if value then
  5. if not prev_value then
  6. off_time = storage.get('last_off_time', 0)
  7. delta = os.time() - off_time
  8.  
  9. if delta > (5 * 60 * 60) then
  10. grp.write('1/1/2', true)
  11. grp.write('1/1/3', true)
  12. end
  13. end
  14. else
  15. storage.set('last_off_time', os.time())
  16. end
  17.  
  18. storage.set('prev_value', value)