Skip to content

Instantly share code, notes, and snippets.

@adamwiggins
Created November 18, 2013 23:27
Show Gist options
  • Save adamwiggins/7537265 to your computer and use it in GitHub Desktop.
Save adamwiggins/7537265 to your computer and use it in GitHub Desktop.
Raspberry Pi sample program
source "https://rubygems.org"
gem 'sinatra'
web: ruby web.rb
require 'bundler/setup'
require 'sinatra'
def run(cmd)
puts "Running: #{cmd}"
out = `#{cmd}`
success = $?.success?
puts out
fail unless success
out
end
configure do
run "echo 17 > /sys/class/gpio/unexport"
run "echo 17 > /sys/class/gpio/export"
run "echo out > /sys/class/gpio/gpio17/direction"
end
get '/' do
if request[:to] == 'On'
run("echo 1 > /sys/class/gpio/gpio17/value")
elsif request[:to] == 'Off'
run("echo 0 > /sys/class/gpio/gpio17/value")
end
"<html><body>" +
"<h2>LED value is: " + run("cat /sys/class/gpio/gpio17/value") + "</h3>" +
"<form><input type=submit name=to value=On /><input type=submit name=to value=Off></form>" +
"</html></body>"
end
get '/on' do
run("echo 1 > /sys/class/gpio/gpio17/value")
"turned on"
end
get '/off' do
run("echo 0 > /sys/class/gpio/gpio17/value")
"turned off"
end
set :bind, '0.0.0.0'
set :port, ENV['PORT'] || 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment