Created
June 28, 2018 10:40
-
-
Save thomasvt1/b855d70f608bc84f79f5ff8359de5614 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 bluetooth | |
import threading | |
import requests | |
from time import sleep | |
import RPi.GPIO as GPIO | |
#Set up GPIO | |
GPIO.setmode(GPIO.BCM) | |
REDPIN = 17 | |
LEDPIN = 27 | |
PIEZOP = 22 | |
GPIO.setup(LEDPIN, GPIO.OUT) | |
GPIO.setup(REDPIN, GPIO.OUT) | |
GPIO.setup(PIEZOP, GPIO.OUT) | |
GPIO.output(REDPIN, GPIO.LOW) | |
GPIO.output(LEDPIN, GPIO.LOW) | |
GPIO.output(PIEZOP, GPIO.LOW) | |
name = "SampleServer" | |
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" | |
url = "https://thomasvanvugt.com/api.php?d1=var1&d2=var2" | |
server_sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) | |
server_sock.bind(("", bluetooth.PORT_ANY)) | |
server_sock.listen(1) | |
port = server_sock.getsockname()[1] | |
bluetooth.advertise_service( server_sock, name, uuid ) | |
dicerolls = ['0', '0'] | |
print ("Waiting for connection on RFCOMM channel %d" % port) | |
class echoThread(threading.Thread): | |
def __init__ (self,sock,client_info, dicerolls): | |
threading.Thread.__init__(self) | |
self.sock = sock | |
self.client_info = client_info | |
self.dicerolls = dicerolls | |
def beep(self, times): | |
for i in range(times): | |
GPIO.output(PIEZOP, GPIO.HIGH) | |
sleep(0.3) | |
GPIO.output(PIEZOP, GPIO.LOW) | |
sleep(0.3) | |
def run(self): | |
try: | |
while True: | |
data = self.sock.recv(1024) | |
if len(data) == 0: break | |
msg = data.decode('ascii') | |
player = msg[0] | |
roll = msg[2] | |
if (player is '1' and self.dicerolls[0] is '0'): | |
self.dicerolls[0] = roll | |
elif (player is '2' and self.dicerolls[1] is '0'): | |
self.dicerolls[1] = roll | |
print(player, roll) | |
game = True | |
for r in self.dicerolls: | |
if r is '0': | |
game = False | |
print(game) | |
if not game: | |
print(self.dicerolls) | |
if game: | |
requrl = url.replace("var1", self.dicerolls[0]).replace("var2", self.dicerolls[1]) | |
print(">------RESULT-------<") | |
print(["D1", "D2"]) | |
print(self.dicerolls) | |
self.dicerolls[0] = '0' | |
self.dicerolls[1] = '0' | |
response = requests.get(requrl) | |
if "1" in response.text: | |
GPIO.output(LEDPIN, GPIO.LOW) | |
GPIO.output(REDPIN, GPIO.HIGH) | |
self.beep(1) | |
elif "2"in response.text: | |
GPIO.output(LEDPIN, GPIO.HIGH) | |
GPIO.output(REDPIN, GPIO.LOW) | |
self.beep(2) | |
else: | |
GPIO.output(LEDPIN, GPIO.LOW) | |
GPIO.output(REDPIN, GPIO.LOW) | |
self.beep(4) | |
print(response.text) | |
print("\n\n\n\n\n\n\n\n") | |
except IOError: | |
pass | |
self.sock.close() | |
print (self.client_info, ": disconnected") | |
while True: | |
client_sock, client_info = server_sock.accept() | |
print (client_info, ": connection accepted") | |
echo = echoThread(client_sock, client_info, dicerolls) | |
echo.setDaemon(True) | |
echo.start() | |
server_sock.close() | |
print ("all done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment