Last active
August 15, 2019 09:51
-
-
Save halcat0x15a/4cd18b91ad7394be46e1d29735c3b015 to your computer and use it in GitHub Desktop.
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
import os | |
import re | |
import time | |
import picamera | |
import slack | |
import RPi.GPIO as GPIO | |
SERVO_OUT = 2 | |
IMAGE_FILENAME = 'test.jpg' | |
RECORDING_FILENAME = 'test.h264' | |
VIDEO_FILENAME = 'test.mp4' | |
camera = picamera.PiCamera() | |
camera.rotation = 180 | |
camera.led = True | |
camera.start_preview() | |
time.sleep(2) | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(SERVO_OUT, GPIO.OUT) | |
def change_duty_cycle(angle): | |
servo = GPIO.PWM(SERVO_OUT, 50) | |
servo.start(0.0) | |
servo.ChangeDutyCycle((12 - 2.5) / 180 * angle + 2.5) | |
time.sleep(1) | |
servo.stop() | |
user_id = None | |
@slack.RTMClient.run_on(event='message') | |
def capture(**payload): | |
data = payload['data'] | |
client = payload['web_client'] | |
global user_id | |
if not user_id: | |
res = client.auth_test() | |
user_id = res['user_id'] | |
if 'text' in data and 'user' in data and data['user'] == user_id: | |
text = data['text'] | |
if 'servo' in text: | |
match = re.search(r'\d+', text) | |
if match: | |
angle = int(match.group()) | |
if 0 <= angle <= 180: | |
change_duty_cycle(angle) | |
if 'capture' in text: | |
camera.resolution = (1280, 720) | |
camera.capture(IMAGE_FILENAME) | |
client.files_upload(channels=data['channel'],file=IMAGE_FILENAME,filetype='jpg') | |
if 'start_recording' in text: | |
camera.resolution = (640, 480) | |
camera.start_recording(RECORDING_FILENAME) | |
if 'stop_recording' in text: | |
camera.stop_recording() | |
os.system(f'ffmpeg -y -i {RECORDING_FILENAME} -c copy {VIDEO_FILENAME}') | |
client.files_upload(channels=data['channel'],file=VIDEO_FILENAME,filetype='mp4') | |
try: | |
slack_token = os.environ['SLACK_API_TOKEN'] | |
rtm_client = slack.RTMClient(token=slack_token) | |
rtm_client.start() | |
finally: | |
camera.close() | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment