Feb 27, 2015

How to setup Bittorrent Sync for IOS 8 and raspberry pi

Adopted from melgrubb.wordpress.com; annotations for IOS devices mine.

UPDATE: SYNC 2.0, see below

On your target computer (where the sync'ed folders will live):
sudo nano /etc/apt/sources.list.d/btsync.list

Add the following to btsync.list
deb http://debian.yeasoft.net/btsync wheezy main contrib non-free
deb-src http://debian.yeasoft.net/btsync wheezy main contrib non-free

Import the repository’s signing key with the following commands:
sudo gpg --keyserver pgp.mit.edu --recv-keys 6BF18B15
sudo gpg --armor --export 6BF18B15 | sudo apt-key add -

You can then install btsync:
sudo apt-get update
sudo apt-get install btsync

When btsync starts, take all the defaults (unless you are special!)
One of the defaults can be for a web server port 8888 (or whatever number above that of your choice). Remember that number and the IP address of your raspberry pi. We will be connecting to it later...

On your IOS device:
  • Go to the App Store and download/install/open btsync
  • Enable Camera backup (green on switch)
  • Click on email
    • Will generate a security certificate first time only
    • Warning - you might make your life hard  if you have special characters in the name of your device, like "Joe's iPhone", especially if you will be rsyncing this folder
    • Two ways to fix this:
      • Don't take the default folder name, rename it when you create it - see below
      • If you run into problems after you have already created the folder, start over:
        • rename your device in Settings\General\About
        • Delete the Sync app and re-download it to capture the new name
    • Accept the identity credentials
  • Click on "email" and mail the identity keys to yourself
On a desktop computer (can be the target raspberry pi):
  • You'll need access to your email on the same computer so you can cut and paste a link
    • Open the email from the btsync app, and in the body of the message click on "Camera Roll" link
    • This will open a web page - highlight and copy the URL in the address bar
  • On a web browser go to the IP for the target raspberry pi, something like:

  • In the upper right corner of this Bittorrent Sync web page, click on the link symbol (will say "Enter a key or link" if you hover your cursor over it)
  • Paste the URL link you copied from the email into the text field and click Next
  • Add to a folder structure of your choice
    • default is /var/lib/btsync, which may not work for you
    • I used /mnt/data/sync, from /mnt:
      • sudo mkdir data
      • cd data
      • sudo mkdir sync
    • Change the folder name from the default here
    • Click Add (the folder) when done
  • Click Connect
Folder you just created will appear and say "Pending Approval"

On your IOS device:

  • In the Sync app, you should see a "bell" symbol with a red number. 
    • This is notification of the Pending Approval you must agree to. 
  • Click on the notification symbol
  • On the next screen, click the green check mark to approve the new sync
  • Click the Left < symbol at the top of the screen to go back to your Camera Backup sync
  • What a few minutes and click on the Camera Backup to start the sync
    • You will see the progress of the sync reported in the app

You will always have to leave the sync window open to complete the backup

UPDATE: SYNC 2.0, March 21, 2015

If you are doing a clean installation (no previous installation), following the instructions should still work. Posters are claiming that your camera backups are forced to appear as 1.4/"Classic". 
When I first saw the folders in 2.0, I thought the pencil with a red line through it and the 1.4 notation meant something was wrong. I deleted everything and started over. After a complete re-install, the 1.4 folder is still there and syncs properly.

Jan 23, 2015

Concise Guide to Troubleshooting the Spark Core

I snatched up a Spark Core from Kickstarter when they first were offered. That was 2013. Then the poor thing languished in a box, forlorn and unloved in a dark corner of the closet. Lately I renewed my interest in the Internet of Things when I started playing with IFTTT and became enamored with its power. I decided to give the Core a whirl.

I had never set this thing up - it was still pristine in its box. That's probably a good thing. There were a number of "issues" getting this early Core working that took Spark and its community time to work out and document. Like the X-Files, The Truth is Out There.

Provided you search for it...and search, and search. Yes, the fixes are out there. Problem is, they're splattered all over the place.

Really, you're calling this a "Concise Guide"?!? Yes! The information here is as complete as possible and will save you lots of time.

Note that this applies to the Core, especially early Core, and not necessarily the Photon.

Required First Steps

  • Sign up for an account at spark.io
  • Download the Spark Core app for your device, either Android or iPhone
  • Since the Core is 802.11b/g only, make sure your router is set up to handle this
Here's the first hurdle: b/g connectivity. I have a dual-band (2.4/5Ghz) Asus N56U router. With stock settings, the Core would not connect. Change to "Legacy" on the 2.4GHz band.

OK, All set up so far?  Connect the Spark to a power source. It can work off computer USB power, but it's better to power it with an external adapter of 1A to make sure it has enough power.

It's blinking... a blue LED.

What do the Colors Mean?
There are two LEDs on the Core. The big fat one in the middle is a full-color RGB LED that shows you the status of the Core's internet connection. The other small blue LED is the user LED; it's hooked up to D7, so when you turn the D7 pin HIGH or LOW, it turns on and off, respectively.
The RGB LED could show the following states:
  • Flashing blue: Listening mode, waiting for network information.
  • Solid blue: Smart Config complete, network information found.
  • Flashing green: Connecting to local Wi-Fi network.
  • Flashing cyan: Connecting to Spark Cloud.
  • High-speed flashing cyan: Spark Cloud handshake.
  • Slow breathing cyan: Successfully connected to Spark Cloud.
  • Flashing yellow: Bootloader mode, waiting for new code via USB or JTAG.
  • White pulse: Start-up, the Core was powered on or reset.
  • Flashing white: Factory Reset initiated.
  • Solid white: Factory Reset complete; rebooting.
  • Flashing magenta: Updating firmware.
  • Solid magenta: May have lost connection to the Spark Cloud. Pressing the Reset (RST) button will attempt the update again.
The RGB LED can also let you know if there were errors in establishing an internet connection. A red LED means an error has occurred. These errors might include:
  • Two red flashes: Connection failure due to bad internet connection. Check your network connection.
  • Three red flashes: The Cloud is inaccessible, but the internet connection is fine. Check our Twitter feed78to see if there have been any reported outages; if not, visit our support page90 for help.
  • Four red flashes: The Cloud was reached but the secure handshake failed. Visit our support page90 for help.
  • Flashing yellow/red: Bad credentials for the Spark Cloud. Contact the Spark team (hello@spark.io).

I started up the app (on Android) and filled in the network information for my SSD. I got a solid blue light, meaning the network information went over. So, the app was talking to the Core, at least. This was soon followed by a flashing green, connecting to the local WiFi network...and flashing green, and flashing green. Nope, not connecting.

I tried the procedure for connecting directly via USB, on a Mac. The command for me was :
        $ screen /dev/tty.usbmodemfa131 9600

There was no prompt on the serial terminal through screen. I typed "i" for the Core identifier and hit enter. Got a number back. OK, we're communicating with the Core. Next, I typed in "w" to supply my WiFi credentials. After a moment, the Core started flashing green. And stayed that way. No help here. 

Since this was an early model of the Core, there was a problem with firmware on the CC3000 TI WiFi chip itself, distinct from firmware on the Core. I saw a post about this issue as late as July 2014, so this may still be affecting some.

Be prepared - you're going to be downloading several items and doing a lot of button pushing on the Core in these next steps. I've written this for the Mac, there's another set of instructions here for Windows. These steps will detail installing the DFU utility and its prerequisites, homebrew for Mac and the GCC (compiler) toolchain for ARM architecture . While we're at it, let's install the spark CLI (Command Line Interface), which can flash the CC3000 for us.

Installs for Firmware Upgrades
        (From a Terminal)
  • Install homebrew
    • ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    • brew doctor
    • do as the Doctor says!
  • Install the GCC Toolchain for ARM
    • brew tap PX4/homebrew-px4
    • brew update
    • brew install gcc-arm-none-eabi-48
  • Install DFU utility
    • brew install dfu-util
  • Install the Spark CLI
    • sudo npm install -g spark-cli
The following instructions are using *.bin files from the downloaded gits. You don't have to do the make/build process.

Flashing the Firmware
          (With the Spark Core attached to USB)
  • Get the CC3000 Program Patch
    • Download from github
    • Unzip the download
    • Go to your Download directory
    • cd to Downloads/cc3000-patch-programmer-master/build
    • spark flash --usb cc3000
  • Now, load the Spark Core firmware
    • Download from github 
      • NOTE: Newer firmware may be available - CHECK!
    • Unzip the download
    • Go to your Download directory
  • Put the Core into dfu-mode:
    • Hold down BOTH buttons (RST and MODE)
    • Release only the RST button, while holding down the MODE button.
    • Wait for the LED to start flashing yellow
    • Release the MODE button
    • Then, with your Core blinking yellow
      • go to Downloads/firmware-spark_2/build
      • dfu-util -d 1d50:607f -a 0 -s 0x08005000:leave -D core-firmware.bin
If you're successful, you should get a the LED to show:
  • Flashing White - reset initiated
  • Flashing Magenta - updating firmware
  • Solid White - rebooting
  • Flashing Blue - listening mode
Now, open up the Spark Core app on your device. I used iPhone, entered my WiFi credentials, and after a few moments, was greeted with the fabulous Slow Breathing Cyan, and the app continued with the Claim, letting me name my Core. I had also tried this on Android and never got to the Claim, but the Breathing Cyan kicked in.

If you see any errors or omissions in this process, please leave corrections in the comments.

Jan 15, 2015

Easy HDMI Sound for rapsberry pi

Here's a quick way to get HDMI quality sound for your raspberry pi. I tested this on a raspberry pi A+, but it should also work on models B+/B/A.

First, you'll need an HDMI to VGA and Audio converter. I got this one from Amazon for $12.99 USD. You'll also need an HDMI Male to Male cable. Here's how mine looked after hooking it up to the A+:

Most of the work is done with software configuration. Follow the instructions on this page and you should be getting test sounds out if you succeeded.

I wanted to configure mpd, the Music Player Daemon, to complete my config. I had previously set up a USB sound dongle on this installation, so I needed to make changes to /etc/mpd.conf and /etc/modprobe.d/alsa-base.conf to work with the HDMI converter. This is basically changing these configurations files back to a stock installation.

I changed mpd.conf to look like this:
 # An example of an ALSA output:
audio_output {
type        "alsa"
name "My ALSA Device"
# device "hw:0,0" # optional                        <<====commented out
format "44100:16:2" # optional
mixer_device "default" # optional
mixer_control "PCM" # optional
#      mixer_control  "Speaker"       # added for CMedia USB     <<====commented out
mixer_index   "0" # optional

For alsa-base.conf, I changed it to this:
# Keep snd-pcsp from being loaded as first soundcard
options snd-pcsp index=-2
# Allow snd-usb-audio to be loaded as first soundcard
#options snd-usb-audio index=0      <<==== commented out

When I first started playing through mpd, I couldn't hear anything. Turned out the PCM Master volume setting was set almost to zero. I had to set it very high, about 88, for a comfortable listening level. On the USB Sound dongle, I was also used to setting the volume to around 30%. That was way too low for the HDMI sound. I needed to set the volume with mpc to around 90%.

Jan 2, 2015

ESP8266 Weather Display

Weather from Wunderground: Temp/Humidity/Wind Direction and Speed/Barometric Pressure

The ESP8266 (AKA Wi07C) is a cheap (<$10), tiny WiFi module that can be hooked up to any micro-controller that can feed it Serial (UART) commands. You can use this module to send data to or receive data from web sites. This puppy is cheap, but capable!

ESP8266 WiFi Module

Currently (as of Nov 15, 2014), there are at least 3 versions of the ESP8266 being sold. The hardware wiki at ESP8266-Wiki has details. I got mine from adafruit. Why?  Adafruit offers a curated collection of products. They're either superior products designed by ladyada herself, or vetted outside products that are the current best available. So, I knew I was getting latest model of the ESP8266 which allows modifying the baud rate. When I got my modules from adafruit, the packing bag was labelled "fw". Good to know.

When I googled for what other people had done with the WiFi module, I came across the weather display at zeflo.com. My wife and I are weather freaks. Even though we live in the San Francisco East Bay, where we enjoy a moderate Mediterranean climate, we always check to see what the day has in store. I had some familiarity with wunderground.com weather feeds from a previous project, so this was a good place to start with the WiFi module. I also got some very good tips from Ian Sexton at this site.

Bill of Materials:

  • ESP8266, available on ebay and others. Got mine at adafruit, $8.95 +Ship
  • Arduino Uno/Leonardo or clone, or adafruit's Pro Trinket 3V version,  $9.95+Ship
    • Update 01-07-2015: See Pro Trinket Notes below for special configuration
  • Breadboard
  • LM317 Voltage Regulator TO-220 package or similar, Mouser has them for $0.80
  • R1: 1x200 Ohm Resistor
  • R2: 1x330 Ohm Resistor
  • 0.1uF capacitor
  • 1uF capacitor
  • TTL Level Shifter (5V to 3.3V logic) if you need it - adafruit has 'em for $1.50
  • M to F jumper wires for connecting the ESP8266 to a breadboard and Arduino
  • M to M jumper wires for connecting components on the breadboard/Arduino
  • Power supply - should be at least 500mA/5V; I used a 5V/2A I had in inventory
  • Display - I used a 4 line x 20 character LCD, with Serial input from a ModernDevices LM117 Serial LCD Backpack, but any SPI/I2C graphical or serial display device will do, provided you have enough RAM
Easy, although not a beginner project.  It's just hooking up a few components and wires, plus installing software and configuring. If you follow the instructions here closely, you should be OK. 

At present, the WiFi connection needs to be reset periodically. I used the kludge of doing a "hard reset" of the ESP8266 in code to keep it going (courtesy of Ian Sexton). 

Hardware Build:
The ESP8266 is a 3.3V module. If you're used to using a 5V Arduino, you'll have to make some adjustments.

If you are using a straight-up Arduino or clone, you'll need a TTL logic level shifter to get the 5V out from the Arduino down to 3.3V that the ESP8266 wants, or risk frying the WiFi module. If you use a Pro Trinket 3.3V model, you won't have to worry about that - it uses 3.3V logic.

Ladyada pointed out the ESP8266 can have spikes of 300mA or more current, much more than can be supplied directly by the Arduino. Youll want to use an external voltage regulator that can handle more at least that much current. That's why I'm using the LM317.

I used R1=200/R2=330 Ohms, with 3.15V out - within workable range

RED (2 CONNECTIONS)= 3.3V/BLK=GND; Connect LM317 GND  to Arduino GND

Software Build:
  • You need at least the Arduino IDE version 1.0.6 which can support the nested folders used in the ArduinoJson library used in this project. Download Arduino 1.0.6 HERE
  • Download the ArduinoJson library from ArduinoJson library from github. How to install library instructions HERE
  • Depending on your display, you may need to download one or more libraries (such as SSD1306, ST7735, etc. - plus GFX library). I used the LM117 serial backpack which does not need its own library, but uses its own command set.
  • Download the Arduino sketch HERE
  • You need an API License Key for wunderground (free), available HERE
Software Configuration:
In the Arduino sketch, there are two variables to update to get to your network:

#define ""    // insert your network SSID
#define "" // insert your WPA2 password

Once you have your free API key for wunderground, you can update the sketch at line:
 cmd = "GET /api/YOURKEY/conditions/q/";
There is also a location variable you must modify:

#define LOCATIONID "" // location id, for the United States


#define LOCATIONID "" // for a city lookup in the US

For International locations, substitute the full English country name/city for the LOCATIONID,
for examaple:
#define LOCATIONID "Germany/Munich"

If found it convenient to check the returned values from the sketch by entering the api line directly into a browser address:

Here are sample API calls for International locations.


You can also get both current conditions and forecast on a single call:

However, you would have to change the Arduino sketch to add forecast:
cmd = "GET /api/YOURKEY/conditions/q/";
          cmd = "GET /api/YOURKEY/conditions/forecast/q/";

How it Works:
WiFi Connection
You communicate with the ESP8266 via old-school AT commands:

  • Hard Reset
  • Connect to WiFi
  • Set to single connection
  • Set up TCP connection
  • Send data (GET) to wunderground
  • Use Serial to read the returned json (JavaScript Object Notation) pairs
  • Parse the key-value pairs
  • Display 
  • Wait 15 minutes
Serial Reads and Processing
When data returns from the API call, we need to read through the returned key-value pairs (fieldname:value) for the data we want. This is a VERY large returned json set, on the order of 1500 characters or larger. Here's a sample of what the data looks like:
                            "city":"Pleasant Hill",
There's an array in the sketch. conds[], that stores the conditions we want to return to the Serial read. A priming Serial.find is used to find each entry stored in conds. The data after that find operation will be the other half of the key-value pair, delimited by a comma. The read routine breaks at the comma: the comma will be added after each entry except the last.

The entries in conds[] look very strange: char* conds[]={"\"city\":","\"weather\":" ... That's because we are including the quote marks (") in the data and we have to tell the sketch, yeah, we want to include this quote mark in the data.

Both the conds key and its associated value are stored in an array (json), with commas and quotes embedded in the string. At the end of the data, a closing curly brace (}) is concatenated to json. We just created a string in a format the ArduinoJson parser can use.

Finally, we parse the json objects and format them for printing.

Notes and Cautions:
  • Keep the ArduinoJson parse in a function. Otherwise, the parsing buffer will fill up and won't work. See the ArduinoJson Wiki HERE for details
  • You can change the entries in conds[], but beware of embedded commas in the data. Remember commas are used as delimiters. Check what the returned data looks like by using a direct API call from a browser
  • Names can be repeated in the returned json data (eg: "full" is used at least twice). This will affect your Serial.find
  • Make sure you are retrieving data in the order that it is presented from the API. It's NOT alphabetic order.
  • I couldn't seem to process more than 7 entries, things would break around the json parse step. This could be that I made a programming mistake, or that I wasn't handling the returned data properly.
  • Be careful how many calls you make to the wunderground API with your free account. For the free license you are limited to no more than 10 calls per minute and no more than 500 calls per day. The sketch for this project has a delay(900000) at the end of the loop, which results in a call about every 15 minutes. 4 calls per hour * 24 hours = 96 calls per day, well within the restrictions.
  • If you make changes, watch out for memory leaks. In the stock sketch in github, I had a little over 500 bytes (of 2048) free. Take care if you use a display or library that requires a large buffer in program memory
Pro Trinket Notes Updated 01-07-2015
The Pro Trinket 3V runs at 12Mhz. SoftwareSerial (required for this project) through Arduino 1.0.6 only has a version of SoftwareSerial that supports 8, 16 and 20MHz. You will need to update the SoftwareSerial.cpp file, available on git HERE.

Instructions for updating SoftwareSerial.cpp on a Mac:

  • Close Arduino if running
  • Open a New Finder Window
  • Open Applications, highlight Arduino (or whatever you name your Arduino app)
  • Show Package Contents
  • Open Resources\Java\libraries\SoftwareSerial
  • Copy SoftwareSerial.cpp (downloaded from github) to this folder, replacing the older version of SoftwareSerial.cpp
  • Restart Arduino
Update April 29, 2016:
New sketch added to github to use the Adafruit Feather Huzzah. The code is based on Mike Rankin's ESP8266 OLED Weather Display and uses Web Client calls from the ESP8266WiFi library. The original sketch code described in this blog post used AT Commands to communicate with the ESP8266 module. 
If you use a 5V 4x20 LCD as in this post, you will still have to use a separate 5V power supply for the LCD and feed 3V to the Huzzah from a voltage regulator, described above. My guess is the Huzzah does not have enough current to power the backlight on typical LCD panels.