---
title: Order Pizza with USB Pizza Button
canonical: https://0110.be/posts/Order_Pizza_with_USB_Pizza_Button
markdown_url: https://0110.be/posts/Order_Pizza_with_USB_Pizza_Button.md
id: 188
published_at: '2009-11-11T00:00:00Z'
updated_at: '2013-12-05T18:19:15Z'
author: Joren
tags:
- name: 0110.be
  markdown_url: https://0110.be/tags/0110.be.md
- name: Code
  markdown_url: https://0110.be/tags/Code.md
- name: Hackerspace Ghent
  markdown_url: https://0110.be/tags/Hackerspace%20Ghent.md
- name: Poging tot humor
  markdown_url: https://0110.be/tags/Poging%20tot%20humor.md
- name: Projecten
  markdown_url: https://0110.be/tags/Projecten.md
---

# Order Pizza with USB Pizza Button

Recently I bought a big shiny red USB-button. It is big, red and shiny. Initially I planned to use it to deploy new versions of websites to a server but I found a much better use: *ordering pizza*. Graphically the use case translates to something akin to:

<div style=" text-align:center;margin-bottom:1em">
<img src="https://0110.be/files/attachments/188/order_pizza_with_panic_button.jpg"/>

</div>
If you would like to enhance your life quality leveraging the power of a USB pizza-button: you can! This is what you need:

1.  A PC running Linux. This tutorial is specifically geared towards Debian-based distos. YMMV.

2.  A big, shiny red USB button. Just google "USB panic button" if you want one.

3.  A location where you can order pizzas via a website. I live in Ghent, Belgium and use just-eat.be. Other websites can be supported by modifying a Ruby script.

Technically we need **a driver** to check when the button was pushed, a way to **communicate** the fact that the button was pushed and lastly we need to be able to **react** to the request.

**The driver**: on the internets I found [a driver for the button](http://www.aeracode.org/2009/1/22/panic-button/). Another modification was done to make the driver process a daemon.

<img src="https://0110.be/files/attachments/188/usb-panic-button.png" style="float:right"/>

**The communication**: The original Python script executed another script on the local pc. A more flexible approach is possible using sockets. With sockets it is possible to notify any computer on a network.

\`\`\`ruby\
if PanicButton().pressed():\
\# create a TCP socket\
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\
\# connect to server on the port\
s.connect((SERVER, SERVER_TCP_PORT))\
\# send the order (margherita at restaurant mario)\
s.send("mario: \[margherita_big\]\\n")\
\`\`\`

**The reaction**: a ruby TCP server waits for message from the driver. When it does it automates a HTTP session on a website. It executes a series of HTTP-GET's and POST's. It uses the [mechanize library](http://mechanize.rubyforge.org/mechanize/).

\`\`\`ruby\
login_url = "http://www.just-eat.be/pages/member/login.aspx"\
a = WWW::Mechanize.new\
a.get(login_url) do \|login_page\|\
#post login_form\
login_form = login_page.forms.first\
login_form.txtUser = "username"\
login_form.txtPass = "password"\
a.submit(login_form, login_form.buttons\[1\])\
end\
\`\`\`

Some libraries are needed. For python you need the usb library, the [python deamons lib](http://pypi.python.org/pypi/python-daemon/) needs to be installed seperatly. Setuptools are needed to install the deamons package.

\`\`\`ruby\
sudo apt-get install python-usb python-setuptools\
\`\`\`

Ruby needs rubygems to install the needed mechanize and daemons library. Mechanize needs the <code>libxslt-dev</code> package. You also need the <code>build-essential package</code> to build mechanize.

\`\`\`ruby\
sudo apt-get install rubygems libxslt-dev\
sudo gem install mechanize daemons\
\`\`\`

To automatically start the daemons on boot you can use the [crontab <code>\@reboot</code> directive](http://mkaz.com/ref/unix_cron.html) of the root user. E.g.:

\`\`\`ruby\
`reboot /opt/pizza_service/pizza_daemon.rb
`reboot /opt/pizza_service/pizza_button_driver.py\
\`\`\`


- [order\_pizza\_with\_panic\_button.jpg](https://0110.be/files/attachments/188/order_pizza_with_panic_button.jpg)

- [pizza\_daemon.rb.txt](https://0110.be/files/attachments/188/pizza_daemon.rb.txt)

- [pizza\_server.rb](https://0110.be/files/attachments/188/pizza_server.rb)

- [usb-panic-button.png](https://0110.be/files/attachments/188/usb-panic-button.png)

- [pizza\_button\_driver.py](https://0110.be/files/attachments/188/pizza_button_driver.py)
