Powered by TinkerSpace
Join free
TinkerPy Projects lessons
๐Ÿ“ฒ
Lesson 10 of 10 ยท Maker ยท 12 min

Talk to your App

Receive data from your phone app over Bluetooth, and send some back.

Your TinkerPy App sends data (one character) when you tap a widget โ€” for example a Switch sends S for ON and s for OFF. The board reads it with bluetooth_read_char().

TinkerPy
from tinkerpy_mega import *

from bluetooth import *

def setup():
    bluetooth_begin()  # switches Bluetooth on when the board starts

def loop():
    bt = bluetooth_read_char()
    if bt == 'S':
        write_output(o1, pwm(100))
    elif bt == 's':
        write_output(o1, 0)
Tap the Switch in your app โ†’ the LED turns on and off.

Sending to the app

bluetooth_write("A") sends data to the app. An output widget โ€” like Fire โ€” changes when its data arrives.

TinkerPy
from tinkerpy_mega import *

from bluetooth import *

def setup():
    bluetooth_begin()  # switches Bluetooth on when the board starts

def loop():
    if read_digital(i2) == ON:
        bluetooth_write("A")
        write_output(o1, pwm(100))
    else:
        bluetooth_write("a")
        write_output(o1, 0)
    wait(500)
A flame sensor on i2 makes the Fire widget burn in the app.
๐Ÿ’ก Tip

Open the App lessons to build the app for this code!

Chat with TinkerPy ๐Ÿ‘‹