from machine import Pin
import bluetooth
from BLE import BLEUART

#Set Color Pins
redPin = Pin(19, Pin.OUT) #Red Color
greenPin = Pin(5, Pin.OUT) #Green Color
bluePin = Pin(17, Pin.OUT) #Blue Color

#Init Bluetooth
name = "ESP32" #Bluetooth Name
ble = bluetooth.BLE() #Bluetooth Instance
uart = BLEUART(ble, name) #Bluetooth Object

#Bluetooth RX Event
def on_rx():
    rx_buffer = uart.read().decode().strip()
    
    """
    .read() = reads the uart serial port
    .decode() = decodes the sent string (needed for serial bluetooth communication)
    .strip() = removes spaces at the beginning and end of the string
    """
    uart.write('ESP 32 says: ' + str(rx_buffer) + '\n') #Writes the given message
    
    if rx_buffer == "R":
        redPin.on()
        greenPin.off()
        bluePin.off()
    elif rx_buffer == "G":
        redPin.off()
        greenPin.on()
        bluePin.off()
    elif rx_buffer == "B":
        redPin.off()
        greenPin.off()
        bluePin.on()

#Registers Bluetooth Event
uart.irq(handler=on_rx) 
$abcdeabcde151015202530fghijfghij
$abcdeabcde151015202530fghijfghij