Created
November 10, 2019 12:12
-
-
Save boochow/74eab2e60888a30c80d7480c063ab636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WIFI_SSID = "ssid" | |
WIFI_PASSWD = "password" | |
import network, time | |
print("=== Configuring FPIOA") | |
from fpioa_manager import fm, board_info | |
from Maix import GPIO | |
from machine import UART | |
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_reset(): | |
global uart | |
wifi_en.value(0) | |
time.sleep_ms(200) | |
wifi_en.value(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 | |
def do_connect(nic): | |
err = 0 | |
while 1: | |
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("\r\n=== Connecting to Wi-Fi AP") | |
nic = wifi_reset() | |
if not nic: | |
raise Exception("WiFi init fail") | |
do_connect(nic) | |
nic.ifconfig() | |
print("\r\n=== Connecting to the Internet") | |
import socket | |
s = socket.socket() | |
addr_info = socket.getaddrinfo("towel.blinkenlights.nl", 23) | |
addr = addr_info[0][-1] | |
s.connect(addr) | |
while True: | |
data = s.recv(10240) | |
print(str(data, 'utf8'), end='') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment