Created
November 18, 2013 23:27
-
-
Save adamwiggins/7537265 to your computer and use it in GitHub Desktop.
Raspberry Pi sample program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem 'sinatra' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: ruby web.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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