Esempio n. 1
0
def enable_esp32():
    from network_esp32 import wifi
    if wifi.isconnected() == False:
        for i in range(5):
            try:
                # Running within 3 seconds of power-up can cause an SD load error
                # wifi.reset(is_hard=False)
                wifi.reset(is_hard=True)
                print('try AT connect wifi...')
                wifi.connect(SSID, PASW)
                if wifi.isconnected():
                    break
            except Exception as e:
                print(e)
    print('network state:', wifi.isconnected(), wifi.ifconfig())
# Licensed under the MIT license:
#   http://www.opensource.org/licenses/mit-license.php
#

# Uasge see readme.md
from network_esp32 import wifi

SSID = "Sipeed_2.4G"
PASW = "XXXXXXXX"

if wifi.isconnected() == False:
    for i in range(5):
        try:
            wifi.reset()
            print('try AT connect wifi...')
            wifi.connect(SSID, PASW)
            if wifi.isconnected():
                break
        except Exception as e:
            print(e)
print('network state:', wifi.isconnected(), wifi.ifconfig())

print("ping baidu.com:", wifi.nic.ping("baidu.com"), "ms")
wifi.nic.disconnect()
'''
    ESP32_SPI firmware version: 1.4.0
    try AT connect wifi...
    network state: True ('192.168.0.180', '255.255.255.0', '192.168.0.1')
    ping baidu.com: 40 ms
    >
    MicroPython v0.5.1-136-g039f72b6c-dirty on 2020-11-18; Sipeed_M1 with kendryte-k210
# create image directory if it doesn't exist
if IMAGE_DIRECTORY not in os.listdir(): 
	os.mkdir(IMAGE_DIRECTORY) 

# RESET WHEN WIFI OR SOCKET CONNECTION IS LOST
def reset():
	lcd.clear()
	lcd.draw_string(20, 100, "ERROR: can't connect to wifi/server", lcd.RED, lcd.BLACK)
	lcd.draw_string(125, 120, "reseting...", lcd.RED, lcd.BLACK)
	sleep_ms(ERROR_SLEEP_TIME)
	machine.reset()

try:
	# INITIALIZE WIFI
	wifi.reset()
	wifi.connect(SSID, PASSWORD)

	# INITIALIZE SOCKET
	socket_client = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
	socket_client.connect((SERVER_ADDRESS, PORT)) # WON'T WORK IF SERVER IS NOT ON
	socket_client.settimeout(0)
except:
	reset()

# RETURNS THE DISTANCE, IN CENTIMETERS, FROM THE GIVEN ECHO AND TRIGGER PINS OF THE ULTRASONIC SENSOR
def get_distance(echo, trigger):
	# set trigger high for 10us
	trigger.value(1)
	sleep_us(10)
	trigger.value(0)