Blink1

From NoskeWiki
Jump to: navigation, search

About

Blink(1) device


The blink(1) is USB device with a bright multicolored LED which can be programmed to flash different colors for different inputs. Blink(1) is developed by "ThimgM" and was successfully funded as a kickstarted project in July 2012.

Blink(1) costs only $30 and from the Thingm Shop and works with all operating platforms. On this page are some instructions getting Blink(1) to do stuff. :)


Blink(1) Software

The BLINK(1) TOOL

The "BLINK(1) TOOL" is a simple command line program which works on all platforms and can be used to send single one line commands to change the state of your light. Here are the easy steps to install and test that it works:

  • Step 1) Plug in your Blink(1) device and allow any device detection / updaters to run.
  • Step 2) Download the tool from here:
  • Step 3) Make a new directory (eg: "/Users/you/Desktop/blink/")
  • Step 4) Unzip the single executable/binary file to this directory.
  • Step 5) Open a terminal window (on Windows you will use the command prompt by typing "cmd" in the start search bar and) and then execute these lines:
 ./<PATH_TO_BLINK_FOLDER>/blink1-tool --red
 ./<PATH_TO_BLINK_FOLDER>/blink1-tool --off --m 2000
 ./<PATH_TO_BLINK_FOLDER>/blink1-tool --rgb 0x00,0xff,0x00 --blink 3
  • TIP: Most decent terminal windows will let you drag and drop your file onto the window to append the path. On windows this will be an "blink1-tool.exe" file.
  • TIP: In the unlikely event you see a "Permission denied" error try running "sudo chmod 777 *" in your blink directory and try again.
  • WARNING: Depending on your machine setup, you *may* need to run "sudo ./path/blink1-tool --red". We had a strange problem at my work where blink1 would only work in certain USB ports or via a hub - so if you can't get it flashing keep trying different ports with "sudo".

As long as your device has recognized, this should make it flash red then turn off over 2 seconds. The last command should flash green 3 times. If you just run the command without any arguments you should see a full list of options:

bash-3.2$ ~/Desktop/blink/blink1-tool 
Usage: 
  blink1-tool <cmd> [options]
where <cmd> is one of:
  --blink <numtimes>          Blink on/off (specify --rgb before to blink a color)
  --random <numtimes>         Flash a number of random colors 
  --rgb <red>,<green>,<blue>  Fade to RGB value
  --on                        Turn blink(1) full-on white 
  --red                       Turn blink(1) off 
  --green                     Turn blink(1) red 
  --blue                      Turn blink(1) green 
  --off                       Turn blink(1) blue 
  --savergb <r>,<g>,<_b>,<pos> Write pattern RGB value at pos
  --readrgb <pos>             Read pattern RGB value at pos
  --play <1/0,pos>            Start playing color sequence (at pos)
  --servertickle <1/0>        Turn on/off servertickle (uses -t msec) 
  --list                      List connected blink(1) devices 
 Nerd functions: (not used normally) 
  --hidread                   Read a blink(1) USB HID GetFeature report 
  --hidwrite <listofbytes>    Write a blink(1) USB HID SetFeature report 
  --eeread <addr>             Read an EEPROM byte from blink(1)
  --eewrite <addr>,<val>      Write an EEPROM byte to blink(1) 
  --version                   Display blink(1) firmware version 
and [options] are: 
  -g -nogamma                 Disable autogamma correction
  -d dNums --id all|deviceIds Use these blink(1) ids (from --list) 
  -m ms,   --millis=millis    Set millisecs for color fading (default 300)
  -t ms,   --delay=millis     Set millisecs between events (default 500)
  --vid=vid --pid=pid         Specifcy alternate USB VID & PID
  -v, --verbose               verbose debugging msgs
 
Examples 
  blink1-tool -m 100 --rgb 255,0,255    # Fade to #FF00FF in 0.1 seconds.
  blink1-tool -t 2000 --random 100      # Every 2 seconds new random color.
  blink1-tool --rgb 0xff,0,00 --blink 3 # Blink red 3 times.


The BLINK(1) CONTROLLER

Blink(1) controller interface

