Made by Nathan

programming and projects

Raspberry Pi Microwave

| Comments

A few months ago, I was inspired by this post on Reddit, titled: Food items should have QR codes that instruct the microwave exactly what to do. Like high for 2 minutes, let stand 1 minute, medium 1 minutes..

I thought this was a pretty cool idea, and that it would be a fun project for a Raspberry Pi. I agreed with the people who thought using UPC barcodes would be better, since products already have them, so I went with a barcode scanner + online product database.

Here’s a summary of the features that I’ve added to my microwave:

  • Re-designed touchpad
  • Nicer sounds
  • Clock is automatically updated from the internet
  • Can be controlled with voice commands
  • Can use a barcode scanner to look up cooking instructions from an online database
  • There weren’t any online microwave cooking databases around, so I made one: http://microwavecookingdb.com
  • The microwave has a web page so you can control it from your phone (why not), and set up cooking instructions for products
  • Tweets after it’s finished cooking something (See https://twitter.com/rbmicrowave)

Demo Video

Using a Raspberry Pi to cook a Raspberry Pie

Here’s the recipe.

Raspberry Pie Ingredients Raspberry Pie Mixing Raspberry Pie Cooking Raspberry Pi - Raspberry Pie A slice of Pi


Keep reading below if you’re interested in electronics or software:

Hardware

I used a microwave with a touchpad, and discovered that the touchpad was a button matrix. I took photos of the touchpad and traced the wires, so that I could tell which pins corresponded to which buttons.

Touchpad Touchpad Traces Original Touchpad UI

I initially wanted to put everything in a case outside the microwave, but I decided that it would be more challenging and fun to try and fit everything inside. Here’s all the PCB revisions, before I settled on a design that would fit neatly on top of the microwave’s original PCB.

PCB revision 1 PCB revision 2 PCB revision 3

I used shift registers and optocouplers to control the touchpad pins. To listen for touchpad presses, an output shift register scans one line at a time on the first touchpad layer, and an input shift register listens for connections to the second layer.

I unsoldered the touchpad connector from the original circuit board, and replaced it with a row of pin headers. I then used the original touchpad connector on my PCB, so that my circuit acts as a kind of proxy for button presses.

Here’s the final product after transferring toner, etching, drilling, and soldering. (I had to use the ribbon cables to save space.)

Ready to Etch Everything Soldered PCB Bottom

And here’s how it fits on top of the microwave controller:

Plugged In Plugged In

When I peeled off the old touchpad overlay, it became wrinkled and ugly, so I thought I may as well have a go at redesigning the interface. My goal was to get rid of the features I don’t really use, and make the basic functions more convenient. The top two rows of buttons are now dedicated to “one touch” cooking times, for either “high” or “medium” power. You can also set the time and power manually.

Touchpad

You might have noticed that I started the project with the intention of using an Arduino Nano plugged into a Raspberry Pi USB port. This was because I was a) familiar with the Arduino, b) not familiar with the Raspberry Pi GPIO, and c) thought it would make testing and debugging a bit easier, since I could just plug the Arduino into my laptop.

However, it turns out that my Raspberry Pi had some issues with the arduino’s FTDI chip if the Raspberry Pi was turned on while the Arduino was plugged in. It wouldn’t recognize the Arduino until I unplugged it, and then plugged it back in again.

So I decided to make an Arduino Nano => Raspberry Pi GPIO adapter, and port my Arduino code to the Raspberry Pi GPIO using the WiringPi library.

Arduino to Raspberry Pi adapter Arduino to Raspberry Pi adapter Arduino to Raspberry Pi adapter Arduino to Raspberry Pi adapter

The Raspberry Pi is powered by a USB hub, which is also plugged into the Raspberry Pi’s USB port. To power the hub, I wired up a power adapter to the microwave’s power source. There’s also a USB powered speaker, USB microphone, wifi adapter, and barcode scanner.

Power adapter USB Hub Microphone + Wifi

Microwave Software

All the software running on the Raspberry Pi is hosted at: https://github.com/ndbroadbent/raspberry_picrowave.

There are 4 main components:

Microwave Daemon

This runs the code that listens for touchpad button presses, and controls the microwave. It also accepts TCP connections so that other programs can send commands or request information about the microwave’s status.

Barcode Instructions

This program listens to the barcode scanner, and requests product information from the Microwave Cooking Database. It also runs the cooking programs.

Voice Control

I used PocketSphinx for voice recognition, which worked very well with my small corpus. I embedded Ruby in the pocketsphinx_contiunous C program, so that it would be easier to script voice commands and send commands to the microwave daemon. It turns out that the acoustics of my kitchen seem to mess up the recognition, so it won’t be used very often.

Sinatra App

There’s a simple sinatra web application that lets you control the microwave from your phone or computer. This may not be a big selling point. It uses a JavaScript EventSource to push updates to the browser, so you could have hundreds of users connected to your microwave at once.

If any barcodes are scanned that can’t be found on the Microwave Cooking Database, this webpage will display the unknown barcodes and provide a link to add the new product.

Microwave Cooking Database

I couldn’t find any existing websites with a database of microwave cooking instructions, so I made one.

Microwave Cooking Database

If cooking instructions are posted for a 1000W microwave, you can request the instructions for a 700W microwave, and the cooking times will be automatically adjusted.

So if you’re also planning on making an internet connected microwave with a barcode scanner, please feel free to sign up and add some products.

Patents?

Nope, this is just a fun project. Microwaves with barcode scanners already exist, and most of these features are already patented.

Thanks for reading!

I’d be interested to hear if you do something similar, or make use of http://www.microwavecookingdb.com.

Ubuntu Keyboard Shortcut: Open a Selected File in Sublime Text 2

| Comments

Whenever I’m looking at backtraces, logs, or failing tests in the terminal, I often need to open one of those files in my text editor. It was previously a semi-arduous process that involved highlighting, copying, pasting and the return key. Now, all I need to do is double-click or highlight a line, and then press a keyboard shortcut to open that file & line in my text editor (currently Sublime Text 2.) I’ve also added a thing to my $PROMPT_SCRIPT that stores my terminal’s most recent directory in ~/.cwd~, so that the script can handle relative paths. (Most of the time I’m just in the root folder of a given project.)

One extra feature for Ruby developers is support for backtrace lines like this:

app/models/post.rb:225:in `sharing_is_caring'

If you double-click that file, you’ll end up with the following selection: app/models/post.rb:225:in. The script will automatically strip the trailing :in, so you can just double-click instead of manually highlighting.

Requirements

  • Ubuntu
  • xclip (install with sudo apt-get install xclip)
  • A text editor, such as Sublime Text 2.

Installation

mkdir -p ~/bin
wget https://raw.github.com/ndbroadbent/dotfiles/master/bin/open_selected_in_editor -O ~/bin/open_selected_in_editor
chmod +x ~/bin/open_selected_in_editor
  • Modify the script to use your preferred text editor

Set up current working directory support

Add the following line to your ~/.bashrc:

PROMPT_COMMAND+="pwd > ~/.cwd~;"

This means that every time you press return in the terminal, the script can use your current directory to determine an absolute path for a highlighted file. It’s not completely foolproof, but good enough for me.

Set up a keyboard shortcut in Ubuntu 12.04

  • Go to System Settings –> Keyboard –> Shortcuts –> Custom Shortcuts
  • Click the + to add a new shortcut with:
    • Name: Open selected in editor
    • Command: ~/bin/open_selected_in_editor
  • Set the keyboard shortcut. I like Ctrl+Shift+X.

Ubuntu Keyboard Shortcuts

All done! Now you can highlight a filename in the terminal, press your keyboard shortcut, and open it in your editor without the need to copy & paste filenames. Please let me know if you need any help, but I’m sorry I don’t know how to do this in OS X or Windows.

Open Files Generated by ‘Rails Generate’ in Your Editor

| Comments

Note: This post is for people who use the terminal to run Rails generators. It probably won’t interest you if your editor has a plugin to run them.

After running a Rails generator, you’ll often need to edit the generated files in your text editor.

Rails 4 may soon have the ability to open generated files in your text editor if you pass the --editor option on the command line. You can follow my pull request for more details.

If you use the --editor option (or its -e alias), the generator will open all generated or copied files in your text editor. The default editor is your GUI_EDITOR or EDITOR environment variable, but you can set the editor manually with --editor=manual-code-editor.

Rails 3 isn’t accepting any new features, so here’s how you can use the --editor option for your Rails 3 apps:

Single Rails 3 app

To install the --editor option in a single Rails 3 app, run the following command from your app directory:

curl https://gist.github.com/raw/4342095/rails -o script/rails

This will update script/rails, and you can commit the change.

Globally for all Rails 3 apps

If you are developing a lot of different apps, it might be too much hassle to update all of them. Instead, you can download the custom rails script to your /bin directory, and add some shortcut functions to your .bashrc.

Download script:

sudo curl https://gist.github.com/raw/4342095/rails3_with_editor -o /bin/rails3_with_editor
sudo chmod +x /bin/rails3_with_editor

Add shortcut functions:

Add the following to your ~/.bashrc:

rge() { rails3_with_editor generate "$@" --editor; }
rgm() { rails3_with_editor generate migration "$@" --editor; }

Now you can run rgm to generate a migration from any Rails 3 app.

Automatically Run ‘Bundle Install’ When Bundler Can’t Find a Gem

| Comments

Every Rails developer has probably experienced the following error:

Could not find <gem> in any of the sources
Run `bundle install` to install missing gems.

This happens if you or someone else adds a gem to your Gemfile, or if a gem version is updated in Gemfile.lock, and you forget to run bundle install before running a Rails command.

Here’s a simple function that handles this automatically, called bundle_install_wrapper(). It first tries to execute the command you pass to it. However, if Bundler exits with status code 7 (GemNotFound), then it will run bundle install. Finally, it retries the original command.

bundle_install_wrapper() {
  # Run command
  eval "$@"
  if [ $? = 7 ]; then
    # If command crashes, try a bundle install
    echo -e "\033[1;31m'$@' failed with exit code 7."
    echo    "This probably means that your system is missing gems defined in your Gemfile."
    echo -e "Executing 'bundle install'...\033[0m"
    bundle install
    # If bundle install was successful, try running command again.
    if [ $? = 0 ]; then
      echo "'bundle install' was successful. Retrying '$@'..."
      eval "$@"
    fi
  fi
}

Usage

Drop the function in your ~/.bashrc, and add aliases for rails commands:

alias rs="bundle_install_wrapper rails server"
alias rc="bundle_install_wrapper rails console"
# etc.

If you want aliases that support any Rails application, you can use something like this:

# Run Rails commands on any version
rails_cmd(){
  # Rails 3
  if [ -e ./script/rails ]; then bundle_install_wrapper rails3_with_editor $@
  # Rails <= 2
  elif [ -e ./script/$1 ]; then bundle_install_wrapper ./script/$@
  # Rails 4
  elif [ -e ./config.ru ] && grep -q Rails config.ru; then bundle_install_wrapper rails $@
  else echo "== I don't think this is a Rails application!"
  fi
}
alias   rs="rails_cmd server"
alias  rsd="rails_cmd server -u"
alias   rc="rails_cmd console"
alias   rg="rails_cmd generate"

See the Ruby on Rails section in my .bashrc if you’re interested in more aliases, and please leave a comment if you have any tips to share.

Capistrano_colors Has Been Merged Into Capistrano

| Comments

capistrano_colors has been merged into capistrano. If you update capistrano to version 2.13.5, you will no longer need to include capistrano_colors in your Gemfile.
Don’t forget to also remove require 'capistrano_colors' from config/deploy.rb.

If you’ve been using capistrano without the capistrano_colors gem, well, you’ve been missing out! Your logs will now be nicely formatted and colored by default, like this:

Capistrano logs with colors and formatting

However, if you happen to be a big fan of black & white logs, you can just add disable_log_formatters to config/deploy.rb. See the Formatting Logs wiki page for more information about customizing the colors and styles.

Thanks to @stjernstrom for creating the capistrano_colors gem, and to @carsomyr for merging my pull request!