Versions Compared

Key

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

...

Set up a unique access token (unique credentials) for each KNOT under the device you've just created, under the "Manage credentials" tab:

RouterOS configuration

Preparation

Before setting everything up, we need to confirm that our Bluetooth tag, actually appears in the KNOT's Bluetooth range and that the KNOT detects them. To do that, you can issue the command:

Code Block
languageros
[admin@MikroTik] > iot bluetooth scanners advertisements print
 # DEVICE     PDU-TYPE        TIME                 ADDRESS-TYPE ADDRESS                    RSSI     LENGTH DATA                                           
 0 bt1        adv-noconn-ind  mar/08/2023 12:35:15 public       2C:C8:1B:4B:BB:0A        -50dBm         22 15ff4f090100b0110100ffff00000019d68d2300005d   
 1 bt1        adv-noconn-ind  mar/08/2023 12:35:16 public       DC:2C:6E:0F:C0:3D        -39dBm         22 15ff4f0901008f3cfcfffbfffaff301783c22c000064   
 2 bt1        adv-noconn-ind  mar/08/2023 12:35:35 public       2C:C8:1B:4B:BB:0A        -50dBm         22 15ff4f09010084d500000400ffff0319ea8d2300005d   
 3 bt1        adv-noconn-ind  mar/08/2023 12:35:45 public       2C:C8:1B:4B:BB:0A        -50dBm         22 15ff4f090100e607faffffff03000319f48d2300005d   
 4 bt1        adv-noconn-ind  mar/08/2023 12:35:46 public       DC:2C:6E:0F:C0:3D        -39dBm         22 15ff4f090100773ffcff000000004017a1c22c000064   
 5 bt1        adv-noconn-ind  mar/08/2023 12:35:55 public       2C:C8:1B:4B:BB:0A        -41dBm         22 15ff4f090100d88000000600ffff0319fe8d2300005d   

Or you can check it using Webfig or Winbox under the IoT>Bluetooth>Advertising reports tab.

The list can be chaotic. Random payloads can appear on the list as the scanner captures everything around it. So, to confirm that your actual tag is within the range, apply a tag's MAC address filter:

Code Block
languageros
> iot bluetooth scanners advertisements print where address=DC:2C:6E:0F:C0:3D
 # DEVICE    PDU-TYPE        TIME                 ADDRESS-TYPE ADDRESS                    RSSI     LENGTH DATA                                           
 0 bt1       adv-noconn-ind  mar/08/2023 12:41:06 public       DC:2C:6E:0F:C0:3D        -49dBm         22 15ff4f0901005ab20100fdfffdff4017e1c32c000064   
 1 bt1       adv-noconn-ind  mar/08/2023 12:41:26 public       DC:2C:6E:0F:C0:3D        -40dBm         22 15ff4f090100349704000000fcff4017f5c32c000064   
 2 bt1       adv-noconn-ind  mar/08/2023 12:41:36 public       DC:2C:6E:0F:C0:3D        -49dBm         22 15ff4f09010073fb0000000000003017ffc32c000064   
 3 bt1       adv-noconn-ind  mar/08/2023 12:41:46 public       DC:2C:6E:0F:C0:3D        -43dBm         22 15ff4f090100b88cffffffffffff401709c42c000064   
 4 bt1       adv-noconn-ind  mar/08/2023 12:41:56 public       DC:2C:6E:0F:C0:3D        -39dBm         22 15ff4f0901006863feff0000ffff401713c42c000064   
 5 bt1       adv-noconn-ind  mar/08/2023 12:42:06 public       DC:2C:6E:0F:C0:3D        -49dBm         22 15ff4f090100952e01000300feff40171dc42c000064   
 6 bt1       adv-noconn-ind  mar/08/2023 12:42:16 public       DC:2C:6E:0F:C0:3D        -41dBm         22 15ff4f090100d025000000000100401727c42c000064   

MQTT broker

On each KNOT setup MQTT broker.

For KNOT A:

Code Block
languageros
/iot/mqtt/brokers/add name=tb address=x.x.x.x port=1883 username=knot-A_access_token

Where:

  • name is the name that you wish to give to the broker and this name will be used later in the script;
  • address is the IP address of the broker/ThingsBoard server;
  • port is the TCP port that the broker is listening for → for non-SSL it is typically TCP 1883;
  • username is dictated by the MQTT broker and, in our case, it is an "access token" that was generated in the ThingsBoard management portal.

For KNOT B → Do the same step. Just change the username to the respective access token that was generated for the KNOT B device (gateway).

Script

Import the script show below:

Code Block
languageros
/system/script/add dont-require-permissions=no name=mqttgps owner=admin policy="ftp,re\
    boot,read,write,policy,test,password,sniff,sensitive,romon" \
    source="    ###Configuration###\r\
    \n    #Enter pre-configured broker's name within \"\":\r\
    \n    :local broker \"tb\"\r\
    \n    #Enter the topic name within \"\", per the broker's config\
    uration:\r\
    \n    :local topic \"v1/devices/me/telemetry\"\r\
    \n\r\
    \n    ###Variables####\r\
    \n    :global lat\r\
    \n    :global lon\r\
    \n    :global alt1\r\
    \n    :global alt2\r\
    \n\r\
    \n    ###GPS####\r\
    \n    :put (\"[*] Capturing GPS coordinates...\")\r\
    \n    /system gps monitor once do={\r\
    \n    :set \$lat \$(\"latitude\");\r\
    \n    :set \$lon \$(\"longitude\");\r\
    \n    :set \$alt1 \$(\"altitude\")}\r\
    \n    ###remove \"meters\" from the value because JSON format wi\
    ll not understand it###\r\
    \n    :set \$alt2 [:pick \$alt1 0 [find \$alt1 \" m\"]]\r\
    \n\r\
    \n    :local message \\\r\
    \n    \"{\\\"latitude\\\":\$lat,\\\r\
    \n    \\\"longitude\\\":\$lon,\\\r\
    \n    \\\"altitude\\\":\$alt2}\"\r\
    \n\r\
    \n    ###MQTT###\r\
    \n    :if (\$lat != \"none\") do={\\\r\
    \n    :put (\"[*] Sending message to MQTT broker...\");\r\
    \n    /iot mqtt publish broker=\$broker topic=\$topic message=\$\
    message} else={:put (\"[*] Lattitude=none, not posting anything!\
    \");:log info \"Latitude=none, not posting anything!\"}"/iot/mqtt/brokers/add name=tb address=x.x.x.x port=1883 username=access_token