Card door — Smart Home
A little door that only opens for your card. Tap a card on the RFID reader and the board prints its number. Tap the right card and the servo swings open, plays a note, waits three seconds and closes again.
This example is in two parts, and the first part is meant to “fail”. You have to find out your card’s number before you can teach the door to recognise it. That is the lesson.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Home.
The program
Section titled “The program”Read the program as text
- MoBot Start
- Print to serial
"Show a card to the reader" - Set Servo Angle 0
- repeat forever
- set card to Read RFID Card
- Print to serial (create text with
"Card: "card) - if (card =
"PUT-YOUR-CARD-NUMBER-HERE")- Print to serial
"Welcome!" - Play Note C5 Duration: 300
- Set Servo Angle 90
- wait 3 seconds
- Set Servo Angle 0
- Print to serial
- wait 1 seconds
What you need
Section titled “What you need”- A Smart Home board with the RFID reader and the servo motor.
- One or two RFID cards or key fobs (the white cards that come with the kit).
- A USB cable and the serial monitor.
- Something light taped to the servo arm makes the “door” much more fun — a strip of card, a paper flag.
Part 1 — find your card’s number
Section titled “Part 1 — find your card’s number”-
Open 06-card-door.mobot in MoBot and press Send to Device.
-
Open the serial monitor. You will see
[MOBOT] Card: Noneonce a second.Nonemeans “no card” — that is the reader working correctly with nothing in front of it. -
Hold a card flat on the reader. One of the lines changes:
[MOBOT] Card: None[MOBOT] Card: 3906f6945d <- your card![MOBOT] Card: None -
Write that number down. Every card has its own, and yours will not be
3906f6945d.
You may have to hold the card still for a second or two: the board only looks once a second, so a quick tap can slip between two looks.
Part 2 — teach the door your card
Section titled “Part 2 — teach the door your card”- In MoBot, find the block that says
PUT-YOUR-CARD-NUMBER-HERE. - Type your card’s number into it instead, exactly as it was printed — all lower case, no spaces.
- Press Send to Device again.
- Tap your card.
What you should see and hear
Section titled “What you should see and hear”[MOBOT] Welcome!in the serial monitor,- one clear note from the buzzer,
- the servo arm swings from 0° to 90°, holds for three seconds, and swings back.
Tap a different card and the number is printed but the door stays shut. That is the test that matters: a door that opens for every card is not a lock.
Why the program remembers the card in a box
Section titled “Why the program remembers the card in a box”Look at the first block inside the loop: set card to Read RFID Card.
Asking the reader is not free — each ask is a real radio conversation, and two
asks in a row can give two different answers (a card may be gone by the second
one). So the program asks once, keeps the answer in a box called card, and
then uses that same box twice: once to print it, once to compare it. The door
can never print one number and unlock on another.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
iot_rfidread |
Asks the reader. Gives the card’s number as text, or None. |
variables_set / variables_get |
The box called card. |
logic_compare |
“Is this card the right card?” |
iot_servo |
Turns the servo to an angle from 0 to 180. The angle is a dial on the block. |
iot_playnote |
Plays a named musical note. Duration in milliseconds — 300 is a third of a second. |
iot_print, text_join, wait |
Commentary and timing. |
The Python code
Section titled “The Python code”The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.
# Card door — iot (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 06-card-door.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.# The firmware 2.x version is 06-card-door.main.py.
from machine import Pinimport time
print("[MOBOT]", 'Show a card to the reader')home.servo.set_angle(0)while True: card = home.rfid.read_card() print("[MOBOT]", ('Card: ' + str(card))) if card == 'PUT-YOUR-CARD-NUMBER-HERE': print("[MOBOT]", 'Welcome!') home.buzzer.play_note('C5', 300) home.servo.set_angle(90) time.sleep(3) home.servo.set_angle(0) time.sleep(1)# Card door — iot## This is what MoBot puts on the board as main.py when you open# 06-card-door.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport time
print("[MOBOT]", 'Show a card to the reader')mobotIOT.servo.set_angle(0)while True: card = mobotIOT.rfid.read() print("[MOBOT]", ('Card: ' + str(card))) if card == 'PUT-YOUR-CARD-NUMBER-HERE': print("[MOBOT]", 'Welcome!') mobotIOT.buzzer.play_note('C5', 300) mobotIOT.servo.set_angle(90) time.sleep(3) mobotIOT.servo.set_angle(0) time.sleep(1)Try changing it
Section titled “Try changing it”- Add a second if for a second card, with a different note, so you can hear who came in.
- Change the two servo angles from 0/90 to 0/180 and watch a much bigger swing.
- Play
successfrom Play sequence instead of a single note.