powered by TinkerSpace.in
Join free
TinkerPy Tutorial ๐Ÿ“˜ Part 2 ยท Coder ยท Chapter 19 of 24

Wireless & TV Remote

Press a button on one board and a bell rings on another board across the room โ€” or across the country! Then pick up an old TV remote and drive your robot with it. No phone, no wires: just a few lines of TinkerPy.

๐Ÿ“–Key words

Device ID

Every TinkerPy board has its own name: TP and 8 numbers, like TP00000002. Find it on the board: Menu โ†’ Device info.

Wireless

Board-to-board radio for boards that are near each other. Tick Wireless in Circuit Connections (import wireless) and write the OTHER boardโ€™s ID before a dot: TP00000002.send("RING").

Wireless IoT

The same functions for boards that are far apart: each board is on its own WiFi and tinkerpy.com passes the messages. Add _iot after the ID: TP00000002_iot.send("RING").

Link

The connection between two boards. It opens only when both programs list each other with .connect() in setup:, so a strangerโ€™s board canโ€™t disturb yours.

TV remote receiver

A small 3-leg IR sensor (TSOP1838) on any pin. It reads the invisible infrared flashes of a TV remote. remote_read() tells you which key was pressed.

๐ŸŽฌSee it work

A message hops between boards

Tap each step to follow โ€œRINGโ€ from the button board to the bell board.

Three ways to talk without wires

Switch between them and compare who talks to whom.

๐Ÿง How it works

Wireless: boards that are near

  • Each board gets its own program. Each program names the other board: the button board writes TP00000002.connect(), the bell board writes TP00000001.connect() โ€” both in setup:.
  • Send: TP00000002.send("RING") โ€” text, a number or a join like "T" + temp, up to 30 characters. It gives 1 when the other board got it and 0 when not.
  • Receive: TP00000001.available() is 1 when a message came; TP00000001.read() gives it as text and TP00000001.read_number() as a number (None when nothing came). A message is there for one loop.
  • Check the link: .connected() is 1 while the link is up, .signal() gives 0โ€“100 (how close your friend is) and .disconnect() closes the link.
  • Many boards (up to 6): wireless_send_all("STOP") sends to all of them; id, data = wireless_read_any() tells you who sent what.

Wireless IoT: boards far apart

  • Same functions, with _iot after the ID: TP00000002_iot.connect(), .send(), .read(), .read_number(), .available(), .connected(), .disconnect().
  • For many boards: wireless_iot_send_all() and id, data = wireless_iot_read_any(). There is no signal() โ€” use connected() to see if the other board is online.
  • Both boards need WiFi. tinkerpy.com passes each message, so it arrives after a few seconds โ€” fine for a plant or a doorbell, too slow to steer a racing robot.
  • When you press Compile, TinkerPy Code looks each Device ID up on tinkerpy.com, so you must be online. A wrong ID gives an error.

TV remote

  • Tick TV Remote in Circuit Connections: it adds import tv_remote and i1 = set_function(tv_remote). Then teach the board your remote: Menu โ†’ TV Remote โ†’ Learn Remote.
  • k = remote_read() gives the key in small letters: "1"โ€ฆ"9", "0", "a" "b" "c" "d" (red, green, yellow, blue), "up" "down" "left" "right" "ok", "v+" "v-" "ch+" "ch-", "play" "pause" "ff" "fb", "menu", "exit". None when no key.
  • Short form: if remote_read() == "ch+": runs the lines below when that key is pressed.
  • Secret codes: code = remote_read_string(3, "menu", "oled", "visible", 10) waits for menu, then 3 keys, shows them on the OLED and gives up after 10 seconds. Compare in quotes: if code == "482":. No code โ†’ 0.

Firmware and boards

  • Wireless needs board firmware 2.24, Wireless IoT 2.25, the TV remote keys 2.23 or newer. Upload tells you when the board must be updated.
  • Wireless and Wireless IoT work on TinkerPy Mega. The TV remote works on Mega and Mini.
  • Tip: give every groupโ€™s boards a sticker with their Device ID โ€” it saves a lot of โ€œwhich board is mine?โ€ time.

๐Ÿ’ปCode

# Add the libraries you need
import wireless

# Give your pins easy-to-remember names
i1 = set_name("bell_button")
o1 = set_name("warn")
o2 = set_name("link")

# ===== Wireless doorbell: button board (TP00000001) =====

# This code block executes only once
setup:
    bell_button = set_input(push_button, active_low)
    warn = set_output(led)
    link = set_output(led)
    TP00000002.connect()  # link to the bell board (its Device ID)

# This code block executes forever
loop:
    if TP00000002.connected() == 1:  # when TP00000002.connected() == 1
        link = ON  # turn link ON
    else:  # otherwise
        link = OFF  # turn link OFF
    if bell_button == ON:  # when bell_button is ON
        ok = TP00000002.send("RING")  # 1 = the bell board got it, 0 = it did not
        if ok == 0:  # when ok == 0
            warn = ON  # turn warn ON
        else:  # otherwise
            warn = OFF  # turn warn OFF
        wait(300)  # pause here for 300 milliseconds before continuing

Upload this to the button board TP00000001 (TinkerPy Mega). The link LED shows when the bell board is linked; the warn LED lights when a RING did not arrive. In TinkerPy Code, put your own boardsโ€™ Device IDs โ€” Compile checks them online.

# Add the libraries you need
import oled_display
import buzzer
import wireless

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

# ===== Wireless doorbell: bell board (TP00000002) =====

# This code block executes only once
setup:
    oled_begin()  # starts the OLED
    o7 = set_function(SDA)  # cannot be changed
    o8 = set_function(SCL)  # cannot be changed
    bell = set_output(buzzer, passive)
    TP00000001.connect()  # link to the button board
    oled_write(1, center, medium, bold, "Doorbell")  # write "Doorbell" on OLED line 1 (medium bold, centred)

# This code block executes forever
loop:
    msg = TP00000001.read()  # the message from the button board as text (None if nothing came)
    if msg == "RING":  # when msg == "RING"
        oled_write(1, center, medium, bold, "Visitor!")  # write "Visitor!" on OLED line 1 (medium bold, centred)
        oled_write(4, left, small, regular, "Signal " + TP00000001.signal())  # write "Signal " + TP00000001.signal() on OLED line 4 (small, from the left)
        buzzer_play(bell, "door_bell")  # play the "door_bell" tone on the buzzer (bell) โ€” the next line waits until it ends
        oled_write(1, center, medium, bold, "Doorbell")  # write "Doorbell" on OLED line 1 (medium bold, centred)

Upload this to the bell board TP00000002. It lists the button board TP00000001, waits for RING, shows Visitor! and the signal strength, and plays the door_bell tune on a passive buzzer.

# Add the libraries you need
import oled_display
import wireless_iot

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

# ===== Plant watcher at Grandmaโ€™s (TP00000002) =====

# This code block executes only once
setup:
    oled_begin()  # starts the OLED
    o7 = set_function(SDA)  # cannot be changed
    o8 = set_function(SCL)  # cannot be changed
    dry_led = set_output(led)
    TP00000001_iot.connect()  # the plant board at home, anywhere on the internet

# This code block executes forever
loop:
    if TP00000001_iot.connected() == 0:  # when TP00000001_iot.connected() == 0
        oled_write(4, left, small, regular, "Plant board offline")  # write "Plant board offline" on OLED line 4 (small, from the left)
    if TP00000001_iot.available() == 1:  # when TP00000001_iot.available() == 1
        soil = TP00000001_iot.read_number()  # the soil reading the plant board sent
        oled_clear_line(4)  # clear line 4 of the OLED
        oled_write(1, left, small, regular, "Soil dryness")  # write "Soil dryness" on OLED line 1 (small, from the left)
        oled_write(2, center, large, bold, soil)  # write the value of soil on OLED line 2 (large bold, centred)
        if soil > 60:  # when soil > 60
            dry_led = ON  # turn dry_led ON
            TP00000001_iot.send("WATER")  # ask the plant board to switch its pump on
        else:  # otherwise
            dry_led = OFF  # turn dry_led OFF

Wireless IoT: this board can be in another city. The plant board at home (TP00000001) runs TP00000002_iot.send(analog_read(soil)) every 10 seconds. Here the reading shows on the OLED; above 60 the LED lights and WATER is sent back so the plant board can switch its pump on.

# Add the libraries you need
import motor
import tv_remote

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

# ===== TV remote robot =====

# This code block executes only once
setup:
    o3 = set_output(right_motor_P)
    o4 = set_output(right_motor_N)
    o5 = set_output(left_motor_P)
    o6 = set_output(left_motor_N)
    i1 = set_function(tv_remote)  # TV remote receiver (TSOP1838) โ€” learn the keys on the board: Menu > TV Remote
    headlight = set_output(led)

# This code block executes forever
loop:
    k = remote_read()  # the TV remote key pressed (1-9, 0, a-d, up, ok, ch+ โ€ฆ) into k โ€” None if no key
    if k == "up":  # when k == "up"
        robot_control("F", 80, "F", 80)  # Left 80% Forward, Right 80% Forward : Robot - Moving Forward
    elif k == "down":  # otherwise, when k == "down"
        robot_control("B", 80, "B", 80)  # Left 80% Backward, Right 80% Backward : Robot - Moving Backward
    elif k == "left":  # otherwise, when k == "left"
        robot_control("B", 60, "F", 60)  # spin left: left wheel back, right wheel forward
    elif k == "right":  # otherwise, when k == "right"
        robot_control("F", 60, "B", 60)  # spin right
    elif k == "ok":  # otherwise, when k == "ok"
        robot_control("S", 0, "S", 0)  # stop
    elif k == "1":  # otherwise, when k == "1"
        headlight = ON  # turn headlight ON
    elif k == "0":  # otherwise, when k == "0"
        headlight = OFF  # turn headlight OFF

A robot car driven by any TV remote: arrows drive and spin, ok stops, 1 and 0 switch the headlight. Learn the remote on the board first (Menu, TV Remote, Learn Remote). Works on TinkerPy Mega and Mini.

Open TinkerPy Code

๐ŸŽ‰Fun examples

๐Ÿ””Classroom doorbell

Put the button board at the lab door and the bell board on the teacherโ€™s desk. Nobody has to shout โ€œMay I come in?โ€ again!

๐ŸƒMessage relay race

Six boards can link to each other. Pass a message from board to board around the room and time how fast it gets back.

๐Ÿ”Remote secret code

With remote_read_string() an old TV remote becomes a keypad: type 4 8 2 and a servo opens your treasure box.

๐ŸŒWhere itโ€™s used

๐Ÿ› ๏ธProject ideas

Two-board doorbell

Upload the button program to one TinkerPy Mega and the bell program to another, each with the otherโ€™s Device ID. Add a second message: a long press sends โ€œURGENTโ€ and the bell board plays a different tone.

๐Ÿงฐ You need: 2 TinkerPy Mega boards, push button, 2 LEDs, passive buzzer, OLED
TV-remote robot

Learn your remote on the board, then upload the TV remote robot. Add speed keys: v+ makes the robot faster and v- slower, using a speed variable inside robot_control().

๐Ÿงฐ You need: robot car with TinkerPy board, TV remote receiver (TSOP1838), any TV remote

โญPoints to remember

  1. A Device ID is TP and 8 numbers: Menu โ†’ Device info.
  2. import wireless = boards near each other; import wireless_iot = boards anywhere with WiFi.
  3. Both programs must list each other with .connect() in setup:.
  4. TP00000002.send(x) sends up to 30 characters and gives 1 (arrived) or 0 (not).
  5. read() gives text, read_number() gives a number, available() is 1 when a message came.
  6. A message is there for one loop only โ€” read it into a variable.
  7. connected() is 1 while linked; signal() 0โ€“100 (Wireless only); disconnect() closes the link.
  8. wireless_send_all() and id, data = wireless_read_any() work with up to 6 boards.
  9. Wireless IoT adds _iot after the ID and takes a few seconds per message.
  10. Wireless and Wireless IoT need a TinkerPy Mega (firmware 2.24 / 2.25).
  11. Learn the TV remote on the board: Menu โ†’ TV Remote โ†’ Learn Remote.
  12. remote_read() gives keys in small letters, like "up", "ok", "ch+" โ€” None when no key.
  13. remote_read_string() waits for a typed code and gives it in quotes, or 0.
๐Ÿคฏ
Did you know?

A TV remote talks with invisible infrared light. Point a phone camera at the front of a remote and press a key: many phone cameras show the light flashing that your eyes cannot see.

โ“Mini quiz

๐Ÿ”‘
Log in to try the mini quiz

5 quick questions to check yourself โ€” free with your TinkerPy account.

Log in