Skip to content

Commit

Permalink
2019-12-09 add the blufi utils
Browse files Browse the repository at this point in the history
2019-12-09 add the blufi utils
  • Loading branch information
xuhongv committed Dec 9, 2019
1 parent d924370 commit ca5b1d7
Show file tree
Hide file tree
Showing 9 changed files with 2,433 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 15_XBlufi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

#If IOT_SOLUTION_PATH is not defined, use relative path as default value
set(IOT_SOLUTION_PATH "$ENV{IOT_SOLUTION_PATH}")
if(NOT IOT_SOLUTION_PATH)
# Documentation says you should set IOT_SOLUTION_PATH in your environment, but we
# can infer it relative to PROJECT_PATH directory if it's not set.
get_filename_component(IOT_SOLUTION_PATH "${PROJECT_PATH}/../.." ABSOLUTE)
endif()
file(TO_CMAKE_PATH "${IOT_SOLUTION_PATH}" IOT_SOLUTION_PATH)
set(ENV{IOT_SOLUTION_PATH} ${IOT_SOLUTION_PATH})

include($ENV{IOT_SOLUTION_PATH}/CMakeLists.txt)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)

project(empty-project)
11 changes: 11 additions & 0 deletions 15_XBlufi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := empty_project
#If IOT_SOLUTION_PATH is not defined, use relative path as default value
IOT_SOLUTION_PATH ?= $(abspath $(shell pwd)/../../)

include $(IOT_SOLUTION_PATH)/Makefile
include $(IDF_PATH)/make/project.mk
16 changes: 16 additions & 0 deletions 15_XBlufi/components/xBlufi/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#
COMPONENT_ADD_INCLUDEDIRS += include

LIB_PATH := $(COMPONENT_PATH)/lib/libxBlufi.a

COMPONENT_ADD_LDFLAGS := $(LIB_PATH) \

COMPONENT_ADD_LINKER_DEPS := $(LIB_PATH)

95 changes: 95 additions & 0 deletions 15_XBlufi/components/xBlufi/include/xBlufi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* @Description: wechat xBlufi to net
* @Author: 徐宏 xuhong
* @Date: 2019-10-03 16:36:21
* @LastEditTime: 2019-12-09 19:29:46
* @LastEditors: Please set LastEditors
*/
#ifndef X_xBlufi_H_
#define X_xBlufi_H_

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "string.h"

typedef enum
{
xBlufi_MSG_TYPE_SATRT = 0, // 开始
xBlufi_MSG_TYPE_GET_BSSID, // 成功获取路由器mac地址
xBlufi_MSG_TYPE_GET_SSID, // 成功获取路由器名字
xBlufi_MSG_TYPE_GET_PASSWORD, // 成功获取路由器密码
xBlufi_MSG_TYPE_REQ_CONNECT_TO_AP, //客户端要求连接热点
xBlufi_MSG_TYPE_REQ_DISCONNECT_TO_AP, //客户端要求断开连接热点

xBlufi_MSG_TYPE_RECIEVE_CUSTON_DATA, // 接收到自定义数据
xBlufi_MSG_TYPE_SEND_ACK_TO_WEICHAT_OVER, // 配网成功后,设备端发送配网成功的ack到微信端
xBlufi_MSG_TYPE_CONNECTED_BLE, // 与蓝牙客户端成连接
xBlufi_MSG_TYPE_DIS_CONNECT_BLE, // 与蓝牙客户端断开连接

xBlufi_MSG_TYPE_NOTIFY_CONNECT_ACK, // 通知蓝牙客户端已成功连接路由器

} xBlufiMsgType;

typedef struct
{
char data[100];
uint16_t dataLen;

xBlufiMsgType type;
} xBlufiMsg;

/**
* @description: 开始 esp32 配置
* @param {type}
* @return:
*/
esp_err_t xBlufi_start();

/**
* @description:
* @param {type}
* @return:
*/
void XBlufi_notify_got_ip(void);

/**
* @description:
* @param {type}
* @return:
*/
void XBlufi_notify_connected(uint8_t *bssid);

/**
* @description: 结束 esp32 微信配网配置和近场发现
* @param {type}
* @return:
*/
esp_err_t xBlufi_stop_all();
/**
* @description: 主动监听事件
* @param {type}
* @return:
*/
esp_err_t xBlufiReceiveMsg(xBlufiMsg *msg);

/**
* @description:
* @param {type}
* @return:
*/
esp_err_t XBlufi_notify_send_custom_data(uint8_t *data, uint32_t len);

/**
* @description:
* @param {type}
* @return:
*/
void XBlufi_notify_set_blufi_name(char *name, uint8_t len);

#endif /* X_xBlufi_H_ */
6 changes: 6 additions & 0 deletions 15_XBlufi/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

set(COMPONENT_SRCS "main.c")

set(COMPONENT_ADD_INCLUDEDIRS ".")

register_component()
5 changes: 5 additions & 0 deletions 15_XBlufi/main/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

178 changes: 178 additions & 0 deletions 15_XBlufi/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* @Author: your name
* @Date: 2019-11-26 11:40:54
* @LastEditTime: 2019-12-09 19:36:01
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \esp-iot-solution\examples\empty_project\main\main.c
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include <stdio.h>
#include "freertos/event_groups.h"
#include "freertos/semphr.h"
#include "nvs_flash.h"
#include "esp_wifi.h"
#include "esp_bt.h"
#include "esp_blufi_api.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_err.h"
#include "nvs.h"
#include "xBlufi.h"
#include "esp_bt_device.h"

static const char *TAG = "iot_blufi_test";
static EventGroupHandle_t s_wifi_event_group;
static void TaskXBlufiListener(void *parm);

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
//wifi_mode_t mode;
ESP_LOGD(TAG, "system_event_handler, event_id: %d", event->event_id);

switch (event->event_id)
{
case SYSTEM_EVENT_STA_START:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
esp_wifi_connect();
break;

case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
XBlufi_notify_got_ip();
break;
case SYSTEM_EVENT_STA_CONNECTED:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_CONNECTED");
XBlufi_notify_connected(event->event_info.connected.bssid);
break;

case SYSTEM_EVENT_STA_DISCONNECTED:
{
system_event_sta_disconnected_t *disconnected = &event->event_info.disconnected;
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED, reason: %d", disconnected->reason);
esp_wifi_connect();
break;
}

default:
break;
}

return ESP_OK;
}

static void initialise_wifi(void)
{
tcpip_adapter_init();
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
xTaskCreate(TaskXBlufiListener, "TaskXBlufiListener", 1024 * 3, NULL, 8, NULL); // 创建任务
}

static void TaskXBlufiListener(void *parm)
{
xBlufi_start();
xBlufiMsg msg;
wifi_config_t wifi_config;
char blufiName[16];
bzero(&wifi_config, sizeof(wifi_config_t));
while (1)
{
if (xBlufiReceiveMsg(&msg))
{
ESP_LOGI(TAG, "xAirKissReceiveMsg %d", msg.type);
switch (msg.type)
{
case xBlufi_MSG_TYPE_NOTIFY_CONNECT_ACK:
//停止,释放内存
// xBlufi_stop_all();
// vTaskDelete(NULL);
break;
case xBlufi_MSG_TYPE_SATRT:
ESP_LOGI(TAG, "BLUFI deinit finish");
//初始化后设置任意蓝牙名字,不能大于16个字节
const uint8_t *addr = esp_bt_dev_get_address(); //查询蓝牙的mac地址,务必要在蓝牙初始化后方可调用!
sprintf(blufiName, "BLUFI_%02x%02x%02x", addr[0], addr[2], addr[5]);
XBlufi_notify_set_blufi_name(blufiName, strlen(blufiName));
break;
case xBlufi_MSG_TYPE_GET_BSSID: // 成功获取路由器mac地址
ESP_LOGI(TAG, " get bssid[len:%d]: %s", msg.dataLen, (char *)msg.data);
break;
case xBlufi_MSG_TYPE_GET_SSID: // 成功获取路由器名字
ESP_LOGI(TAG, " get ssid[len:%d]: %s", msg.dataLen, (char *)msg.data);
memcpy(wifi_config.sta.ssid, (char *)msg.data, msg.dataLen);
break;
case xBlufi_MSG_TYPE_GET_PASSWORD: // 成功获取路由器密码
ESP_LOGI(TAG, " get password[len:%d]: %s", msg.dataLen, (char *)msg.data);
memcpy(wifi_config.sta.password, (char *)msg.data, msg.dataLen);
break;
case xBlufi_MSG_TYPE_RECIEVE_CUSTON_DATA: //接收到了自定义的数据
{
ESP_LOGI(TAG, " get custon data[len:%d]: %s", msg.dataLen, (char *)msg.data);
//把接收到的自定义数据,原封不住返回回去给手机
XBlufi_notify_send_custom_data((uint8_t *)msg.data, msg.dataLen);
//如果发送是 stop 则断开连接
if (strcmp("stop", (char *)msg.data) == 0)
{
xBlufi_stop_all();
vTaskDelete(NULL);
}
break;
}

case xBlufi_MSG_TYPE_REQ_CONNECT_TO_AP: //手机要求接入热点
//连接路由器
ESP_ERROR_CHECK(esp_wifi_disconnect());
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_connect());
break;
case xBlufi_MSG_TYPE_REQ_DISCONNECT_TO_AP: //手机要求断开当前热点的接入
esp_wifi_disconnect();
break;
default:
break;
}
}
}

vTaskDelete(NULL);
}

/**
* @description: 程序入口
* @param {type}
* @return:
*/
void app_main()
{

//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);

printf("\n\n-------------------------------- Get Systrm Info------------------------------------------\n");
//获取IDF版本
printf(" SDK version:%s\n", esp_get_idf_version());
//获取芯片可用内存
printf(" esp_get_free_heap_size : %d \n", esp_get_free_heap_size());
//获取从未使用过的最小内存
printf(" esp_get_minimum_free_heap_size : %d \n", esp_get_minimum_free_heap_size());
//获取mac地址(station模式)
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_WIFI_STA);
printf("esp_read_mac(): %02x:%02x:%02x:%02x:%02x:%02x \n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("--------------------------------------------------------------------------\n\n");

initialise_wifi();
}
Loading

0 comments on commit ca5b1d7

Please sign in to comment.