☰
Rtthread OS 功能开发之 AP6212 模块WiFi联网
2026/10/16 6:32:31 网站建设 项目流程

以ART-PI为例介绍WiFi的使用方法,新建项目工程,打开AP6212硬件设备

并取消C基础库,CTRL+S保存。

编译程序,下载。

等待程序运行到了识别网卡成功后代表WiFi模块正式完成初始化。

输入WiFi联网指令:(名称和密码不需要加"”)

wifi join WiFi名称 WiFi密码

既可以连接网络。获取到IP地址基本上就已经联网成功了。

也可以ping 百度试一下,能正常获取返回就代表正常上网了。

二、设置自动联网功能

在上面的操作中需要手动连接WiFi,输入密码和名称,这点在实际操作中很麻烦,可以使用以下的操作实现WiFi自动联网。

首先开启easyflash 软件包,并保存。

在port 组下面增加以下两个文件。

ef_fal_port.c

/* * This file is part of the EasyFlash Library. * * Copyright (c) 2015, Armink, <armink.ztl@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * 'Software'), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * Function: Portable interface for FAL (Flash Abstraction Layer) partition. * Created on: 2018-05-19 */ #include <easyflash.h> #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <sfud.h> #include <rthw.h> #include <rtthread.h> #include <fal.h> /* EasyFlash partition name on FAL partition table */ #define FAL_EF_PART_NAME "easyflash" /* default ENV set for user */ static const ef_env default_env_set[] = { {"boot_times", "0"} }; static char log_buf[RT_CONSOLEBUF_SIZE]; static struct rt_semaphore env_cache_lock; static const struct fal_partition *part = NULL; /** * Flash port for hardware initialize. * * @param default_env default ENV set for user * @param default_env_size default ENV size * * @return result */ EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size) { EfErrCode result = EF_NO_ERR; *default_env = default_env_set; *default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]); rt_sem_init(&env_cache_lock, "env lock", 1, RT_IPC_FLAG_PRIO); part = fal_partition_find(FAL_EF_PART_NAME); EF_ASSERT(part); return result; } /** * Read data from flash. * @note This operation's units is word. * * @param addr flash address * @param buf buffer to store read data * @param size read bytes size * * @return result */ EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size) { EfErrCode result = EF_NO_ERR; fal_partition_read(part, addr, (uint8_t *)buf, size); return result; } /** * Erase data on flash. * @note This operation is irreversible. * @note This operation's units is different which on many chips. * * @param addr flash address * @param size erase bytes size * * @return result */ EfErrCode ef_port_erase(uint32_t addr, size_t size) { EfErrCode result = EF_NO_ERR; /* make sure the start address is a multiple of FLASH_ERASE_MIN_SIZE */ EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0); if (fal_partition_erase(part, addr, size) < 0) { result = EF_ERASE_ERR; } return result; } /** * Write data to flash. * @note This operation's units is word. * @note This operation must after erase. @see flash_erase. * * @param addr flash address * @param buf the write data buffer * @param size write bytes size * * @return result */ EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size) { EfErrCode result = EF_NO_ERR; if (fal_partition_write(part, addr, (uint8_t *)buf, size) < 0) { result = EF_WRITE_ERR; } return result; } /** * lock the ENV ram cache */ void ef_port_env_lock(void) { rt_sem_take(&env_cache_lock, RT_WAITING_FOREVER); } /** * unlock the ENV ram cache */ void ef_port_env_unlock(void) { rt_sem_release(&env_cache_lock); } /** * This function is print flash debug info. * * @param file the file which has call this function * @param line the line number which has call this function * @param format output format * @param ... args * */ void ef_log_debug(const char *file, const long line, const char *format, ...) { #ifdef PRINT_DEBUG va_list args; /* args point to the first variable parameter */ va_start(args, format); ef_print("[Flash] (%s:%ld) ", file, line); /* must use vprintf to print */ rt_vsprintf(log_buf, format, args); ef_print("%s", log_buf); va_end(args); #endif } /** * This function is print flash routine info. * * @param format output format * @param ... args */ void ef_log_info(const char *format, ...) { va_list args; /* args point to the first variable parameter */ va_start(args, format); ef_print("[Flash] "); /* must use vprintf to print */ rt_vsprintf(log_buf, format, args); ef_print("%s", log_buf); va_end(args); } /** * This function is print flash non-package info. * * @param format output format * @param ... args */ void ef_print(const char *format, ...) { va_list args; /* args point to the first variable parameter */ va_start(args, format); /* must use vprintf to print */ rt_vsprintf(log_buf, format, args); rt_kprintf("%s", log_buf); va_end(args); }
/* * File : wifi_config.c * This file is part of RT-Thread RTOS * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * Change Logs: * Date Author Notes * 2018-09-04 ZeroFree first implementation * 2019-06-14 armink add easyflash v4.0 support */ #include <rtthread.h> #ifdef BSP_USING_WIFI #include <wlan_mgnt.h> #include <wlan_cfg.h> #include <wlan_prot.h> #include <easyflash.h> #include <fal.h> #include <stdio.h> #include <stdlib.h> #if (EF_SW_VERSION_NUM < 0x40000) static char *str_base64_encode_len(const void *src, char *out, int input_length); static int str_base64_decode(const char *data, int input_length, char *decoded_data); static const unsigned char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char base64_decode_table[256] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; static char *str_base64_encode_len(const void *src, char *out, int len) { unsigned char *pos; const unsigned char *end, *in; size_t olen; olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */ olen += olen / 72; /* line feeds */ olen++; /* nul termination */ end = (const unsigned char *)src + len; in = (const unsigned char *)src; pos = (unsigned char *)out; while (end - in >= 3) { *pos++ = base64_table[in[0] >> 2]; *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)]; *pos++ = base64_table[in[2] & 0x3f]; in += 3; } if (end - in) { *pos++ = base64_table[in[0] >> 2]; if (end - in == 1) { *pos++ = base64_table[(in[0] & 0x03) << 4]; *pos++ = '='; } else { *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; *pos++ = base64_table[(in[1] & 0x0f) << 2]; } *pos++ = '='; } *pos = '\0'; return (char *)out; } /* * return: length, 0 is error. */ static int str_base64_decode(const char *data, int input_length, char *decoded_data) { int out_len; int i, j; if (input_length % 4 != 0) return 0; out_len = input_length / 4 * 3; if (data[input_length - 1] == '=') out_len--; if (data[input_length - 2] == '=') out_len--; for (i = 0, j = 0; i < input_length;) { uint32_t sextet_a = data[i] == '=' ? 0 & i++ : base64_decode_table[data[i++]]; uint32_t sextet_b = data[i] == '=' ? 0 & i++ : base64_decode_table[data[i++]]; uint32_t sextet_c = data[i] == '=' ? 0 & i++ : base64_decode_table[data[i++]]; uint32_t sextet_d = data[i] == '=' ? 0 & i++ : base64_decode_table[data[i++]]; uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6); if (j < out_len) decoded_data[j++] = (triple >> 2 * 8) & 0xFF; if (j < out_len) decoded_data[j++] = (triple >> 1 * 8) & 0xFF; if (j < out_len) decoded_data[j++] = (triple >> 0 * 8) & 0xFF; } return out_len; } static int read_cfg(void *buff, int len) { char *wlan_cfg_info = RT_NULL; wlan_cfg_info = ef_get_env("wlan_cfg_info"); if (wlan_cfg_info != RT_NULL) { str_base64_decode(wlan_cfg_info, rt_strlen(wlan_cfg_info), buff); return len; } else { return 0; } } static int get_len(void) { int len; char *wlan_cfg_len = RT_NULL; wlan_cfg_len = ef_get_env("wlan_cfg_len"); if (wlan_cfg_len == RT_NULL) { len = 0; } else { len = atoi(wlan_cfg_len); } return len; } static int write_cfg(void *buff, int len) { char wlan_cfg_len[12] = {0}; char *base64_buf = RT_NULL; base64_buf = rt_malloc(len * 4 / 3 + 4); /* 3-byte blocks to 4-byte, and the end. */ if (base64_buf == RT_NULL) { return -RT_ENOMEM; } rt_memset(base64_buf, 0, len); /* interger to string */ sprintf(wlan_cfg_len, "%d", len); /* set and store the wlan config lengths to Env */ ef_set_env("wlan_cfg_len", wlan_cfg_len); str_base64_encode_len(buff, base64_buf, len); /* set and store the wlan config information to Env */ ef_set_env("wlan_cfg_info", base64_buf); ef_save_env(); rt_free(base64_buf); return len; } #else static int read_cfg(void *buff, int len) { size_t saved_len; ef_get_env_blob("wlan_cfg_info", buff, len, &saved_len); if (saved_len == 0) { return 0; } return len; } static int get_len(void) { int len; size_t saved_len; ef_get_env_blob("wlan_cfg_len", &len, sizeof(len), &saved_len); if (saved_len == 0) { return 0; } return len; } static int write_cfg(void *buff, int len) { /* set and store the wlan config lengths to Env */ ef_set_env_blob("wlan_cfg_len", &len, sizeof(len)); /* set and store the wlan config information to Env */ ef_set_env_blob("wlan_cfg_info", buff, len); return len; } #endif /* (EF_SW_VERSION_NUM < 0x40000) */ static const struct rt_wlan_cfg_ops ops = { read_cfg, get_len, write_cfg }; void wlan_autoconnect_init(void) { fal_init(); easyflash_init(); rt_wlan_cfg_set_ops(&ops); rt_wlan_cfg_cache_refresh(); } #endif

wifi_config.c。

在main函数中增加以下:

int main(void) { rt_uint32_t count = 1; rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT); wlan_autoconnect_init(); rt_wlan_config_autoreconnect(RT_TRUE); while(count++) { rt_thread_mdelay(500); rt_pin_write(LED_PIN, PIN_HIGH); rt_thread_mdelay(500); rt_pin_write(LED_PIN, PIN_LOW); } return RT_EOK; }

编译运行,再次使用join 指令之后就能正常保存了,复位之后可以自动联网。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询