Archives For Ruby

blinkin blue leds with ruby

October 23, 2016
#!/usr/bin/env ruby

require 'rpi_gpio'

def init
    RPi::GPIO.set_numbering :bcm

    [17,18,27,22].each do |pin|
        RPi::GPIO.setup pin, :as => :output
        RPi::GPIO.set_low pin
    end
end

def cycle
    [17,18,27,22,27,18].each do |pin|
        RPi::GPIO.set_high pin
        sleep (0.25)
        RPi::GPIO.set_low pin
    end
end

init
(1..9).each{ cycle }

RPi::GPIO.reset

This is the Ruby version of my Cylon LED test cycler that I’ve written in various languages on Arch Linux and the Raspberry Pi. This time it was Ruby’s turn to see if I could use it to manipulate the various I/O subsystems on the Pi. Since setting output pins to drive LEDs is the easiest place for me to start, I wrote the code you see above. Before I could make it work I had to install the rpi_gpio gem. To get some idea of how to use it I looked at the GitHub repository for rpi_gpio. The reason for this is to be able to bridge between the Ruby on Rails work I started earlier this week with both input and output I/O. The only other comment I will make is to make sure that the udev rules have been set up to manipulate the I/O as a regular user. Trying to run the code shown above as root will fail, as the installation of rpi_gpio was local to a regular account, not globally installed.

My only comment on rpi_gpio so far is this: I discovered I had to code a set_low immediately after every enabling of a pin as an output, as the pin was enabled high. I consider this a bug, and as it stands it may not be suitable for what I need. If even a microsecond pulse goes out from those pins after being enabled by Ruby then that’s enough to cause a false start on that pin. The Ruby code isn’t something to write home about, just something to do some very basic testing.

screen-shot-2016-10-20-at-10-05-26-pm

I’ve been asked to create a web monitoring application running on a Raspberry Pi (in this case the 3). The requirements are very modest; run a low-traffic web page to monitor another machine next to it, and provide a simple web report on demand across local WiFi. For this particular project I’ve elected to go back in time, as it were, and install Ruby and Rails as the web foundation. And when I say back in time, I’m talking 2007, when I created another embedded system using a milliwatt CMOS 486 clone chip running a custom built Linux kernel with µclib and BusyBox running Ruby and Rails.

This small post documents the steps, in order, for installing Ruby and Rails peculiar to Arch Linux ARM and the Raspberry Pi 3.

First, make sure that Ruby is installed. That’s as simple as ‘sudo pacman -S ruby’, and for the documentation, ‘sudo pacman -S ruby-docs’. Ruby Gems are a part of Ruby, and you can look to see if they’ve been installed with either a ‘gem -v’ or by looking down /usr/lib/ruby.

Second, add the following line to your .bashrc:

  • export PATH=”$(ruby -e ‘print Gem.user_dir’)/bin:$PATH”

Log out, then log back in. Make sure to do all this before you install Rails, or else Very Bad Things will happen when you do install Rails. It took two attempts to install Rails, all because I failed to add that line to my bashrc file before the first attempt. You can read all about RubyGems setup here.

Third, install Rails with ‘gem install rails’. This will install an account local copy of Rails under ~/.gem, specifically ~/.gem/ruby/2.3.0/gems with this version of Gems. Installing a local account copy is no different than what happens with Node.js installations. There’s always a debate about local vs global installation; due to security and my personal paranoia I always prefer local (non-root) account installation to minimize any unintended consequences a global installation might engender.

Fourth, to be able to reach the Ruby instance outside the Raspberry Pi, I modified the file ~/[project]/config/boot.rb and added the following lines of code at the end of the file:

require 'rails/commands/server'

module Rails
    class Server
        def default_options
            super.merge({Port: 8081, Host: '0.0.0.0'})
        end
    end
end

The merge binds the web server to whatever IP address DNS assigns to the Raspberry Pi (‘0.0.0.0’) instead of using localhost as the defult, and changes the port from the default of 3000 to 8081.

Finally, and this is just extra, I made a tiny modification to the default index file at ~/.gem/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/templates/rails/welcome/index.html.erb to add the text “Raspberry Pi 3” in order to drive home that the default web page was in fact coming from the Raspberry Pi 3.

Now on to doing something more useful…

ruby and annie

It’s been a while since I lead off with any of the household animals, especially the Labs. I can still call out plural Labs because Annie is a Labradoodle mix. Pride of place in this photo belongs to Ruby, who has perfected the side-long look like no other Lab that’s ever lived with us. She loves to hop up on the sofa next to me, sidle over close, and then give me that “come hither” look, which for a Lab means “walk hither.” Annie, back in the shadows, is watching, picking up tips. Her schtick are her flying leaps, which I’m assuming come from the Poodle side of the Doodle mix.

cat condo

The Mackerel Tabbies seem to be getting along pretty well these days, at least in the big cat tree. I’ve not checked in about a month, but back in April, at six months, the Gingersnaps were weighed at nine pounds each. Lulu, resting at the bottom, is a little over fourteen. Everybody realizes Lulu is our special needs cat, and they give her reasonable wide berth. But when it comes time to nap, everybody gets along in the cat tree. As long as nobody but Lulu goes in the cat condo.

Gear

I’m photographing more, but posting less. The breaks are good as long as the don’t become indefinite. Top photo was taken with the Olympus E-M10 and the 14-42 EZ pancake zoom, bottom with the E-M10 and 45mm Zuiko. I also took the luxury of post processing with Lightroom 6.

the critters

January 31, 2016

hey!

Here, in no particular order, are some interesting photos of some of the critters that live with me and my wife. They fill our lives with joy and keep us entertained and on our toes (if not standing on our toes). They are, from top to bottom, Annie, Ponder, Ruby, and Annie again. I try as best I can to capture little moments from their lives. I have no favorites, I love them all. However, I have to admit that as Annie gets older she’s looking more and more like a living Muppet. She reminds me of Animal.

look into my eyes...

my pink gorilla

my pink gorilla

Three weeks after loosing Max, we have Dreamboat Annie the Labradoodle and a much happier Ruby Tuesday. Or as Annie’s known informally, the wooly doodle. Or Annie-Banannie. She came to live with us a week after Max passed. She’s now five months. As can be seen in the back of mom’s Prius, they’re both pretty tight as far as canine buddies go.

She and Ruby have gotten used to one another and now play pretty heavily together. Tug-of-war, chase, dragon fights (where they play-mouth one another and make grunting sounds), and other doggish activities. Long walks every evening are now de rigueur once again. When they walk they walk together out front at a rapid pace. It’s been years since Max wanted to walk with any kind of pace and side-by-side out front with Ruby. Ruby is happy and I think pretty much over loosing Max.

As for me, it’s more complex. I have a lot of digital photos of the same period when Ruby first came to live with Max, at about the same age as Ruby is now. Being human my memories are a lot sharper, especially when helped by what I photographed and wrote about back then. Having Annie triggers an odd mixture of simultaneous pleasure in having her here while still feeling the pain of Max’s loss.

Life is neither simple nor slow. Annie’s introduction into, and interaction with, the household can’t be ignored while I mope around feeling sorry. It’s not fair to anyone, least of all little Annie.

Obviously there’s more to come. I just need to sit down and gather my thoughts into coherent sentences and paragraphs.