Skip to content

Instantly share code, notes, and snippets.

@thomasvt1
Created June 28, 2018 10:41
Show Gist options
  • Save thomasvt1/449d0786c72373d9f5533fef06084f7b to your computer and use it in GitHub Desktop.
Save thomasvt1/449d0786c72373d9f5533fef06084f7b to your computer and use it in GitHub Desktop.
from bluetooth import *
import sys
import RPi.GPIO as GPIO
import time
import random
#If using python 2
if sys.version < '3':
input = raw_input
#Set address
addr = sys.argv[1]
print("Searching for server on %s" % addr)
#Search for server
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
service_matches = find_service( uuid = uuid, address = addr )
#If no server found
if len(service_matches) == 0:
print("No server found!")
sys.exit(0)
#Set up first match
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("Connecting to %s on %s using port %s" % (name, host, port))
#Create client socket
socket = BluetoothSocket(RFCOMM)
socket.connect((host, port))
print("\nConnected! Ready to play!")
print("Press the button to roll a dice!")
print("Score higher than your opponent to win!\n")
#Set up GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Continually run
while True:
if(GPIO.input(15) == GPIO.HIGH):
print("ROLL!")
dice_roll_result = random.randint(1, 6)
print("You rolled: %s\n" % dice_roll_result)
data = "1:" + str(dice_roll_result)
socket.send(data.encode('ascii'))
time.sleep(2)
socket.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment