工程目录:AiPi-Open-Kits/AiPi-Eyes_weather
文件目录:AiPi-Open-Kits/AiPi-Eyes_weather / main.c
使用指南
1.功能介绍
自动连接: AiPi-Eyes 天气站会自动连接历史连接过的WiFi
自动更新时间: 根据SNTP 服务器更新时间
自动更新天气情况: 通过HTTPS 获取天气情况
支持触摸配网: 可以利用键盘输出相应的SSID及PASSWORD 配置网络
支持未来七天的天气预报,默认显示三天
2.配网操作
点击 右上角的"WiFi"网络图标后,会切换到配网界面,在SSID 输入框中输入WiFi 名称,在PASS输入框中输入WiFi密码后,点击 "确定按钮"后可触发配网,配网成功后会自动切换到主页面。
main 工程代码:
--------------------------------------------------
/****************************************************************************
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files 加载各种头文件
****************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include <lwip/tcpip.h>
#include <lwip/sockets.h>
#include <lwip/netdb.h>
#include "sntp.h"
#include "bl_fw_api.h"
#include "wifi_mgmr_ext.h"
#include "wifi_mgmr.h"
#include "bflb_irq.h"
#include "bflb_uart.h"
#include "bflb_l1c.h"
#include "bflb_mtimer.h"
#include "bl616_glb.h"
#include "rfparam_adapter.h"
#include "board.h"
#define DBG_TAG "MAIN"
#include "log.h"
#include "lv_conf.h"
#include "lvgl.h"
#include "lv_port_disp.h"
#include "lv_port_indev.h"
#include "demos/lv_demos.h"
#include "lcd.h"
#include "portable.h"
//#include "fhost.h"
#include "gui_guider.h"
#include "custom.h"
#include "easyflash.h"
#include "bflb_mtd.h"
extern xQueueHandle queue;
/****************************************************************************
* Pre-processor Definitions 预处理器定义
****************************************************************************/
#define WIFI_STACK_SIZE (1536)
#define TASK_PRIORITY_FW (16)
/****************************************************************************
* Private Types 私有类型
****************************************************************************/
/****************************************************************************
* Private Data 专用数据
****************************************************************************/
static struct bflb_device_s* uart0;
#if 1
static TaskHandle_t wifi_fw_task;
static wifi_conf_t conf =
{
.country_code = "CN",
};
static uint32_t sta_ConnectStatus = 0;
extern void shell_init_with_task(struct bflb_device_s* shell);
extern int es8388_voice_init(void);
extern int audio_init(void);
extern int audio_pcm_init(void);
extern void play_voice_open(void);
extern void play_voice_close(void);
static int bl61x_get_heap_size(void);
/****************************************************************************
* Private Function Prototypes 私有函数原型
****************************************************************************/
/****************************************************************************
* Functions 函数
****************************************************************************/
int wifi_start_firmware_task(void) // wifi 开始固件任务
{
LOG_I("Starting wifi ...\r\n"); //输出 Starting wifi ...
/* enable wifi clock */ 启用wifi时钟
GLB_PER_Clock_UnGate(GLB_AHB_CLOCK_IP_WIFI_PHY | GLB_AHB_CLOCK_IP_WIFI_MAC_PHY | GLB_AHB_CLOCK_IP_WIFI_PLATFORM);
GLB_AHB_MCU_Software_Reset(GLB_AHB_MCU_SW_WIFI);
/* set ble controller EM Size */ 可设置控制器EM大小
GLB_Set_EM_Sel(GLB_WRAM160KB_EM0KB);
if (0 != rfparam_init(0, NULL, 0)) {
LOG_I("PHY RF init failed!\r\n");
return 0;
}
LOG_I("PHY RF init success!\r\n");
/* Enable wifi irq */ 启用wifi irq
extern void interrupt0_handler(void);
bflb_irq_attach(WIFI_IRQn, (irq_callback)interrupt0_handler, NULL);
bflb_irq_enable(WIFI_IRQn);
xTaskCreate(wifi_main, (char*)"fw", WIFI_STACK_SIZE, NULL, TASK_PRIORITY_FW, &wifi_fw_task);
return 0;
}
void wifi_event_handler(uint32_t code) //wifi事件处理程序
{
sta_ConnectStatus = code;
switch (code) {
case CODE_WIFI_ON_INIT_DONE:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_INIT_DONE\r\n", __func__);
wifi_mgmr_init(&conf);
}
break;
case CODE_WIFI_ON_MGMR_DONE:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_MGMR_DONE\r\n", __func__);
}
break;
case CODE_WIFI_ON_SCAN_DONE:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_SCAN_DONE\r\n", __func__);
wifi_mgmr_sta_scanlist();
}
break;
case CODE_WIFI_ON_CONNECTED:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_CONNECTED\r\n", __func__);
void mm_sec_keydump();
mm_sec_keydump();
}
break;
case CODE_WIFI_ON_GOT_IP:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_GOT_IP\r\n", __func__);
}
break;
case CODE_WIFI_ON_DISCONNECT:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_DISCONNECT\r\n", __func__);
}
break;
case CODE_WIFI_ON_AP_STARTED:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_AP_STARTED\r\n", __func__);
}
break;
case CODE_WIFI_ON_AP_STOPPED:
{
LOG_I("[APP] [EVT] %s, CODE_WIFI_ON_AP_STOPPED\r\n", __func__);
}
break;
case CODE_WIFI_ON_AP_STA_ADD:
{
LOG_I("[APP] [EVT] [AP] [ADD] %lld\r\n", xTaskGetTickCount());
}
break;
case CODE_WIFI_ON_AP_STA_DEL:
{
LOG_I("[APP] [EVT] [AP] [DEL] %lld\r\n", xTaskGetTickCount());
}
break;
default:
{
LOG_I("[APP] [EVT] Unknown code %u \r\n", code);
}
}
}
#endif
/* lvgl log cb */ lvgl日志cb
void lv_log_print_g_cb(const char* buf)
{
printf("[LVGL] %s", buf);
}
static TaskHandle_t lvgl_TaskHandle;
#define LVGL_STACK_SIZE 1024 //定义LVGL堆栈大小为1024
#define LVGL_TASK_PRIORITY 15 //定义LVGL任务优先级为15
void lvgl_task(void* param) //lvgl_任务
{
while (1) //死循环
{
lv_task_handler(); // lv任务处理程序
vTaskDelay(1); // 延时任务
// bflb_mtimer_delay_ms(1);
}
}
void io2_set(uint8_t value) // io2_设置
{
struct bflb_device_s* gpio;
gpio = bflb_device_get_by_name("gpio");
bflb_gpio_init(gpio, GPIO_PIN_2, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
if (value)
{
bflb_gpio_set(gpio, GPIO_PIN_2); // gpio2 置位 =1
}
else
{
bflb_gpio_reset(gpio, GPIO_PIN_2); // gpio2 复位 =0
}
}
uint8_t wifi_connect(char* ssid, char* passwd) // wifi_连接
{
int ret = 255;
// struct fhost_vif_ip_addr_cfg ip_cfg = { 0 };
uint32_t ipv4_addr = 0;
static char* queue_buff;
queue_buff = pvPortMalloc(128);
memset(queue_buff, 0, 128);
if (NULL==ssid || 0==strlen(ssid)) {
return 1;
}
if (wifi_mgmr_sta_state_get() == 1) {
wifi_sta_disconnect();
}
if (wifi_sta_connect(ssid, passwd, NULL, NULL, 0, 0, 0, 1)) {
return 4;
}
//等待连接成功
sta_ConnectStatus = 0;
for (int i = 0;i<10*30;i++) {
vTaskDelay(100);
switch (sta_ConnectStatus) {
case CODE_WIFI_ON_CONNECTED: //连接成功(表示wifi sta状态的时候表示同时获取IP(DHCP)成功,或者使用静态IP)
break;;
case CODE_WIFI_ON_DISCONNECT: //连接失败(超过了重连次数还没有连接成功的状态)
wifi_sta_disconnect();
return 4;
case CODE_WIFI_ON_GOT_IP:
wifi_sta_ip4_addr_get(&ipv4_addr, NULL, NULL, NULL);
printf("wifi connened %s,IP:%s\r\n", ssid, inet_ntoa(ipv4_addr));
sprintf(queue_buff, "{\"ip\":{\"IP\":\"%s\"}}", inet_ntoa(ipv4_addr));
flash_erase_set(SSID_KEY, ssid);
flash_erase_set(PASS_KEY, passwd);
xQueueSend(queue, queue_buff, portMAX_DELAY);
return 0;
default:
//等待连接成功
break;
}
}
vPortFree(queue_buff);
return 14; //连接超时
}
void lwip_sntp_init(void) // lwip_sntp_初始化 sntp:简单网络时间协议
{
sntp_setoperatingmode(SNTP_OPMODE_POLL);
// sntp_setserver();
// sntp_setservername(0, "pool.ntp.org");
sntp_setservername(0, "ntp1.aliyun.com"); //连接阿里云
// sntp_setservername(0, "114.67.237.130");
sntp_init(); // 同步时间
printf("getservername:%s\r\n", sntp_getservername(0));
}
static void bl61x_show_heap_size_task(void* arg) // bl61x显示堆大小任务
{
while (1) {
LOG_F("[%s:%d]heap_size-------> %d\r\n", DBG_TAG, __LINE__, bl61x_get_heap_size());
vTaskDelay(3000/portTICK_PERIOD_MS);
}
}
lv_ui guider_ui; // 指南_用户界面
int main(void) //主程序
{
board_init(); //初始化开发板
// audio_init();
// audio_pcm_init();
// es8388_voice_init();
// uart0 = bflb_device_get_by_name("uart0");
// shell_init_with_task(uart0);
// printf("HeapSize:%d\r\n",xPortGetFreeHeapSize());
tcpip_init(NULL, NULL); // 初始化 tcpip
wifi_start_firmware_task();
lwip_sntp_init();
//init easyflash
bflb_mtd_init();
easyflash_init();
/* lvgl init */
lv_log_register_print_cb(lv_log_print_g_cb);
lv_init();
lv_port_disp_init(); //lv 显示端口初始化
lv_port_indev_init();
setup_ui(&guider_ui);
printf("lv_task_handler\r\n");
// lv_task_handler();
custom_init(&guider_ui);
printf("lvgl success\r\n");
xTaskCreate(lvgl_task, (char*)"lvgl", LVGL_STACK_SIZE, NULL, LVGL_TASK_PRIORITY, &lvgl_TaskHandle);
xTaskCreate(bl61x_show_heap_size_task, (char*)"heap", 1024, NULL, 2, NULL);
// printf("HeapSize:%d\r\n",xPortGetFreeHeapSize());
vTaskStartScheduler(); // v任务启动计划程序
// while (1) {
// lv_task_handler();
// bflb_mtimer_delay_ms(1);
// }
while (1) {
// printf("\r\n[%s] heap size ------------------- %d\r\n", __func__, xPortGetFreeHeapSize());
vTaskDelay(5000/portTICK_RATE_MS);
}
}
static int bl61x_get_heap_size(void) // bl61x获取堆大小
{
struct meminfo info1 = { 0 };
struct meminfo info2 = { 0 };
uint32_t total_free_size = 0;
// return xPortGetFreeHeapSize();
bflb_mem_usage(KMEM_HEAP, &info1);
bflb_mem_usage(PMEM_HEAP, &info2);
total_free_size = info1.free_size + info2.free_size;
return total_free_size;
}
|