Showing posts with label raspberry pi. Show all posts
Showing posts with label raspberry pi. Show all posts

Saturday, January 18, 2014

Project idea: Barcode scanner to shopping list

I've had this idea for a while in various forms, and I'm not alone. Until recently not all of the pieces of the puzzle were going to work together though.


The first attempt, google tasks API to product search/lookup. I got it working, but it was too hard and the results too limited - amazon, and google product searches were all that were supported.

Most recently I wanted to set up my raspberry pi, opencv, and have a shot at webcam based image recognition, but got bogged down in opencv installation.

Oscar solves the problem more simply. The python code is on github. I've just ordered a barcode scanner from ebay, with stand, $25 AUD.

More importantly, for Australia, the major supermarkets have finally gotten into online ordering - I already use this.

Here's examples of searching for products by barcodes.

Coles: Barcode 9300650008861
Woolworths: Barcode 9300650008861

It's not quite to the point I can call an API on their site(s) and add it to a cart, but it's likely I can update the trello list items to link to the woolworths/coles pages - one click to buy per product.

That's pretty close to automatic.

Looking a bit deeper, woolworths appears to support basic POST for authenticated users:

As does Coles, once you strip away the ajax behaviour

I think it would be reasonable, if you could ensure you were authenticated, to end up with a 'one click add to cart' behaviour.

Monday, June 24, 2013

Managing Raspberry Pi with Chef & Bitbucket

The problem:
You have a Pi.
You've overcome that hurdle of thinking up a neat idea.
... but you don't have a means to deploy it to the Pi, and someone keeps coming along to steal the power supply for their Samsung phone.
That, plus your Pi depends on a lot of network configuration or other services to work fully.

The solution:

1) Go and put your Pi next to your Router and ensure both are out of reach. 
Neat cabling often implies secure cabling, so liberal use of zipties will protect your Pi from being beaten out by the collection of hungry smartphones in the house.

2) Make sure you have a passing familiarity with

  • Ruby
  • Bundler
  • Git
  • Bitbucket
  • SSH keys


3) Create a new git repo locally, and a skeleton Chef, Berkshelf setup.
mkdir mypi
cd mypi

# Add bundler
bundle init

# Add some helpful gems
echo 'gem "knife-solo" >> Gemfile
echo 'gem "berkshelf" >> Gemfile

bundle
rbenv rehash
knife solo init
berks init

git add .
git commit -m "Just adding a skeleton"

4) Ensure your SSH keys are on the pi, and go set up your ssh config.
echo "" >> ~/.ssh/config
echo "Host pi.local" >> ~/.ssh/config
echo "User pi" >> ~/.ssh/config

# ssh-keygen if you need to
ssh-copy-id pi@pi.local

5) Finally, start configuring your pi!
echo '{"run_list": [] }' > nodes/pi.local.json
git add nodes/pi.json

bundle exec knife solo prepare pi@pi.local
bundle exec knife solo cook pi@pi.local

5) Go make a pi specific cookbook
cd site-cookbooks
berks cookbook pi
echo 'package "weather-util" do :action install end' >> pi/recipies/default.rb
echo '{"run_list": ["recipe[pi]"] }' > nodes/pi.local.json
git add .
git commit -m "Making some custom pi"
cd ..


6) Run it!
bundle exec knife solo cook -VV pi@pi.local


What just happened?

We added knife-solo, a gem that lets you run chef commands without a chef server.

We made sure our ssh config was right, so we could connect to the machine.

We added berkshelf, a gem to manage chef dependencies/cookbooks (more on this later)

We created nodes/pi.local.json which describes the machine called pi.local. By default, your pi will try to make itself available on this address.

In that recipe, we first gave it an empty run list, but later we added a recipe for your Pi... called... pi.

We added
package "weather-util" do
  action :install
end
to site-cookbooks/pi/recipes/default.rb

... and then updated the nodes/pi.local.json

Finally, we cooked out recipe - the -VV put it into verbose mode.

That installed the 'weather' command onto your pi.

Wouldn't have been easier to do by hand?

Probably, or with Bash scripts + SSH, commititng those to git. What you get from Chef though is more than just bash scripts with a few prebuilt 'test' commands - you can use the entire community of Chef cookbooks to solve a vast majority of your needs.

For example, adding your typically LAMP stack is about as difficult as adding to pi/recipes/default.rb

["mysql", "apache", "php5"].each do |pkg|
  package pkg do
    action :install

  end
end

But then you have to muck around with configuration, starting services, etc.

Instead, I recommend you do something like http://community.opscode.com/cookbooks/apache2

Go do
echo 'cookbook "apache2"'  >> Berksfile
berks install -p cookbooks/

and edit your recipe (pi/recipes/default.rb) to add in

web_app "my_site" do
  server_name node['hostname']
  server_aliases ["pi.local"]
  docroot "/srv/www/my_site"
end

Don't forget to add the relevant depends to the metadata
echo "depends "apache2"' >> site-cookbooks/pi/metadata.rb

And to do the installation steps (remove any previous lines about installing apache you added)
$ cat nodes/pi.local/json

{
  "run_list": ["recipe[apache2::default]", "recipe[pi]"],
  "hostname": "pi.local"
}


and 
bundle exec knife solo cook -VV pi@pi.local

All of a sudden you should have Apache, a named virtualhost, and the contents of "my_site" being served up.

Compare that with manually trying to update a virtualhost, a2ensite it, etc - quite a bit of time can be saved.

More importantly, if you simply add this to git, and publish it to Bitbucket, you instantly have a way to upgrade to a newer server, create a cluster of Pis running your software, or a way to trivially migrate onto the cloud.

Where to from here?

I encourage you to look at cookbooks like newrelic (newrelic is a bad example, as it doesn't have binaries for the pi), so that you can tell if your Pi is up and running when away from the home, as well as things like hamachi - things that let you remotely admin, even behind your home router.

There's plenty of version control cookbooks, allowing you to check out your application from git, or more capistrano like mechanisms.

In general, think of whatever you might need and google "chef opscode (foo) cookbook"

Enhanced by Zemanta

Sunday, January 27, 2013

Air con remote control success

After yet more hacking, I've stuck together just enough code to think I want to go and find a proper home automation framework.

I've got weather-util installed on a cron job doing:
0 * * * * weather ypad ~/butler/weather.txt

This gets me a small text file:
$ cat weather.txt 
Current conditions at Adelaide Airport, Australia (YPAD) 34-56S 138-31E 4M
Last updated Jan 26, 2013 - 11:30 PM EST / 2013.01.27 0430 UTC
   Temperature: 73 F (23 C)
   Relative Humidity: 46%
   Wind: from the SW (230 degrees) at 14 MPH (12 KT)
   Sky conditions: mostly clear
The first argument is an airport identifier. Next, I've used Blather to pull together a quick XMPP bot. It supports commands like
!ac on
!ac cool
!ac temp 23
!ac off
!weather

!weather just pipes the weather.txt back to whomever messaged the bot.

The !ac commands invoke some of the code to send commands to the air conditioner I talked about earlier.

Finally, the bot checks the configured owners for all Auth, and if it hasn't seen you in 8 hours, says hello.

It's far from the cleanest code I've ever written, and Blather is good but needs that little bit more to become "the rails of XMPP". Most importantly here though, I'm a little bit of regex away from writing some simple rules (IFTTT style), and adding the relevant cronjobs. The rules are going to be fairly terse:
while temp > 30; turn on AC

while temp < 25; turn off AC

I'd say if I did much more I'd want to refactor a lot of stuff to be more componentized - IE: jabber notification service when a decision is made doesn't need to be right next to the jabber command service.

I can't decide if I'd want to bring in something like Whenbot, or deal with something as complicated seeming as rhouse. Rhouse (linuxMCE) has a lot of UPnP stuff built it, but annoyingly the samsung smart devices are only using a subset of it - my phone notifies, the air con notifies back; rather than properly using M-SEARCHs.

Having spent a lot of time on this, I suspect that would be quite annoying to work with.

The next step I'll probably do is a further cleanup of my nmap code - when my phone's mac address walks in the door, the butler-bot should message me a greeting / fire up the AC decision engine.

Finally, I have to stick this on it's production server - my new raspberry pi.

More than happy to share the code I've got with anyone interested, it's in a few private bitbucket repos for now - I figured there would be few people with the same setup as I have; so kept it private (at least till I can make it into a few gems)...