Announcement

Collapse
No announcement yet.

Unifi Software interface for Ubiquiti networks (using MQTT)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unifi Software interface for Ubiquiti networks (using MQTT)

    Just installed this at home, works a treat. You are able to get information from your Unifi software and populate devices in HS using MQTT

    https://github.com/hobbyquaker/unifi2mqtt

    Pete
    HS 2.2.0.11

  • #2
    petez69

    A bit of a tangent....

    Here have been using modded micro OpenWRT travel routers for testing the SonOff WiFi modules. Latest test product here has been the GL.iNet micro router with exposed GPIO ports. These are cheap here. The GL.iNet microrouter has a bit more play space for programs.

    Click image for larger version  Name:	image_70455.jpg Views:	1 Size:	55.8 KB ID:	1238232

    Current test device and OS is:

    Code:
    BusyBox v1.25.1 () built-in shell (ash)
    
    _________
    / /\ _ ___ ___ ___
    / LE / \ | | | __| \| __|
    / DE / \ | |__| _|| |) | _|
    /________/ LE \ |____|___|___/|___| lede-project.org
    \ \ DE /
    \ LE \ / -----------------------------------------------------------
    \ DE \ / Reboot (17.01.4, r3560-79f57e422d)
    \________\/ -----------------------------------------------------------
    uname -a
    Linux ICS-1 4.4.92 #0 Tue Oct 17 17:46:20 2017 mips GNU/Linux

    Python installation
    1 - opkg update
    2 - opkg install python-light

    Mosquitto installation:

    1 - opkg update
    2 - opkg install mosquitto mosquitto-client libmosquitto
    Package mosquitto-nossl (1.4.14-1) installed in root is up to date.
    Package mosquitto-client-nossl (1.4.14-1) installed in root is up to date.
    Package libmosquitto-nossl (1.4.14-1) installed in root is up to date.

    Using your OpenWRT Router's Wifi to detect if a person's smartphone is still in/near the apartment and publish via MQTT

    A simple shell script to detect presence of Wifi devices (smartphones, tablets, Amazon Dash Buttons, ..) and post the results via MQTT. This information can be processed in Homeautomation Systems like OpenHAB to turn down the heating when everyone left the appartment.
    • mosquitto-client
    • coreutils-nohup
    opkg install coreutils-nohup

    Configuring coreutils.
    Configuring coreutils-nohup.


    Installing coreutils-nohup (8.23-3) to root...Use SCP to copy the presence_report script to /usr/bin/presence_report on the target device. Call chmod u+x /usr/bin/presence_report to allow script execution.

    presence_report

    Code:
     #!/bin/sh
    
    DEFAULT_MODE="event"
    DEFAULT_MQTT_SERVER="10.1.1.50"
    DEFAULT_LAST_SEEN_UPDATE_PERIOD_S=120
    
    MODE=$DEFAULT_MODE
    MQTT_SERVER=$DEFAULT_MQTT_SERVER
    LAST_SEEN_UPDATE_PERIOD_S=$DEFAULT_LAST_SEEN_UPDATE_PERIOD_S
    
    MQTT_ID_EVENT="OpenWRT-Presence-Event"
    MQTT_ID_LASTSEEN="OpenWRT-Presence-LastSeen"
    MQTT_TOPIC="owrtwifi/status/mac-"
    
    SCRIPT_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
    
    
    # Parse command line args
    test_for_mode(){
      param_mode=$1
      if [ "$param_mode" == "event" -o "$param_mode" == "lastseen" ]; then
        MODE=$param_mode
        return 0
      fi
      return 1
    }
    
    test_for_ipv4(){
      param_ip=$1
      echo $param_ip | grep -E '\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b' > /dev/null
      if [ $? -eq 0 ]; then
        MQTT_SERVER=$param_ip
        return 0
      fi
      return 1
    }
    
    test_for_update_periode_s(){
      param_up=$1
      expr $param_up : '[0-9][0-9]*$'
      if [ $? -eq 0 ]; then
        LAST_SEEN_UPDATE_PERIOD_S=$param_up
        return 0
      fi
      return 1
    }
    
    print_usage(){
    cat << EOF
    Supported optional parameters:
      mode: "event" or "lastseen" (default: $DEFAULT_MODE)
        In event mode changes of registered mac addresses are imediately pushed to the MQTT server
        In lastseen mode the registered mac addresses are periodically pushed to the MQTT server
      MQTT server IP: the IPv4 address of the MQTT server (default $DEFAULT_MQTT_SERVER)
      Udate periode [s]: only relevant for lastseen mode  (default $LAST_SEEN_UPDATE_PERIOD_S)
    Examples:
      $SCRIPT_NAME
      $SCRIPT_NAME 192.168.1.2 *
      $SCRIPT_NAME lastseen 300
    EOF
    }
    
    for param in "$@"; do
      test_for_mode $param || \
      test_for_ipv4 $param || \
      test_for_update_periode_s $param || \
      { print_usage; exit 1; }
    done
    
    
    if [ "$MODE" == "event" ]; then
      echo "$SCRIPT_NAME, mode: $MODE, MQTT server: $MQTT_SERVER"
      iw event | \
      while read LINE; do
        if echo $LINE | grep -q -E "(new|del) station"; then
          EVENT=`echo $LINE | awk '/(new|del) station/ {print $2}'`
          MAC=`echo $LINE | awk '/(new|del) station/ {print $4}'`
    
          echo "Mac: $MAC did $EVENT"
          mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_EVENT -t "$MQTT_TOPIC${MAC//:/-}/event" -m $EVENT
        fi
      done
    elif [ "$MODE" == "lastseen" ]; then
      echo "$SCRIPT_NAME, mode: $MODE, MQTT server: $MQTT_SERVER, period: $LAST_SEEN_UPDATE_PERIOD_S"
      while true
      do
        for interface in `iw dev | grep Interface | cut -f 2 -s -d" "`
        do
          # for each interface, get mac addresses of connected stations/clients
          maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "`
          for mac in $maclist
          do
            echo "lastseen epoch   ${mac//:/-} $(date +%s)"
            mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_LASTSEEN -t "$MQTT_TOPIC${mac//:/-}/lastseen/epoch" -m "$(date +%s)" -r
            echo "lastseen iso8601 ${mac//:/-} $(date +%Y-%m-%dT%H:%M:%S%z)"
            mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_LASTSEEN -t "$MQTT_TOPIC${mac//:/-}/lastseen/iso8601" -m "$(date +%Y-%m-%dT%H:%M:%S%z)" -r
          done
        done
        sleep ${LAST_SEEN_UPDATE_PERIOD_S}
      done
    fi
    Add the script to rc.local


    Place the following lines
    • nohup /usr/bin/presence_report event 192.168.1.2* >/dev/null 2>&1 &
    • nohup /usr/bin/presence_report lastseen 192.168.1.2* >/dev/null 2>&1 &

    inside the /etc/rc.local file before the exit 0.
    Bold IP * = broker IP.

    You can to this via command-line or via LuCI in System -> Startup -> Local Startup.

    The script will be executed after reboot.

    Usage


    After installation the following topics will be published for each WiFi device, using the lowercase MAC address:

    owrtwifi/status/mac-00-00-00-00-00-00/lastseen/iso8601

    Payload contains the timestamp when the device was seen in an ISO 8601 (and OpenHAB) compatible format, like this: 2017-08-25T19:29:57+0200

    owrtwifi/status/mac-00-00-00-00-00-00/lastseen/epoch

    Unix epoch in seconds
    owrtwifi/status/mac-00-00-00-00-00-00/event

    Message will be new or del and is sent right after the device connected/disconnected to/from WiFi.

    Another tangent install syslog_ng with a custom configuration for Mosquitto and trying that one.

    mcsMQTT sees:

    Note the common IP mentioned above in the rc.local and script is the IP of the broker.

    Sub: ICSOpenWRT/status/mac-2c-3a-e8-44-ad-c3/lastseen/iso8601 2018-09-01T11:31:57-0500

    Sub: ICSOpenWRT/status/mac-2c-3a-e8-44-ad-c3/lastseen/epoch 1535819637

    Sub: ICSOpenWRT/status/mac-28-b2-bd-4e-79-83/event del

    when I reboot the test Sonoff wifi basic #2 test module.

    Noticed if I reboot the OpenWRT micro router then the SonOff disconnects and goes to blinky mode. To get it to reconnect I unpower the Sonoff and power it back up.

    Click image for larger version  Name:	OOpenWRT-Mosquitto.jpg Views:	1 Size:	104.9 KB ID:	1245041

    You can track other wireless devices and traffic with the OpenWRT OS. You can get more granular specific to Mosquitto logging using systemlog-ng with a Mosquitto only configuration. Only issue is the space required for the logging on the OpenWRT micro router. Then you go to using a USB stick for storage.

    Note too that the Gli.net microrouter has two different GUIs. One easy peasey and the OpenWRT GUI. This is identical to the Almond Plus automation combo router touchscreen device.
    Last edited by Pete; September 1st, 2018, 07:03 PM.
    - Pete

    Auto mator
    Homeseer 3 Pro - 3.0.0.500 (Linux) - Ubuntu 18.04 64 bit Intel CPU - Mono 5.18
    Homeseer Zee2 (Lite) - 3.0.0.531 (Linux) - Ubuntu 18.04/W7e BeeLink 4Gb BT3 Pro - Mono 5.20

    Testing Mini Intel quad core 4Gb Ram - 64Gb eMMC - micro PC

    X10, UPB, Zigbee, ZWave and Wifi MQTT automation.

    Comment


    • #3
      how does this work exaclty? i can't figure out how to install it
      HS3 Pro on Windows 8 64bit
      53 Z-wave nodes(46 devices, 7 remotes), 15 DS10a's, 10 ms16a's, 9 Oregon Sensors, W800, RFXCOMtrx433, Way2Call, 3 HSTouch Clients, 2xRussound CAS44, Global Cache GC100-12,10 Rollertrol blinds(+ zwave) ,3 Squeezebox Radios and 1 Squeezebox Boom,DMX Arduino via ethernet,Rain8Net,3x Echo Dot's


      Check out my electronics blog here:
      https://www.facebook.com/RaptorsIrrationalInventions

      Comment


      • #4
        It is just a Python script running.

        Both Ubiquiti and OpenWRT run a simple linux.

        For OpenWRT I install Mosquitto first then Python and test run the script and then configure to auto start the script on boot.

        - Pete

        Auto mator
        Homeseer 3 Pro - 3.0.0.500 (Linux) - Ubuntu 18.04 64 bit Intel CPU - Mono 5.18
        Homeseer Zee2 (Lite) - 3.0.0.531 (Linux) - Ubuntu 18.04/W7e BeeLink 4Gb BT3 Pro - Mono 5.20

        Testing Mini Intel quad core 4Gb Ram - 64Gb eMMC - micro PC

        X10, UPB, Zigbee, ZWave and Wifi MQTT automation.

        Comment


        • #5
          Just installed this at home, works a treat. You are able to get information from your Unifi software and populate devices in HS using MQTT

          https://github.com/hobbyquaker/unifi2mqtt

          Pete
          This looks like a sibling of zigbee2mqtt. Of course one needs to have the Unifi controller running. Only recently have I installed the controller on a RPi. Previoulsy I only started it up when I needed to do something with my AP.

          I believe Pete is showning a different approach based upon Python rather than what I guess is node.js for what Petez69 is showing. In the Petez69 approach the install is with "sudo npm install -g unifi2mqtt", but this assumes dependencies have already been installed. The zigbee2mqtt wiki https://github.com/Koenkk/zigbee2mqt...ing-the-bridge shows the steps including dependencies for the zigbee connector.

          Comment


          • #6
            Made this work, im seeing about 11 seconds to detect a connect and about 5 mins to detect a disconnect.

            note unifi controller is assumed to be running on the same machine.


            here's how to install this from windows (borrowed most of this from Michaels zigbee tutorial:

            1. Install node.js from https://nodejs.org/en/download/. I used the Windows Installer (.msi).

            2. Create folder to place unifi2MQTT. I used C:\unifi2mqtt, but likely can be anywhere. The remaining instructions use this location.

            3. Open command window as administrator (Windows search for "cmd", right click, run as administrator)

            4. Clone unifi2MQTT from git repository from command window. (git clone https://github.com/hobbyquaker/unifi2mqtt.git C:\unifi2mqtt). If git is not yet installed on Windows then it can be from https://git-scm.com/download/win . You CAN download the zip and expand it into a Windows folder which is how i did it.


            5. Navigate to the install folder (CD C:\unifi2mqtt) in Command Window

            5a. (added after initial post). Install unifi2mqtt dependencies. From command prompt run "npm install"

            6. Run unifi2mqttfrom Command Window (unifi2mqtt [options]). Feedback will be in the Command Window. You should also observe the MQTT message being online on your MQTT client, such as mcsMQTT. If it does not work then it is possible that there is feedback.
            i had to use: "unifi2mqtt -k -u mqtt://127.0.0.1 -s --unifi-password whateveryourunifipasswordis"

            7. To run on windows startup I followed the following process. While it may not be the most elegant it does work:
            8b. Create batch file that will be used to start the js application. I called it unifi2mqtt.bat with one line contents or "unifi2mqtt -k -s whateveryourunifipasswordis"
            8c. Create shortcut to the bat file (right click, create shortcut)
            8d. Copy/Paste shortcut into Windows startup folder. In my case it was at C:\Users\Dell\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup for user Dell
            HS3 Pro on Windows 8 64bit
            53 Z-wave nodes(46 devices, 7 remotes), 15 DS10a's, 10 ms16a's, 9 Oregon Sensors, W800, RFXCOMtrx433, Way2Call, 3 HSTouch Clients, 2xRussound CAS44, Global Cache GC100-12,10 Rollertrol blinds(+ zwave) ,3 Squeezebox Radios and 1 Squeezebox Boom,DMX Arduino via ethernet,Rain8Net,3x Echo Dot's


            Check out my electronics blog here:
            https://www.facebook.com/RaptorsIrrationalInventions

            Comment


            • #7
              If anyone has successfully installed this on Linux, I'm assuming on the MQTT broker RPi, please do a quick write up.

              Thanks,

              Jim

              Comment


              • #8
                Im not a linux guy but this should work (also borrowed from zigbee2mqtt) i have not tested this so please reply with what happened
                Code:
                # Setup Node.js repository
                sudo curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
                
                # Install Node.js
                
                sudo apt-get install -y nodejs git make g++ gcc
                
                # Verify that the correct nodejs and npm (automatically installed with nodejs)
                # version has been installed
                node --version # Should output v10.X
                npm --version # Should output 6.X
                
                # Clone unifi2mqtt repository
                sudo git clone https://github.com/hobbyquaker/unifi2mqtt.git /opt/unifi2mqtt
                sudo chown -R pi:pi /opt/unifi2mqtt # Install dependencies cd /opt/unifi2mqtt npm install
                
                #then to run
                sudo unifi2mqtt -k -s whateveryourunifipasswordis
                HS3 Pro on Windows 8 64bit
                53 Z-wave nodes(46 devices, 7 remotes), 15 DS10a's, 10 ms16a's, 9 Oregon Sensors, W800, RFXCOMtrx433, Way2Call, 3 HSTouch Clients, 2xRussound CAS44, Global Cache GC100-12,10 Rollertrol blinds(+ zwave) ,3 Squeezebox Radios and 1 Squeezebox Boom,DMX Arduino via ethernet,Rain8Net,3x Echo Dot's


                Check out my electronics blog here:
                https://www.facebook.com/RaptorsIrrationalInventions

                Comment

                Working...
                X