wlan = network.WLAN(network.STA_IF) # create station interface wlan.active(True) # activate the interface wlan.scan() # scan for access points wlan.isconnected() # check if the station is connected to an AP wlan.connect('essid', 'password') # connect to an AP wlan.config('mac') # get the interface's MAC adddress wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
而在 AT_IF 模式下,则是我们作为 WiFi 热点,允许其他人连接我们。
1 2 3 4 5
import network
ap = network.WLAN(network.AP_IF) # create access-point interface ap.active(True) # activate the interface ap.config(essid='ESP-AP') # set the ESSID of the access point
# Publish test messages e.g. with: # mosquitto_pub -t foo_topic -m hello
# Received messages from subscriptions will be delivered to this callback defsub_cb(topic, msg): print((topic, msg))
defmain(server="iot.eclipse.org"): # test server : iot.eclipse.org c = MQTTClient("RT-Thread", server) c.set_callback(sub_cb) c.connect() c.subscribe(b"foo_topic") # subscribe foo_topic tipic whileTrue: ifTrue: # Blocking wait for message c.wait_msg() else: # Non-blocking wait for message c.check_msg() # Then need to sleep to avoid 100% CPU usage (in a real # app other useful actions would be performed instead) time.sleep(1)
c.disconnect()
if __name__ == "__main__": main()
MQTT 发布
1 2 3 4 5 6 7 8 9 10 11 12 13
from umqtt.simple import MQTTClient
# Test reception e.g. with: # mosquitto_sub -t foo_topic