← Back to ratslair.com
aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: 7362778e1cd7027db3508b8a889dc6336e5755af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import RPi.GPIO as GPIO
from time import sleep

import os
import sys
import glob
import bluetooth
import re

# 3 Pins to communicate with Arduino
P1 = 17
P2 = 27
P3 = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(P1, GPIO.OUT)
GPIO.setup(P2, GPIO.OUT)
GPIO.setup(P3, GPIO.OUT)
GPIO.output(P1, False)
GPIO.output(P2, False)
GPIO.output(P3, False)

server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

print("Waiting for connection on RFCOMM channel %d..." % port)

client_sock, client_info = server_sock.accept()

print("Connected to %s." % client_info[0])

while client_sock:
    read = client_sock.recv(1024)
    read = read.decode("utf-8").strip('\n')
    print(read)

    if read == "0":      # Do nothing
        GPIO.output(P1, False)
        GPIO.output(P2, False)
        GPIO.output(P3, False)
    elif read == "11":   # Drive forward
        GPIO.output(P1, True)
        GPIO.output(P2, False)
        GPIO.output(P3, False)
    elif read == "22": # Drive backward
        GPIO.output(P1, False)
        GPIO.output(P2, True)
        GPIO.output(P3, False)
    elif read == "33": # Rotate right, 90 degrees
        GPIO.output(P1, True)
        GPIO.output(P2, True)
        GPIO.output(P3, False)
    elif read == "44": # Rotate left, 90 degrees
        GPIO.output(P1, False)
        GPIO.output(P2, False)
        GPIO.output(P3, True)
    elif read == "55": # Rotate right, 45 degrees
        GPIO.output(P1, True)
        GPIO.output(P2, False)
        GPIO.output(P3, True)
    elif read == "66": # Rotate left, 45 degrees
        GPIO.output(P1, False)
        GPIO.output(P2, True)
        GPIO.output(P3, True)
    elif read == "98": # Turn on light at landmark
        GPIO.output(P1, True)
        GPIO.output(P2, True)
        GPIO.output(P3, True)
    else:              # Do nothing if command is not recognized
        GPIO.output(P1, False)
        GPIO.output(P2, False)
        GPIO.output(P3, False)

GPIO.output(P1, False)
GPIO.output(P2, False)
GPIO.output(P3, False)

print("Shutting down...")
server_sock.close()