Room thermometer — Smart Home
The board reads the DHT11 sensor and writes the temperature and the humidity of the room into the serial monitor, over and over.
This is the example that proves the DHT11 works. There is nothing to look at on the board itself — the whole test is the two lines that keep appearing.
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
"Thermometer starting" - repeat forever
- Print to serial (create text with
"Temperature (C): "Read DHT11 Temperature) - wait 2 seconds
- Print to serial (create text with
"Humidity (%): "Read DHT11 Humidity) - wait 2 seconds
- Print to serial (create text with
What you need
Section titled “What you need”- A Smart Home board with the DHT11 temperature-and-humidity sensor.
- A USB cable, and the serial monitor open in MoBot.
How to run it
Section titled “How to run it”- Open 02-thermometer.mobot in MoBot.
- Plug the board in, pick its port, press Send to Device.
- Open the serial monitor.
What you should see
Section titled “What you should see”[MOBOT] Thermometer starting[MOBOT] Temperature (C): 24[MOBOT] Humidity (%): 41[MOBOT] Temperature (C): 24[MOBOT] Humidity (%): 41...A new pair every four seconds.
The real test: breathe gently on the sensor for a few seconds. The humidity number should climb — often past 70 — and then fall back on its own over the next minute. The temperature may rise a degree. A sensor that answers but never changes is not really working.
Sensible readings indoors: temperature 15–30, humidity 20–70. A DHT11 only reports whole numbers, so a room that “feels the same” really will print the same number for minutes at a time.
Why the two-second waits
Section titled “Why the two-second waits”A DHT11 is slow. It needs about a second between measurements, and asking it twice in a row can make it complain or hand back the last answer again. So the program asks for the temperature, waits, asks for the humidity, waits.
If you shorten the waits to 0.1 the sensor starts failing — the program stops
with an OSError in the serial monitor. That is worth doing once, on
purpose, so a child sees what an impatient program looks like.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
iot_dht |
Reads the DHT11. The dropdown picks Temperature or Humidity. |
text_join |
Glues the words and the number into one line. |
iot_print |
Sends that line to the serial monitor. |
wait |
Pauses, in seconds. |
repeat_forever |
Keeps it going. |
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.
# Room thermometer — iot (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 02-thermometer.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 02-thermometer.main.py.
from machine import Pinimport time
print("[MOBOT]", 'Thermometer starting')while True: print("[MOBOT]", ('Temperature (C): ' + str(home.temperature_sensor.temperature()))) time.sleep(2) print("[MOBOT]", ('Humidity (%): ' + str(home.temperature_sensor.humidity()))) time.sleep(2)# Room thermometer — iot## This is what MoBot puts on the board as main.py when you open# 02-thermometer.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]", 'Thermometer starting')while True: print("[MOBOT]", ('Temperature (C): ' + str(mobotIOT.dht11.read(0)))) time.sleep(2) print("[MOBOT]", ('Humidity (%): ' + str(mobotIOT.dht11.read(1)))) time.sleep(2)Try changing it
Section titled “Try changing it”- Take the board somewhere cold and watch the number fall.
- Change the words to
Il fait (C):— the block will print whatever you type.