Skip to content

Search the documentation

Kit
Firmware

Hello, house — Smart Home (Python)

This example is written twice, once for each firmware. Pick the tab that matches the firmware of your Smart Home: the tab you pick is remembered on the other examples.

Firmware 3.0 renamed the Python names a program writes, and kept none of the old ones: a Smart Home on 2.x does not know the 3.0 names, and a Smart Home on 3.0 does not know the 2.x ones. The name map lists every change. If you open the example from inside MoBot, it picks the version for the firmware of your Smart Home. If you download a file from this page, use the one in the tab for your firmware.

A guided tour of everything on the board you can see or hear. The screen draws a picture and pulls three faces, the colour light walks through the rainbow and fades, the buzzer plays three different ways, the fan spins up, and the servo opens and closes the door.

Run this first on a new Smart Home board. If every part of this works, the board is healthy and every other example is worth trying.

This is the version for a house on firmware 3.0, where the house is called home. For a house that still runs firmware 2.x, MoBot opens the 2.x version of this example instead.

Project for firmware 3.0: 01-hello-house.firmware-3.mobot. Open it in MoBot.

  • A MoBot Smart Home board (ESP32-S3), powered.
  • Its OLED screen, RGB light, buzzer, fan and servo fitted.
  • A USB cable and the serial monitor.

It takes about a minute.

part what
1 Hello! / I am your house on the screen, then a drawing: a frame, a horizon line, a filled sun and a triangle roof
1 then three big faces — happy, sad, love — and the MoBot logo
2 the light goes red, green, blue, yellow, white — check the first one really is red! — then fades up and down in pink
3 a single A note, then four rising notes, then the victory tune while the light flashes red in time, then a short tune written inside the program
4 the fan at 30 %, 60 %, 100 %, with the screen and monitor agreeing on the number
5 the servo goes to the middle, fully closed, fully open, then glides smoothly back to the middle and sweeps twice
end the colour light blinks green three times and the happy face comes back

The colour light is written red first, each from 0 to 255:

home.rgb_led.set_color(255, 0, 0) # red, green, blue

That is exactly what the board does: each lamp pin is named by the colour it really drives. If the light comes on green here, this board’s lamp is wired differently from the ones MoBot ships — tell whoever looks after the kits.

  • draw_triangle() — there are blocks for lines, rectangles and circles, but not triangles. The roof in the drawing is Python only.
  • show_emoji() and show_logo() — the three built-in faces (happy, sad, love) and the MoBot logo can only be drawn from Python. happy is the face the board wears when it switches on.
  • servo.move_to(), sweep(), center(), move_to_min(), move_to_max() — the block can only jump to a fixed angle. move_to glides, and you can hear the difference.

This is a reading copy of the program in the project: editing it changes nothing, so open the project and edit it there.

main.py
# main.py — Hello, house (Python) (iot)
#
# A reading copy of one file inside 01-hello-house.firmware-3.mobot.
# Editing this copy changes nothing: open the project and edit it there.
# This is the entry point — the board runs it as main.py.
# Written for firmware 3.0; the 2.x version is 01-hello-house.main.py.
"""Hello, house - MoBot Smart Home.
A tour of everything on the board that you can see or hear: the little screen,
the colour light, the buzzer, the fan and the servo.
Some of this has blocks. Quite a lot of it does not:
display.show_emoji() the face the board wears when it wakes up
display.draw_triangle() there are blocks for lines, boxes and circles only
buzzer.play_notes() ...well, there IS a block, but only for the tunes
in its list. Here you can write your own.
servo.sweep() and move_to(), center(), move_to_min(), move_to_max()
fan.speed asking the fan what it is doing
And one thing that is easy to get wrong: the colour light wants RED first.
"""
import time
from sounds import SEQUENCES
display = home.display
lamp = home.rgb_led
# ------------------------------------------------------------------ 1 -------
# The screen. 128 dots across, 64 down, and the letters are 8 dots wide -
# so a line of text can be 16 letters long and there is room for 8 lines.
print("Part 1: the screen.")
display.clear()
display.show_text("Hello!", 0, 0)
display.show_text("I am your house", 0, 16)
time.sleep(2)
display.clear()
display.draw_rectangle(4, 4, 120, 56) # a frame round the edge
display.draw_line(4, 32, 124, 32) # a horizon
display.draw_circle(32, 18, 10, filled=True) # a sun
display.draw_triangle(70, 30, 100, 30, 85, 10) # a roof - no block for this
time.sleep(3)
# The three built-in faces, big. show_emoji() clears the screen first.
for face in ("happy", "sad", "love"):
display.show_emoji(face, scale=3)
time.sleep(1.2)
display.show_logo() # the MoBot logo
time.sleep(2)
# ------------------------------------------------------------------ 2 -------
# The colour light. RED, GREEN, BLUE - each from 0 to 255.
print("Part 2: the colour light. Watch carefully - the first one is RED.")
display.clear()
for name, r, g, b in (
("red", 255, 0, 0),
("green", 0, 255, 0),
("blue", 0, 0, 255),
("yellow", 255, 255, 0),
("white", 255, 255, 255),
):
display.show_text(name, 0, 24)
lamp.set_color(r, g, b) # set_color(RED, GREEN, BLUE) - in that order
print(" ", name)
time.sleep(0.8)
lamp.set_color(0, 0, 0)
# A slow fade, which needs a hundred little steps - not something to do with blocks.
print(" fading up and down")
for level in range(0, 256, 5):
lamp.set_color(level, 0, level // 2)
time.sleep(0.02)
for level in range(255, -1, -5):
lamp.set_color(level, 0, level // 2)
time.sleep(0.02)
# ------------------------------------------------------------------ 3 -------
# The buzzer. Three ways to make a noise.
print("Part 3: sounds.")
display.clear()
display.show_text("Listen...", 0, 24)
# One tone: a frequency in hertz, and how long for, in milliseconds.
home.buzzer.play_tone(440, 300) # 440 Hz is the note A
time.sleep(0.2)
# A note by name. C3 up to B6, and # means sharp.
for note in ("C4", "E4", "G4", "C5"):
home.buzzer.play_note(note, 200)
time.sleep(0.3)
# A whole ready-made tune. This one does NOT wait - it runs on a timer while
# your program carries on, which is why the light can change during it.
print(" playing 'victory' while the light flashes")
home.buzzer.play_notes(SEQUENCES["victory"])
while home.buzzer.is_playing():
lamp.set_color(255, 0, 0)
time.sleep(0.1)
lamp.set_color(0, 0, 0)
time.sleep(0.1)
# Your own tune: a list of (note, milliseconds).
print(" and one written here")
home.buzzer.play_notes([("G4", 150), ("G4", 150), ("A4", 300),
("G4", 300), ("C5", 300), ("B4", 600)])
time.sleep(2.5)
home.buzzer.stop()
# ------------------------------------------------------------------ 4 -------
# The fan and the servo.
print("Part 4: the fan.")
display.clear()
for speed in (30, 60, 100):
display.show_text("Fan " + str(speed) + "%", 0, 24)
home.fan.set_speed(speed)
print(" set", speed, "- fan says it is at", home.fan.speed)
time.sleep(1.5)
home.fan.off()
print("Part 5: the servo - the door.")
display.clear()
display.show_text("Door", 0, 24)
home.servo.center() # 90 degrees
time.sleep(0.6)
home.servo.move_to_min() # 0 degrees - closed
time.sleep(0.6)
home.servo.move_to_max() # 180 degrees - wide open
time.sleep(0.6)
home.servo.move_to(90, step=2, delay_ms=25) # glide back, slowly
print(" the servo is now at", home.servo.angle, "degrees")
home.servo.sweep(60, 120, step=5, delay_ms=20, cycles=2)
home.servo.center()
# Last of all, the colour light blinks green three times: the tour is over.
for _ in range(3):
lamp.set_color(0, 255, 0)
time.sleep(0.2)
lamp.set_color(0, 0, 0)
time.sleep(0.2)
home.display.show_emoji("happy")
print("Tour finished.")
  • Draw your own house on the screen: rectangles for walls and windows, a triangle for the roof.
  • Write your own tune in the play_notes([...]) at the end of part 3. Each step is ("note", milliseconds); notes run C3 to B6.
  • Make the fan speed follow the knob — see 06-doorbell for how to read it.