Site Tools


integration_with_openhab

OpenHab integration

Overview

The approach to control this units is bash scripts based, so you will need to run OpenHab in a linux box to use this scripts.

To use this scripts it's necessary to know the IP of the relay and to pair the RF devices with the SkyDance App. Those scripts are not able to discover the Wifi relay neither pair the RF devices.

This is the structure of the files:

/etc/init.d/mqtt_client    # mqtt daemon
/etc/default/mqtt_client   # mqtt daemon default values
/etc/mqtt_client/devices   # Configuration for paired RF devices
/etc/mqtt_client/scripts   # Scripts to operate the devices
/etc/mqtt_client/conf      # Configuration of Wifi relay

The working process is as follows:

  • There's a init.d daemon (mqtt_client) which listens to a mqtt topic (i.e. /wifirelay/light1/command)
  • Based on the topic received, this daemon calls a script placed in the scripts directory and send the byte code to the device

Scripts

Scripts and configuration of Relay

The configuration and operation scripts of each Wifi relay are placed in /etc/mqtt_client.

Configuration

Each Wifi relay as it's own configuration file placed in /etc/mqtt_client/conf/ there will be one config file for each Wifi Relay. At the moment It's only probed for 1 Wifi relay. Test should be done to use more than 1.

Files look like this:

# filename: wifirelay.conf
# Parameters of the TCP listener
IP="192.168.1.239"
PORT="8899"
LOGFILE="/var/log/mqtt_client.log"
LOGDATEFORMAT="%Y-%m-%d %H:%M:%S.%3N"
# Minimum value at which the relay should turn of LED
MINDIM=5

Operation script

This script is called from MQTT Daemon and gets 2 parameters:

  • The MQTT topic
  • The MQTT value for this topic

The script should be also called from console to get the paired RF devices and generate a configuration file in /etc/mqtt_client/devices/ every time a new RF device is paired.

#!/bin/bash
# Here's where we convert every topic read to the string or hex
# to send to the TCP listener

# Read parameters of the TCP listener
# Only for 1 relay. Easy to expand to multiple
[ -f  /etc/mqtt_client/conf/wifirelay.conf ] && . /etc/mqtt_client/conf/wifirelay.conf || exit 1
DEVICES_FILE="/etc/mqtt_client/devices/wifirelay.dev"
OPID="00"
OPSKYFILE="/tmp/opsky"

do_log()
{
        echo "`date +\"$LOGDATEFORMAT\"` [/etc/init.d/mqtt_client...] - $1" >> $LOGFILE
}

add_opid()
{
        [ -r $OPSKYFILE ] && OPSKY=$(cat $OPSKYFILE) || OPSKY=1

        if [ "$OPSKY" -lt 255 ]; then
                OPSKY=$(( $OPSKY + 1 ))
        else
                OPSKY=1
        fi
        echo $OPSKY > $OPSKYFILE
        OPID=$(printf "%02x" $OPSKY)
}
get_devices()
{
        B="55aa5aa57e"
        E="007e"

        # Ask Relay ID
        R=$(echo $B"00000000010000000100700000"$E | xxd -r -p | netcat -w1 $IP $PORT | xxd -p)
        RC=${R:22:4}
        echo "Relay code: "$RC
        echo "Devices discovered:"
        # Loop $I and $N (1,2,3... 1,2,4,8...)
        # To ask every DIM device and print name and code
        >$DEVICES_FILE
        I=0
        while true; do
                N=$(printf "%02x" $((2**I)))
                I=$(printf "%04x" $(( $I + 1 )))
                #echo "N: "$N
                #echo "I: "$I
                add_opid
                # echo "--$OPID--"
                # echo $B$OPID"c00c000100"$RC$N"007301"$I$E
                R=$(echo $B$OPID"c00c000100"$RC$N"007301"$I$E | xxd -r -p | netcat -w1 $IP $PORT | xxd -p)
                [ "${R:44:4}" == "0000" ] && break
                CODE=${R:44:4}${R:26:4}
                NAME=$(echo ${R:52:30} | xxd -r -p)
                echo "Code: "$CODE
                echo "Name: "$NAME
                echo ""
                echo -e "Enter the subtopic for this device and press [ENTER]: \c"
                read TOPIC
                echo ""
                # Generate devices file
                echo "[$TOPIC]" >> $DEVICES_FILE
                echo "name="$NAME >> $DEVICES_FILE
                echo "code="$CODE >> $DEVICES_FILE
                echo "" >> $DEVICES_FILE
        done
        echo "Great! Settings successfully saved into $DEVICES_FILE"
}

dim() {
        DIM=$2
        CODE=$1
        DIMHEX=$(printf "%02x" $(( $DIM*255/100 )))
        # echo "DIM: $DIMHEX"
        add_opid
        ON="55aa5aa57e"$OPID"0101000100"$CODE"0a010001007e"
        OF="55aa5aa57e"$OPID"0101000100"$CODE"0a010000007e"
        add_opid
        do_log "OPID: $OPID"
        if [ $DIM -lt $MINDIM ]; then
                do_log "$OF"
                echo $OF | xxd -r -p | ncat $IP $PORT
        else
                do_log "$ON"
                echo $ON | xxd -r -p | ncat $IP $PORT
                add_opid
                do_log "55aa5aa57e"$OPID"0101000100"$CODE"010700"$DIMHEX"000000000001007e ->"$IP":"$PORT
                echo "55aa5aa57e"$OPID"0101000100"$CODE"010700"$DIMHEX"000000000001007e" | xxd -r -p | ncat $IP $PORT
                # echo $ON | xxd -r -p | ncat $IP $PORT
        fi
}
case "$1" in
    get_devices)
        get_devices
        ;;
    *)
        TOPIC=$1
        VALUE=$2
        SUBTOPIC=$(echo "$TOPIC" | cut -d '/' -f 3)
        COMMAND=$(echo "$TOPIC" | cut -d '/' -f 4)
        CODE=$(sed -nr "/^\[$SUBTOPIC\]/ { :l /^code[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" $DEVICES_FILE)
        if [ "$COMMAND" = "command" ]; then
                case $VALUE in
                        "ON" ) dim $CODE 100 ;;
                        "OFF" ) dim $CODE 0 ;;
                        * ) dim $CODE $VALUE ;;
                esac
        fi
    ;;
esac

MQTT Client daemon

This daemon runs in the background.

#! /bin/sh

### BEGIN INIT INFO
# Provides:          mqtt_client
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start mosquitto client
# Description:       Start MQTT client
### END INIT INFO

# Author: Jaume Obrador <obrador@espaiweb.net>

DESC="Mosquitto CLient"
NAME=mqtt_client
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
SUB_BIN="/usr/bin/mosquitto_sub"
TOPICS=""

. /lib/lsb/init-functions

[ -r /etc/default/$NAME ] && . /etc/default/$NAME

do_log()
{
        echo "`date +\"$LOGDATEFORMAT\"` [/etc/init.d/mqtt_client...] - $1" >> $LOGFILE
}

do_start()
{
        for f in "$SCRIPTDIR/"*".sh"
        do
                file=$(basename -- "$f")
                topic="${file%.*}"
                TOPICS="-t /$topic/# $TOPICS"
        done
        do_log "Starting $DESC. $SUB_BIN listening at $TOPICS"
        $SUB_BIN -R -v -h $HOST $TOPICS | while IFS=" " read topic value
        do
                CONVERTER=$SCRIPTDIR"/"$(echo $topic | cut -d '/' -f 2)".sh $topic $value"
                do_log "Received $topic $value. Calling $CONVERTER"
                $CONVERTER
        done &
}

do_stop() {
  pkill -f $SUB_BIN
  do_log "Stopping $DESC"
  return 0
}

case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
    do_start
    ;;

  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    case "$?" in
      0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
    
  status)
    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;

  restart)
    log_daemon_msg "Restarting $DESC" "$NAME"
    do_stop
    case "$?" in
      0|1)
        do_start
        ;;
      *)
        log_end_msg 1
        ;;
    esac
    ;;

  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
    exit 3
    ;;
esac
integration_with_openhab.txt · Last modified: 2019/03/12 10:11 by 80.25.41.111