python嵌入式开发


title: python嵌入式开发
tags: micropython
date: 2018-02-28 22:53:00


现在是凌晨6:00,从下午我刚知道microPython,到现在我做成第一个硬件作品–mqtt协议远程控制LED灯。
感觉这才是充实的感觉,心里舒服,当我看到python的历程里有那么多的教程和资料,欣喜若狂,好想赶紧学一些东西,做出更好玩,更有趣的东西。


第一个硬件的制作。

python嵌入式开发_第1张图片


One net平台服务器端截图。
python嵌入式开发_第2张图片

创作流程–思维导图
python嵌入式开发_第3张图片


测试代码备份(一)
上电自动连接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()

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

from simple import MQTTClient
from machine import Pin
import machine
import micropython
#选择G4引脚
g4 = Pin(2, Pin.OUT, value=0)
# MQTT服务器地址域名为:183.230.40.39,不变
SERVER = "183.230.40.39"
#设备ID
CLIENT_ID = "25781333"
#随便起个名字
TOPIC = "python"
#产品ID
username='121252'
#产品APIKey:
password='X6WkL=PlCydW6y6rQ71ZCAzkQmQ='
state = 0
def 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()

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

import network
from machine import Pin
import socket
import urllib
import time

def 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')

代码已上传我的coding–码市仓库


生活,简简单单,不以物喜不以己悲。
追逐梦想,不忘初心。–2018年2月28日-凌晨。

你可能感兴趣的:(嵌入式)