Created
August 26, 2020 22:35
-
-
Save ptagell/d1b25eebb86a67b4e1e58d81e4239a16 to your computer and use it in GitHub Desktop.
Make it Rain Raspberry Pi GPIO controlling script
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 '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