Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make it Rain Raspberry Pi GPIO controlling script
require 'httparty'
require 'json'
require 'dotenv/load'
url = 'https://websiteURLgoesHere'
if ENV['RaspberryPi'] == true
require 'rpi_gpio'
end
def trigger_water(relay)
if ENV['RaspberryPi'] == true
RPi::GPIO.clean_up
RPi::GPIO.set_numbering :bcm
RPi::GPIO.setup relay, :as => :output
RPi::GPIO.set_high relay
sleep 10
RPi::GPIO.set_low relay
RPi::GPIO.clean_up
else
sleep 10
end
end
loop do
begin
response = HTTParty.get(url+"/retrieve.json")
puts response.code
puts "---------------"
if response.code == 200
puts response.body
puts "---------------"
json = JSON.parse(response.body)
puts json
id = json['id']
value = json['value']
puts value
if value >= 99 && value <= 499
puts "Watering body"
trigger_water(4)
end
if value >= 500
puts "Watering head"
trigger_water(17)
end
HTTParty.post(url+"/complete_event", body: { event_id: id, completed: true })
else
sleep 5
end
rescue Exception => e
puts "Cannot connect to server"
sleep 5
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment