Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
RT200T_ESP32
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
RT200T
RT200T_ESP32
Commits
12945184
Commit
12945184
authored
May 27, 2024
by
李茂军
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:添加hhttp文件
parent
4a77af30
Changes
19
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
1416 additions
and
102 deletions
+1416
-102
Dockerfile
RT200T_2_ESP32/.devcontainer/Dockerfile
+47
-0
devcontainer.json
RT200T_2_ESP32/.devcontainer/devcontainer.json
+45
-0
CMakeLists.txt
RT200T_2_ESP32/CMakeLists.txt
+2
-2
CMakeLists.txt
RT200T_2_ESP32/main/CMakeLists.txt
+1
-0
Protocol_User.c
RT200T_2_ESP32/main/Protocol_User.c
+29
-0
main_user.c
RT200T_2_ESP32/main/main_user.c
+18
-9
sdkconfig
RT200T_2_ESP32/sdkconfig
+290
-91
CMakeLists.txt
RT200T_2_ESP32/source/wifi/CMakeLists.txt
+4
-0
favicon.ico
RT200T_2_ESP32/source/wifi/favicon.ico
+0
-0
favicon.ico
RT200T_2_ESP32/source/wifi/html/favicon.ico
+0
-0
index.html
RT200T_2_ESP32/source/wifi/html/index.html
+20
-0
ota.html
RT200T_2_ESP32/source/wifi/html/ota.html
+103
-0
http_server.c
RT200T_2_ESP32/source/wifi/http_server.c
+386
-0
http_server.h
RT200T_2_ESP32/source/wifi/http_server.h
+30
-0
ota_page.html
RT200T_2_ESP32/source/wifi/ota_page.html
+119
-0
softap.c
RT200T_2_ESP32/source/wifi/softap.c
+145
-0
softap.h
RT200T_2_ESP32/source/wifi/softap.h
+24
-0
wifi_service.c
RT200T_2_ESP32/source/wifi/wifi_service.c
+123
-0
wifi_service.h
RT200T_2_ESP32/source/wifi/wifi_service.h
+30
-0
No files found.
RT200T_2_ESP32/.devcontainer/Dockerfile
0 → 100644
View file @
12945184
FROM
espressif/idf
ARG
DEBIAN_FRONTEND=nointeractive
ARG
CONTAINER_USER=esp
ARG
USER_UID=1000
ARG
USER_GID=$USER_UID
RUN
apt-get update
\
&&
apt
install
-y
-q
\
cmake
\
git
\
libglib2.0-0
\
libnuma1
\
libpixman-1-0
\
&&
rm
-rf
/var/lib/apt/lists/
*
# QEMU
ENV
QEMU_REL=esp_develop_8.2.0_20240122
ENV
QEMU_SHA256=e7c72ef5705ad1444d391711088c8717fc89f42e9bf6d1487f9c2a326b8cfa83
ENV
QEMU_DIST=qemu-xtensa-softmmu-${QEMU_REL}-x86_64-linux-gnu.tar.xz
ENV
QEMU_URL=https://github.com/espressif/qemu/releases/download/esp-develop-8.2.0-20240122/${QEMU_DIST}
ENV
LC_ALL=C.UTF-8
ENV
LANG=C.UTF-8
RUN
wget
--no-verbose
${
QEMU_URL
}
\
&&
echo
"
${
QEMU_SHA256
}
*
${
QEMU_DIST
}
"
|
sha256sum
--check
--strict
-
\
&&
tar
-xf
$QEMU_DIST
-C
/opt
\
&&
rm
${
QEMU_DIST
}
ENV
PATH=/opt/qemu/bin:${PATH}
RUN
groupadd
--gid
$USER_GID
$CONTAINER_USER
\
&&
adduser
--uid
$USER_UID
--gid
$USER_GID
--disabled-password
--gecos
""
${
CONTAINER_USER
}
\
&&
usermod
-a
-G
root
$CONTAINER_USER
&&
usermod
-a
-G
dialout
$CONTAINER_USER
RUN
chmod
-R
775 /opt/esp/python_env/
USER
${CONTAINER_USER}
ENV
USER=${CONTAINER_USER}
WORKDIR
/home/${CONTAINER_USER}
RUN
echo
"source /opt/esp/idf/export.sh > /dev/null 2>&1"
>>
~/.bashrc
ENTRYPOINT
[ "/opt/esp/entrypoint.sh" ]
CMD
["/bin/bash", "-c"]
\ No newline at end of file
RT200T_2_ESP32/.devcontainer/devcontainer.json
0 → 100644
View file @
12945184
//
For
format
details
,
see
https
:
//aka.ms/devcontainer.json.
For
config
options
,
see
the
README
at
:
//
https
:
//github.com/microsoft/vscode-dev-containers/tree/v
0.183
.
0
/containers/ubuntu
{
"name"
:
"ESP-IDF QEMU"
,
"build"
:
{
"dockerfile"
:
"Dockerfile"
},
//
Add
the
IDs
of
extensions
you
want
installed
when
the
container
is
created
"workspaceMount"
:
"source=${localWorkspaceFolder},target=${localWorkspaceFolder},type=bind"
,
/*
the
path
of
workspace
folder
to
be
opened
after
container
is
running
*/
"workspaceFolder"
:
"${localWorkspaceFolder}"
,
"mounts"
:
[
"source=extensionCache,target=/root/.vscode-server/extensions,type=volume"
],
"customizations"
:
{
"vscode"
:
{
"settings"
:
{
"terminal.integrated.defaultProfile.linux"
:
"bash"
,
"idf.espIdfPath"
:
"/opt/esp/idf"
,
"idf.customExtraPaths"
:
""
,
"idf.pythonBinPath"
:
"/opt/esp/python_env/idf5.3_py3.10_env/bin/python"
,
"idf.toolsPath"
:
"/opt/esp"
,
"idf.gitPath"
:
"/usr/bin/git"
},
"extensions"
:
[
"espressif.esp-idf-extension"
],
},
"codespaces"
:
{
"settings"
:
{
"terminal.integrated.defaultProfile.linux"
:
"bash"
,
"idf.espIdfPath"
:
"/opt/esp/idf"
,
"idf.customExtraPaths"
:
""
,
"idf.pythonBinPath"
:
"/opt/esp/python_env/idf5.3_py3.10_env/bin/python"
,
"idf.toolsPath"
:
"/opt/esp"
,
"idf.gitPath"
:
"/usr/bin/git"
},
"extensions"
:
[
"espressif.esp-idf-extension"
],
}
},
"runArgs"
:
[
"--privileged"
]
}
\ No newline at end of file
RT200T_2_ESP32/CMakeLists.txt
View file @
12945184
...
...
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.16)
# (Not part of the boilerplate)
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
set
(
EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common
)
set
(
EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common
"./source/wifi"
)
include
(
$ENV{IDF_PATH}/tools/cmake/project.cmake
)
project
(
QJ500-7C
)
project
(
RT200T-2
)
RT200T_2_ESP32/main/CMakeLists.txt
View file @
12945184
...
...
@@ -8,5 +8,6 @@ idf_component_register(SRCS "app_BT_User.c" "bt_app_main.c" "bt_app_hf.c" "bt_ap
"MCU_Core_Protocol.c"
"app_Ble_User.c"
"main_user.c"
"../source/wifi"
INCLUDE_DIRS
"."
EMBED_TXTFILES
${
project_dir
}
/server_certs/ca_cert.pem
)
RT200T_2_ESP32/main/Protocol_User.c
View file @
12945184
...
...
@@ -383,3 +383,32 @@ void Prot_Send_Msg_Process(void )
}
}
}
void
SetUpgradeStart
(
void
)
{
// uint32_t fills;
// G031toBoot();
// vTaskDelay(pdMS_TO_TICKS(100));
// G031tReset();
// vTaskDelay(pdMS_TO_TICKS(200));
// TaskSchOnUpdateNAT32G031();//关闭任务
// fills=temp_Size%16;
// if (fills!=0)
// {
// memset(&_acN32G031dat[temp_Size],0xFF,16-fills);
// Flash_Size+=(16-fills);//下载数据需要对齐16字节
// }
// UpgradeStep=0;
}
void
SetUpgradeFlashSize
(
uint32_t
size
)
{
// Flash_Size=size;
// temp_Size=0;
}
void
Cache_data
(
unsigned
char
*
p
,
uint32_t
len
)
{
// memcpy(&_acN32G031dat[temp_Size],p,len);
// temp_Size+=len;
}
RT200T_2_ESP32/main/main_user.c
View file @
12945184
...
...
@@ -28,7 +28,7 @@
#include "app_BT_User.h"
#include "bt_app_hf.h"
#include "wifi_service.h"
#define TAG "MAIN_USER"
extern
esp_bd_addr_t
peer_addr
;
...
...
@@ -122,20 +122,29 @@ static void Sys_Run_Tasks(void *arg)
}
vTaskDelete
(
NULL
);
}
TaskHandle_t
wifi_service_hdl
;
static
void
wifi_service_task
(
void
*
arg
)
{
while
(
1
)
{
wifi_service_timer
();
wifi_service
();
vTaskDelay
(
pdMS_TO_TICKS
(
100
));
}
}
void
app_main
(
void
)
{
Protocol_User_Ctrl_Init
();
BT_User_Init
();
Ble_User_Init
();
Get_Mac_Init
();
bsp_BT_Init
();
//
BT_User_Init();
//
Ble_User_Init();
//
Get_Mac_Init();
//
bsp_BT_Init();
// bsp_Ble_Init();
bsp_Uart_Init
();
printf
(
"KL30 INIT OVER~~~~~~
\r\n
"
);
xTaskCreatePinnedToCore
(
Sys_Run_Tasks
,
"Sys_Run_Tasks"
,
4096
,
NULL
,
3
,
NULL
,
1
);
//
printf("KL30 INIT OVER~~~~~~\r\n");
// xTaskCreatePinnedToCore(Sys_Run_Tasks, "Sys_Run_Tasks", 4096, NULL, 3, NULL, 1);
xTaskCreatePinnedToCore
(
wifi_service_task
,
"wifi_service_task"
,
4096
,
NULL
,
5
,
&
wifi_service_hdl
,
1
);
}
...
...
RT200T_2_ESP32/sdkconfig
View file @
12945184
This diff is collapsed.
Click to expand it.
RT200T_2_ESP32/source/wifi/CMakeLists.txt
0 → 100644
View file @
12945184
idf_component_register
(
SRCS
"wifi_service.c"
"softap.c"
"http_server.c"
INCLUDE_DIRS
"."
"../../main"
PRIV_REQUIRES nvs_flash esp_netif esp_wifi esp_http_server app_update driver
EMBED_FILES
"favicon.ico"
"ota_page.html"
)
RT200T_2_ESP32/source/wifi/favicon.ico
0 → 100644
View file @
12945184
6.28 KB
RT200T_2_ESP32/source/wifi/html/favicon.ico
0 → 100644
View file @
12945184
6.28 KB
RT200T_2_ESP32/source/wifi/html/index.html
0 → 100644
View file @
12945184
<!DOCTYPE html>
<html
lang=
"zh-CN"
>
<head>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1"
charset=
"utf-8"
>
<title>
ESP32
</title>
</head>
<body>
<h2>
ESP32
</h2>
<hr><br>
<button
id=
"ota"
type=
"button"
onclick=
"ota()"
>
ESP32 固件升级
</button>
<script>
function
ota
()
{
window
.
location
.
href
=
"http://192.168.4.1/ota"
;
}
</script>
</body>
</html>
\ No newline at end of file
RT200T_2_ESP32/source/wifi/html/ota.html
0 → 100644
View file @
12945184
<!DOCTYPE html>
<html
lang=
"zh-CN"
>
<head>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1"
charset=
"utf-8"
>
<title>
固件升级
</title>
</head>
<body>
<h2>
固件升级
</h2>
<hr>
<a
href=
"http://192.168.4.1"
style=
"font-size: smaller;"
>
[返回首页]
</a><br><br>
<!-- 选择文本按钮 -->
<label
for=
"newFile"
>
加载文件:
</label>
<input
id=
"newFile"
type=
"file"
accept=
".bin"
onchange=
"checkFile()"
>
<br><br>
<!-- 上传操作 -->
<label
for=
"otaStart"
>
执行操作:
</label>
<button
id=
"otaStart"
type=
"button"
onclick=
"otaStart()"
>
开始升级
</button>
<button
id=
"reload"
type=
"button"
onclick=
"reload()"
>
刷新界面
</button>
<br><br>
<!-- 进度显示 -->
<label
for=
"otaPercent"
>
进度显示:
</label>
<input
id=
"otaPercent"
type=
"text"
value=
"0.00%"
style=
"width: 75px;border: none;font-size:large;"
>
<progress
id=
"otaProgress"
value=
"0"
max=
"100"
></progress>
<br><br><hr>
<p
id=
"viewName"
></p>
<p
id=
"viewSize"
></p>
<p
id=
"viewFlashStart"
></p>
<p
id=
"viewFlashResult"
></p>
<script>
function
reload
()
{
location
.
reload
();
}
function
getFileSize
(
size
)
{
if
(
!
size
)
return
""
;
var
num
=
1024.00
;
if
(
size
<
num
)
{
return
size
+
"B"
;
}
if
(
size
<
Math
.
pow
(
num
,
2
))
{
return
(
size
/
num
).
toFixed
(
2
)
+
"KB"
;
}
if
(
size
<
Math
.
pow
(
num
,
3
))
{
return
(
size
/
Math
.
pow
(
num
,
2
)).
toFixed
(
2
)
+
"MB"
;
}
}
function
checkFile
()
{
var
newFile
=
document
.
getElementById
(
"newFile"
).
files
[
0
];
var
newFileName
=
newFile
.
name
;
var
newFileSize
=
newFile
.
size
;
if
(
newFileSize
>
2
*
1024
*
1024
)
{
alert
(
'所选文件超出最大限制
\
n点击确定后将自动刷新页面'
);
location
.
reload
();
}
else
{
document
.
getElementById
(
"viewName"
).
innerHTML
=
"文件名称:"
+
newFileName
;
document
.
getElementById
(
"viewSize"
).
innerHTML
=
"文件大小:"
+
getFileSize
(
newFileSize
);
}
}
function
otaStart
()
{
var
res
=
document
.
getElementById
(
"newFile"
).
files
[
0
];
if
(
typeof
(
res
)
==
"undefined"
)
{
alert
(
'未选择文件!'
);
}
else
{
var
filepath
=
document
.
getElementById
(
"newFile"
).
files
[
0
].
name
;
var
upload_path
=
"/ota/"
+
filepath
;
document
.
getElementById
(
"viewFlashStart"
).
innerHTML
=
"锁定页面,请务必保持WiFi在线,正在上传..."
;
document
.
getElementById
(
"newFile"
).
disabled
=
true
;
document
.
getElementById
(
"otaStart"
).
disabled
=
true
;
var
file
=
document
.
getElementById
(
"newFile"
).
files
[
0
];
var
xhttp
=
new
XMLHttpRequest
();
xhttp
.
onreadystatechange
=
function
()
{
if
(
xhttp
.
readyState
==
4
)
{
if
(
xhttp
.
status
==
200
)
{
document
.
getElementById
(
"viewFlashResult"
).
innerHTML
=
"上传成功!"
;
}
else
if
(
xhttp
.
status
==
0
)
{
document
.
getElementById
(
"viewFlashResult"
).
innerHTML
=
"服务器断开!<br><br>请检查服务器是否在线,刷新页面后重新上传..."
;
}
else
{
alert
(
xhttp
.
status
+
" Error!
\n
"
+
xhttp
.
responseText
);
}
}
};
let
progressBar
=
document
.
getElementById
(
"otaProgress"
);
xhttp
.
upload
.
onprogress
=
function
(
e
)
{
var
percentage
=
(
event
.
loaded
*
100.00
)
/
(
event
.
total
*
1.00
);
percentage
=
percentage
.
toFixed
(
2
);
progressBar
.
value
=
Math
.
round
(
percentage
);
document
.
getElementById
(
"otaPercent"
).
value
=
percentage
+
"%"
;
}
xhttp
.
open
(
"POST"
,
upload_path
,
true
);
xhttp
.
send
(
file
);
}
}
</script>
</body>
</html>
\ No newline at end of file
RT200T_2_ESP32/source/wifi/http_server.c
0 → 100644
View file @
12945184
This diff is collapsed.
Click to expand it.
RT200T_2_ESP32/source/wifi/http_server.h
0 → 100644
View file @
12945184
#ifndef _GTTP_SERVER_H_
#define _GTTP_SERVER_H_
#include "esp_event.h"
#include "esp_http_server.h"
#define HTTP_SERVER_STOP 0
#define HTTP_SERVER_INIT 1
#define HTTP_SERVER_START 2
#define HTTP_SERVER_DEINIT 3
#define MAX_OTA_BUFF 4096u
extern
char
buf
[
MAX_OTA_BUFF
];
extern
httpd_handle_t
http_server
;
void
set_softap_ota_status
(
unsigned
char
status
);
unsigned
char
get_softap_ota_status
(
void
);
void
connect_handler
(
void
*
arg
);
void
disconnect_handler
(
void
*
arg
);
httpd_handle_t
http_service
(
void
);
extern
void
delele_tasks
(
void
);
extern
void
Catch_data
(
unsigned
char
*
p
,
uint32_t
len
);
extern
void
SetUpgradeFlashSize
(
uint32_t
size
);
extern
void
SetUpgradeStart
(
void
);
// ʼ״̬
void
Nat32G031_ReqReset
(
void
);
#endif
RT200T_2_ESP32/source/wifi/ota_page.html
0 → 100644
View file @
12945184
<!DOCTYPE html>
<html
lang=
"zh-CN"
>
<head>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1"
charset=
"utf-8"
>
<title>
OTA Page
</title>
</head>
<body>
<h2>
OTA Page
</h2>
<hr>
<!-- 下拉菜单 -->
<label
for=
"selectPartition"
>
目标分区:
</label>
<select
id=
"selectPartition"
name=
"selectPartition"
>
<option
selected
value=
"RT200T-2.bin"
>
Application
</option>
<option
value=
"mmap_assert.bin"
>
Pictures
</option>
<option
value=
"storage.bin"
>
Fonts
</option>
<option
value=
"BAT32G139.bin"
>
Nation
</option>
</select>
<br><br>
<!-- 选择文本按钮 -->
<label
for=
"newfile"
>
目标文件:
</label>
<input
id=
"newfile"
type=
"file"
accept=
".bin"
onchange=
"checkFile()"
>
<br><br>
<!-- 上传操作 -->
<label
for=
"upload"
>
执行操作:
</label>
<button
id=
"upload"
type=
"button"
onclick=
"upload()"
>
开始上传
</button>
<button
id=
"home"
type=
"button"
onclick=
"homePage()"
>
重置页面
</button>
<br><br>
<!-- 进度显示 -->
<label
for=
"uploadPercent"
>
进度显示:
</label>
<input
id=
"uploadPercent"
type=
"text"
value=
"0.00%"
style=
"width: 75px;border: none;font-size:large;"
>
<progress
id=
"uploadProgress"
value=
"0"
max=
"100"
></progress>
<hr>
<p
id=
"view_name"
></p>
<p
id=
"view_size"
></p>
<p
id=
"view_flash_start"
></p>
<p
id=
"view_flash_result"
></p>
<script>
function
homePage
()
{
location
.
reload
();
}
function
getfilesize
(
size
)
{
if
(
!
size
)
return
""
;
var
num
=
1024.00
;
if
(
size
<
num
)
{
return
size
+
"B"
;
}
if
(
size
<
Math
.
pow
(
num
,
2
))
{
return
(
size
/
num
).
toFixed
(
2
)
+
"KB"
;
}
if
(
size
<
Math
.
pow
(
num
,
3
))
{
return
(
size
/
Math
.
pow
(
num
,
2
)).
toFixed
(
2
)
+
"MB"
;
}
}
function
checkFile
()
{
var
currentPartition
=
document
.
getElementById
(
"selectPartition"
).
value
;
var
newFile
=
document
.
getElementById
(
"newfile"
).
files
[
0
];
var
newFileName
=
newFile
.
name
;
var
newFileSize
=
newFile
.
size
;
filepath
=
newFileName
;
if
(
newFileName
!=
currentPartition
)
{
alert
(
'所选文件与目标分区不匹配
\
n点击确定后将自动刷新页面
\
n'
);
homePage
();
}
if
(
newFileSize
>
16
*
1024
*
1024
)
{
alert
(
'所选文件超出最大限制
\
n点击确定后将自动刷新页面'
);
homePage
();
}
else
{
document
.
getElementById
(
"view_name"
).
innerHTML
=
"文件名称:"
+
newFileName
;
document
.
getElementById
(
"view_size"
).
innerHTML
=
"文件大小:"
+
getfilesize
(
newFileSize
);
}
}
function
upload
()
{
var
res
=
document
.
getElementById
(
"newfile"
).
files
[
0
];
if
(
typeof
(
res
)
==
"undefined"
)
{
alert
(
'未选择文件!'
);
}
else
{
var
filepath
=
document
.
getElementById
(
"newfile"
).
files
[
0
].
name
;
var
upload_path
=
"/upload/"
+
filepath
;
document
.
getElementById
(
"view_flash_start"
).
innerHTML
=
"锁定页面,请务必保持WiFi在线,正在上传..."
;
document
.
getElementById
(
"selectPartition"
).
disabled
=
true
;
document
.
getElementById
(
"newfile"
).
disabled
=
true
;
document
.
getElementById
(
"upload"
).
disabled
=
true
;
var
file
=
document
.
getElementById
(
"newfile"
).
files
[
0
];
var
xhttp
=
new
XMLHttpRequest
();
xhttp
.
onreadystatechange
=
function
()
{
if
(
xhttp
.
readyState
==
4
)
{
if
(
xhttp
.
status
==
200
)
{
document
.
getElementById
(
"view_flash_result"
).
innerHTML
=
"上传成功!"
;
}
else
if
(
xhttp
.
status
==
0
)
{
document
.
getElementById
(
"view_flash_result"
).
innerHTML
=
"服务器断开!<br><br>请检查服务器是否在线,刷新页面后重新上传..."
;
}
else
{
alert
(
xhttp
.
status
+
" Error!
\n
"
+
xhttp
.
responseText
);
}
}
};
let
progressBar
=
document
.
getElementById
(
"uploadProgress"
);
xhttp
.
upload
.
onprogress
=
function
(
e
)
{
var
percentage
=
(
event
.
loaded
*
100.00
)
/
(
event
.
total
*
1.00
);
percentage
=
percentage
.
toFixed
(
2
);
progressBar
.
value
=
Math
.
round
(
percentage
);
document
.
getElementById
(
"uploadPercent"
).
value
=
percentage
+
"%"
;
}
xhttp
.
open
(
"POST"
,
upload_path
,
true
);
xhttp
.
send
(
file
);
}
}
</script>
</body>
</html>
\ No newline at end of file
RT200T_2_ESP32/source/wifi/softap.c
0 → 100644
View file @
12945184
#include <stdio.h>
#include <string.h>
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_mac.h"
#include "esp_netif.h"
#include "esp_log.h"
#include "softap.h"
#include "wifi_service.h"
static
const
char
*
TAG
=
"softAP"
;
esp_netif_t
*
softap_netif
=
NULL
;
wifi_sta_list_t
softap_sta_list
;
unsigned
char
softap_sta_connect
=
STA_CONNECTED_NEVER
;
static
void
softap_event_handler
(
void
*
event_handler_arg
,
esp_event_base_t
event_base
,
int32_t
event_id
,
void
*
event_data
)
{
if
(
event_base
==
WIFI_EVENT
&&
event_id
==
WIFI_EVENT_AP_STACONNECTED
)
{
wifi_event_ap_staconnected_t
*
event
=
(
wifi_event_ap_staconnected_t
*
)
event_data
;
ESP_LOGI
(
TAG
,
"---- 设备加入网络, MAC地址 :"
MACSTR
", 编号(AID) = %d."
,
MAC2STR
(
event
->
mac
),
event
->
aid
);
softap_sta_connect
=
STA_CONNECTED_ONCE
;
connect_handler
(
event_handler_arg
);
wifi_service_stop_timer_clear
();
}
if
(
event_base
==
WIFI_EVENT
&&
event_id
==
WIFI_EVENT_AP_STADISCONNECTED
)
{
wifi_event_ap_stadisconnected_t
*
event
=
(
wifi_event_ap_stadisconnected_t
*
)
event_data
;
ESP_LOGI
(
TAG
,
"---- 设备退出网络, MAC地址 :"
MACSTR
", 编号(AID) = %d."
,
MAC2STR
(
event
->
mac
),
event
->
aid
);
disconnect_handler
(
event_handler_arg
);
wifi_service_stop_timer_clear
();
}
}
void
softap_service_start
(
void
)
{
// NVS分区初始化
ESP_LOGI
(
TAG
,
"---- nvs分区初始化..."
);
esp_err_t
ret
=
nvs_flash_init
();
if
(
ret
==
ESP_ERR_NVS_NO_FREE_PAGES
||
ret
==
ESP_ERR_NVS_NEW_VERSION_FOUND
)
{
ESP_ERROR_CHECK
(
nvs_flash_erase
());
ret
=
nvs_flash_init
();
}
ESP_ERROR_CHECK
(
ret
);
// 创建系统事件循环
ESP_LOGI
(
TAG
,
"---- 创建系统事件循环..."
);
ESP_ERROR_CHECK
(
esp_event_loop_create_default
());
// 将WiFi的所有事件作为实例注册进系统事件循环中
ESP_LOGI
(
TAG
,
"---- 注册WiFi事件实例..."
);
ESP_ERROR_CHECK
(
esp_event_handler_register
(
WIFI_EVENT
,
ESP_EVENT_ANY_ID
,
softap_event_handler
,
&
http_server
));
// 初始化网卡相关的底层配置
ESP_LOGI
(
TAG
,
"---- 网卡初始化..."
);
ESP_ERROR_CHECK
(
esp_netif_init
());
// 以默认的方式创建一个ap类型的网卡
ESP_LOGI
(
TAG
,
"---- 创建AP类型网卡..."
);
softap_netif
=
esp_netif_create_default_wifi_ap
();
// 初始化WiFi底层配置
ESP_LOGI
(
TAG
,
"---- WiFi初始化..."
);
wifi_init_config_t
cfg
=
WIFI_INIT_CONFIG_DEFAULT
();
ESP_ERROR_CHECK
(
esp_wifi_init
(
&
cfg
));
// 将WiFi设置成AP模式
ESP_LOGI
(
TAG
,
"---- 配置WiFi为AP模式..."
);
ESP_ERROR_CHECK
(
esp_wifi_set_mode
(
WIFI_MODE_AP
));
// 创建AP配置
ESP_LOGI
(
TAG
,
"---- 创建AP配置..."
);
wifi_config_t
wifi_config
=
{
.
ap
=
{
.
ssid
=
ESP_WIFI_SSID
,
.
ssid_len
=
strlen
(
ESP_WIFI_SSID
),
.
password
=
ESP_WIFI_PASSWD
,
.
max_connection
=
1
,
.
authmode
=
WIFI_AUTH_WPA_WPA2_PSK
}};
// 获取当前AP的MAC地址
unsigned
char
softap_mac
[
6
];
esp_wifi_get_mac
(
WIFI_IF_AP
,
softap_mac
);
// 将MAC地址后两个字节转为字符串
char
softap_temp
[
5
];
sprintf
(
softap_temp
,
"%02X%02X"
,
softap_mac
[
4
],
softap_mac
[
5
]);
// 将MAC地址拼接到 ESP_WIFI_SSID 后面
char
softap_ssid
[
32
]
=
ESP_WIFI_SSID
;
strcat
(
softap_ssid
,
softap_temp
);
// 重新配置 ssid 和 ssid_len
memcpy
(
wifi_config
.
ap
.
ssid
,
softap_ssid
,
sizeof
(
softap_ssid
));
wifi_config
.
ap
.
ssid_len
=
strlen
(
softap_ssid
);
// 配置AP的属性
ESP_ERROR_CHECK
(
esp_wifi_set_config
(
WIFI_IF_AP
,
&
wifi_config
));
// 开启WiFi
ESP_LOGI
(
TAG
,
"---- 开启WiFi, 名称 : %s 密码 : %s"
,
wifi_config
.
ap
.
ssid
,
wifi_config
.
ap
.
password
);
ESP_ERROR_CHECK
(
esp_wifi_start
());
// 首次开启http_server
ESP_LOGI
(
TAG
,
"---- 首次开启HTTP服务器..."
);
http_server
=
http_service
();
// 启动服务关闭计时器
ESP_LOGI
(
TAG
,
"---- 启动服务关闭计时器..."
);
wifi_service_stop_timer_clear
();
}
void
softap_service_stop
(
void
)
{
// 关闭http_server
ESP_LOGI
(
TAG
,
"---- 关闭HTTP服务器..."
);
disconnect_handler
(
&
http_server
);
// 关闭WiFi
ESP_LOGI
(
TAG
,
"---- 关闭WiFi..."
);
ESP_ERROR_CHECK
(
esp_wifi_stop
());
// WiFi反初始化
ESP_LOGI
(
TAG
,
"---- WiFi反初始化..."
);
ESP_ERROR_CHECK
(
esp_wifi_deinit
());
// 销毁网卡实例
ESP_LOGI
(
TAG
,
"---- 销毁网卡实例..."
);
esp_netif_destroy_default_wifi
(
softap_netif
);
// 网卡反初始化 - 官方未实现的功能
// ESP_LOGI(TAG, "---- 网卡反初始化...");
// ESP_ERROR_CHECK(esp_netif_deinit());
// 注销WiFi事件实例
ESP_LOGI
(
TAG
,
"---- 注销WiFi事件实例..."
);
ESP_ERROR_CHECK
(
esp_event_handler_unregister
(
WIFI_EVENT
,
ESP_EVENT_ANY_ID
,
softap_event_handler
));
// 删除系统事件循环
ESP_LOGI
(
TAG
,
"---- 删除系统事件循环..."
);
ESP_ERROR_CHECK
(
esp_event_loop_delete_default
());
// NVS分区反初始化
ESP_LOGI
(
TAG
,
"---- NVS分区反初始化..."
);
ESP_ERROR_CHECK
(
nvs_flash_deinit
());
ESP_LOGI
(
TAG
,
"---- WiFi服务已关闭, 重置WiFi参数"
);
wifi_service_init
();
}
RT200T_2_ESP32/source/wifi/softap.h
0 → 100644
View file @
12945184
#ifndef _SOFTAP_H_
#define _SOFTAP_H_
#include "esp_wifi.h"
#include "http_server.h"
#define SOFTAP_STOP 0
#define SOFTAP_INIT 1
#define SOFTAP_START 2
#define SOFTAP_DEINIT 3
#define STA_CONNECTED_NEVER 0
#define STA_CONNECTED_ONCE 1
#define ESP_WIFI_SSID "ESP32S3_AP_"
#define ESP_WIFI_PASSWD "12345677"
extern
wifi_sta_list_t
softap_sta_list
;
extern
unsigned
char
softap_sta_connect
;
void
softap_service_start
(
void
);
void
softap_service_stop
(
void
);
#endif
RT200T_2_ESP32/source/wifi/wifi_service.c
0 → 100644
View file @
12945184
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "esp_wifi.h"
#include "wifi_service.h"
static
const
char
*
TAG
=
"wifi_service"
;
/*****************************************************
* 首次启动HTTP服务器后, 如果一直没有STA设备加入
* 就不能触发STA disconnect事件,也就不会自动关闭HTTP服务器
* 所以需要额外判断这种情况, 当30秒关闭的定时器触发2次后直接关闭
******************************************************/
static
unsigned
char
http_server_stop_counter
=
0
;
static
unsigned
char
softap_service_stop_flag
=
0
;
static
unsigned
short
wifi_service_stop_timer
=
0
;
wifi_service_t
wifi_service_info
;
void
wifi_service_init
(
void
)
{
wifi_service_stop_timer
=
0
;
softap_service_stop_flag
=
0
;
http_server_stop_counter
=
0
;
wifi_service_info
.
status
=
WIFI_SERVICE_STOP
;
wifi_service_info
.
softap_status
=
SOFTAP_STOP
;
wifi_service_info
.
http_server_status
=
HTTP_SERVER_STOP
;
wifi_service_info
.
ota_partition
=
0
;
}
void
wifi_service_timer
(
void
)
{
if
(
wifi_service_info
.
status
==
WIFI_SERVICE_STOP
)
{
// 开启WiFi服务
wifi_service_info
.
status
=
WIFI_SERVICE_INIT
;
}
if
(
wifi_service_info
.
status
==
WIFI_SERVICE_START
)
{
if
(
wifi_service_stop_timer
<
300
)
{
++
wifi_service_stop_timer
;
}
// 每30秒尝试关闭WiFi服务
if
(
wifi_service_stop_timer
==
300
)
{
wifi_service_info
.
status
=
WIFI_SERVICE_DEINIT
;
}
}
}
void
wifi_service_stop_timer_clear
(
void
)
{
wifi_service_stop_timer
=
0
;
}
void
wifi_service
(
void
)
{
if
(
wifi_service_info
.
status
==
WIFI_SERVICE_INIT
)
// 收到指令: WiFi服务初始化
{
wifi_service_info
.
status
=
WIFI_SERVICE_START
;
ESP_LOGI
(
TAG
,
"---- 关闭CAN总线, 卸载LIN driver, 开启WiFi服务..."
);
// Bsp_Can_Sleep_Init();
// uart_driver_delete(UART_NUM_1);
softap_service_start
();
}
else
{
if
(
wifi_service_info
.
status
==
WIFI_SERVICE_DEINIT
)
// 收到指令: WiFi服务反初始化, 先读取当前连接的STA数量
{
esp_err_t
ret
=
esp_wifi_ap_get_sta_list
(
&
softap_sta_list
);
if
(
ret
==
ESP_OK
)
{
if
(
softap_sta_list
.
num
==
0
)
// 当前没有STA连接
{
if
(
softap_sta_connect
==
STA_CONNECTED_NEVER
)
// 从未有STA接入过
{
if
(
http_server_stop_counter
==
1
)
{
softap_service_stop_flag
=
1
;
}
else
{
ESP_LOGI
(
TAG
,
"首次启动HTTP服务器, 1分钟内不会关闭WiFi"
);
++
http_server_stop_counter
;
// 服务器开启状态且没有设备接入过, 延后30秒再关闭
}
}
else
{
softap_service_stop_flag
=
1
;
// 关闭WiFi服务
}
}
else
{
ESP_LOGI
(
TAG
,
"---- 有STA设备在线, 拒绝关闭WiFi"
);
}
}
else
{
ESP_LOGI
(
TAG
,
"---- 读取当前连接的STA数量失败, 打印错误信息"
);
esp_err_to_name
(
ret
);
}
if
(
softap_service_stop_flag
==
1
)
{
wifi_service_info
.
status
=
WIFI_SERVICE_STOP
;
ESP_LOGI
(
TAG
,
"---- 关闭WiFi服务, 开启CAN总线, 重装LIN driver..."
);
softap_service_stop
();
// Bsp_Can_Wakeup_Init();
// bsp_Uart1_Init();
}
else
{
wifi_service_info
.
status
=
WIFI_SERVICE_START
;
wifi_service_stop_timer
=
0
;
// 重置HTTP服务器关闭的定时器
}
}
}
}
RT200T_2_ESP32/source/wifi/wifi_service.h
0 → 100644
View file @
12945184
#ifndef _WIFI_SERVICE_H_
#define _WIFI_SERVICE_H_
#include "softap.h"
#define WIFI_SERVICE_STOP 0
#define WIFI_SERVICE_INIT 1
#define WIFI_SERVICE_START 2
#define WIFI_SERVICE_DEINIT 3
typedef
struct
{
unsigned
char
status
;
unsigned
char
softap_status
;
// 暂时用不上 2024.05.08
unsigned
char
http_server_status
;
// 暂时用不上 2024.05.08
unsigned
char
ota_partition
;
// 暂时用不上 2024.05.08
}
wifi_service_t
;
extern
wifi_service_t
wifi_service_info
;
void
wifi_service_init
(
void
);
void
wifi_service_timer
(
void
);
void
wifi_service_stop_timer_clear
(
void
);
void
wifi_service
(
void
);
extern
void
Bsp_Can_Sleep_Init
(
void
);
extern
void
Bsp_Can_Wakeup_Init
(
void
);
extern
void
bsp_Uart1_Init
(
void
);
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment