Programming with MicroPython: WiFi & IoT
Last updated
Last updated
Import the Library to make use of its functions.
from future import *
wifi.connect('router','password')
FutureBoard can only connect to wireless networks with 2.4GHz frequency.
wifi.sta.isconnected()
Returns 1 if FutureBoard is connected to the network, 0 if not.
wifi.sta.ifconfig()
Returns a list with the configuration including IP Address, Subnet Mask, Gateway, Domain.
import machine
import ubinascii
x = ubinascii.hexlify(machine.unique_id()).decode().upper()
Returns the physical address of the FutureBoard.
wifi.download("PASTE FILE LINK","FILE NAME")
Downloads a file from a file URL and saves it to the SD card.
#/bin/python
from future import *
from time import sleep
import machine
import ubinascii
wifi.connect(str("name"), "pwd")
sleep(5)
if wifi.sta.isconnected():
screen.clear()
screen.text(str("IP: ")+str(wifi.sta.ifconfig()[0]),5,10,1,(0, 119, 255))
screen.text("MAC Address: ",5,25,1,(0, 119, 255))
screen.text(ubinascii.hexlify(machine.unique_id()).decode().upper(),5,35,1,(0, 119, 255))
screen.text("Subnet Mask: ",5,50,1,(0, 119, 255))
screen.text(wifi.sta.ifconfig()[1],5,60,1,(0, 119, 255))
screen.text(str("Gateway: ")+str(wifi.sta.ifconfig()[2]),5,75,1,(0, 119, 255))
screen.text(str("DNS: ")+str(wifi.sta.ifconfig()[3]),5,90,1,(0, 119, 255))
import mqttsimple
MQTT operations require the mqttsimple library.
myMQTT = mqttsimple.MQTTClient(server,client_id,port=0,user=None,password=None,keepalive=0,ssl=False,ssl_params={})
Creates an object with the connection to the MQTT Broker.
For most brokers, insert the host address into server and the client id into client_id.
For some brokers, please refer to the broker’s documentations for the details required to connect to the server.
myMQTT.connect()
myMQTT.disconnect()
myMQTT.subscribe(topic=)
Fill in the topic name according to the documentation of the MQTT Broker.
myMQTT.publish(topic,msg)
Fill in the message and topic name according to the documentation of the MQTT Broker.
myMQTT.read(topic)
Fill in the topic name according to the documentation of the MQTT Broker.
myMQTT.check_msg()
myMQTT.wait_msg()
def myFunction(topic,msg):
print(topic)
print(msg)
myMQTT.set_callback(myFunction())
import mqttsimple
if not(wifi.sta.isconnected()):
wifi.connect('router', 'password')
c = mqttsimple.MQTTClient("myServer", 'myID')
c.connect()
c.subscribe('/topic1')
x= 0
# mqttRead() returns None if no new message is found
while 1:
if sensor.btnValue('a'):
x+=1
c.publish('/topic1','x'+str(x))
sleep(0.2)
# Print message if message is not None
mqttT = c.read('/topic1')
if mqttT:
print(mqttT)
import mqttsimple
if not(wifi.sta.isconnected()):
wifi.connect('router', 'password')
c = mqttsimple.MQTTClient("myServer", 'myID')
c.connect()
# Automacically triggered when topic is updated
def sub_cb(topic, msg):
print((topic, msg))
c.set_callback(sub_cb)
c.subscribe('/ttt')
while 1:
if sensor.btnValue('a'):
c.publish('/ttt', 'hello')
sleep(0.2)
# check message
c.check_msg()
# try below for waiting until message update
# c.wait_msg()
urequests and ujson libraries are also available to use. Please visit their official documentation for details.
import urequests
import json