Last active
April 11, 2019 08:20
-
-
Save whomwah/0d3a5930be856ba0476dd0c501a92100 to your computer and use it in GitHub Desktop.
[Raspberry Pi] A simple bot in Ruby that uses RPi::GPIO to check a pin on a Raspberry PI with a moisture sensor on it to see if it needs watering. #pi
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
45 11 * * 1-5 /bin/bash -c 'export PATH=~/.rbenv/shims:~/.rbenv/bin:"$PATH"; cd /home/pi/app/plant/; ruby thirsty.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
source 'https://rubygems.org' | |
gem 'rpi_gpio', '~> 0.3.3' | |
gem 'slack-ruby-client', '~> 0.10.0' |
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
# | |
# Run this file using CRON | |
# | |
require 'rubygems' | |
require 'logger' | |
require 'bundler/setup' | |
Bundler.require(:default) | |
Slack.configure do |config| | |
config.token = 'xxxxxxxxxxxxxxxxxxxxxx' | |
end | |
class Thirsty | |
PIN_NUM=17 | |
SLACK_CHANNEL='#slack-channel' | |
def initialize | |
$stdout.sync = true | |
RPi::GPIO.set_numbering :bcm | |
RPi::GPIO.setup(PIN_NUM, as: :input) | |
@slack = Slack::Web::Client.new | |
@logger = Logger.new($stdout) | |
end | |
def self.run! | |
new.check_plant | |
end | |
def check_plant | |
water_me if RPi::GPIO.high? PIN_NUM | |
end | |
private | |
def water_me | |
@slack.chat_postMessage( | |
channel: SLACK_CHANNEL, | |
text: ":wave: Hey, I'm the plant on the desk.\nI really need a :shower: it's like a :desert: in here.", | |
as_user: true | |
) | |
@logger.info 'I need watering...' | |
end | |
end | |
Thirsty.run! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment