I'm using the WiringPi gem. This block of code works as expected:
pinNumber = 7
io = WiringPi::GPIO.new do |gpio|
gpio.pin_mode(pinNumber, WiringPi::INPUT)
end
pin_state = io.digital_read(pinNumber)
return pin_state
However, when I enclose this in a method so I can make a call using Sinatra, I get the following error when I try to refresh:
wiringPiSetup*: You must only call this once per program run. This is a fatal error. Please fix your code.
Why must this be run only once, and what is the best solution? Ultimately pin_state
needs to be retrieved every time I navigate to the root url.
Here's the full code:
require 'wiringpi'
require 'sinatra'
def getstate()
pinNumber = 7
io = WiringPi::GPIO.new do |gpio|
gpio.pin_mode(pinNumber, WiringPi::INPUT)
end
pin_state = io.digital_read(pinNumber)
return pin_state
end
get '/' do
getstate()
end