tutorial

connecting an android phone to an arduino via bluetooth

Friday, May 11th, 2012

The Goal

I’ve been working on having my Android phone talk to my Arduino wirelessly with Bluetooth.  I want to do it because I think it is pretty cool how powerful smartphone gesture interaction is- swipes and taps on a touchscreen is compelling- and I am surprised there are not more products and things around us that can talk to smartphones through local wireless communication-  even basic consumer electronics. Once something is connected to a smartphone, the possible functions expands.  Most simply, just consider any electronic object being able to interface not just with touch gesture, but also with the web through tethering.

Materials

I am using an HTC Incredible  and BlueSmirf Silver with an Arduino Uno.

Read the rest of this entry »

how to download a facebook video using wget

Saturday, February 25th, 2012

In this post I will show you how to go about downloading a video from facebook.  It won’t involve putting the URL in some video downloader.  You will use wget and the browser to download the video.

Background: 

A few weeks I wrote a post on getting tracks from bandcamp as a learning exercise for exploring a web page with the javascript console.

This post will follow along similar lines, but in this case it also highlights the value of learning a little bit of programming. Currently there is no option to download a video posted on facebook. You can download a photo, but not a video you are tagged in.  This became important to me recently because I wanted to pull some videos people took of some artwork I made.  Knowing a little bit of code helped me figure out how the video is being displayed and eventually download it.  I think it is kind of a political thing too- the video is provides some value for facebook, why they would make it difficult to download I dont know, but I do know that it confines personal data my friends have shared with me that I want to liberate and use for myself.

And I also think this is a pretty awesome example of something cool you can do knowing a little code, or rather, a bit about the Terminal and the Browser.

Now get that video

I’m going to be doing this using the Chrome browser and Terminal for mac.  You should be able to do this on Windows or Linux machines, or with Firefox or Safari.

Open up Terminal.  You can find it under /Applications/Utilities/Terminal.app

Get wget.  I use Homebrew, so I just wrote

brew install wget

You can also download it online.

Now open up your browser and go to the video you want.  If you right click it to download, you’ll get something like this.

So you can’t get to it that way.  Pause the video.  We’re going to play it again, but now find the stream url for the video.  This is the url sent to your browser which contains the video.

Instead we’ll observe the network traffic.  In Chrome open up the developer tools.  It is at View-> Developer -> Developer Tools, or just command-option-i.  Click the network tab. Push play on the video.  You should see a lot of activity.  Find the one that ends in “.mp4″.  For me it is at the top.  In Safari you can open up the Activity Monitor, under Window-> Activity.  You are looking for the URL that begins with “http://video.ak.fbcdn.net/…”

click to enlarge

This URL is the one that contains the video.  Right click it, and copy the link address.  It is a really long address.  Move over to Terminal.  Now we’ll download it.  type the following, replacing the italics with the appropriate content.  movie_title is whatever you want to save the movie file as, and in the quotes is the URL we found above.

wget -O movie_title paste-url-here

Hit enter and you’ll see the movie being downloaded.

And thats it! The movie is yours.

I searched a bit online to find a nice tool, and some required the videos to be public, or seemed a bit clunky.  This way was a bit fun because I could dig into the facebook code a little bit, and after I finished I realized it felt really satisfying.  While it wasn’t necessarily programming, it was getting something I wanted with some tools I regularly use while programming on the web, and I figured a great way to get people interested in command line tools or looking under the hood of internet traffic in the browser.

 

A quick exercise web scraping with javascript and bash in bandcamp

Sunday, January 8th, 2012

Here is a quick exercise to explore web scraping in bash on Bandcamp.  We’ll get our hands dirty with some simple console and bash scripting. This is written in January 2012, so things may change between now and when you read this.

example album page

If you take a look at the source on Bandcamp, you notice that all of the music data is located in a Javascript variable.  On your browser there should be a ‘view-source’ option.  In Chrome on the mac it is command-option-u.

 

Notice that in the comment Bandcamp includes information on their terms of service and their stance on applications that pull music off the site. You should probably read those and remember this is only meant as an exercise.  Looking at the FAQ I imagine they would be okay with this tutorial.  (Otherwise, if not okay, message me and I’ll take this down.)

So open up your web console.  I’m using Chrome, so it is command-option-i.  You can then explore the object TralbumData. You can retrieve the track information with TralbumData['trackinfo'].

We’re going to pull the titles and the files.

First get the titles.  You can use `console.log(str) ` to get things printed onto the console. We’ll write a loop over all of the objects and print out the ‘title’ value for each.

for (i in TralbumData['trackinfo']){console.log(TralbumData['trackinfo'][i]['title']);}

Now copy that and paste it in a txt file called titles.

Then we need to get the URLs for the files. We can use the same loop, replacing ‘title’ with ‘file’

for (i in TralbumData['trackinfo']){console.log(TralbumData['trackinfo'][i]['file']);}
 That will give you a list of URLs.  Put that in a file called playlist. Now in Bash we’ll download the file at each URL to the appropriate title.mp3 file. It took some googling to figure this out since I don’t normally do any bash scripting beyond simple command line tools.  Open up Terminal.  First we’ll create an array called name and put each line of titles to an entry in name. In doing this we also have the change the Internal Field Separator.  This is so that when we loop over cat titles we’ll get a new item for each line, rather than for each word.  You can do that with this command:
IFS=’
The second quote is on a new line define the IFS as newlines (IFS=’\n’ doesn’t work [source]). And then run the loop to fill the names array.

j=0;for i in `cat titles`; do names[$j]=$i; j=$(expr $j + 1); done

names contains the track titles.  To get the value out for index 0, you’d use ${name[0]}.  We’ll use that for the output file name.  This time we’ll loop over the entries in playlist and use wget to download the file at each url.

j=0;for i in `cat playlist`; do wget -O ${names[$j]}\.mp3 $i; j=$(expr $j + 1); done

The -O command line switch for wget specifies that the following argument will be the name of the output file.

 

simple DIY square wave synth

Wednesday, November 16th, 2011

We’re going to make a simple square wave synth using a hex schmitt trigger.  In later posts, I’ll build off this form and add cool things.  This is geared towards beginners in electronics, or people interested in simple ways to make sounds with digital circuits.

What you need: 

  • Speakers, with an 1/8 inch jack
  • 35 mm audio connector
  • 1 uF capacitor
  • 40106 Hex Inverter
  • 100k potentiometer
  • 5V power supply (I just use the Arduino for this)

Most parts you can find at Radio Shack.  The only one I don’t think they carry is the 40106, which you can order online.

So basically the hex inverter has digital input, and flips it for the output.  So if the input is HIGH (+5V), the output is LOW (0V).  If the input is LOW, the output is HIGH.  Now if you hook up the output back to the input, you have HIGH to LOW to HIGH to LOW… this is our oscillator, its a square wave.

So lets take a look at the 40106 to see whats up.  Here is the data sheet, and all we’re focusing on now is what each pin means:

Thats a diagram of whats going on.  On the left you have the inputs, labelled by the pin number, on the right you have the output, labelled by the pin number.  You have six inverters on this chip. To see where each pin is, we look at the schematic:

So look for that little notch on the 40106. Here’s how we make sense of the last two images.  Pin 14, VDD, is where our +5V goes.  Pin 7, VSS goes to ground.  I use the Arduino here for simplicity since it has a constant 5V source.  You can also use a 5V regulator with a 9V battery.

The rest of the pins on the 40106 correspond to those 6 inverters in the diagram.  We’ll just focus on one for now.  Pin 1 and 2, just to the left of that notch, will be the input (pin 1) and output (pin 2) of our oscillator.

Refer to the Fritzing image below for the rest of the tutorial.

Click to enlarge. The black rectangle is the IC. That blue thing below the IC is the capacitor. To the right is the audio connector. The Arduino is just used for power here, you need 5V.

We connect pin 1 to ground with a capacitor.  Here we use 1 uF.  The capacitance determines the pitch.  A higher capacitance would lower the overall pitch, a lower once would raise the pitch.  So if you play the synth and you want is higher, switch out the 1uF for .1 uF.

Then we connect pin 1 and pin 2 by the potentiometer.  Connect one of the outside pins on the pot to pin 1, and the center pin of the pot to pin 2.  This creates a loop between pin 1 and 2, and allows us to vary the resistance.

The output, pin 7, is connected to the audio connector.  The other pin of the audio connector is connected to ground.

Plug in the speakers, slowly turn the volume up.  You should hear your synth.  Turn the potentiometer to change the pitch.  You can swap out the potentiometer for other types of variable resistors, like force sensitive resistors, or photoresistors.

Making a simple sitemap on WordPress

Thursday, August 25th, 2011

I wanted a simple hands on way to make a site map. On an SEO point of view, a site map makes a website easier to read for search engines. To me, an even more important use for it is that it is a simple interface for a human to see everything on the website.  No images, just a list.  Since all of the posts in my blog fall into categories, I simply listed out the posts by category, placing the categories in an order I liked. If you have created a taxonomy in a similar way, this method may be useful for you.  Here’s how to do it. (Note: You’ll need shell access)

The first thing I did was create a WordPress Page for it, and call it “Sitemap”. You can find this function in the dashboard under “Pages>>Add New”. Then click “All Pages” and move your mouse cursor over the “Sitemap” one you just made. You’ll see it points to a link. This gives you the ID of your page.

http://whichlight.com/wp-admin/post.php?post=1266&action=edit

In my case it’s 1266. Now ssh onto your server and navigate to the folder with all of your blog templates. Its under

wp-content/themes/<your-theme>/

Here copy your Page template and call it page-ID.php. In my case it was “page-1266.php”. You can really begin with any template, or you don’t have to. I just find it easier to start with something.

In my template, my structure is as follows:

  • Header
  • Sitemap
  • Enormous Tag Cloud (optional)
  • Sidebar
  • Footer

For the “Sitemap”, as the name reveals, contains all the meat for this page. For each category, I write the following

<h2> Category Name </h2>

<ul>
<?php
global $post;
$args = array(‘numberposts’ => -1, ‘category’ => category_ID );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
<li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

I’ve bolded the things you fill in for each category.  I just simply copy and pasted this snippet for each category.   Here is an example for my blog posts:

<h2> Blog </h2>

<ul>
<?php
global $post;
$args = array(‘numberposts’ => -1, ‘category’ => 284 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
<li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

 

The Tag Cloud is just something I like.

  <h2>Super Tag Cloud </h2>
<div id=’tagcloud’><?php wp_tag_cloud(array(‘number’    => 0) ); ?> </div>

 

And that’s it!