esp32与 micropython 编写IP修改界面
时间: 2023-10-04 22:13:38 浏览: 187
ESP8266_MicroPython.zip
1. 导入必要的库
```
import network
import time
import ujson
from machine import Pin
from machine import SoftSPI
from ili934xnew import ILI9341
```
2. 设置SPI总线和ILI9341显示屏
```
spi = SoftSPI(
baudrate=40000000,
polarity=1,
phase=0,
sck=Pin(18),
mosi=Pin(23),
miso=Pin(19))
lcd = ILI9341(
spi,
cs=Pin(5),
dc=Pin(27),
rst=Pin(33),
width=320,
height=240,
rotation=0)
```
3. 设置WiFi
```
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
```
4. 定义IP修改界面
```
def ip_setting():
ip = sta_if.ifconfig()[0]
subnet = sta_if.ifconfig()[1]
gateway = sta_if.ifconfig()[2]
dns = sta_if.ifconfig()[3]
lcd.fill(ILI9341.BLACK)
lcd.text("IP Setting", 50, 50, ILI9341.WHITE)
lcd.text("IP Address:", 20, 80, ILI9341.WHITE)
ip_input = ""
ip_cursor = 0
while True:
lcd.fill_rect(20, 100, 280, 30, ILI9341.WHITE)
lcd.text(ip_input, 20, 100, ILI9341.BLACK)
lcd.rect(20 + ip_cursor*10, 100, 10, 30, ILI9341.RED)
if len(ip_input) >= 15:
break
if lcd.touch_in_rect(20, 140, 80, 40):
break
if lcd.touch_in_rect(20, 180, 80, 40):
ip_input = ip_input[:-1]
ip_cursor = max(0, ip_cursor-1)
if lcd.touch_in_rect(240, 140, 60, 40):
break
if lcd.touch_in_rect(240, 180, 60, 40):
ip_input += "."
ip_cursor += 1
for i in range(10):
if lcd.touch_in_rect(110+i*20, 140, 20, 40):
ip_input += str(i)
ip_cursor += 1
ip = ip_input
lcd.text("Subnet Mask:", 20, 150, ILI9341.WHITE)
subnet_input = ""
subnet_cursor = 0
while True:
lcd.fill_rect(20, 170, 280, 30, ILI9341.WHITE)
lcd.text(subnet_input, 20, 170, ILI9341.BLACK)
lcd.rect(20 + subnet_cursor*10, 170, 10, 30, ILI9341.RED)
if len(subnet_input) >= 15:
break
if lcd.touch_in_rect(20, 210, 80, 40):
break
if lcd.touch_in_rect(20, 250, 80, 40):
subnet_input = subnet_input[:-1]
subnet_cursor = max(0, subnet_cursor-1)
if lcd.touch_in_rect(240, 210, 60, 40):
break
if lcd.touch_in_rect(240, 250, 60, 40):
subnet_input += "."
subnet_cursor += 1
for i in range(10):
if lcd.touch_in_rect(110+i*20, 210, 20, 40):
subnet_input += str(i)
subnet_cursor += 1
subnet = subnet_input
lcd.text("Gateway:", 20, 260, ILI9341.WHITE)
gateway_input = ""
gateway_cursor = 0
while True:
lcd.fill_rect(20, 280, 280, 30, ILI9341.WHITE)
lcd.text(gateway_input, 20, 280, ILI9341.BLACK)
lcd.rect(20 + gateway_cursor*10, 280, 10, 30, ILI9341.RED)
if len(gateway_input) >= 15:
break
if lcd.touch_in_rect(20, 320, 80, 40):
break
if lcd.touch_in_rect(20, 360, 80, 40):
gateway_input = gateway_input[:-1]
gateway_cursor = max(0, gateway_cursor-1)
if lcd.touch_in_rect(240, 320, 60, 40):
break
if lcd.touch_in_rect(240, 360, 60, 40):
gateway_input += "."
gateway_cursor += 1
for i in range(10):
if lcd.touch_in_rect(110+i*20, 320, 20, 40):
gateway_input += str(i)
gateway_cursor += 1
gateway = gateway_input
lcd.text("DNS Server:", 20, 370, ILI9341.WHITE)
dns_input = ""
dns_cursor = 0
while True:
lcd.fill_rect(20, 390, 280, 30, ILI9341.WHITE)
lcd.text(dns_input, 20, 390, ILI9341.BLACK)
lcd.rect(20 + dns_cursor*10, 390, 10, 30, ILI9341.RED)
if len(dns_input) >= 15:
break
if lcd.touch_in_rect(20, 430, 80, 40):
break
if lcd.touch_in_rect(20, 470, 80, 40):
dns_input = dns_input[:-1]
dns_cursor = max(0, dns_cursor-1)
if lcd.touch_in_rect(240, 430, 60, 40):
break
if lcd.touch_in_rect(240, 470, 60, 40):
dns_input += "."
dns_cursor += 1
for i in range(10):
if lcd.touch_in_rect(110+i*20, 430, 20, 40):
dns_input += str(i)
dns_cursor += 1
dns = dns_input
sta_if.ifconfig((ip, subnet, gateway, dns))
lcd.fill(ILI9341.BLACK)
lcd.text("IP Setting", 50, 50, ILI9341.WHITE)
lcd.text("IP Address:", 20, 80, ILI9341.WHITE)
lcd.text(ip, 120, 80, ILI9341.WHITE)
lcd.text("Subnet Mask:", 20, 110, ILI9341.WHITE)
lcd.text(subnet, 120, 110, ILI9341.WHITE)
lcd.text("Gateway:", 20, 140, ILI9341.WHITE)
lcd.text(gateway, 120, 140, ILI9341.WHITE)
lcd.text("DNS Server:", 20, 170, ILI9341.WHITE)
lcd.text(dns, 120, 170, ILI9341.WHITE)
time.sleep(2)
```
5. 在主程序中调用IP修改界面
```
while True:
if lcd.touch_in_rect(10, 10, 60, 40):
ip_setting()
time.sleep(0.1)
```
阅读全文