掌握Python嵌入式开发:轻松操控硬件,打造智能家电

发表时间: 2020-09-29 16:13

使用microPython控制硬件:mqtt协议远程控制LED灯。
Python的知识宝库里有很多的教程和资料,赶紧学一些东西,做出更好玩,更有趣的东西。


硬件的制作:



One net平台服务器端截图。

创作流程–思维导图


测试代码备份(一)
上电自动连接wifi

def do_connect():                import network                sta_if = network.WLAN(network.STA_IF)                ap_if = network.WLAN(network.AP_IF)                if ap_if.active():                                ap_if.active(False)                if not sta_if.isconnected():                                print('connecting to network...')                sta_if.active(True)                sta_if.connect('Msun3', '13456789') #wifi的SSID和密码                while not sta_if.isconnected():                                pass                print('network config:', sta_if.ifconfig())do_connect()1234567891011121314

代码备份(二)
网络访问服务器

from simple import MQTTClientfrom machine import Pinimport machineimport micropython#选择G4引脚g4 = Pin(2, Pin.OUT, value=0)# MQTT服务器地址域名为:183.230.40.39,不变SERVER = "183.230.40.39"#设备IDCLIENT_ID = "25781333"#随便起个名字TOPIC = "python"#产品IDusername='121252'#产品APIKey:password='X6WkL=PlCydW6y6rQ71ZCAzkQmQ='state = 0def sub_cb(topic, msg):        global state        print((topic, msg))        if msg == b"on":                        g4.value(1)                        state = 1                        print("1")        elif msg == b"off":                        g4.value(0)                        state = 0                        print("0")        elif msg == b"toggle":                        state = 1 - state                        g4.value(state)def main(server=SERVER):        #端口号为:6002        c = MQTTClient(CLIENT_ID, server,6002,username,password)        c.set_callback(sub_cb)        c.connect()        c.subscribe(TOPIC)        print("Connected to %s, subscribed to %s topic" % (server, TOPIC))        try:                        while 1:                                        c.wait_msg()        finally:                        c.disconnect()123456789101112131415161718192021222324252627282930313233343536373839404142434445

网络初级调试代码(简单get)

import networkfrom machine import Pinimport socketimport urllibimport timedef led_state():    p2 = Pin(2, Pin.OUT)    p2.value(0)    time.sleep_ms(500)    p2.value(1)    time.sleep_ms(500)    p2.value(0)    time.sleep_ms(500)    p2.value(1)def do_connect():    sta_if = network.WLAN(network.STA_IF)    p2 = Pin(2, Pin.OUT)    sta_if.active(False)    if not sta_if.isconnected():        p2.low()        print('connecting to network...')        sta_if.active(True)        sta_if.connect('Msun3', '13456789')        while not sta_if.isconnected():            pass    if sta_if.isconnected():        print('connect success')        p2.high()        print('network config:', sta_if.ifconfig())def http_get(url):    _, _, host, path = url.split('/', 3)    addr = socket.getaddrinfo(host, 80)[0][-1]    s = socket.socket()    s.connect(addr)    s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))    while True:        data = s.recv(50)        if data:            recive=str(data, 'utf8')            #print('recive:',recive)            print(str(data, 'utf8'), end='')            if(recive.find('begin')>-1):                led_state()        else:            break    s.close()do_connect()http_get('http://back.waphx.com/guessbook/book_list.aspx?siteid=24233&classid=49392&page=1&sid=D1BD2C713EF464690_3_23240_25313_23240-2-0-0-0-320')123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051