├── listen_serial.sh ├── code.py ├── listen_serial.py └── readme.md /listen_serial.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | . $HOME/.profile 3 | export XDG_RUNTIME_DIR="/run/user/1000" 4 | /usr/bin/python3 /home/pi/listen_serial.py "/home/pi/newlogs.txt" & 5 | -------------------------------------------------------------------------------- /code.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import time 3 | import busio 4 | import board 5 | import digitalio 6 | import os 7 | from adafruit_circuitplayground.express import cpx 8 | 9 | uart = busio.UART(board.TX, board.RX, baudrate=115200) 10 | 11 | switch_position = cpx.switch 12 | 13 | #Outputs button signals to serial 14 | while True: 15 | if cpx.button_a: 16 | print(1) #Output 1 to start recording 17 | #Light CPX red while recording 18 | cpx.pixels.fill((50, 0, 0)) 19 | time.sleep(0.1) 20 | if cpx.button_b: 21 | print(2) #Output 2 to stop recording 22 | cpx.pixels.fill((0, 0, 0)) 23 | if switch_position is not cpx.switch: 24 | print(3) #Output 3 to reboot RPi 25 | time.sleep(0.1) -------------------------------------------------------------------------------- /listen_serial.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import serial 3 | import subprocess 4 | import os 5 | import time 6 | import sys 7 | 8 | def write_log(log_file, log): 9 | with open(log_file, "a") as f: 10 | f.write(log+"\n") 11 | log_file = sys.stdout 12 | if (len(sys.argv) > 1): 13 | log_file = sys.argv[1] 14 | write_log(log_file, "Starting script") 15 | try: 16 | ser = serial.Serial('/dev/ttyACM0', 115200) 17 | except: 18 | write_log(log_file, "Caught exception creating serial object") 19 | sys.exit() 20 | ffmpeg_file = None 21 | 22 | #(Ex: /dev/video0, /dev/video1) 23 | input_video_device = "YOUR VIDEO DEVICE" #Can find available devices with "v4l2-ctl --list-devices" 24 | #(Ex: hw:=Headset,DEV=0, hw:CARD=Link,DEV=0, hw:0) 25 | input_audio_device = "YOUR AUDIO DEVICE" #Can find available inputs with "aplay -L", or "arecord -l" 26 | input_resolution = None #Leave None or specify a resolution (ex: 1920x1080) 27 | input_framerate = None #Leave None or specify a framerate (ex: 60) 28 | output_resolution = None #Leave None or specify a resolution (ex: 1920x1080) 29 | output_framerate = None #Leave None or specify a framerate (ex: 60) 30 | stream_key = "YOUR STREAM KEY HERE" #Copied from your Twitch channel settings. 31 | 32 | 33 | #Command to start ffmpeg streaming. 34 | ffmpeg_command = ("ffmpeg -nostdin -f v4l2 {0} {1} -i {2} " 35 | "-f alsa -ac 1 -i {3} " 36 | "-vcodec libx264 -rtbufsize 2000k {4} {5} -preset ultrafast -pix_fmt yuv420p -crf 17 -force_key_frames " 37 | "'expr:gte(t,n_forced*2)' -minrate 850k -maxrate 1000k -b:v 1000k -bufsize 1000k " 38 | "-acodec libmp3lame -rtbufsize 2000k -b:v 96k -ar 44100 -f flv 'rtmp://live.twitch.tv/app/{6}'" 39 | .format("-s {0}".format(input_resolution) if input_resolution is not None else '', 40 | "-framerate {0}".format(input_framerate) if input_framerate is not None else '', 41 | input_video_device, 42 | input_audio_device, 43 | "-s {0}".format(output_resolution) if output_resolution is not None else '', 44 | "-framerate {0}".format(output_framerate) if output_framerate is not None else '', 45 | stream_key)) 46 | proc_running = False 47 | while True: 48 | line = ser.readline() 49 | if (b"1" in line and proc_running is False): #if left button is pressed, start streaming 50 | write_log(log_file, "Starting ffmpeg") 51 | open_log = open(log_file,"a") 52 | subprocess.Popen(["bash","-c",ffmpeg_command], stdout=open_log, stderr=open_log) 53 | proc_running = True 54 | if (b"2" in line and proc_running is True): #if right button is pressed, terminate streaming 55 | subprocess.Popen(["bash", "-c", "pkill ffmpeg"]) 56 | proc_running = False 57 | open_log.close() 58 | write_log(log_file, "terminated script") 59 | if (b"3" in line): #if CPX switch is moved, reboot RPi 60 | os.system('sudo reboot') 61 | time.sleep(.5) 62 | ser.close() -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Circuit Playground Express Setup 2 | 3 | ### For ffmpeg streaming to Twitch on the Raspberry Pi 4 | 5 | We used these steps here at Connectify to set up the Adafruit Circuit Playground Express as a remote for Twitch livestreaming on the Raspberry Pi. We have it set up so that the left button starts the live stream from a camera connected via an Elgato Cam Link plugged into one of your USB 3.0 ports, and the right button terminates the stream. Moving the switch to the right will reboot the Raspberry Pi if needed. 6 | 7 | 1. First, you will need to install ffmpeg on the Raspberry Pi for streaming to Twitch. The install command is `apt-get install ffmpeg` but for more detailed compilation instructions see [this compilation guide](https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu). 8 | 9 | 1. Next, plug the Circuit Playground Express in via USB and make sure you have CircuitPy installed on it. [Install instructions for CircuitPy are here](https://learn.adafruit.com/welcome-to-circuitpython/installing-circuitpython). 10 | 11 | 1. Install [Mu Editor](https://codewith.mu/en/howto/1.0/install_raspberry_pi) or another preferred python editor if you don't already have it, and save the code.py file to the top level directory of Circuit Playground Express device. This file should output button inputs over serial. 12 | 13 | 1. Save the listen_serial.py file in your home directory. Open the file in your editor and set the stream_key, input_video_device, and input_audio_device variables. The stream_key variable is your own Twitch stream key inside quotes - found in your Twitch channel settings. Comments in listen_serial.py detail how to find the right audio and video devices. This python script will listen over serial and execute the appropriate bash commands when the buttons are pressed. 14 | 15 | 1. Save the listen_serial.sh file in your home directory. This is a shell script to start the python script as a background process. If you would like to save log files from the Python script's output for debugging, you can add a directory flag in this file before the '&'. e.g. `/usr/bin/python3 /home/pi/listen_serial.py "/home/pi/log.txt" &` 16 | 17 | 1. Enter `crontab -e` into the terminal to edit the crontab and add this line to the bottom. This will wait 30 seconds after Pi reboot and then run the listen_serial.sh shell script. 18 | ``` @reboot sleep 30; /home/pi/listen_serial.sh ``` 19 | 20 | Now, with the Circuit Playground express plugged into the pi via usb, your camera plugged into the Cam Link and powered on, and a stable internet connection, you should be able to press the left button, see the remote light up red, and stream to your Twitch channel. 21 | 22 | If it doesn't start streaming after a few seconds, try hitting the right button to quit and starting the stream with left button again. If it still isn't working, you may want to enable logs in the shell script and check for errors. You might have to modify the ffmpeg command in the listen_serial.py file to suit your own equipment setup. Additionally, if you aren't seeing the red ring of lights on the Circuit Playground Express when you press the left button, then there may be an issue with the CircuitPy installation. 23 | 24 | --------------------------------------------------------------------------------