Friday, January 27, 2012

How to Carbonate Pretty Much Anything

Carbonated apple juice.

This has been a project I've been wanting to do for several years, but I've always lacked the confidence to try it until after watching Ben Krasnow do many things of the same ilk.  The concept is relatively simple; soda gains its bubbly taste and much of its acidity through a process called carbonation, where carbon dioxide is dissolved into a water-based solution.  Gases are actually always soluble in water (which is why fish don't drown), but if you raise the pressure of the gas pressing down on the water (or cool the water), progressively more gas will dissolve into the water.  Conversely, if you lower the pressure, the dissolved gas will come out of solution and bubble out.

Here's a fun experiment: Fill a drinking glass with tap water and leave it somewhere undisturbed for several hours.  Notice how lots of small bubble of gas form on the inside of the glass.  Why is that?

So soda is carbonated by dissolving more CO2 into it than naturally exists in the Earth's atmosphere.  This causes the soda to bubble once you release the pressure in the bottle, and the carbon dioxide actually combines with the water to form carbonic acid, which gives soda much of the tangy flavor which you'll notice is lost when soda goes flat:

The chemistry really isn't so bad; take something water-based, add excess CO2, enjoy the pleasurable bubbly and tangy sensation you traditionally associate with soda and other carbonated beverages.

Doing this at home is very easy, but potentially rather dangerous.  Getting high pressure CO2 isn't too difficult; you can go to the grocery store and buy dry ice for about $1 per pound.  Dry ice is solid CO2, so as it sublimates into a gas, it vastly expands (there is an almost 800x volume difference between CO2 as a solid and a gas), but if you instead trap it inside a pressure vessel, it becomes a high pressure gas.

Now you just need to build a pressure vessel to contain the CO2 and whatever you want the CO2 to go into.

There's a hitch; you can't just drop a chunk of dry ice into a plastic bottle with some water.  Well, you can, but this is a traditional past-time done by teenage delinquents due to the fact that the bottle inevitably explodes.  We don't want our pressure vessel to explode.  We want to hold it at something like 30PSI for awhile and then be able to pour out our freshly carbonated beverage.  This is the problem that has previously always scared me off of this project.  With Ben's help, I've managed to come up with the beginnings of a solution to this, which prevents such inconveniences as injury or death.

Meet my pressure vessel.  I built this out of 3" schedule 40 PVC pipe, a couple pipe fittings, a 100PSI pressure gauge, and a needle valve drain cock.  I horse-traded the pressure gauge from my ever-supportive father for helping to clean up his shop this winter break, but was able to buy the rest of it at an Ace Hardware for about $25.
Building the chamber out of 3" schedule 40 PVC is important.  Each diameter and schedule of pipe has a different operating pressure, which is the highest pressure at which the pipe is still safe.  larger schedule numbers indicate higher pressures, so if the 260PSI operating pressure of 3" schedule 40 was too low for this, we would move up to schedule 80 or schedule 120 NPS, at the expense of higher cost and it being much more difficult to purchase SCH80 or SCH120 pipe retail.  Although 3" SCH40 pipe has a rated 260PSI operating pressure, and a bursting pressure well above that, in my calculations I derated the end cap to half that because of the two holes drilled into it for the pressure gauge and needle valve.  The PVC parts needed for this should all be easy to find in your local hardware store's gardening or plumbing sections:
  • 16" of 3" SCH40 PVC pipe.  Many hardware stores will custom-cut this to length for you if you ask nicely.  I ordered 16" to give me a final chamber volume of about 8.5 cups, but feel free to build your chamber as big or small as you like.
  • 1x 3" slip end cap.  This is the fitting in the bottom center of the picture above, and is to be cemented onto the bottom end of the pipe.
  • 1x 3" slip to female NPT coupling.  You can get 3" pipe with pipe threads on it, but it's often much easier and cheaper to use slip fittings everywhere except where you need your access port.
  • 1x 3" male NPT plug.  This is a threaded plug which I installed the pressure gauge and needle valve into, and which I take on and off to refill the chamber between batches.
  • Teflon tape - This is often called plumber's tape, and is used to seal the threaded joints between the pressure gauge, needle valve, plug, and NPT coupling.
  • PVC cement and primer - The slip fittings can all be permanently joined, since the threaded plug can be removed to fill, empty, and clean the chamber.
This project was the first time I've ever dealt with NPT, or National Pipe Thread.  This is a special pressure-tight thread designed for pipe fittings, which has a distinctive taper to it.  As you're wandering through the hardware store, pause for a moment to appreciate the threaded pipe and notice that it gets skinnier towards the end.  This gives the thread much more compressive force when it's screwed together than normal straight threads, which causes it to be pressure tight.  Fun fact: NPT tapers off at 1° 47′ 24″, which works out to be an inch for every 16 inches of thread, and a crazy small angle.

The pressure gauge and needle valve are really important.  Since the CO2 is trying to expand to 800 times its original volume, it is very easy to add so much dry ice to the chamber such that we will go well beyond the 260PSI limit of the pipe, and create a very impressive, if unfortunate, bomb.  The pressure gauge lets us monitor how high the pressure gets inside the chamber, and the needle valve allows us to just crack it and bleed off CO2 as slowly as we want.

Fixing the valve and gauge on the plug does require access to a fairly well stocked machine shop.  Pulling out my copy of Industrial Presses Machinery's Handbook, I was able to find tables for how large of a hole to drill for both the 1/8" and 1/4" NPT taps I need for these two fittings (28th ed, p1863, table 1b, 1/8" NPT - drill letter Q, 1/4" NPT - 7/16").  Drill, tap, apply teflon tape, screw in, enjoy.

Charging the chamber
 Now for the actual contents of the chamber.  There are several possible sources of high-pressure CO2, but probably one of the simplest and cheapest is dry ice.  You will need to be a little deliberate to make sure you  choose a grocery store that carries dry ice (Many carry it, but nowhere near all of them), but once you find one that does carry it, you just need to ask the cashier for 1-2 pounds of it when you're checking out; they'll go unlock the box, weigh out a chunk, and bag it for you.  You'll want to break up the dry ice into smaller pieces, since each batch isn't going to use very much dry ice at all, and it'll go faster with small pieces.

As far as exactly what you carbonate, the sky is pretty much the limit.  The classics include water (optionally with flavored syrups to make Italian sodas), fruit juices, and reviving flat sodas or beer, but anything with a high water content works.  I sized this chamber rather deliberately such that I can easily fit apple and orange slices in it.  The carbonated fruit comes out with a very intriguing sparkling aftertaste.  I only let the fruit soak for ~20 minutes, which wasn't quite long enough (they went flat rather quickly), so solid fruit would probably be something you'd want to pressurize several hours early, get the chamber stabilized, and just let it sit until ready to serve.

When filling the chamber, you will not want to fill it much beyond half way with whatever you're carbonating.  The dry ice will sublimate very rapidly, and the less head space you have in the chamber, the faster the pressure will rise in the chamber.  By leaving several cups of head room above the liquid, it becomes much easier to throttle the needle valve to keep the pressure below the operating limit of the chamber.

Be very careful while the chamber is pressurizing.  You will find that the pressure rise is not at all linear (for a variety of reasons) and it's quite possible for the chamber to go from 50PSI to pegging my 100PSI gauge in a matter of seconds.  You simply can not walk away from the chamber while it's coming up to pressure, but once the dry ice finishes sublimating, you can seal the chamber and leave it pressurized for as long as you like.  You might even be able to install another stop cock on the bottom of the chamber and have carbonated drink on tap for as long as you like.

Fundamental problem with this setup

Generating high pressure CO2 gas from dry ice is fundamentally a problematic affair.  In the ideal world, I wouldn't have anything to do with the stuff and use a commercial gas cylinder and gas regulator to charge the chamber, but those are expensive.

The real problem is that it is very easy for the dry ice to get away from you, particularly when you're using it to charge liquids.  Water has a very high thermal mass, which is why dry ice sublimates so quickly when you drop it in the punch bowl during Halloween.  Unfortunately, the water tends to freeze on the dry ice, cause it to sublimate slower.  Once the chamber finally reaches your target pressure and you start venting it, the ice will often crack off the dry ice and you'll see the chamber start to rise in pressure quite rapidly.  During one of the batches, I cracked the needle valve when the chamber reached 60PSI, and in a matter of seconds the pressure inside the vessel pegged above 100PSI before I was able to spin the valve wide open.

A possible modification to solve this icing issue would be to separate the solid dry ice from the water entirely.  Building a second (much smaller) chamber to sublime the dry ice, and then pipe the high pressure CO2 back into the main chamber, would be much easier to control.  You would still want the dry ice to be sitting in some sort of liquid to act as a heat source, but you would then be able to use something with a much lower freezing point, such as high proof ethanol, and could even make the secondary generation chamber somehow detachable to charge multiple vessels.

In the end, I still rank this as a rather difficult and dangerous project, and you can buy carbonation tools retail which are much much safer than this, so I won't tell you not to do this, but make well sure you know what you're doing.  I found it an interesting way to spend an afternoon; anyone else in Davis want to have a sparkling fruit juice party?

Thursday, January 19, 2012

Weather Alerts Through Prowl

The fun with Prowl continues.  After getting it running on my Chumby and WRT54G, I was digging through the Prowl Forums looking for other interesting applications when I found a post by acrollet showing how he has his server watch for severe weather alerts from and send them to him as push alerts.  I thought it was a good start, but he crammed the whole thing into his cron table as one line, which is a style I just don't like, so I've rewritten his script with some improvements.

How this alert works is that every hour the cron scheduler on my alarm clock downloads a chunk of XML from which they make available for every region across the US.  So, for example, Yolo county has the county code CAC113, so I have the script hourly download and check to see if there is a new severe weather alert for Yolo county.

If there is an alert, and it's at all different from the one seen an hour ago (which the script stores in /tmp/weather.CAC113.old.txt), then the script uses some grep, awk, and tr magic to extract nice clean summaries and an event name for the alert.  It then uses the typical curl request I figured out before to fire off the push alert and it blows up my iPod with weather alerts.

One thing that you might notice about the script is that I have all of the heavy lifting done in a single function (queryWeather()), which I then call repeatedly, passing it the zone code and my API key.  I did this such that it will be very easy to add or remove zones I want to watch, as well as add friends who also have Prowl onto various zones that they're interested in (I'm toying with having this automated through a web form).

I have this installed on my Chumby's SD card (/mnt/storage/scripts/), but this should run in any decent unix environment.

Script code:

Wednesday, January 18, 2012

Posting Prowl Messages from BusyBox

Yesterday, I showed how to send Prowl notifications to your iOS device from a full featured unix shell.  Unfortunately, more and more devices are now running Linux, but lack the full featured shell needed to use curl or wget to create POST requests over SSL.  This is because many devices, such as WRT54GL routers, NASes, etc, don't really ever need a fully featured user space, so why waste flash storage on utility features which won't be used?  You will find most embedded Linux devices are based on a project called BusyBox.

BusyBox is a stripped down replacement for the traditional GNU user space found in most modern unix environments.  BusyBox implements all of the basic shell utilities such as rm, ls, touch, vi, awk, sed, et cetera, but you will find that most of these utilities are a far cry from their GNU equivalents, only implementing  the bare essentials to have a functional Linux system.  For example, GNU wget has more than 100 available options which you can use to change the behavior as to how wget downloads files or forms requests.  BusyBox wget has seven, and unfortunately, these seven available options don't happen to include the "--post-data" needed to have wget form the POST HTTP request we need to use the Prowl API.

So we've got a real problem.  Prowl's API works through HTTP POST commands (preferably over SSL, but no longer required), but BusyBox's wget lacks the "--post-data" option.  So what the heck do you do?

The obvious answer is to port GNU wget, curl, or even write your own utility to create HTTP POST requests and send them down the line.  HTTP is a fairly simple protocol, and this shouldn't be too big of a deal.  Heck, if you're nimble on the keyboard, you can type in HTTP requests by hand using netcat.

I didn't like that.  BusyBox gives us such a simple but powerful set of tools.  Why go through all the pain of porting something over to a MIPS processor when you can do it with the tools you already have?

So what are we trying to do with a HTTP POST command, really?

HTTP is the protocol used to deliver information over the web.  When you type in a URL (such as, you are telling your web browser to generate a HTTP GET request and to send it off to whatever server is specified by the URL.  HTTP POST requests are very similar, but have extra information in them beyond simply the URL, most often used to pass information back to the web server after you fill out web forms.

But POST requests aren't the only way to pass information back to web servers.  We can use GET requests as well, by appending the information to return to the end of the URL after a question mark.  So, for example, we want to pass to the page our request which includes our apikey, application, event, and description.  This would end up as a HTTP request that would like like this:

After the URL, a question mark begins a number of option=value pairs, which would include our apikey=XXXX, application=WRT54GL, etc.  This isn't as nice as POST requests, because we're more limited at to which characters we can use (which we'll get into later on in this post), and these long ugly URLs tend to lead to users accidentally leaking information more often than POST requests, but the brain dead wget in BusyBox MUST handle GET requests, or else it really wouldn't be a very useful utility at all.

So what's the big deal with building this special GET request with all our info?  Couple shell variables, string 'em all together using a ? and a couple &s, and you've got your URL, right?

URLs are very limited at to the characters they can use in a request.  This is why you'll often see your browser replace such characters as spaces in a URL with %20, which is the ASCII hex representation of " ", because the space otherwise looks like the end of the URL.  So once we generate our long string of option=value pairs, we need to encode them to avoid using any illegal symbols when forming the URL, such as spaces, new lines, %, ~, ^, etc.

This URL encoding can be almost completely accomplished just using a tool called sed, or "stream editor."  Sed operates by you feeding a string of text into it, give it a set of conditions where to replace one thing with another, and then it outputs this edited stream of text.

For example, to encode all the spaces in our request, we need to replace them with their ASCII hex value of 20.  This is done by giving sed the command s/ /%20/g, which says to substitute every instance of " " with %20 through all of every line.  Put together a long boring list of all of these needed sed commands, and we're 95% of the way there.

The one piece of the puzzle missing with using sed is that sed doesn't consider new lines.  Sed operates on a stream one line at a time, which it uses new lines to split the stream into, but it doesn't actually see the new lines traveling through the stream.  So after we convert all the other special characters into their hex equivalent, we need to somehow replace every new line with %0a.  To do this, we use a different tool called tr ("Translate"), which you can give one set of characters to replace with another.

For example, if you were to give tr a-z A-Z, it would substitute every lower case letter in the stream with its corresponding upper case letter.  But we don't need it to do that; we just need it to replace new lines with %0a...

Unfortunately, in a word, it can't.  Tr can't replace a single character with multiple characters...  If only we knew one that did... Hmmm...

What about sed?  We could have tr, which can see the new lines, replace all of the new lines with some special character which sed CAN see, and which is unlikely to already be in the stream, and then have sed replace each of these characters representing new lines with the needed %0a.  Luckily, we have a whole set of characters which we know aren't in the stream anymore, because we just had sed do all this work to encode spaces and tildes into their hex equivalents.

I arbitrarily picked tildes, but any special character would do.  Take the otherwise scrubbed stream, feed it through tr '\n' '~' to replace new lines with ~, and then feed that through sed again with the single command 's/~/%0a/g' and we have an entirely encoded URL, which we can pass on to wget to post to Prowl from our wifi router.

Pretty awesome, if I may say so myself.  In the example code below, I have my router generate a little report including it's CPU load average and file system usage, but you can replace those two lines feeding text into $CACHE with whatever you like, or even have the shell script take the message content as an argument through the shell.  Tomato (and likely most other third party firmwares) makes it real easy to trigger scripts on a regular basis, or even when someone presses the button on the front of it.

Don't forget to insert your API key at the top of the script.

Shell code:

Edit: I probably should have reread the RFCs and Wikipedia pages on HTTP before publishing this.  I believe I've now fixed most of my confusion between GET and POST requests.

Sunday, January 15, 2012

Sending Prowl Notifications from Linux

More than a year ago, I set up my lovely Chumby alarm clock as a web server (go check it out!), which I find useful every now and again for quickly hosting small files.  Unfortunately, even after upgrading it to an 8GB internal SD card, between all the files I dump on it and my poor log rotation schedule on it, it sometimes manages to run out of file system space and gets kind of cranky.

I recently purchased a copy of Prowl, which is a growl notification app for iOS.  Growl is a universal notification system which I'm planning on building into some embedded hardware projects I'm working on, but I figured that while I had already bought the application, I would see how many other nails I can pound in with this hammer.  Turns out, this was a fairly simple application of said hammer to problem.
I wrote a simple shell script and installed it on my Chumby such that every morning at 8:30AM I wake up to a message on my iPod informing me on how my Chumby is doing.  This is done by generating the report via several lines of Bash, storing it in a temporary file, and then passing it along with my Prowl API key to one magic curl command, which you can see at the end of my code:

Shell code:

To get this working, you'll need to copy this shell script onto your Chumby, and then add a line to your Chumby's crontab to schedule it to run at the desired time.  You'll also need to replace my API key with one of your own.

Now, obviously since this is just using cURL to post the notifications, you can run this script in any environment which supports curl, be it almost any Linux environment, OSX, or pretty much anything else.

I found a forum thread on cocoaforge useful while putting this together.

This can also be done using wget instead of curl, if you prefer.  Note that the version of wget on the Chumby doesn't support SSL, so you'll need to drop back to http instead of the suggested https:
$ wget --post-data 'apikey=2541[snip]4d77&application=wget&event=Test message&description=I can use wget' -O -

Thursday, January 12, 2012

Pogo Your Programmer

A lot of my projects are getting to the point where I'm going to be flashing them with firmware once and then installing them to be never flashed again.  This means soldering all the ICSP headers in seems a little silly, but I found a better solution.

Turns out the pogo pins sold by AdaFruit fit in female connectors, so I (carefully) pressed six into my USBtinyISP and bam; bed of nails programmer.  You have to be very careful inserting them; I managed to destroy two pins in the process, so I'm dedicating a whole programmer with these pins, and switched to a spare for main development, because you don't want to be swapping these in and out.

Monday, January 9, 2012

Book Giveaway - Benchtop Electronics Handbook

Hope everyone is having a good start to their 2012.  I've decided that my new years resolution is to try and clean out some of the free stuff I get, so I'm going to experiment with giving stuff away on here.

The first piece of nerdy goodness up for grabs is a copy of Veley's Benchtop Electronics Handbook.  This is a good introductory text on electronics, with 260 educational blurbs on everything from electrical units to basic mathematics to boolean algebra to Karnaugh charts.  I'd call it the reference book for the beginner; the second book you'll want to own after your getting started book.

Giveaway Rules:
  • To enter, go back through this blogs archives and leave a comment below with a link to your favorite project or blog post you can find.
  • Make sure to make your comment using some sort of name (not just anonymous) such that I can announce the winner on January 16th when I do a random drawing.
  • Mention this giveaway on your Twitter, Google Plus, Facebook, etc.  The more comments I get below, the more likely I am to bother continuing to give these things away, and there's better things to come.
  • For this first one, lets open it up to everyone; I'll ship this action international.
Come back on January 16th and I'll post the winner, who should then email me a shipping address to get this rather useful book to add to their collection.

Friday, January 6, 2012

Shooting at the Yolo Sportsmen's Association

As my last celebration of winter break before school starts up again Monday, I figured I would finally get around to driving out to the local trap range and treat myself to an afternoon of shooting.  It turned out to be surprisingly pleasant of an experience, and I had quite a bit of fun.  The staff was very friendly every time I interacted with them, and when I went out to the trap range, there was two guys who let me join in for hot seat shooting, so we traded off every five shots, which was a lot of fun.

The Yolo Sportsmen's Association is, in good Yolo county form, out in the middle of nowhere.  Off of 113, take county road 29 five miles West, and it's on the left.
Of course, Aviation Ave lacks a road sign, but the airport next to the range is well marked, so just follow the signs for it (or the YSA sign right above it to the right in the picture; see if I care).

 The trap range had one thing new to me, which was a set of microphones, so I was able to go shooting by myself by just shouting "pull" into the five microphones and the machine would launch clays for me automatically.  You did need to be a little careful reloading because the microphones would often pick up action noise and launch clays when you closed your action if you did it too loudly.

The price was decent.  $3 + $6 per string, so when I first showed up I bought four tokens in the club house for $24, and then was able to drop the tokens in the trap machine to add 27 clay credits each (they give you two spare clays for misfires).  They don't allow using your magazine, so you can only have one shell in your gun at a time, but they did let me show up and shoot by myself, which was nice because many ranges require you to be there with a friend.

This was the first time I've gotten a chance to shoot my Browning A-5 Light-12, having previously only shot my Winchester 1897, both in 12 gauge.  I did alright; I managed to shoot about 22/25 on the '97 and 17/25 on the Browning.  One of the other shooters was kind enough to come over and explain that the Browning shoots about 18" higher than most guns, so you need to aim low, which is what threw me off.  As usual, they gave me some friendly flack for having the audacity to shoot guns older than they were (My '97 was built in 1935, the A-5 in 1955).  Of course, it's my luck that I managed to slice my finger open on my '97 knuckle buster on the first shot, and never did manage to get it to stop bleeding before I got home.  Next time, I'm bringing band-aids because of that gun.

The YSA also has 25, 50, and 100 yard pistol and rifle ranges, and an archery range, so they can facilitate pretty much anything you want to shoot.

Overall, the staff was refreshingly friendly, and I'd recommend them for anyone in the Yolo area who wants to go to a formal range to go shooting.