ESP32使用按键配网并通过LED指示网络状态

2024-06-24 03:18

本文主要是介绍ESP32使用按键配网并通过LED指示网络状态,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

上面我们已经可以通过 ESPTOUCH 和 Airkiss 给模块配网,并且存储在 nvs 中,重启后仍然可以联网,只是这样仍然不能满足我们实际的应用,这次我们增加按键作为输入,LED作为输出,实现长按按键配网,并可以通过LED指示网络状态。

添加自己的组件

为了让程序结构更加清晰,所以我们在smart_config例程的基础上做了修改,在main文件夹里新建了main.c 、smartconfig_button.c , smartconfig_led.c ,将原来的smartconfig_main.c改为smartconfig_wifi.c,及其对应的.h文件

增加自己的组件
增加文件后,然后将 CMakeList.txt 文件修改为:

idf_component_register(SRCS “iot_button.c” “smartconfig_wifi.c” “smartconfig_button.c” “smartconfig_led.c” “main.c”
INCLUDE_DIRS “.”)

增加按键清除配网信息的功能

这里我们使用了乐鑫官方有个仓库叫做esp-iot-solution,里面有很多常用外设的驱动和物联网场景的实现代码。其中就有一个button模块来实现按键的长按、短按检测,我们将 botton 模块里的 iot_button.c 和 iot_button.h 文件添加到main文件夹中。
然后实现通过 smartconfig_button.c 文件实现长按清除网络信息的功能,具体如下:

//--------------- smartconfig_button.c ---------------//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "iot_button.h"
#include "smartconfig_button.h"
#include "smartconfig_wifi.h"#define BUTTON_IO_NUM           0   //GPIO0
#define BUTTON_ACTIVE_LEVEL     0   //信号有效电平:低电平static const char* TAG_BTN = "SMARTCONFIG_BUTTON";void button_tap_cb(void* arg)
{char* pstr = (char*) arg;pstr = pstr;ESP_LOGI(TAG_BTN, "key tap \n");
}void button_press_3s_cb(void* arg)
{char* pstr = (char*) arg;pstr = pstr;ESP_LOGI(TAG_BTN,"key 3s long press\n");                    //按键长按,清除配网信息并重启                  nvs_handle_t wificonfig_set_handle;ESP_ERROR_CHECK( nvs_open("wificonfig",NVS_READWRITE,&wificonfig_set_handle) );ESP_ERROR_CHECK( nvs_set_u8(wificonfig_set_handle,"WifiConfigFlag", wifi_unconfiged) );ESP_ERROR_CHECK( nvs_commit(wificonfig_set_handle) );nvs_close(wificonfig_set_handle);ESP_LOGI(TAG_BTN,"Set Restart now.\n");esp_restart();
}void smartconfig_button_init(void)
{//配置配网按键button_handle_t btn_handle = iot_button_create(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL);//注册单击事件iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, button_tap_cb, "TAP");//注册 3s 长按事件iot_button_add_custom_cb(btn_handle, 3, button_press_3s_cb, NULL);}

加入配网指示灯

灯的不同状态代表不同的配网状态,设定:慢闪代表未联网,快闪代表正在联网,长亮代表网络已连接。这里会用到ESP32的系统定时器,使用方法也比较简单,创建定时器,开启或者关闭定时器,源码如下:

//--------------- smartconfig_led.c ---------------//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "driver/gpio.h"
#include "esp_timer.h"
#include "esp_log.h"
#include "smartconfig_led.h"#define WIFI_STATUS_LED_GPIO  15static const char* TAG = "WIFI_STATUS_LED";
static esp_timer_handle_t  smartconfig_led_soft_timer;//定时器回调里实现灯的闪烁
static void periodled_timer_callback(void* arg)
{static uint8_t s_LEDToggle  = 0;s_LEDToggle = ~s_LEDToggle;if(s_LEDToggle){gpio_set_level(WIFI_STATUS_LED_GPIO, 1);}       else{gpio_set_level(WIFI_STATUS_LED_GPIO, 0);}   
}static void handle_smartconfig_led_status(Wifi_Status_t WifiState)
{static uint8_t s_WifiDisconnectNotice;switch(WifiState){case WIFI_DISCONNECT:if(s_WifiDisconnectNotice == 0){ESP_ERROR_CHECK(esp_timer_start_periodic(smartconfig_led_soft_timer, ConnectStatusInterval));s_WifiDisconnectNotice = 1;}                      break;case WIFI_CONNECTING://正在联网的时候LED快闪if(s_WifiDisconnectNotice == 1){ESP_ERROR_CHECK(esp_timer_stop(smartconfig_led_soft_timer));}ESP_ERROR_CHECK(esp_timer_start_periodic(smartconfig_led_soft_timer, DisconnectStatusInterval));break;case WIFI_CONNECTED://连上网后LED常亮,并注销软件定时器,减少消耗gpio_set_level(WIFI_STATUS_LED_GPIO, 1);if(s_WifiDisconnectNotice == 1){               ESP_ERROR_CHECK(esp_timer_stop(smartconfig_led_soft_timer));ESP_ERROR_CHECK(esp_timer_delete(smartconfig_led_soft_timer));s_WifiDisconnectNotice = 0;}           break;default:break;}
}//LED管脚初始化
void wifi_status_led_init(void)
{gpio_config_t smartconfig_IO_conf;smartconfig_IO_conf.intr_type = GPIO_PIN_INTR_DISABLE;smartconfig_IO_conf.mode = GPIO_MODE_OUTPUT;smartconfig_IO_conf.pin_bit_mask = 1 << WIFI_STATUS_LED_GPIO;smartconfig_IO_conf.pull_down_en = 0;smartconfig_IO_conf.pull_up_en = 0;gpio_config(&smartconfig_IO_conf); gpio_set_level(WIFI_STATUS_LED_GPIO, 0); esp_timer_create_args_t periodled_timer_args = {.callback = &periodled_timer_callback,/* name is optional, but may help identify the timer when debugging */.name = "periodled"};    ESP_ERROR_CHECK(esp_timer_create(&periodled_timer_args, &smartconfig_led_soft_timer));
}//通知网络未连接
void delegate_wifi_disconnect_status(void)
{ESP_LOGI(TAG,"Delegate wifi is disconnect\n"); handle_smartconfig_led_status(WIFI_DISCONNECT); }
//通知网络已连接
void delegate_wifi_connected_status(void)
{ESP_LOGI(TAG,"Delegate wifi has connected\n");handle_smartconfig_led_status(WIFI_CONNECTED);}
//通知网络正在连接中
void delegate_wifi_connecting_status(void)
{   ESP_LOGI(TAG,"Delegate wifi is connecting\n"); handle_smartconfig_led_status(WIFI_CONNECTING);   
}//--------------- smartconfig_led.h ---------------//
#ifndef __SMARTCONFIG_LED_H__
#define __SMARTCONFIG_LED_H__#ifdef __cplusplus
extern "C" {
#endif#define ConnectStatusInterval    1000000    //单位为us
#define DisconnectStatusInterval 200000typedef enum{WIFI_DISCONNECT = 1,WIFI_CONNECTING,WIFI_CONNECTED,
}Wifi_Status_t;void wifi_status_led_init(void);
void delegate_wifi_disconnect_status(void);
void delegate_wifi_connected_status(void);
void delegate_wifi_connecting_status(void);#ifdef __cplusplus
}
#endif#endif

主函数

删除 smartconfig_wifi.c 中的 app_main() 函数,在 main.c 中增加如下源码:

#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "smartconfig_button.h"
#include "smartconfig_wifi.h"
#include "smartconfig_led.h"void app_main(void)
{ESP_ERROR_CHECK( nvs_flash_init() );wifi_status_led_init();smartconfig_button_init();//initialise_wifi();check_wifi_config_in_nvs();
}

实验结果

编译下载程序后,长按 Boot 按键 3S后,就自动清除配网标志位,并自动重启后,重启后就等待配网,同时LED闪烁,等网络连接后,LED指示灯常亮。


纯手写文章,转载请注明出处,谢谢!
如有任何错误,欢迎留言指正!

这篇关于ESP32使用按键配网并通过LED指示网络状态的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1089030

相关文章

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面