Networking: Linking with mPython Board 3.0
This example demonstrates how to enable multi-node networking communication between mPython IoT and mPython Board 3.0 (mpython-s3) via the MQTT protocol. mPython Board 3.0 serves as a more feature-rich central node that can work together with mPython IoT nodes to build more complete IoT applications.
Networking Architecture
Similar to networking with mPython Core 2.0, all nodes connect to the same MQTT broker and communicate through agreed-upon topics:
mPython IoT (Node A): Acts as a distributed IoT node, collecting and publishing data
mPython Board 3.0 (Node B): Acts as the central node, subscribing to data, aggregating and processing it, and sending back control commands
Note
For mPython Board 3.0 MQTT communication usage, see mPython Board 3.0 IoT Tutorial .
Node A: mPython IoT Publishing Data
mPython IoT collects environmental data and publishes to the topic classroom/env:
from umqtt.simple import MQTTClient
import network
import time
sta = network.WLAN(network.STA_IF)
sta.active(True)
sta.connect('your_ssid', 'your_password')
while not sta.isconnected():
time.sleep(0.5)
SERVER = "192.168.1.100"
TOPIC = "classroom/env"
client = MQTTClient("mpython_iot_001", SERVER, 1883)
client.connect()
value = 25
while True:
client.publish(TOPIC, str(value))
print("Reported: %d" % value)
value += 1
time.sleep(5)
Node B: mPython Board 3.0 Subscribing and Responding
mPython Board 3.0 subscribes to data, displays the received values on the screen, and sends commands back to the control topic. The following code runs on mPython Board 3.0:
from umqtt.simple import MQTTClient
from mpython import *
SERVER = "192.168.1.100"
TOPIC_ENV = "classroom/env"
TOPIC_CTRL = "classroom/ctrl"
mywifi = wifi()
mywifi.connectWiFi("your_ssid", "your_password")
def sub_cb(topic, msg):
print((topic, msg))
oled.fill(0)
oled.DispChar(msg.decode(), 0, 0, 1) # Display received data on screen
oled.show()
client = MQTTClient("mpython_s3_001", SERVER, 1883)
client.connect()
client.set_callback(sub_cb)
client.subscribe(TOPIC_ENV)
while True:
client.wait_msg()
Hint
mPython Board 3.0 has an OLED display for visualizing received data, while mPython IoT serves as a lightweight node deployed at various locations for data collection. Together they enable a “distributed collection, centralized display” IoT architecture.
Bidirectional Communication
mPython IoT can also subscribe to the control topic classroom/ctrl published by mPython Board 3.0 to receive commands from the central node, enabling bidirectional communication and remote control. For specific subscription and control logic, see Remote Actuator Control .