492|0

172

帖子

3

TA的资源

一粒金砂(高级)

楼主
 

【得捷电子Follow me第3期】+任务4:连接WiFi网络 [复制链接]

 

ESP32C3支持Wifi功能,可以连接互联网,从网上获取信息,比如网络时间信息。

连接网络

参考官方提供的代码,填入自己的wifi名称和密码,即可实现联网。

  1. import network
  2. import urequests
  3. import utime as time
  4. # Network settings
  5. wifi_ssid = "user_wifi"
  6. wifi_password = "user_passwd"
  7. def scan_and_connect():
  8. station = network.WLAN(network.STA_IF)
  9. station.active(True)
  10. print("Scanning for WiFi networks, please wait...")
  11. for ssid, bssid, channel, RSSI, authmode, hidden in station.scan():
  12. print("* {:s}".format(ssid))
  13. print(" - Channel: {}".format(channel))
  14. print(" - RSSI: {}".format(RSSI))
  15. print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid))
  16. print()
  17. while not station.isconnected():
  18. print("Connecting...")
  19. station.connect(wifi_ssid, wifi_password)
  20. time.sleep(10)
  21. print("Connected!")
  22. print("My IP Address:", station.ifconfig()[0])
  23. # Execute the functions
  24. scan_and_connect()

串口输出的结果如下:

  1. Scanning for WiFi networks, please wait...
  2. .....
  3. Connected!
  4. My IP Address: 192.168.170.168

获取网络时间并显示

网络时间的获取是通过向网络时间服务器发起请求,在返回的json字符串中提取相关的信息,配合之前实现的ssd1306显示功能,从而实现获取网络时间并显示的功能。

代码如下

  1. from machine import Pin, SoftI2C
  2. import ssd1306
  3. from time import sleep
  4. import time
  5. import network
  6. import urequests
  7. import ujson
  8. # ESP32 Pin assignment
  9. # i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
  10. # ESP32C3 Pin assignment
  11. i2c = SoftI2C(scl=Pin(7), sda=Pin(6))# Adjust the Pin numbers based on your connections
  12. oled_width = 128
  13. oled_height = 64
  14. oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
  15. station = network.WLAN(network.STA_IF)
  16. station.active(True)
  17. # Network settings
  18. wifi_ssid = "user_wifi"
  19. wifi_password = "user_passwd"
  20. url = "http://worldtimeapi.org/api/timezone/America/New_York"
  21. print("Scanning for WiFi networks, please wait...")
  22. authmodes = ['Open', 'WEP', 'WPA-PSK' 'WPA2-PSK4', 'WPA/WPA2-PSK']
  23. for (ssid, bssid, channel, RSSI, authmode, hidden) in station.scan():
  24. print("* {:s}".format(ssid))
  25. print(" - Channel: {}".format(channel))
  26. print(" - RSSI: {}".format(RSSI))
  27. print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid))
  28. print()
  29. # Continually try to connect to WiFi access point
  30. while not station.isconnected():
  31. # Try to connect to WiFi access point
  32. print("Connecting...")
  33. station.connect(wifi_ssid, wifi_password)
  34. time.sleep(10)
  35. # Display connection details
  36. print("Connected!")
  37. print("My IP Address:", station.ifconfig()[0])
  38. while True:
  39. # Perform HTTP GET request on a non-SSL web
  40. response = urequests.get(url)
  41. # Check if the request was successful
  42. if response.status_code == 200:
  43. # Parse the JSON response
  44. data = ujson.loads(response.text)
  45. # Extract the "datetime" field for New York
  46. ny_datetime = data["datetime"]
  47. # Split the date and time components
  48. date_part, time_part = ny_datetime.split("T")
  49. # Get only the first two decimal places of the time
  50. time_part = time_part[:8]
  51. # Get the timezone
  52. timezone = data["timezone"]
  53. # Clear the OLED display
  54. oled.fill(0)
  55. # Display the New York date and time on separate lines
  56. oled.text("New York Date:", 0, 0)
  57. oled.text(date_part, 0, 10)
  58. oled.text("New York Time:", 0, 20)
  59. oled.text(time_part, 0, 30)
  60. oled.text("Timezone:", 0, 40)
  61. oled.text(timezone, 0, 50)
  62. # Update the display
  63. oled.show()
  64. else:
  65. oled.text("Failed to get the time for New York!")
  66. # Update the display
  67. oled.show()

显示效果如图

总结

wifi功能给了开发者获取外部信息的方式,使嵌入式设备接入网络,实现贴近实际的应用。

点赞 关注
 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
立即报名 | 2025 瑞萨电子工业以太网技术日即将开启!
3月-4月 深圳、广州、北京、苏州、西安、上海 走进全国6城
2025瑞萨电子工业以太网技术巡回沙龙聚焦工业4.0核心需求,为工程师与企业决策者提供实时通信技术最佳解决方案。
预报从速,好礼等您拿~

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表