405 views|1 replies

28

Posts

3

Resources
The OP
 

【Digi-Key Follow me Issue 3】Task 4: Connect to WiFi network [Copy link]

 

Requirements

Connect Seeed Studio XIAO ESP32C3 to a WiFi network and access Internet information

Hardware Preparation

This task can be completed using Seeed Studio XIAO ESP32C3 , but the IPEX antenna needs to be connected, otherwise the network connection will be unstable due to excessive signal attenuation.

Code

Here I will use Seeed Studio XIAO ESP32C3 to connect to WIFI, and use the http protocol to ask for the current time and display it on the OLED screen as the goal to complete this task.

The implementation code is as follows:

import time
import utime
import network
import urequests
import ujson
import task2


# 网络配置
wifi_ssid = "WIFI_SSID"
wifi_password = "WIFI_PASSWORD"

# 获取当前时间API
url = "https://f.m.suning.com/api/ct.do"

station = network.WLAN(network.STA_IF)
station.active(True)

# 连接至wifi
def connect_wifi():
    # 不断尝试连接到配置的WIFI
    while not station.isconnected():
        print("Connecting...")
        station.connect(wifi_ssid, wifi_password) # 连接至配置的WIFI AP
        time.sleep(10)

    # 连接成功,输出IP地址
    print("Connected!")
    print("My IP Address:", station.ifconfig()[0])
    # 显示IP地址在OLED上
    task2.text_ip = station.ifconfig()[0]
    

# 获取wifi连接状态
def get_wifi_state():
    return station.isconnected()


def get_time():
    # 请示当前时间
    response = urequests.get(url)
    # 检查返回请求返回状态码
    if response.status_code == 200:        
        '''
        获取到的结果JSON:
            {"api":"time","code":"1","currentTime": 1700054614641,"msg":""}
        '''
        # 解析返回的JSON数据
        data = ujson.loads(response.text)
        # 获取到的时间戳单位是毫秒,MicroPython time 转换时间戳的单位是秒,因此需要先进行换算。
        # 另外获取到的时间戳起始时间是1970年1月1日,而 MicroPython esp32 time 模块起始时间为 2000年1月1日 因此还需要减去差值
        local_time = utime.localtime(int(data["currentTime"] / 1000) - 946656000)
        # 格式化显示时间, 由于屏幕宽度有限这里只显示 月-日 时:分:秒
        local_data_time = "{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(local_time[1], local_time[2], local_time[3], local_time[4], local_time[5])
        print("Local Date Time:", local_data_time)
        # 更新时间显示
        task2.show_date_time(local_data_time)
    else:
        task2.show_date_time("Failed to get the time!")

Show results

After successfully connecting to the network and correctly obtaining the time, the current time is displayed on the OLED . The effect is as shown in the figure below:

Demo Video


This post is from DigiKey Technology Zone

Latest reply

Thanks for the technical sharing provided by the host. I will collect and study it first and then express my personal opinion.   Details Published on 2023-11-23 19:51
 
 

725

Posts

4

Resources
2
 

Thanks for the technical sharing provided by the host. I will collect and study it first and then express my personal opinion.

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list