Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add wifi connection #2

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions DevelepSource/DesktopPet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import usys
import time
import asyncio
import sys
import os

from ssd1306 import SSD1306_I2C

from wifi_connection import connect_wifi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wifi connection不是一个标准库吧?另外,我不建议直接修改main文件,你可以单独开一个文件叫做mian_wifi

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好嘞


ENABLE_SEND = True

if ENABLE_SEND:
Expand Down Expand Up @@ -40,6 +44,15 @@ def servo_move(servo, angle):

# 程序入口
if __name__ == "__main__":
# 连接 Wi-Fi
ip_address = connect_wifi()
if ip_address:
oled.fill(0)
oled.text("Connected:", 0, 0)
oled.text(ip_address, 0, 12)
oled.show()
time.sleep(2) # 显示 IP 地址 2 秒

servo_move(servo1, 90)
servo_move(servo2, 30)
oled.fill(0)
Expand Down
24 changes: 24 additions & 0 deletions DevelepSource/DesktopPet/wifi_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import network
import time

# 设置 Wi-Fi SSID 和密码
SSID = 'wifi的名字'
PASSWORD = 'wifi的密码'
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
max_wait = 15 # 最长等待时间为15秒
while max_wait > 0:
if wlan.isconnected():
break
print('等待连接...')
time.sleep(1)
max_wait -= 1
if wlan.isconnected():
print('已连接到 Wi-Fi')
print('网络配置:', wlan.ifconfig())
return wlan.ifconfig()[0] # 返回分配的 IP 地址
else:
print('无法连接到 Wi-Fi')
return None