Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Tuesday, December 13, 2016

Testing a basic Zwave setup for the first time

I have myself ordered a Zwave.me USB stick and an Aeotec MultiSensor 6 to start building my sensor network at home. There are a few tools out there to get you started with Zwave on a Raspberry Pi, or see my howto. But the issue I had was that I was not able to see the technical configuration of my hardware devices. There is a script called api_help.py that comes with the python_openzwave library, that I have slightly modified so that it prints the useful information in a friendly way.

Output:
pi@raspberrypi:~/git/python_openzwave $ python3 api_demo.py
------------------------------------------------------------
------------------------------------------------------------
Memory use : 14.61328125 Mo
INFO:openzwave:Start Openzwave network.
------------------------------------------------------------
Waiting for network awaked :
------------------------------------------------------------
.INFO:openzwave:Driver ready using library Static Controller version Z-Wave 4.05
INFO:openzwave:home_id 0x00000000, controller node id is 1
......... done
------------------------------------------------------------
Memory use : 15.96484375 Mo
------------------------------------------------------------
Use openzwave library : 1.4.2272
Use python library : 0.3.1
Use ZWave library : Static Controller version Z-Wave 4.05
Network home id : 0x00000000
Controller node id : 1
Controller node version : 4
Nodes in network : 2
------------------------------------------------------------
Waiting for network ready :
------------------------------------------------------------
 done in 10 seconds
Memory use : 15.96484375 Mo
------------------------------------------------------------
Controller capabilities : {'primaryController'}
Controller node capabilities :
{'primaryController', 'listening', 'beaming'}
Nodes in network : 2
Driver statistics :
{   'ACKCnt': 22,
    'ACKWaiting': 3,
    'CANCnt': 3,
    'NAKCnt': 0,
    'OOFCnt': 0,
    'SOFCnt': 44,
    'badChecksum': 0,
    'badroutes': 0,
    'broadcastReadCnt': 0,
    'broadcastWriteCnt': 12,
    'callbacks': 0,
    'dropped': 0,
    'netbusy': 0,
    'noack': 0,
    'nondelivery': 0,
    'readAborts': 0,
    'readCnt': 44,
    'retries': 3,
    'routedbusy': 0,
    'writeCnt': 25}
------------------------------------------------------------
New node: Node 1
   ---------
Node 1 - Name :
Node 1 - Manufacturer name / id : Z-Wave.Me / 0x0115
Node 1 - Product name / id / type : ZME_UZB1 USB Stick / 0x0001 / 0x0400
Node 1 - Version : 4
Node 1 - Command classes :
{'COMMAND_CLASS_NO_OPERATION', 'COMMAND_CLASS_BASIC'}
Node 1 - Capabilities : {'primaryController', 'listening', 'beaming'}
Node 1 - Neigbors : {2}

Thursday, March 5, 2015

Add leading zero to integer in bash

I was composing a script that would store a file per day in the month, but wanted to have a leading zero not to mix up when dir listing. In bash, you have a couple of options.

1. Using range
#!/bin/bash
for i in {01..31}
do
   echo "Day: $i"
done

Works, but has downside that you cannot feed it with a variable.

2. Three-expression example
#!/bin/bash
for (( i=1; i<=31; i++ ))
do
   i=$(("0"$i))
   echo "Day: $i"
done

Will throw an error being: value too great for base (error token is “08”) Reason being that Bash will treat the variable as an octal, even with leading zero. Prepending the string "10#" to the front of your variable will have it treated as decimal by Bash.

3. Using printf
#!/bin/bash
for (( i=1; i<=31; i++ ))
do
   i=$(printf "%02d" $i)
   echo "Day: $i"
done

Works & easy solution, my favorite.

4. Using seq
#!/bin/bash
for i in `seq -f ‘%02g’ 1 31`
do
   echo "Day: $i"
done

Works also

Monday, March 2, 2015

Synology default shell does not support regex

I was trying to write a script today that would parse a file and check if a line matches a regex. It would fail for unknown reasons, until I found out that the default shell within a Synology (ash), does not support this. Installing bash with ipkg solves this.

Script (not working in ash):
#!/bin/sh

FILENAME="status.txt"
LINENUM=0
REGEX="^(CLIENT_LIST)(.+)"

while read SINGLELINE
do
 LINENUM=$((LINENUM+1))

  if [[ $SINGLELINE =~ $REGEX ]]; then
  NUM_CLIENT=$((NUM_CLIENT+1))
  echo "Match CLIENT_LIST: $LINENUM"
 else
  echo "No match: $LINENUM"
 fi

done < "$FILENAME"

In my terminal:
$ ./status.sh
sh: 2.3.6: unknown operand

Script (working under bash):
#!/opt/bin/bash

FILENAME="status.txt"
LINENUM=0
REGEX="^(CLIENT_LIST)(.+)"

while read SINGLELINE
do
 LINENUM=$((LINENUM+1))

 if [[ $SINGLELINE =~ $REGEX ]]; then
  NUM_CLIENT=$((NUM_CLIENT+1))
  echo "Match CLIENT_LIST: $LINENUM"
 else
  echo "No match: $LINENUM"
 fi
done < "$FILENAME"

In my terminal:
$ ./status.sh
./status.sh
No match: 1
No match: 2
No match: 3
Match CLIENT_LIST: 4
No match: 5
No match: 6
No match: 7

Tip from: http://forum.synology.com/enu/viewtopic.php?f=27&t=77899