For Windows and OS X there is a "BLINK(1) CONTROL" software which you can easy download and install from the blink(1) site. With this controller there are four options:

  1. IFTTT - ifttt.com is a website you can sign up to for free and create "if this then this" rules using various channels including gmail, blogger, facebook, twitter, youtube, blink(1) and heaps of others. Once you create an account you can setup a rule, and at the end specify the "IFTTT Key" which is shown in the control programmer when you click the gear icon. Great concept and seems like the most useful option but sadly I haven't got this working yet after trying both youtube and gmail.
  2. Web Page URL - listens to a URL for the first occurrence of a hex color code (eg: "#00ff00"), checks every 5 seconds and shines this color (see figure). Sadly it doesn't provide any other string matching options meaning this option is only useful if you write custom server side script. Client side script like javascript wont work.
  3. File - as above, but loads a file and searches again for a hex code color.
  4. Script - a script file which will be executed every 15 seconds, must live in ~/Documents/blink1-scripts/ directory and echo / print out a hex color code.

With all these they check every 15 seconds and the blink1-control program must be running. Unfortunately I've found the Web Page URL options very limited and the controller program doesn't work for Linux users hence I recommend the command line tool as the better installation option for developers.


Other

On the download page you'll also see a couple more software programs aimed at specific things like blinking when you get a new twitter message.

Blink(1) with packaging


Working Examples

Depending on what you want to do, there are various ways you can go. Here are some examples.


Detecting a website change using a shell script and "curl"

In this example lets say we want to listen to a particular webpage and flash when it changes or when particular text "SearchText" occurs.

In theory we could do this using server-side scripting and the "blink(1) controller", but here we'll use a bash shell script with the "curl" command to load web content. It also uses the "blink(1) tool", which you should already have installed and tested in a new directory (see "The BLINK(1) TOOL" section above). Into the same directory as your blink1 executable create an empty text file called "blinky.sh" and enter this text:

html_text="`curl http://www.andrewnoske.com/wiki/Blink1 -A Mozilla/4.0`";
   # Call and load webpage text.
   # NOTE: The "-A Mozilla/4.0" option is important for many pages to mock a browser call.
 
search_result=`echo $html_text | awk "/SearchText/{ print $1 }"`;     # Looks for 'SearchText'
if [[ $search_result ]]; then       # If our search text exits:
  ./blink1-tool --green;            #   shine green
else                                # Else:
  ./blink1-tool --red;              #   shine blue
fi

To make this run make sure you are in bash and run the command like this:

$ bash
$ cd ~/Desktop/blink/
$ ./blinky.sh

NOTE: If you see a "Permission denied" error then run "sudo chmod 777 *" in the directory.

When you run this, it loads this wiki webpage and should, of course, shine green since this text exists. You can change "SearchText" to something else to make it appear red.... keep in mind that all the HTML is visible, so something like "</head>" should almost always exists. This is a good starting place, but really we want this to run in a loop, so we will add a "while true" loop. Also it would be nice if we could change the URL and other parameters without necessary changing the SH script, so we'll use conditional operators for that ("[ $URL ] || URL='http://default_url_if_not_given_already.com/';"). Finally we will check if the page changes between each time we loop. Here's what it looks like:

# @file: blink.sh
#
# @descripiton:
# This bash script is designed to work with the Blink(1) LED USB device.
#   > see: http://www.andrewnoske.com/wiki/index.php?title=Blink1
#
# This script is design to periodically check a desired URL for a given
# regex string and, if a match is found, the device will glow red. If the
# webpage changes it will flash blue and if nothing is found (possibly
# the server is down) it will flash all different colors.
# Note that the URL, colors and search term call all be changed when
# the .sh is called using the variable names.
#
# @instructions:
# Make sure this file is in the same directory as 'blink1-tool' - downloadable
# from http://thingm.com/products/blink-1.html > "BLINK(1) TOOL" - insert
# your blink(1) device then open a bash terminal and run this:
#
#   $ cd ~/yourdirectory/blinky/
#   $ URL='http://yourpage.com' REFRESH=5 SEARCH='40\4' ./blinky.sh
#
# This example will glow red if for some reason your webpage returns a 4O4 error.
# Another example of use:
#
#   $ URL=http://www.andrewnoske.com/wiki/index.php?title=Special:RecentChanges \
#     SEARCH='deleted' SEARCH_COLOR='255,0,0' REFRESH=30 \
#     CHANGE_SECS=5 SEARCH_COLOR='0,255,0' ./blinky.sh && bg
#
#     ... this example will flash green 5 seconds whenever someone changes my wiki
#         or show red if someone deletes a page (represented by 'deleted' keyword).
 
