Community discussions

MikroTik App
 
sandis56
just joined
Topic Author
Posts: 1
Joined: Tue Dec 27, 2022 8:18 am

TG-BT5-OUT temperature data. (Newbie)

Tue Dec 27, 2022 2:25 pm

Greetings.

I need solution for a simple task.
I have Mikrotik Knot and 2 TG-BT5-OUT sensors. I want to get sensor temperature data to my HTTP server.

I am using this script:
   # POSIX regex for filtering advertisement Bluetooth addresses. E.g. "^BC:33:AC"
    # would only include addresses which start with those 3 octets.
    # To disable this filter, set it to ""
    :local addressRegex ""

    # 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 ""

    ################################## Bluetooth ##################################
    :global invertU16 do={
        :local inverted 0
        :for idx from=0 to=15 step=1 do={
            :local mask (1 << $idx)
            :if ($1 & $mask = 0) do={
                :set $inverted ($inverted | $mask)
            }
        }
        return $inverted
    }

    :global le16ToHost do={
        :local lsb [:pick $1 0 2]
        :local msb [:pick $1 2 4]

        :return [:tonum "0x$msb$lsb"]
    }

    :local le32ToHost do={
        :local lsb [:pick $1 0 2]
        :local midL [:pick $1 2 4]
        :local midH [:pick $1 4 6]
        :local msb [:pick $1 6 8]

        :return [:tonum "0x$msb$midH$midL$lsb"]
    }

    :local from88 do={
        :global invertU16
        :global le16ToHost
        :local num [$le16ToHost $1]

        # Handle negative numbers
        :if ($num & 0x8000) do={
            :set num (-1 * ([$invertU16 $num] + 1))
        }

        # Convert from 8.8. Scale by 1000 since floating point is not supported
        :return (($num * 125) / 32)
    }

    :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]]
    }

    # 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 advJson ""
    :local advSeparator ""

    # Remove semicolons from MAC/Bluetooth addresses
    :local minimizeMac do={
        :local minimized
        :local lastIdx ([:len $address] - 1)
        :for idx from=0 to=$lastIdx step=1 do={
            :local char [:pick $address $idx]
            :if ($char != ":") do={
                :set $minimized "$minimized$char"
            }
        }
        :return $minimized
    }

    :foreach adv in=$advertisements do={
        :local address ($adv->"address")
        :local rssi ($adv->"rssi")
        :local epoch ($adv->"epoch")
        :local ad ($adv->"data")
        :local version [:tonum "0x$[:pick $ad 8 10]"]
        :local encrypted [:tonum "0x$[:pick $ad 10 12]"]
        :local salt [$le16ToHost [:pick $ad 12 16]]
        :local accelX [$from88 [:pick $ad 16 20]]
        :local accelY [$from88 [:pick $ad 20 24]]
        :local accelZ [$from88 [:pick $ad 24 28]]
        :local temp [$from88 [:pick $ad 28 32]]
        :local uptime [$le32ToHost [:pick $ad 32 40]]
        :local flags [:tonum "0x$[:pick $ad 40 42]"]
        :local bat [:tonum "0x$[:pick $ad 42 44]"]

        :put ("$advCount: \
            address=$address \
            ts=$epoch \
            rssi=$rssi \
            version=$version \
            encrypted=$encrypted \
            salt=$salt \
            accelX=$accelX \
            accelY=$accelY \
            accelZ=$accelZ \
            temp=$temp \
            uptime=$uptime \
            flags=\"$[$flagStr $flags]\" \
            bat=$bat" \
        )
        :set $advCount ($advCount + 1)
        :set $lastAdvTimestamp $epoch
    }
    :if ($advCount > 0) do={
        :set $btOldestAdvertisementTimestamp $lastAdvTimestamp
    }

/tool fetch http-method=post output=user http-header-field="Content-Type:application/json" http-data=$message url="myserverurl"
I get temperature data on Mikrotik terminal, but I dont get anything on my server. I am sure problem is in last line, but I could not find solution in last days..
Will be glad for any help!
Last edited by BartoszP on Wed Dec 28, 2022 8:49 am, edited 1 time in total.
Reason: Use proper tags to make posts more readable

Who is online

Users browsing this forum: loloski, massinia and 18 guests