Skip to content

Instantly share code, notes, and snippets.

@whomwah
Last active April 11, 2019 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whomwah/0d3a5930be856ba0476dd0c501a92100 to your computer and use it in GitHub Desktop.
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
45 11 * * 1-5 /bin/bash -c 'export PATH=~/.rbenv/shims:~/.rbenv/bin:"$PATH"; cd /home/pi/app/plant/; ruby thirsty.rb'
source 'https://rubygems.org'
gem 'rpi_gpio', '~> 0.3.3'
gem 'slack-ruby-client', '~> 0.10.0'
#
# 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