Skip to content

Search the documentation

Kit
Firmware

Web dashboard — Smart Home

The board makes its own Wi-Fi network, called MoBotHouse. Join it with a phone or a laptop, open a web address, and the page shows what the house feels right now: the temperature, the humidity, the gas, the light, the knob and the motion sensor. A second address holds a switch that turns the fan on and off.

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 blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
Read the program as text
  • MoBot Start
  • Print to serial "Starting the web page"
  • Start Web Server on its own WiFi network, SSID: "MoBotHouse" Password (8+ characters): "mobot1234"
  • set fan_switch to (Register Toggle at Endpoint (start with /): "/fan")
  • Clear OLED Screen
  • Display Text "Join MoBotHouse" at X: 0 Y: 0 .
  • Display Text "192.168.4.1" at X: 0 Y: 16 .
  • Display Text "pw mobot1234" at X: 0 Y: 32 .
  • Print to serial "Join the Wi-Fi network MoBotHouse (password mobot1234), then open http://192.168.4.1/sensors"
  • repeat forever
    • Update Web Data Temperature Value: Read DHT11 Temperature
    • wait 2 seconds
    • Update Web Data Humidity Value: Read DHT11 Humidity
    • Update Web Data Gas Value: Read Gas Sensor (MQ135)
    • Update Web Data Light Value: Read Light Sensor (LDR)
    • Update Web Data Potentiometer Value: Read Potentiometer
    • Update Web Data Movement Value: Motion Detected (PIR) ?
    • Print to serial (create text with "Fan switch is: " (Get Toggle Value of control id fan_switch))
    • if (Get Toggle Value of control id fan_switch)
      • Set Fan Speed 80
    • else
      • Set Fan Speed 0
    • wait 2 seconds
  • A Smart Home board with the screen, the fan, and the temperature, gas, light, knob and motion sensors.
  • A USB cable and the serial monitor.
  • A phone, tablet or laptop that can join a Wi-Fi network and open a web address.
  • To flip the switch: a laptop with a terminal (see The fan switch).
  1. Open 10-web-dashboard.mobot in MoBot.

  2. Plug in, pick the port, press Send to Device.

  3. Watch the serial monitor. Starting the network takes up to ten seconds, so be patient.

  4. On your phone, open the Wi-Fi list and join:

    • network MoBotHouse
    • password mobot1234

    Your phone will warn you that this network has no internet. That is right: the network is the board, and the board is not the internet.

  5. Open a browser and go to http://192.168.4.1/sensors

On the board’s screen:

Join MoBotHouse
192.168.4.1
pw mobot1234

In the serial monitor:

[MOBOT] Starting the web page
[SERVER] listening on port 80 (ap, ip 192.168.4.1)
[MOBOT] Join the Wi-Fi network MoBotHouse (password mobot1234), then open http://192.168.4.1/sensors
[MOBOT] Fan switch is: False
[MOBOT] Fan switch is: False

In the browser, a page of numbers something like this:

{"sensors": {"temperature": 23, "humidity": 45, "gas": 812, "pot": 2047,
"pir": false, "ldr": 3300, "toggle_1": false}, "ts": 91234}
  • temperature is in degrees Celsius and humidity is in percent.
  • gas, ldr (the light) and pot (the knob) are raw readings from 0 to 4095. A bigger gas means more gas or smoke, a bigger ldr means more light.
  • pir is true while the motion sensor sees something moving.
  • toggle_1 is the fan switch.
  • ts is the board’s own clock, in milliseconds.

The program sends fresh readings to the page about every 4 seconds. Reload the page after you cover the light sensor with your hand or turn the knob, and the numbers change.

Before the program has sent its first reading, the page holds fixed starting numbers, such as a temperature of 25.0. That is why the program updates every reading in a loop: so the page shows the real ones.

Other addresses to try: /temperature, /humidity, /gas, /pot, /pir, /ldr and /status, which lists every address the page answers.

The program added a switch at the address /fan. Open http://192.168.4.1/fan in the browser and you see {"value": false}.

A browser’s address bar can only look at an address. To change the switch you have to send to it. From a laptop joined to MoBotHouse, open a terminal and type:

curl -X POST http://192.168.4.1/fan -d '{"value": true}'

If quoting the curly brackets is a nuisance, put the value in the address instead:

curl -X POST "http://192.168.4.1/fan?value=1"

Then, within about 4 seconds:

  • the serial monitor changes to [MOBOT] Fan switch is: True,
  • the fan starts spinning at 80 %.

Send {"value": false} (or ?value=0) and it stops again.

Keep hair and fingers away from the fan.

The Start Web Server block comes first, and the Register Toggle block that adds the switch comes after it. Starting the web page builds a fresh page, so a switch added before it would not work.

Register Toggle gives back the switch’s number. The program keeps that number in the variable fan_switch, and Get Toggle Value asks for the switch by it. The first switch of each kind is number 1.

This program uses Start Web Server on its own WiFi network. The board makes a network of its own, which is why your phone has to join it, and the password you type must have at least 8 characters: that is the usual rule for WPA2 Wi-Fi networks.

The other block, Start Web Server on the saved WiFi, serves the page on the Wi-Fi the house already knows (the one you set up from the app). You then open the house’s address on that Wi-Fi instead. While this program runs, the board’s own network takes the place of the page the house serves on your Wi-Fi. The Smart Home web page and dashboard page tells more.

block what it does
iot_initweb Makes the board’s own Wi-Fi network, with the name and password you type, and starts the web page on it.
iot_updatedata Puts one reading on the page: Light, Temperature, Humidity, Gas, Potentiometer or Movement.
iot_addtoggle Adds a control at a web address and gives back its number: Toggle, Slider, RGB or Text.
iot_getactuators Reads what was last sent to that control.
iot_dht, iot_gaz, iot_ldr, iot_pot, iot_pir The sensors whose readings go on the page.
iot_fan, controls_if “If the switch is on, run the fan.”
iot_oledclear, iot_oledtext The screen that says how to join.
variables_set, variables_get The box called fan_switch that remembers the switch’s number.

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.

10-web-dashboard.firmware-3.main.py
# Web dashboard — iot (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 10-web-dashboard.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 10-web-dashboard.main.py.
from machine import Pin
import time
print("[MOBOT]", 'Starting the web page')
home.dashboard.start('MoBotHouse','mobot1234')
fan_switch = home.dashboard.add_switch('/fan')
home.display.clear()
home.display.show_text('Join MoBotHouse', 0, 0)
home.display.show_text('192.168.4.1', 0, 16)
home.display.show_text('pw mobot1234', 0, 32)
print("[MOBOT]", 'Join the Wi-Fi network MoBotHouse (password mobot1234), then open http://192.168.4.1/sensors')
while True:
home.dashboard.update_temperature(home.temperature_sensor.temperature())
time.sleep(2)
home.dashboard.update_humidity(home.temperature_sensor.humidity())
home.dashboard.update_gas(home.gas_sensor.level())
home.dashboard.update_ldr(home.light_sensor.level())
home.dashboard.update_pot(home.knob.position())
home.dashboard.update_pir(home.motion_sensor.motion_detected())
print("[MOBOT]", ('Fan switch is: ' + str(home.dashboard.switch(fan_switch))))
if home.dashboard.switch(fan_switch):
home.fan.set_speed(80)
else:
home.fan.set_speed(0)
time.sleep(2)
  • Change the network name and the password. The password needs at least 8 characters. The screen and the serial monitor spell the name and the password out as text, so change those too.
  • Register a slider at /speed instead of the toggle, and plug its value straight into the fan’s speed socket. Then curl -X POST http://192.168.4.1/speed -d '{"value": 60}' runs the fan at 60 %. The fan speed goes from 0 to 100.
  • Add a Register block of another kind, such as Text at /say, and print what was sent to it in the serial monitor.