# PRINT USAGE INFORMATION:
 
if [[ ! $URL || ! $SEARCH ]]; then
  echo "Usage:";
  echo "  $ URL='http://www.yourwebpage.com/' SEARCH='ERROR' REFRESH=5 ./blinky.sh";
fi
 
 
# SETUP VARIABLES:
 
[ $URL ] || URL='http://www.andrewnoske.com/wiki/index.php?title=Blink1';
                                               # Url we want to check.
[ $REFRESH ] || REFRESH=10;                    # Number of seconds to wait between checking the url.
[ $SEARCH ] || SEARCH='40\4';                  # Regex string that, if found, light will stay on.
[ $SEARCH_COLOR ] || SEARCH_COLOR='255,0,0';   # Color to show if string found (default: red).
[ $CHANGE_SECS ] || CHANGE_SECS=1;             # Light will shine this many seconds if webpage changes.
[ $CHANGE_COLOR ] || CHANGE_COLOR='0,0,255';   # Color to show if website has changed (default: blue).
[ $BLINK_PATH ] || BLINK_PATH='./blink1-tool'; # Path to the 'blink1-tool' executable
[ $DEBUG ] || DEBUG=false;                     # If true, prints out the html text and result.
 
html_text='';           # The text content of the URL.
html_text_prev='';      # Text output from previous URL.
i=0;                    # Number of iterations we've made.
 
# PRINT VARIABLES:
 
echo "";
echo "STARTING BLINKY    (press ctrl+c to stop)";
echo "  Variables:";
echo "    URL:           '$URL'";
echo "    REFRESH:        $REFRESH            (secs)";
echo "    SEARCH:        '$SEARCH'            (regex)";
echo "    SEARCH_COLOR:  '$SEARCH_COLOR'";
echo "    CHANGE_SECS:    $CHANGE_SECS        (secs)";
echo "    CHANGE_COLOR:  '$CHANGE_COLOR'";
echo "    BLINK_PATH:    '$BLINK_PATH'";
echo "    DEBUG:          $DEBUG";
echo "";
 
# KEEP CHECKING URL PERIODICALLY:
 
