BeatSaber ShockBot
A Python script, a RaspberryPi, a WebSocket mod and a TENS unit to take BeatSaber.
Installing the WebSocket mod
To begin you will need to install the websocket mod for BeatSaber. To do this you will need to download and install the ModAssistant. Once installed open the application on your machine and install the websocket-sharp
mod. You can find more details about this mod on Github.
As a final step you will want to confirm the port number the Websocket is running on to update in the BeatSaber-Shockbot
script. Start by running the beatsaber game with the mod installed. Then open PowerShell
and run the below command.
Get-NetUDPEndpoint -OwningProcess (get-process 'Beat Saber').id
This will provide the port number that can be added to the Python script on the RaspberryPi.
Connecting the TENS unit to the RaspberryPi
To connect the TENS unit to the RaspberryPi you will need to cut one of the cables from the TENS unit to the pads that connect to your body. Connect the pad side of the cut cable into the Normally Open
terminal of a breakout relay. Connect the TENS unit side of the cut cabel into the Common
terminal of the relay.
You will then need three female
to female
cables to connect the relay to the RaspberryPi. The Power and Ground
pins will connect to power and Ground
on the Pi and the Signal
pin will connect to pin 22
of the Pi. Google search for the pinouts to match the model Pi you are using (You may need to adjust the pin number in the BeatSaber-ShockBot.py script).
RaspberryPi setup
You will need to start a terminal on the Pi, this can be done directly on the machine or by remoteing on via SSH. Start by updating the Pi by running the following:
sudo apt-get update
sudo apt-get upgrade
Next you will need to download the following packages needed for the script to run:
sudo apt-get install python-pip
Lets then create a directory for this project and set-up the script.
sudo mkdir shockbot
cd shockbot
nano BeatSaber-ShockBot.py
Copy the Python script and make sure to update the ipaddress and port number for the device running BeatSaber in the ws
variable.
import websocket
import time
import datetime
import json
import RPi.GPIO as GPIO
from websocket import create_connection
# Setup GPIO as output
PIN = 22
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN, GPIO.OUT)
GPIO.output(PIN, GPIO.LOW)
# Variable
numfail = 0
maxfail = 3
deltafail = 2
start = datetime.datetime.now()
def shock():
print('Shock')
GPIO.output(PIN, GPIO.HIGH)
time.sleep(.5)
GPIO.output(PIN, GPIO.LOW)
self.start = datetime.datetime.now()
class OnMessage():
def __init__(self):
# Variable
self.numfail = 0
self.maxfail = 2
self.deltafail = 2
self.start = datetime.datetime.now()
def __call__(self, ws, message):
evtjson = json.loads(message)
evt = evtjson["event"]
if(evt == "noteMissed" or evt == "bombHit"):
self.end = datetime.datetime.now()
delta = self.end - self.start
if (delta.seconds < self.deltafail and numfail > self.maxfail):
self.start = datetime.datetime.now()
elif (delta.seconds < self.deltafail and numfail < self.maxfail):
self.start = datetime.datetime.now()
shock()
self.numfail = numfail + 1
elif (delta.seconds > deltafail and numfail > maxfail):
self.start = datetime.datetime.now()
shock()
self.numfail = 0
self.numfail = numfail + 1
elif (delta.seconds > self.deltafail and numfail < self.maxfail):
self.start = datetime.datetime.now()
shock()
self.numfail = 0
self.numfail = numfail + 1
else:
print('Error processing Event: ' + evt)
elif(evt == "menu"):
print ("In Menu, Waiting for Song to Start")
on_message = OnMessage()
def on_error(ws, error):
print("Error")
def on_close(ws):
print("### closed ###")
def on_open(ws):
print ("Opened Session")
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://192.168.1.39:6557/socket/", on_message = on_message, on_error = on_error, on_close = on_close)
ws.on_open = on_open
ws.run_forever()
Save and exit. Once all setup you can run this using the following command.
sudo python BeatSaber-ShockBot.py
You can get this script to autostart using PM2. You can use this guide to do it.