o 2015.2.23 リリース
o 2015.10.15 タイマーを5分へ変更
o リトライタイマー変更
o 2016.1.20バージョンアップ
o I2C温湿度計AM2321センサーのリード機能追加
o Tocos TWE-Lite DIP Ver1.6以上対応でチェックサム付加は省略しています
o 2016.10.31バージョンアップ
o 受信時にチェックサムをチェックするようにした
o AI1~4のアナログ入力リードコマンド追加
o 2017.3.23バージョンアップ
o ライトアフターリードコマンド追加
o ウエイトリードコマンド追加
pepotocsctl単独では動作しません、別途epiconが必要です
Tocos無線モジュールのDIOとAM2321センサーのみをコマンドで操作できます
root@tmp# pepotocsctl
root@tmp# pepotocosctl
usage /usr/local/bin/pepotocosctl read->chno:0-6 | write->chno:0-6 [DO:0|1|f] to 300000ms
DI(デジタルインプット)の4ポート分リード
root@tmp# pepowirelessdioctl 0 7
DI(デジタルインプット)の一番ポートリード
root@tmp# pepowirelessdioctl 1 1
DO(デジタルアウトプウト)一番ポートへ1をライト
root@tmp# pepowirelessdioctl 1 1 1
DO(デジタルアウトプウト)一番ポートへ1をライト、5秒間保持
root@tmp# pepowirelessdioctl 1 1 5000 0
I2C温湿度計AM2321センサーのリード
root@tmp# ./pepowirelessdioctl 5
2016/01/20,19:16:19,4.0℃,75.4%
アナログ入力リード
root@tmp# pepotocosctl 6
4080,4080,4080,4080
ライトアフターリード
root@tmp# pepotocosctl 0 0
7;4080,4080,4080,4080
デジタルインプットとアナログインプットリード
root@tmp# pepotocosctl 7
f;1952,4080,4080,4080
リードウエイト・DIの変化を指定時間待つ
root@tmp# pepotocosctl 7 120
e;4080,4080,4080,4080
Raspberry_piとTocos無線モジュールのコラボ動画→https://www.youtube.com/watch?v=-_FqOdLbzMw
- #!/bin/sh
- # licence GPLv2 ; this scripts designed by Isamu.Yamauchi 2015.2.10 update 2017.4.25 for i386 & raspberry pi
- # pepotocsctl for contorl TWE-Lite DIP:Ver1.6 or later(Tocos wireless engine) digital-IN/digital-OUT signal.
- # pepotocsctl $1:ch[0-7] [$2:0-f] [$3: timer->1 to 300000ms]
- PATH=$PATH:/usr/local/bin:/usr/local/sbin
- usage(){
- echo "usage $0 read->chno:0-6 | write->chno:0-7 [DO:0|1|f] [timer->1 to 300000ms]" >/dev/stderr
- exit
- }
- if [ $# -eq 0 ];then
- usage
- elif [ "$1" -gt 7 -o "$1" -lt 0 ];then
- usage
- elif [ "$1" -eq 6 -a $# -ne 1 ];then
- usage
- elif [ $# -eq 2 -a "$1" -eq 0 ];then
- ALLDO=NO
- if [ $((0x$2 - 0)) -gt $((0xf - 0)) -o $((0x$2 - 0)) -lt 0 ];then
- usage
- else
- ALLDO=YES
- fi
- elif [ $# -eq 3 ];then
- if [ "$2" -gt 0 -a "$2" -gt 1 -o "$3" -gt 300000 -o "$3" -lt 1 ];then
- usage
- fi
- fi
- prog=pepotocsctl
- HEDER=":"
- # am2321 i2c adderess 5C
- I2CADDR="5C"
- I2CHEAD="7888AA"
- I2COK="01"
- I2CNG="00"
- I2CTEPHUM="03020004"
- I2CWRITE="01"
- I2CREAD="02"
- I2CREAD_CT="0006"
- I2CWRITE_CT="0000"
- I2CAM2321_WAIT=1000
- I2CREADCT=28
- ADDR="788001"
- DUMMY="0000000000000000"
- DO4ON="0808"
- DO4OF="0008"
- ALLON="0F0F"
- ALLOF="0F00"
- ZEROHUM="0000"
- TEMPMAX="80"
- HUMMAX="100"
- WAIT=50
- TRY=5
- READTM=1
- READCT=49
- pid=`echo -en $$`
- CHECKPG_SUM=/usr/local/bin/pepochecksum
- DIR=/www/remote-hand/tmp
- ALIAS_DI=${DIR}/.alias_di
- CMD="${DIR}/.$prog.${pid}"
- TCOSWD=$DIR/.tocos_write_data
- tTCOSWD=$DIR/.tocos_write_data_tmp
- TCOSRD=$DIR/.tocos_read_data
- tTCOSRD=$DIR/.tocos_write_data_tmp
- [ -e $ALIAS_DI ] && . "$ALIAS_DI"
- [ ! -z "${TOCOS_TTY}" ] && DIO_DEV="/dev/"${TOCOS_TTY} || DIO_DEV=/dev/none_tocos
- if [ ! -e $DIO_DEV ];then
- [ -e $TCOSWD ] && rm -f $TCOSWD
- [ -e $TCOSRD ] && rm -f $TCOSRD
- [ -e $tTCOSWD ] && rm -f $tTCOSWD
- [ -e $tTCOSRD ] && rm -f $tTCOSRD
- echo -en "-1" >/dev/stderr
- exit
- fi
- LOCK=/var/lock/LCK..${TOCOS_TTY}
- error(){
- [ -e ${CMD} ] && rm -f ${CMD}
- exit 0
- }
- trap error SIGHUP SIGTERM SIGQUIT SIGKILL SIGINT SIGCHLD
- CMD_DIO="/usr/local/bin/epicon -s 115200 -ql ${DIO_DEV} -c ${CMD}"
- send_dio(){
- local send d4off d4on tmp SD RD retry_time
- SD=$1
- tmp=${ADDR}${SD}${DUMMY}
- send=${HEDER}${tmp}X
- tmp=${ADDR}${DO4OF}${DUMMY}
- d4of=${HEDER}${tmp}X
- tmp=${ADDR}${DO4ON}${DUMMY}
- d4on=${HEDER}${tmp}X
- RETRY=$TRY
- while [ -e $LOCK ]
- do
- retry_time=`echo -en $RANDOM |cut -c 1-3`
- msleep $retry_time
- RETRY=$(($RETRY - 1))
- if [ $RETRY -eq 0 ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- done
- cat >${CMD}<<EOF
- #!/bin/sh
- RETRY=$TRY
- while [ \${RETRY} -ne 0 ];do
- retry_time=\`echo -en \$RANDOM |cut -c 1-2\`
- echo -en "${send}\\r\\n"
- msleep $WAIT
- echo -en "${d4on}\\r\\n"
- msleep $WAIT
- read -s -n ${READCT} -t ${READTM} RD || RD="-1"
- echo -en "${d4of}\\r\\n"
- TMP=\`echo -en \${RD} | wc -c\`
- [ \${TMP} -eq ${READCT} ] && break
- RETRY=\$((\${RETRY} - 1))
- msleep \$retry_time
- RD="-1"
- done
- echo -en \${RD} >/dev/stderr
- EOF
- msleep ${WAIT}
- chmod +x ${CMD}
- ${CMD_DIO}
- }
- i2c_read(){
- local send read retry_time tmp
- I2CCMD="00"
- I2CRES="00"
- RETRY=$TRY
- while [ -e $LOCK ]
- do
- retry_time=`echo -en $RANDOM |cut -c 1-3`
- msleep $retry_time
- RETRY=$(($RETRY - 1))
- if [ $RETRY -eq 0 ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- done
- # wake-up
- tmp=${I2CHEAD}${I2CWRITE}${I2CADDR}${I2CWRITE_CT}
- wake_up=${HEDER}${tmp}X
- # temperature & Humidity start command
- tmp=${I2CHEAD}${I2CWRITE}${I2CADDR}${I2CTEPHUM}
- send=${HEDER}${tmp}X
- # temperature & Humidity read command
- tmp=${I2CHEAD}${I2CREAD}${I2CADDR}${I2CREAD_CT}
- read=${HEDER}${tmp}X
- cat >${CMD}<<EOF
- #!/bin/sh
- RETRY=$TRY
- I2CRD="-1"
- while [ \${RETRY} -ne 0 ];do
- retry_time=\`echo -en \$RANDOM |cut -c 1-2\`
- echo -en "${wake_up}\\r\\n"
- msleep ${WAIT}
- read -s -t ${READTM} I2CRD || I2CRD="-1"
- echo -en "${send}\\r\\n"
- msleep ${WAIT}
- read -s -t ${READTM} I2CRD || I2CRD="-1"
- msleep ${AM2321_WAIT}
- echo -en "${read}\\r\\n"
- msleep ${WAIT}
- read -s -n ${I2CREADCT} -t ${READTM} I2CRD || I2CRD="-1"
- TMP=\`echo -en \${I2CRD} | wc -c\`
- [ \${TMP} -eq ${I2CREADCT} ] && break
- RETRY=\$((\${RETRY} - 1))
- [ \${RETRY} -eq 0 ] && break
- RETRY=\$((\${RETRY} - 1))
- msleep \$retry_time
- I2CRD="-1"
- done
- echo -en \${I2CRD} >/dev/stderr
- EOF
- chmod +x ${CMD}
- ${CMD_DIO}
- }
- read_temp_hum(){
- local RD TMP CHECK_DATA SUM RDCHECKPG_SUM
- RD="-1"
- RD=`i2c_read 2>&1`
- TMP=`echo -en ${RD} |wc -c`
- if [ $TMP -ne ${I2CREADCT} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- CHECK_DATA=`echo -en ${RD}|awk '{printf (substr($1,2,length($1)-2))}'`
- RDCHECKPG_SUM=`echo -en ${RD} |awk '{printf(substr($1,length($1)-2,2))}'`
- SUM=`${CHECKPG_SUM} ${CHECK_DATA}`
- if [ ${RDCHECKPG_SUM} != ${SUM} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- HUMTMP=`echo -en ${RD} |awk '{printf(substr($1,18,4))}'`
- if [ $HUMTMP = $ZEROHUM ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- COMMAND=`echo -en ${RD} |awk '{printf(substr($1,8,2))}'`
- if [ $COMMAND != $I2CREAD ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- RESULT=`echo -en ${RD} |awk '{printf(substr($1,10,2))}'`
- if [ $RESULT != $I2COK ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- NOWDATE=`date '+%Y/%m/%d,%H:%M:%S'|awk '{printf("%s",$1)}'`
- HUMH=`echo -en ${RD} |awk '{printf(substr($1,18,2))}'`
- HUMLH=`echo -en ${RD} |awk '{printf(substr($1,20,1))}'`
- HUMLL=`echo -en ${RD} |awk '{printf(substr($1,21,1))}'`
- DHUMH=$((0x$HUMH * 256))
- DHHML=$((0x$HUMLH * 16))
- HUM=$(($(($(($DHUMH + $DHHML)) + 0x$HUMLL)) / 10))
- HUMMOD=$(($(($(($DHUMH + $DHHML)) + 0x$HUMLL)) % 10))
- TEMPH=`echo -en ${RD} |awk '{printf(substr($1,22,2))}'`
- TEMPLH=`echo -en ${RD} |awk '{printf(substr($1,24,1))}'`
- TEMPLL=`echo -en ${RD} |awk '{printf(substr($1,25,1))}'`
- if [ $((0x$TEMPH & 0x80)) != 0 ];then
- TEMPH=$((0x$TEMPH & 0x7F))
- TMP="minus"
- else
- TMP="plus"
- fi
- DTEMPH=$((0x$TEMPH * 256))
- DTEMPLH=$((0x$TEMPLH * 16))
- TEMP=$(($(($(($DTEMPH + $DTEMPLH)) + 0x$TEMPLL)) / 10))
- TEMPMOD=$(($(($(($DTEMPH + $DTEMPLH)) + 0x$TEMPLL)) % 10))
- if [ ${TEMP} -gt ${TEMPMAX} -o ${HUM} -gt ${HUMMAX} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- echo -en ${NOWDATE} ${TMP} ${TEMP} ${TEMPMOD} ${HUM} ${HUMMOD}|awk '{
- I=""
- if ($2 != "plus") I="-"
- printf("%s,%s%s.%s℃,%s.%s%",$1,I,$3,$4,$5,$6)}'>/dev/stderr
- }
- write_dio(){
- # write_dio CH 0-1 Timer
- local RD SD CH DO DATA TM TMP
- [ $# -eq 3 ] && TM=$3 || TM=0
- SD=$(($2 ^ 1))
- case $1 in
- 0) CH="0F" ;;
- 1) CH="01" ;;
- 2) CH="02" ;;
- 3) CH="04" ;;
- 4) CH="08" ;;
- esac
- if [ $CH = "0F" ];then
- case $2 in
- 0) SD="0F" ;;
- 1) SD="0E" ;;
- 2) SD="0D" ;;
- 3) SD="0C" ;;
- 4) SD="0B" ;;
- 5) SD="0A" ;;
- 6) SD="09" ;;
- 7) SD="08" ;;
- 8) SD="07" ;;
- 9) SD="06" ;;
- A) SD="05" ;;
- B) SD="04" ;;
- C) SD="03" ;;
- D) SD="02" ;;
- E) SD="01" ;;
- F) SD="00" ;;
- esac
- DATA="$SD"${CH}
- RD=`send_dio ${DATA} 2>&1`
- TMP=`echo -en ${RD} | wc -c`
- [ ${TMP} != ${READCT} ] && RD="-1" || SD=$RD
- else
- [ $SD -eq 1 ] && DATA=${CH}${CH} || DATA="00"${CH}
- RD=`send_dio ${DATA} 2>&1`
- if [ ${TM} -ne 0 ];then
- SD=$(($SD ^ 1))
- [ `echo -en ${RD} | wc -c` != ${READCT} ] && RD="-1" || RD=$2
- if [ ${RD} != "-1" ];then
- msleep ${TM}
- [ $SD -eq 1 ] && DATA=${CH}${CH} || DATA="00"${CH}
- RD=`send_dio ${DATA} 2>&1`
- fi
- fi
- SD=$(($SD ^ 1))
- TMP=`echo -en ${RD} | wc -c`
- [ ${TMP} != ${READCT} ] && RD="-1" || RD=$SD
- fi
- if [ ${RD} != "-1" ];then
- echo -en ${SD} >/dev/stderr
- else
- echo -en -1 >/dev/stderr
- fi
- }
- read_dio(){
- local CH XCH DATA RD RESULT TMP CHECK_DATA SUM RDCHECKPG_SUM
- case $1 in
- 0) CH="0F" ; XCH=0xff ;;
- 1) CH="01" ; XCH=0x01 ;;
- 2) CH="02" ; XCH=0x02 ;;
- 3) CH="04" ; XCH=0x04 ;;
- 4) CH="08" ; XCH=0x08 ;;
- esac
- DATA=${DO4OF}
- RD=`send_dio ${DATA} 2>&1`
- TMP=`echo -en ${RD} | wc -c`
- if [ ${TMP} != ${READCT} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- CHECK_DATA=`echo -en ${RD}|awk '{printf (substr($1,2,length($1)-2))}'`
- RDCHECKPG_SUM=`echo -en ${RD} |awk '{printf(substr($1,length($1)-1,2))}'`
- SUM=`${CHECKPG_SUM} ${CHECK_DATA}`
- if [ ${RDCHECKPG_SUM} != ${SUM} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- TMP=`echo -en ${RD} | awk '{i=substr(\$0,34,2);printf i}'`
- if [ $1 -eq 0 ];then
- DATA=$((0x${TMP} & 0x${CH}))
- DATA=$(($DATA ^ 0x0f))
- else
- DATA=$(($((0x${TMP} & 0x${CH})) / ${XCH}))
- DATA=$(($DATA ^ 0x01))
- fi
- printf "%1x" ${DATA} >/dev/stderr
- }
- read_ai(){
- local DATA RD TMP VOLT A1VOLT A2VOLT A3VOLT A4VOLT A1 A2 A3 A4 AISUB CHECK_DATA SUM RDCHECKPG_SUM
- DATA=${DO4OF}
- RD=`send_dio ${DATA} 2>&1`
- TMP=`echo -en ${RD} | wc -c`
- if [ ${TMP} != ${READCT} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- CHECK_DATA=`echo -en ${RD}|awk '{printf (substr($1,2,length($1)-2))}'`
- RDCHECKPG_SUM=`echo -en ${RD} |awk '{printf(substr($1,length($1)-1,2))}'`
- SUM=`${CHECKPG_SUM} ${CHECK_DATA}`
- if [ ${RDCHECKPG_SUM} != ${SUM} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- TMP=`echo -en ${RD} | awk '{i=substr(\$0,46,2);printf i}'`
- A1=`echo -en ${RD} | awk '{i=substr(\$0,38,2);printf i}'`
- VOLT=$((0x$A1 * 4))
- AISUB=$(($TMP & 0x03))
- A1VOLT=$(($(($VOLT + $AISUB)) * 4))
- A2=`echo -en ${RD} | awk '{i=substr(\$0,40,2);printf i}'`
- AISUB=$(($(($TMP >> 2 )) & 0x03))
- VOLT=$((0x$A2 * 4))
- A2VOLT=$(($(($VOLT + $AISUB)) * 4))
- A3=`echo -en ${RD} | awk '{i=substr(\$0,42,2);printf i}'`
- AISUB=$(($(($TMP >> 4 )) & 0x03))
- VOLT=$((0x$A3 * 4))
- A3VOLT=$(($(($VOLT + $AISUB)) * 4))
- A4=`echo -en ${RD} | awk '{i=substr(\$0,44,2);printf i}'`
- AISUB=$(($(($TMP >> 6 )) & 0x03))
- VOLT=$((0x$A4 * 4))
- A4VOLT=$(($(($VOLT + $AISUB)) * 4))
- printf "%4d,%4d,%4d,%4d" ${A1VOLT} ${A2VOLT} ${A3VOLT} ${A4VOLT} >/dev/stderr
- }
- wait_di(){
- local RD READWAIT TMP
- READWAIT=$1
- cat >${CMD}<<EOF
- #!/bin/sh
- RD="-1"
- read -s -n ${READCT} -t ${READWAIT} RD || RD="-1"
- if [ \${RD} = "-1" ];then
- echo -en "-1" >/dev/stderr
- exit
- fi
- TMP=\`echo -en \${RD} | wc -c\`
- if [ \${TMP} -eq ${READCT} ];then
- echo -en \${RD} >/dev/stderr
- else
- echo -en "-1" >/dev/stderr
- fi
- EOF
- msleep ${WAIT}
- chmod +x ${CMD}
- ${CMD_DIO}
- }
- cmd_di(){
- local RD TMP DATA
- RD=$1
- TMP=`echo -en ${RD} | awk '{i=substr(\$0,34,2);printf i}'`
- DATA=$((0x${TMP} & 0x0F))
- DATA=$(($DATA ^ 0x0f))
- printf "%1x" ${DATA} >/dev/stderr
- }
- cmd_ai(){
- local DATA RD TMP VOLT A1VOLT A2VOLT A3VOLT A4VOLT A1 A2 A3 A4 AISUB
- RD=$1
- TMP=`echo -en ${RD} | awk '{i=substr(\$0,46,2);printf i}'`
- A1=`echo -en ${RD} | awk '{i=substr(\$0,38,2);printf i}'`
- VOLT=$((0x$A1 * 4))
- AISUB=$(($TMP & 0x03))
- A1VOLT=$(($(($VOLT + $AISUB)) * 4))
- A2=`echo -en ${RD} | awk '{i=substr(\$0,40,2);printf i}'`
- AISUB=$(($(($TMP >> 2 )) & 0x03))
- VOLT=$((0x$A2 * 4))
- A2VOLT=$(($(($VOLT + $AISUB)) * 4))
- A3=`echo -en ${RD} | awk '{i=substr(\$0,42,2);printf i}'`
- AISUB=$(($(($TMP >> 4 )) & 0x03))
- VOLT=$((0x$A3 * 4))
- A3VOLT=$(($(($VOLT + $AISUB)) * 4))
- A4=`echo -en ${RD} | awk '{i=substr(\$0,44,2);printf i}'`
- AISUB=$(($(($TMP >> 6 )) & 0x03))
- VOLT=$((0x$A4 * 4))
- A4VOLT=$(($(($VOLT + $AISUB)) * 4))
- printf "%4d,%4d,%4d,%4d" ${A1VOLT} ${A2VOLT} ${A3VOLT} ${A4VOLT} >/dev/stderr
- }
- read_di_ai(){
- local DATA DATA1 RD TMP CHECK_DATA RDCHECKPG_SUM SUM
- DATA=${DO4OF}
- RD=`send_dio ${DATA} 2>&1`
- TMP=`echo -en ${RD} | wc -c`
- if [ ${TMP} != ${READCT} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- CHECK_DATA=`echo -en ${RD}|awk '{printf (substr($1,2,length($1)-2))}'`
- RDCHECKPG_SUM=`echo -en ${RD} |awk '{printf(substr($1,length($1)-1,2))}'`
- SUM=`${CHECKPG_SUM} ${CHECK_DATA}`
- if [ ${RDCHECKPG_SUM} != ${SUM} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- DATA=`cmd_di ${RD} 2>&1`
- DATA1=`cmd_ai ${RD} 2>&1`
- echo -en ${DATA}";"${DATA1} >/dev/stderr
- }
- read_di_ai_wait(){
- local DATA DATA1 RD TMP CHECK_DATA RDCHECKPG_SUM SUM
- DATA=$1
- RD=`wait_di ${DATA} 2>&1`
- TMP=`echo -en ${RD} | wc -c`
- if [ ${TMP} != ${READCT} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- TMP=`echo -en ${RD} | wc -c`
- CHECK_DATA=`echo -en ${RD}|awk '{printf (substr($1,2,length($1)-2))}'`
- RDCHECKPG_SUM=`echo -en ${RD} |awk '{printf(substr($1,length($1)-1,2))}'`
- SUM=`${CHECKPG_SUM} ${CHECK_DATA}`
- if [ ${RDCHECKPG_SUM} != ${SUM} ];then
- echo -en "-1" >/dev/stderr
- return
- fi
- DATA=`cmd_di ${RD} 2>&1`
- DATA1=`cmd_ai ${RD} 2>&1`
- echo -en ${DATA}";"${DATA1} >/dev/stderr
- }
- write_do_rd(){
- local RD TMP
- RD=`write_dio $1 $2 $3 2>&1`
- if [ "$RD" != "-1" ];then
- if [ $1 -eq 0 ];then
- DATA=`cmd_di ${RD} 2>&1`
- DATA1=`cmd_ai ${RD} 2>&1`
- echo -en ${DATA}";"${DATA1} >/dev/stderr
- return
- else
- echo -en ${RD} >/dev/stderr
- return
- fi
- fi
- echo -en ${RD} >/dev/stderr
- }
- if [ $# -eq 1 ];then
- if [ $1 -eq 5 ];then
- read_temp_hum
- elif [ $1 -eq 6 ];then
- read_ai
- elif [ $1 -eq 7 ];then
- read_di_ai
- else
- read_dio $1
- fi
- elif [ $# -eq 2 -a $1 -eq 7 ];then
- read_di_ai_wait $2
- elif [ $# -eq 2 ];then
- write_do_rd $1 $2
- elif [ $# -eq 3 ];then
- write_do_rd $1 $2 $3
- fi
- rm -f ${CMD}
[PageInfo]
LastUpdate: 2017-04-29 07:49:22, ModifiedBy: pepolinux
[License]
GNU Free Documentation License
[Permissions]
view:all, edit:doc editors, delete/config:doc editors