while true; do
 
  # LOAD PAGE:
  html_text="`curl $URL -A Mozilla/4.0`";   # Call and load webpage text.
 
  # CHECK IF SEARCH STRING EXISTS:
  search_result=`echo $html_text | awk "/$SEARCH/{ print $1 }"`;
  if [[ $search_result ]]; then
  $BLINK_PATH --rgb $SEARCH_COLOR;
    echo "... match found ($SEARCH)";
  else
    $BLINK_PATH --off;
  fi
 
  # CHECK IF WEBPAGE HAS CHANGED (USING STRING LENGTH):
  curr_len=${#html_text};                 # Gets string length before
  prev_len=${#html_text_prev};            # and after this iteration.
  if [ $curr_len != $prev_len ]; then       # NOTE: We could add '$i > 0' but it's nice
    $BLINK_PATH --rgb $CHANGE_COLOR         #       to see a flash when it starts up.
    sleep $CHANGE_SECS                      # Wait a little while.
    $BLINK_PATH --rgb off --m 1000          # Fade out over 1 second.
    echo "... change detected"
  fi
  html_text_prev=$html_text
 
  # IF NO TEXT LOADED AT ALL, SHOW ERROR AND FLASH DIFFERENT COLORS:
  if [ $curr_len == 0 ]; then
    echo "ERROR: Could not load any text from '$URL'"
    echo "... server may be down"
    $BLINK_PATH --play 1                    # Will play different colors.
  fi
 
  # PRINT DEBUG INFORMATION:
  if [[ $DEBUG == true ]]; then
    echo "----------------------------------------";
    echo "DEBUG INFO, iteration #$i";
    echo "----------------------------------------";
    echo "HTML CONTENT:        string length=$curr_len";
    echo "$html_text";
    echo "----------------------------------------";
    echo "SEARCH RESULT:";
    [ $search_result ] && echo "$search_result" || echo "(empty)";
    echo "----------------------------------------";
  fi
 
  # ITERATE AND DELAY:
  i=$(($i + 1));
  sleep $REFRESH;          # Wait several seconds before checking again.
 
done

To execute this try:

$ URL='http://www.andrewnoske.com/wiki/title=Blink1' SEARCH='li>' ./blinky.sh

A few things to notice:

  • You can change any of the other parameters here too.
  • We check page change using string length.... so hopefully if all that happens between page refreshes is a time change (10:00 > 10:01) or counter change (number of visitors), it won't flip the switch, because that would be annoying.
  • Your blink(1) will cycle though all colors if the page is totally unresolvable.
  • The search string supports Regular Expression so if you are a master of regex you can potentially do some fancy things like look only between certain text (body tags, div tags or certain text), count occurrences and so forth. I'm no regex master, so if you read this and come up with some better examples please email me!


Detecting new e-mails using a Mozilla "Mailbox Alert" Add-on

Adding a new "Mailbox Alert" to call blink1-tool
Apply alert to a folder/mailbox

To detect the arrival of new e-mails to a G-mail account you are *supposed* to be able to use the "blink(1) control" program with an ifttt.com rule. These rules are easy to setup, but for whatever reason they never worked for me on my Mac laptop, and the blink(1) control program isn't available at all for Linux. Luckily a co-worker discovered a way around this using a Mozilla add-on called Mailbox Alert 0.16.3 which can launch command line programs. If you already use Mozilla or are prepared to have it running minimized this might be a great solution for you too. To get this working:

  • 1) In not already, make sure you install the blink1-tool and Thunderbird.
  • 2) Setup your web mail to enable POP3 or (better yet) IMAP.
    • For gmail this is free an easy by clicking settings > forwarding and IMAP (see instructions).
    • For yahoo and some of of the other main web mail clients you have to pay and thus it worn't work for you (unless you pay).
    • Most webmail clients support "forwarding", so here you might consider creating a new gmail account (eg: "myblinknotifier@gmail.com") and setting up rules to forward all or a subset of your mail to this account.
    • For those luck few with "@google.com" (corp) accounts, forwarded is not allowed (as you would expect).
  • 3) Install and/or open thunderbird.
    • When it starts up you can enter your email address and password information. To add another account click Edit > Account Settings > Account Actions > New Account.
  • 4) Install 'Mailbox Alert' by clicking Tools > Add-ons > Get Add-ons then search for "Mailbox Alert" and wait for it to install.
  • 5) Close the add-on manager and restart Thunderbird.
  • 6) Add a blink rule by clicking Tools > Edit Mailbox Alerts.
  • 7) In the mailbox alerts list click "Add" then tick the "Execute a command" option and enter a line like this with the full and correct path to your blink1-tool:
/usr/local/google/home/anoske/bin/blink/blink1-tool --blue --blink 2
  • 8) If you click text, you should see your Blink(1) flash blue twice. Give the rule a sensible name like "Flash blue twice", and then do the same with "--red" and making a few more rules with different colors then close the Mailbox Alert List dialog.
  • 9) Right click your inbox folder and then select Mailbox Alert > Flash blue twice so that's it's checked.
  • 10) Send yourself and e-mail to that address and you should see it blink twice.

Now you have this working, something to consider is using different colors for different messages. You can do this adding multiple folders and a filter for each type of message you want to blink. Create a new folder by right clicking your main e-mail address (top left) and clicking "New Folder" and call it "red". Right click this folder and enable "". Now click "Tools > Message Filters > New", and you can create a rule such as this one:

  • Mach: | from | contains | "jaredjohnstone@utah.com
  • Perform: | move message to | "red on youremail@normalperson.com"

Once you've added your rules, you can right click on your "Mailbox Alert > Flash red for utah people" folder. In this same manner you can create lots of rules, but sadly you won't get a lot of versatility in terms of the light staying on to represent unread messages an so one. For something more advanced you can possibly write a shell script which takes the message details as input and compiles them into a text file for analysis (and the shell script itself would call blink). Mailbox Alert supports a lot of different macros such as demonstrated in this example:

/usr/bin/notify-send %sendername %subject


Links


Acknowledgements: Allen Hutchison from Google for introducing everyone in our group to blink(1). Allen was one of the original backers of Blink(1) and bought everyone in the group one in January 2013, shortly after it hit the market. Also thanks to: Jared Johnson, for finding the Mozilla plugin.