示例#1
0
def esp_connect():
    fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX)
    fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX)
    uart = machine.UART(machine.UART.UART2,
                        115200,
                        timeout=1000,
                        read_buf_len=4096)
    try:
        nic = network.ESP8285(uart)
        nic.connect(SSID, SSID_PWD)
        if (nic.isconnected):
            print("ip:{}/{}".format(nic.ifconfig()[0], nic.ifconfig()[1]))
            return True
        else:
            sleep(1)  # retry
            if (nic.isconnected):
                print("ip:{}/{}".format(nic.ifconfig()[0], nic.ifconfig()[1]))
                return True
            else:
                print("network connection failed.\n")
                return False

    except:
        print(" try to connect ESP8286 TX to pin-{},RX to pin-{}".format(
            board_info.WIFI_TX, board_info.WIFI_RX))
        print(" ,and ESP8286 must be runing on AT command mode.\n")
        return False
示例#2
0
文件: wifi.py 项目: noahzhy/K210
def connect_wifi(WIFI_SSID=None, WIFI_PASSWD=None):
    if WIFI_SSID or WIFI_PASSWD:
        # for new MaixGO board, if not, remove it
        fm.register(0, fm.fpioa.GPIOHS1, force=True)
        wifi_io0_en = GPIO(GPIO.GPIOHS1, GPIO.OUT)
        wifi_io0_en.value(0)

        # En ESP8285
        fm.register(8, fm.fpioa.GPIOHS0, force=True)
        wifi_en = GPIO(GPIO.GPIOHS0, GPIO.OUT)
        fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX, force=True)
        fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX, force=True)

    def wifi_enable(en):
        wifi_en.value(en)

    def wifi_reset():
        global uart
        wifi_enable(0)
        time.sleep_ms(200)
        wifi_enable(1)
        time.sleep(2)
        uart = UART(UART.UART2, 115200, timeout=1000, read_buf_len=4096)
        tmp = uart.read()
        uart.write("AT+UART_CUR=921600,8,1,0,0\r\n")
        print(uart.read())
        # important! baudrate too low or read_buf_len too small will loose data
        uart = UART(UART.UART2, 921600, timeout=1000, read_buf_len=10240)
        uart.write("AT\r\n")
        tmp = uart.read()
        print(tmp)
        if not tmp.endswith("OK\r\n"):
            print("reset fail")
            return None
        try:
            nic = network.ESP8285(uart)
        except Exception:
            return None
        return nic

    nic = wifi_reset()
    if not nic:
        raise Exception("WiFi init fail")

    nic = network.ESP8285(uart)
    err = 0

    while True:
        try:
            nic.connect(WIFI_SSID, WIFI_PASSWD)
        except Exception:
            err += 1
            print("Connect AP failed, now try again")
            if err > 3:
                raise Exception("Conenct AP fail")
            continue
        break

    print(nic.ifconfig())
    return nic.isconnected()
示例#3
0
def wifi_reset():
    global uart
    wifi_enable(0)
    time.sleep_ms(200)
    wifi_enable(1)
    time.sleep(2)
    uart = UART(UART.UART2,115200,timeout=1000, read_buf_len=4096)
    tmp = uart.read()
    uart.write("AT+UART_CUR=921600,8,1,0,0\r\n")
    print(uart.read())
    uart = UART(UART.UART2,921600,timeout=1000, read_buf_len=10240) # important! baudrate too low or read_buf_len too small will loose data
    uart.write("AT\r\n")
    tmp = uart.read()
    print(tmp)
    if not tmp.endswith("OK\r\n"):
        print("reset fail")
        return None
    try:
        nic = network.ESP8285(uart)
    except Exception:
        return None
    return nic
示例#4
0
def wifi_init():
    global uart
    wifi_enable(0)
    time.sleep_ms(200)
    wifi_enable(1)
    time.sleep(2)
    uart = UART(UART.UART2, 115200, timeout=1000, read_buf_len=4096)
    tmp = uart.read()
    uart.write("AT+UART_CUR=921600,8,1,0,0\r\n")
    print(uart.read())
    uart = UART(UART.UART2, 921600, timeout=1000,
                read_buf_len=10240)  #实测模块波特率太低或者缓存长度太短会导致数据丢失。
    uart.write("AT\r\n")
    tmp = uart.read()
    print(tmp)
    if not tmp.endswith("OK\r\n"):
        print("reset fail")
        return None
    try:
        nic = network.ESP8285(uart)
    except Exception:
        return None
    return nic
示例#5
0
 def reset(force=False, reply=5):
     if force == False and __class__.isconnected():
         return True
     __class__.init()
     for i in range(reply):
         print('reset...')
         __class__.enable(False)
         time.sleep_ms(50)
         __class__.enable(True)
         time.sleep_ms(500) # at start > 500ms
         if __class__._at_cmd(timeout=500):
             break
     __class__._at_cmd()
     __class__._at_cmd('AT+UART_CUR=921600,8,1,0,0\r\n', "OK\r\n")
     __class__.uart = UART(UART.UART2, 921600, timeout=1000, read_buf_len=10240)
     # important! baudrate too low or read_buf_len too small will loose data
     #print(__class__._at_cmd())
     try:
         __class__.nic = network.ESP8285(__class__.uart)
         time.sleep_ms(500) # wait at ready to connect
     except Exception as e:
         print(e)
         return False
     return True
示例#6
0
def wifi_enable(en):
    global wifi_en
    wifi_en.value(en)


def wifi_deal_ap_info(info):
    res = []
    for ap_str in info:
        ap_str = ap_str.split(",")
        info_one = []
        for node in ap_str:
            if node.startswith('"'):
                info_one.append(node[1:-1])
            else:
                info_one.append(int(node))
        res.append(info_one)
    return res


wifi_enable(1)
time.sleep(2)
nic = network.ESP8285(uart)

ap_info = nic.scan()
ap_info = wifi_deal_ap_info(ap_info)

ap_info.sort(key=lambda x: x[2], reverse=True)  # sort by rssi
for ap in ap_info:
    print("SSID:{:^20}, RSSI:{:>5} , MAC:{:^20}".format(ap[1], ap[2], ap[3]))
import network, time, socket
from machine import UART
from fpioa_manager import fm, board_info

if 'wlan' not in locals():

    # En EP8285 AT

    fm.register(27, fm.fpioa.UART2_TX, force=True)
    fm.register(28, fm.fpioa.UART2_RX, force=True)

    uart = UART(UART.UART2, 921600, 8, 2, 2, timeout=10000, read_buf_len=40960)

    time.sleep(3)
    wlan = network.ESP8285(uart)

    WIFI_SSID = "webduino.io"
    WIFI_PASW = "webduino"

    err = 0
    while 1:
        try:
            wlan.connect(WIFI_SSID, WIFI_PASW)
        except Exception:
            err += 1
            print("Connect AP failed, now try again")
            if err > 3:
                raise Exception("Conenct AP fail")
            continue
        break