powered by TinkerSpace.in
Join free
TinkerPy Tutorial 📘 Part 2 · Coder · Chapter 18 of 24

Bluetooth & IoT

Your phone can be the remote control — from across the room with Bluetooth, or from across the city with IoT over the internet. The board can talk back too: send a temperature, a distance or a “watered!” message straight to your app.

📖Key words

Bluetooth

A short-range radio link between the phone and the board: instant, no internet needed. Only the TinkerPy Mega has Bluetooth.

IoT

Internet of Things: the board joins WiFi and tinkerpy.com passes messages between it and your app, so you can be anywhere. Messages take a few seconds.

TinkerPy App

Where you build a phone app from widgets (Switch, Fan, Drive Pad, Colour Picker…) and connect it by Bluetooth, IoT or the Virtual Board.

Data character

One letter or digit (A–Z, a–z, 0–9) a widget sends, like S for ON and s for OFF.

Device ID and PIN

How the app finds your board over IoT: an ID like TP00000001 plus a PIN. The PIN starts as 1234 — change it so only you can control the board.

🎬See it work

Phone to board

Tap the switch on the phone. Watch the data fly to the board and the code decide what to do.

An IoT message’s journey

Tap each step to follow a tap in the app all the way to the pump — and the reading back.

🧠How it works

How the phone app and board talk

  • In TinkerPy App you place widgets. Input widgets send data: a Switch sends S / s, a Fan F / f, a Drive Pad F, B, L, R, S. Check the letters in Code Mode — they can change.
  • Big widgets send more: an Analog Joystick sends a position (x, y), a Colour Picker sends a colour (r, g, b).
  • Output widgets show what the board sends — a Fire widget burns on A; a Radar Screen (Bluetooth only) draws the angle and distance you send.
  • Capital and small letters are different: 'S' is not 's'.
  • No board yet? Connect the app to the Virtual Board, and test your board code with the Simulator’s Bluetooth / IoT message box.

Bluetooth on the board (TinkerPy Mega)

  • Switch on Bluetooth in Circuit Connections: it writes import bluetooth and bluetooth_begin() in setup.
  • The phone must type the board’s 4-digit password. set_device_password("5678") sets your own (the same password is used for uploads).
  • bluetooth_connected() is 1 while a phone is connected — use it for a safety stop.
  • Read: bluetooth_read_char() a letter · bluetooth_read_string() text · bluetooth_read_number() a number · x, y = bluetooth_read_position() · r, g, b = bluetooth_read_rgb().
  • Each reading lasts one pass of loop, then gives None until something new arrives — so always check if n != None:.

Sending to the app

  • bluetooth_write("fan ready") sends text (up to 32 characters), a letter or a number.
  • bluetooth_write(angle, distance()) sends up to 3 numbers in one message, like “90,35”.
  • The same message twice in a row is sent only once — so writing a reading every loop is fine.
  • bluetooth_read_string() can be compared, shown or sent — but not stored in a variable.

IoT: control and watch from anywhere

  • Tick the IoT library: just import iot — no setup line. The board joins WiFi by itself (wifi("name", "password") adds another network).
  • The functions match Bluetooth with iot_ in front: iot_write(), iot_read_char(), iot_read_string(), iot_read_number(), iot_read_position(), iot_read_rgb().
  • In the app choose IoT (Internet) and type the board’s Device ID and PIN.
  • Near the board? Bluetooth is instant. Far away? IoT works anywhere with internet, a few seconds slower. (Board to board over the internet comes in chapter 7.)

💻Code

# Add the libraries you need
import bluetooth

# Give your pins easy-to-remember names
o1 = set_name("fan")
o2 = set_name("link_led")

# ===== Phone-controlled fan =====

# This code block executes only once
setup:
    fan = set_output(fan)
    link_led = set_output(led)
    bluetooth_begin()  # switches Bluetooth on when the board starts
    set_device_password("5678")  # your own 4-digit board password

# This code block executes forever
loop:
    if bluetooth_connected() == 1:  # when bluetooth_connected() == 1
        link_led = ON  # turn link_led ON
    else:  # otherwise
        link_led = OFF  # turn link_led OFF
    c = bluetooth_read_char()  # a letter from a widget, or None
    if c == 'F':  # when c == 'F'
        fan = ON  # turn fan ON
    elif c == 'f':  # otherwise, when c == 'f'
        fan = OFF  # turn fan OFF
    n = bluetooth_read_number()  # a number from the app, or None
    if n != None:  # when n != None
        fan = pwm(n)  # set fan to n
    if bluetooth_read_string() == "status":  # when bluetooth_read_string() == "status"
        bluetooth_write("fan ready")  # send "fan ready" to the app over bluetooth

A phone-controlled fan (TinkerPy Mega). link_led shows when a phone is connected; the Fan widget’s F / f switches the fan; a number from the app sets its speed with pwm(); and when the app sends the text status the board answers fan ready.

# Add the libraries you need
import bluetooth
import ultrasonic
import motor
import rgb_single

# Give your pins easy-to-remember names
o1 = set_name("headlight")

# ===== Phone joystick robot =====

# This code block executes only once
setup:
    i3 = set_input(ultrasonic_trig)  # cannot be changed
    i4 = set_input(ultrasonic_echo)  # cannot be changed
    o3 = set_output(right_motor_P)
    o4 = set_output(right_motor_N)
    o5 = set_output(left_motor_P)
    o6 = set_output(left_motor_N)
    headlight = set_output(rgb_led, 1)
    bluetooth_begin()  # switches Bluetooth on when the board starts

# This code block executes forever
loop:
    if bluetooth_connected() == 0:  # when bluetooth_connected() == 0
        robot_control("S", 0, "S", 0)  # safety stop: phone gone
    x, y = bluetooth_read_position()  # Analog Joystick: -100 to 100
    if y != None:  # when y != None
        if y > 30:  # when y > 30
            robot_control("F", 70, "F", 70)  # Left 70% Forward, Right 70% Forward : Robot - Moving Forward
        elif y < -30:  # otherwise, when y < -30
            robot_control("B", 70, "B", 70)  # Left 70% Backward, Right 70% Backward : Robot - Moving Backward
        elif x > 30:  # otherwise, when x > 30
            robot_control("F", 70, "B", 70)  # Left 70% Forward, Right 70% Backward : Robot - Turning Right
        elif x < -30:  # otherwise, when x < -30
            robot_control("B", 70, "F", 70)  # Left 70% Backward, Right 70% Forward : Robot - Turning Left
        else:  # otherwise
            robot_control("S", 0, "S", 0)  # drives the robot’s two motors
    r, g, b = bluetooth_read_rgb()  # Colour Picker
    if r != None:  # when r != None
        rgb_hex_color(headlight, 1, r, g, b)  # mix a colour for LED 1 on headlight: red r, green g, blue b
    bluetooth_write(distance())  # send the distance to the app
    wait(50)  # pause here for 50 milliseconds before continuing

A phone joystick robot (TinkerPy Mega). The app’s Analog Joystick sends x and y: up drives forward, down reverses, left and right spin. A Colour Picker sets the colour of the RGB headlight, the board sends the ultrasonic distance back, and the robot stops if the phone disconnects.

# Add the libraries you need
import iot

# Give your pins easy-to-remember names
i1 = set_name("soil")
o1 = set_name("pump")

# ===== IoT plant monitor =====

# This code block executes only once
setup:
    soil = set_input(soil, adc)
    pump = set_output(pump)

# This code block executes forever
loop:
    moisture = analog_read(soil)  # 0 to 100
    iot_write(moisture)  # send the reading over the internet
    c = iot_read_char()  # a letter from the app, or None
    if c == 'P':  # when c == 'P'
        pump = ON  # turn pump ON
    elif c == 'p':  # otherwise, when c == 'p'
        pump = OFF  # turn pump OFF
    if iot_read_string() == "water":  # when iot_read_string() == "water"
        pump = ON  # turn pump ON
        wait(3000)  # pause here for 3000 milliseconds before continuing
        pump = OFF  # turn pump OFF
        iot_write("watered")  # send "watered" to the app over the internet
    wait(1000)  # pause here for 1000 milliseconds before continuing

An IoT plant monitor: every second the soil reading goes to the app over the internet. A Pump widget’s P / p switches the pump, and the text water gives the plant a 3-second drink and replies watered. Check which letters your Pump widget really sends in Code Mode.

Open TinkerPy Code

🎉Fun examples

👑A king’s name

Bluetooth is named after Harald Bluetooth, a Viking king who united parts of Scandinavia — just as Bluetooth unites devices.

🎧Earbuds

Wireless earbuds use Bluetooth too — the same kind of short-range radio that your TinkerPy Mega uses.

🌍Water from anywhere

With IoT you can switch on a pump at home while you are at school — the message travels through the internet in seconds.

🌍Where it’s used

🛠️Project ideas

Phone-controlled fan

In TinkerPy App add a Fan widget and a Serial Monitor. Write the board code so F / f switches a small fan and a number sets its speed, and test it on the Virtual Board before connecting your TinkerPy Mega.

🧰 You need: TinkerPy Mega, small fan motor with driver, phone or computer with TinkerPy App
IoT plant monitor

Read a soil sensor (adc) and send the reading with iot_write every few seconds. In the app, show it and add a Pump widget to water the plant from anywhere. Change the board’s IoT PIN from 1234 first!

🧰 You need: TinkerPy board on WiFi, soil sensor, small 5 V pump with driver, a plant

⭐Points to remember

  1. Bluetooth needs a TinkerPy Mega; it is instant and needs no internet.
  2. IoT works through the internet, from anywhere, a few seconds slower.
  3. import bluetooth + bluetooth_begin(); IoT is just import iot.
  4. Widgets send letters like S / s; capital and small are different.
  5. bluetooth_read_char() gets a letter, _string() text, _number() a number.
  6. x, y = bluetooth_read_position() · r, g, b = bluetooth_read_rgb().
  7. Each reading lasts one pass of loop, then None.
  8. Check if n != None: before using a number.
  9. bluetooth_write() sends text, a letter, a number or up to 3 numbers.
  10. bluetooth_connected() is 1 while a phone is connected.
  11. set_device_password("5678") sets the 4-digit board password.
  12. IoT functions are the same with iot_ in front.
  13. IoT needs the Device ID and PIN — change the PIN from 1234.
  14. Test with the Virtual Board and the Simulator message box first.
🤯
Did you know?

With IoT your board does not need to know where your phone is: both just talk to tinkerpy.com, so a farmer could check a pump from another city.

❓Mini quiz

🔑
Log in to try the mini quiz

5 quick questions to check yourself — free with your TinkerPy account.

Log in