Skip to content

Instantly share code, notes, and snippets.

@ksouthworth
Last active August 1, 2019 18:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksouthworth/a877d355c6c7ca3456a1 to your computer and use it in GitHub Desktop.
Save ksouthworth/a877d355c6c7ca3456a1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Written for Python 3.2.3
# sudo apt-get update
# sudo apt-get upgrade
# sudo apt-get install python3-picamera
# sudo apt-get install python3-pip
# sudo pip3 install readchar
# sudo pip3 install slacker
import time
import readchar
import pprint
import picamera
from slacker import Slacker
VERSION = 0.1
SLACK_TOKEN = "API_TOKEN" # Replace with your Slack API token - https://api.slack.com/web
SLACK_CHANNEL_ID = "CHANNEL_ID" # Replace with your Slack channel ID - https://api.slack.com/methods/channels.list/test
BREW_TIME_SECONDS = (11 * 60) # should be adjusted based on your coffee-maker
CAMERA_VFLIP = True # our PiCamera is mounted upside-down
CAMERA_HFLIP = True
PROMPT = ("-" * 50) + "\nPress \'b\' to start brewing, any other key to exit\n"
ASCII_LOGO = """
_____ __ __ ____ _
/ ____| / _|/ _| | _ \ | |
| | ___ | |_| |_ ___ ___ | |_) | ___ | |_
| | / _ \| _| _/ _ \/ _ \ | _ < / _ \| __|
| |___| (_) | | | || __/ __/ | |_) | (_) | |_
\_____\___/|_| |_| \___|\___| |____/ \___/ \__|
"""
slack = Slacker(SLACK_TOKEN)
def print_slack_channels():
print("Your current Slack channels:")
response = slack.channels.list()
for channel in response.body["channels"]:
print("\t{0:<30} (ID: {1})".format(channel["name"], channel["id"]))
print("")
def start_brewing():
print(("*" * 50) + "\n STARTING A BREW \n" + ("*" * 50))
# Post a text-only message to Slack
print("\t[Slack] posting message")
slack.chat.post_message(SLACK_CHANNEL_ID, ':coffee: brewing... :clock4: ')
# Take a photo of the coffee label
print('\t[PiCamera] taking a photo')
photo_filename = 'coffee-label.jpg'
photo_taken = False
with picamera.PiCamera() as camera:
camera.vflip = CAMERA_VFLIP
camera.hflip = CAMERA_HFLIP
camera.resolution = (640, 480)
#camera.resolution = (320, 240)
camera.capture(photo_filename)
photo_taken = True
if photo_taken:
# Upload photo to Slack
print('\t[Slack] uploading photo')
slack.files.upload(photo_filename, title='now percolating...', initial_comment='', channels=[SLACK_CHANNEL_ID])
else:
print('\t[PiCamera] FAILED TO TAKE PHOTO')
print('Waiting for coffee maker to finish brewing...')
time.sleep(BREW_TIME_SECONDS)
end_brewing()
def end_brewing():
print('Brew Complete!')
slack.chat.post_message(SLACK_CHANNEL_ID, ':coffee: READY!')
print(ASCII_LOGO)
print("CoffeeBot v{0} at your service!\n\nPush the 'button' to post a message to slack and upload a photo of your bag of coffee.".format(VERSION))
print("")
print_slack_channels()
print("Using Slack Channel: {0}".format(SLACK_CHANNEL_ID))
print("Brew Time: {0} minutes".format(BREW_TIME_SECONDS / 60.0))
while True:
print(PROMPT)
c = readchar.readchar().lower()
if (c == 'b'):
start_brewing()
else:
print('You entered: ' + c)
print('Exiting...')
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment