Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
car-controlserver
Project
Project
Details
Activity
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
wenzhongjian
car-controlserver
Commits
6d5a6a99
Commit
6d5a6a99
authored
Jun 05, 2025
by
学习的菜鸟
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mqtt重写,音频测试完成,WiFi扫描未测试,换连WiFi已完成大半
parent
37fcaa35
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
536 additions
and
139 deletions
+536
-139
CMakeLists.txt
CMakeLists.txt
+1
-1
Makefile
build/Makefile
+27
-0
version.h
build/include/version.h
+2
-2
main
build/main
+0
-0
judg.c
device_judg/judg/judg.c
+4
-0
audioplay.h
include/audioplay.h
+4
-3
mqtt.h
include/mqtt.h
+4
-2
thread_main.h
include/thread_main.h
+2
-1
wifichange.h
include/wifichange.h
+23
-0
audioplay.c
src/audioplay.c
+22
-16
download.c
src/download.c
+1
-1
mqtt.c
src/mqtt.c
+109
-95
thread_main.c
src/thread_main.c
+36
-9
wifichange.c
src/wifichange.c
+288
-0
gpio_common.h
system/gpio/gpio_common.h
+2
-4
gpio_pwm_tank0202.c
system/gpio/gpio_pwm_tank0202.c
+11
-5
No files found.
CMakeLists.txt
View file @
6d5a6a99
cmake_minimum_required
(
VERSION 3.10
)
cmake_minimum_required
(
VERSION 3.10
)
project
(
DeviceControlSystem
project
(
DeviceControlSystem
VERSION 1.1.
6
VERSION 1.1.
7
LANGUAGES C
LANGUAGES C
)
)
...
...
build/Makefile
View file @
6d5a6a99
...
@@ -640,6 +640,30 @@ src/thread_main.c.s:
...
@@ -640,6 +640,30 @@ src/thread_main.c.s:
$(MAKE)
$(MAKESILENT)
-f
CMakeFiles/main.dir/build.make CMakeFiles/main.dir/src/thread_main.c.s
$(MAKE)
$(MAKESILENT)
-f
CMakeFiles/main.dir/build.make CMakeFiles/main.dir/src/thread_main.c.s
.PHONY
:
src/thread_main.c.s
.PHONY
:
src/thread_main.c.s
src/wifichange.o
:
src/wifichange.c.o
.PHONY
:
src/wifichange.o
# target to build an object file
src/wifichange.c.o
:
$(MAKE)
$(MAKESILENT)
-f
CMakeFiles/main.dir/build.make CMakeFiles/main.dir/src/wifichange.c.o
.PHONY
:
src/wifichange.c.o
src/wifichange.i
:
src/wifichange.c.i
.PHONY
:
src/wifichange.i
# target to preprocess a source file
src/wifichange.c.i
:
$(MAKE)
$(MAKESILENT)
-f
CMakeFiles/main.dir/build.make CMakeFiles/main.dir/src/wifichange.c.i
.PHONY
:
src/wifichange.c.i
src/wifichange.s
:
src/wifichange.c.s
.PHONY
:
src/wifichange.s
# target to generate assembly for a file
src/wifichange.c.s
:
$(MAKE)
$(MAKESILENT)
-f
CMakeFiles/main.dir/build.make CMakeFiles/main.dir/src/wifichange.c.s
.PHONY
:
src/wifichange.c.s
system/gpio/gpio_common.o
:
system/gpio/gpio_common.c.o
system/gpio/gpio_common.o
:
system/gpio/gpio_common.c.o
.PHONY
:
system/gpio/gpio_common.o
.PHONY
:
system/gpio/gpio_common.o
...
@@ -1503,6 +1527,9 @@ help:
...
@@ -1503,6 +1527,9 @@ help:
@
echo
"... src/thread_main.o"
@
echo
"... src/thread_main.o"
@
echo
"... src/thread_main.i"
@
echo
"... src/thread_main.i"
@
echo
"... src/thread_main.s"
@
echo
"... src/thread_main.s"
@
echo
"... src/wifichange.o"
@
echo
"... src/wifichange.i"
@
echo
"... src/wifichange.s"
@
echo
"... system/gpio/gpio_common.o"
@
echo
"... system/gpio/gpio_common.o"
@
echo
"... system/gpio/gpio_common.i"
@
echo
"... system/gpio/gpio_common.i"
@
echo
"... system/gpio/gpio_common.s"
@
echo
"... system/gpio/gpio_common.s"
...
...
build/include/version.h
View file @
6d5a6a99
#define PROJECT_VERSION_MAJOR 1
#define PROJECT_VERSION_MAJOR 1
#define PROJECT_VERSION_MINOR 1
#define PROJECT_VERSION_MINOR 1
#define PROJECT_VERSION_PATCH
6
#define PROJECT_VERSION_PATCH
7
#define GIT_HASH ""
#define GIT_HASH ""
#define BUILD_TIMESTAMP "2025-06-0
3T10:33:2
6"
#define BUILD_TIMESTAMP "2025-06-0
5T10:05:0
6"
#define BUILD_USER "orangepi"
#define BUILD_USER "orangepi"
build/main
View file @
6d5a6a99
No preview for this file type
device_judg/judg/judg.c
View file @
6d5a6a99
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
char
TOPIC_middle
[
23
];
char
TOPIC_middle
[
23
];
char
TOPIC2_middle
[
23
];
char
TOPIC2_middle
[
23
];
char
TOPIC3_middle
[
15
];
char
TOPIC3_middle
[
15
];
char
TOPIC4_middle
[
23
];
bool
device_name_exists
=
0
;
bool
device_name_exists
=
0
;
...
@@ -24,6 +25,7 @@ void topic_middle_value() {//指针中转函数,在使用指针前调用
...
@@ -24,6 +25,7 @@ void topic_middle_value() {//指针中转函数,在使用指针前调用
TOPIC
=
TOPIC_middle
;
TOPIC
=
TOPIC_middle
;
TOPIC2
=
TOPIC2_middle
;
TOPIC2
=
TOPIC2_middle
;
TOPIC3
=
TOPIC3_middle
;
TOPIC3
=
TOPIC3_middle
;
TOPIC4
=
TOPIC4_middle
;
}
}
// void topic_back_middle_value() {//指针中转函数,在使用指针前调用
// void topic_back_middle_value() {//指针中转函数,在使用指针前调用
...
@@ -145,10 +147,12 @@ int Device_File_Init() {//设备文件读取函数
...
@@ -145,10 +147,12 @@ int Device_File_Init() {//设备文件读取函数
sprintf
(
TOPIC2_middle
,
"dev2app/%s"
,
readbuf
);
sprintf
(
TOPIC2_middle
,
"dev2app/%s"
,
readbuf
);
sprintf
(
TOPIC_middle
,
"app2dev/%s"
,
readbuf
);
sprintf
(
TOPIC_middle
,
"app2dev/%s"
,
readbuf
);
sprintf
(
TOPIC3_middle
,
"%s"
,
readbuf
);
sprintf
(
TOPIC3_middle
,
"%s"
,
readbuf
);
sprintf
(
TOPIC4_middle
,
"ser2dev/%s"
,
readbuf
);
}
else
{
}
else
{
sprintf
(
TOPIC2_middle
,
"dev2app/%s"
,
"00000000000000"
);
sprintf
(
TOPIC2_middle
,
"dev2app/%s"
,
"00000000000000"
);
sprintf
(
TOPIC_middle
,
"app2dev/%s"
,
"00000000000000"
);
sprintf
(
TOPIC_middle
,
"app2dev/%s"
,
"00000000000000"
);
sprintf
(
TOPIC3_middle
,
"%s"
,
"00000000000000"
);
sprintf
(
TOPIC3_middle
,
"%s"
,
"00000000000000"
);
sprintf
(
TOPIC4_middle
,
"ser2dev/%s"
,
"00000000000000"
);
}
}
...
...
include/audioplay.h
View file @
6d5a6a99
...
@@ -2,8 +2,8 @@
...
@@ -2,8 +2,8 @@
#define AUDIOPLAY_H
#define AUDIOPLAY_H
#include <cjson/cJSON.h>
#include <cjson/cJSON.h>
extern
int
audio_status
;
//
extern int audio_status;
void
audioplay_mqtt_receive
(
cJSON
*
json
)
;
void
audioplay_mqtt_receive
(
cJSON
*
body
);
//接收音频mqtt播放函数
void
audioplay_cycle
();
void
audioplay_cycle
();
//音频播放线程中函数
#endif
#endif
\ No newline at end of file
include/mqtt.h
View file @
6d5a6a99
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
extern
char
*
TOPIC
;
extern
char
*
TOPIC
;
extern
char
*
TOPIC2
;
extern
char
*
TOPIC2
;
extern
char
*
TOPIC3
;
extern
char
*
TOPIC3
;
extern
char
*
TOPIC4
;
extern
char
*
TOPIC_BACK
;
extern
char
*
TOPIC_BACK
;
extern
char
*
TOPIC2_BACK
;
extern
char
*
TOPIC2_BACK
;
...
@@ -27,11 +28,11 @@ extern int gPwmCount; // 计数
...
@@ -27,11 +28,11 @@ extern int gPwmCount; // 计数
extern
int
gmessage_type
;
//message消息值
extern
int
gmessage_type
;
//message消息值
int
mqtt_init
();
//mqtt初始化
int
mqtt_init
();
//mqtt初始化
void
mqtt_create
(
struct
mosquitto
*
mosq
);
//创建mqtt客服端
int
mqtt_create
(
struct
mosquitto
*
mosq
);
//创建mqtt客服端
void
on_connect
(
struct
mosquitto
*
mosq
,
void
*
obj
,
int
rc
);
//回调函数
void
on_connect
(
struct
mosquitto
*
mosq
,
void
*
obj
,
int
rc
);
//回调函数
void
on_message
(
struct
mosquitto
*
mosq
,
void
*
obj
,
const
struct
mosquitto_message
*
message
);
//消息回调函数
void
on_message
(
struct
mosquitto
*
mosq
,
void
*
obj
,
const
struct
mosquitto_message
*
message
);
//消息回调函数
void
mqtt_wirte
();
//发送mqtt,现为心跳
void
mqtt_wirte
();
//发送mqtt,现为心跳
void
mqtt_cycle
(
struct
mosquitto
*
mosq
);
//循环
int
mqtt_cycle
(
struct
mosquitto
*
mosq
);
//循环
void
mqtt_clean
(
struct
mosquitto
*
mosq
);
//清理
void
mqtt_clean
(
struct
mosquitto
*
mosq
);
//清理
#endif
#endif
\ No newline at end of file
include/thread_main.h
View file @
6d5a6a99
...
@@ -11,7 +11,7 @@ void *Mqttbeat(void *arg) ;//心跳线程
...
@@ -11,7 +11,7 @@ void *Mqttbeat(void *arg) ;//心跳线程
void
*
opensh
(
void
*
arg
)
;
//启动游览器线程
void
*
opensh
(
void
*
arg
)
;
//启动游览器线程
void
*
Mqtt_onnect
(
void
*
arg
)
;
//mqtt自带重连线程
void
*
Mqtt_onnect
(
void
*
arg
)
;
//mqtt自带重连线程
void
*
delay_count
(
void
*
arg
)
;
//延时计算延迟
void
*
delay_count
(
void
*
arg
)
;
//延时计算延迟
void
*
play_mp3_thread
(
void
*
arg
);
void
*
play_mp3_thread
(
void
*
arg
);
//等待播放音频线程
void
thread_end
()
;
void
thread_end
()
;
#endif
#endif
\ No newline at end of file
include/wifichange.h
0 → 100644
View file @
6d5a6a99
#ifndef WIFICHANGE_H__
#define WIFICHANGE_H__
#define SSID_MAX_LEN 128
#define PASS_MAX_LEN 128
#define CMD_BUFFER_SIZE 256
#define MAX_WIFI_LIST 100
#define default_SSID jking
#define default_password 12345678
#define WIFI_CONF_PATH "/home/orangepi/car/master/device_wifi.conf"
typedef
struct
{
char
ssid
[
SSID_MAX_LEN
];
int
signal
;
}
wifi_info_t
;
void
scan_wifi_json
();
//显示以连接WiFi名称和周围已有的WiFi和过滤信号强度低于50的WiFi
int
delete_wifi_by_ssid
(
const
char
*
ssid
);
//忘记指定的WiFi的密码
#endif
\ No newline at end of file
src/audioplay.c
View file @
6d5a6a99
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
#include "mqtt.h"
#include "mqtt.h"
#include "judg.h"
#include "judg.h"
int
audio_status
=
3
;
int
audio_status
=
7
;
char
urlbuf
[
512
];
char
urlbuf
[
512
];
char
keybuf
[
256
];
char
keybuf
[
256
];
double
audio_volume
=
0
.
8
;
double
audio_volume
=
0
.
8
;
...
@@ -26,7 +26,8 @@ void audioplay_mqtt_receive(cJSON *json) {
...
@@ -26,7 +26,8 @@ void audioplay_mqtt_receive(cJSON *json) {
char
*
key
=
key_char
->
valuestring
;
char
*
key
=
key_char
->
valuestring
;
snprintf
(
keybuf
,
sizeof
(
keybuf
),
"%s"
,
key
);
snprintf
(
keybuf
,
sizeof
(
keybuf
),
"%s"
,
key
);
}
else
{
}
else
{
my_zlog_warn
(
"错误:无法解析密钥字段
\n
"
);
my_zlog_warn
(
"错误:无法解析音频链接"
);
audio_status
=
5
;
}
}
// 解析"status"字段
// 解析"status"字段
...
@@ -48,16 +49,21 @@ void audioplay_mqtt_receive(cJSON *json) {
...
@@ -48,16 +49,21 @@ void audioplay_mqtt_receive(cJSON *json) {
}
}
}
}
void
audioplay_send_mqtt
()
{
void
audioplay_send_mqtt
()
{
//发送音频播放是否完毕
cJSON
*
root
=
cJSON_CreateObject
();
cJSON
*
root
=
cJSON_CreateObject
();
cJSON
*
body
=
cJSON_CreateObject
();
cJSON
*
head
=
cJSON_CreateObject
();
// 添加各个字段到 JSON 对象
// 添加各个字段到 JSON 对象
cJSON_AddStringToObject
(
root
,
"type"
,
"audio"
);
cJSON_AddStringToObject
(
body
,
"type"
,
"audio"
);
cJSON_AddStringToObject
(
root
,
"audioLink"
,
urlbuf
);
cJSON_AddStringToObject
(
body
,
"audioLink"
,
urlbuf
);
cJSON_AddStringToObject
(
root
,
"key"
,
keybuf
);
cJSON_AddStringToObject
(
body
,
"key"
,
keybuf
);
cJSON_AddNumberToObject
(
root
,
"status"
,
audio_status
);
cJSON_AddNumberToObject
(
body
,
"status"
,
audio_status
);
cJSON_AddNumberToObject
(
root
,
"volume"
,
audio_volume
);
cJSON_AddNumberToObject
(
body
,
"volume"
,
audio_volume
);
cJSON_AddNumberToObject
(
head
,
"message_type"
,
3001
);
cJSON_AddItemToObject
(
root
,
"body"
,
body
);
cJSON_AddItemToObject
(
root
,
"head"
,
head
);
// 将 JSON 对象转换为字符串
// 将 JSON 对象转换为字符串
char
*
json_string
=
cJSON_PrintUnformatted
(
root
);
char
*
json_string
=
cJSON_PrintUnformatted
(
root
);
my_zlog_debug
(
"%s"
,
json_string
);
my_zlog_debug
(
"%s"
,
json_string
);
...
@@ -85,15 +91,14 @@ void audioplay_cycle(){
...
@@ -85,15 +91,14 @@ void audioplay_cycle(){
}
}
if
(
WIFEXITED
(
ret
)
&&
WEXITSTATUS
(
ret
)
==
0
)
{
if
(
WIFEXITED
(
ret
)
&&
WEXITSTATUS
(
ret
)
==
0
)
{
my_zlog_debug
(
"播放已成功完成 : %s
\n
"
,
urlbuf
);
my_zlog_debug
(
"播放已成功完成 : %s "
,
urlbuf
);
audio_status
=
1
;
audio_status
=
1
;
audioplay_send_mqtt
();
}
else
{
}
else
{
my_zlog_warn
(
"播放失败或中断: %s
\n
"
,
urlbuf
);
my_zlog_warn
(
"播放失败或中断: %s "
,
urlbuf
);
audio_status
=
2
;
audio_status
=
2
;
}
}
audioplay_send_mqtt
();
audioplay_send_mqtt
();
}
}
}
}
}
}
\ No newline at end of file
src/download.c
View file @
6d5a6a99
...
@@ -15,7 +15,7 @@ static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, voi
...
@@ -15,7 +15,7 @@ static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, voi
char
*
ptr
=
realloc
(
mem
->
memory
,
mem
->
size
+
realsize
+
1
);
char
*
ptr
=
realloc
(
mem
->
memory
,
mem
->
size
+
realsize
+
1
);
if
(
!
ptr
)
{
if
(
!
ptr
)
{
my_zlog_warn
(
"not enough memory (realloc returned NULL)
\n
"
);
my_zlog_warn
(
"not enough memory (realloc returned NULL)"
);
return
0
;
return
0
;
}
}
...
...
src/mqtt.c
View file @
6d5a6a99
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
#include "device_change.h"
#include "device_change.h"
#include "judg.h"
#include "judg.h"
#include "ads1115.h"
#include "ads1115.h"
#include "audioplay.h"
struct
mosquitto
*
mosq
;
struct
mosquitto
*
mosq
;
...
@@ -21,6 +22,7 @@ int heartbeat_count=0;
...
@@ -21,6 +22,7 @@ int heartbeat_count=0;
char
*
TOPIC
=
NULL
;
char
*
TOPIC
=
NULL
;
char
*
TOPIC2
=
NULL
;
char
*
TOPIC2
=
NULL
;
char
*
TOPIC3
=
NULL
;
char
*
TOPIC3
=
NULL
;
char
*
TOPIC4
=
NULL
;
char
*
TOPIC_BACK
=
NULL
;
char
*
TOPIC_BACK
=
NULL
;
char
*
TOPIC2_BACK
=
NULL
;
char
*
TOPIC2_BACK
=
NULL
;
...
@@ -48,17 +50,14 @@ char *glon=NULL;//加入gps后删除,心跳预留,不更改
...
@@ -48,17 +50,14 @@ char *glon=NULL;//加入gps后删除,心跳预留,不更改
time_t
gStart
;
//时间戳
time_t
gStart
;
//时间戳
int
mqtt_init
()
{
int
mqtt_init
()
{
// 初始化 mosquitto 库
mosquitto_lib_init
();
mosquitto_lib_init
();
// 这里创建mosq可以保留,也可以删除这一行,让创建放到 Mqtt_onnect 里
mosq
=
mosquitto_new
(
NULL
,
true
,
NULL
);
mosq
=
mosquitto_new
(
NULL
,
true
,
NULL
);
//
if
(
!
mosq
)
{
if
(
!
mosq
)
{
my_zlog_fatal
(
"Failed to create Mosquitto client"
);
my_zlog_fatal
(
"Failed to create Mosquitto client"
);
return
-
1
;
return
-
1
;
}
}
mosquitto_int_option
(
mosq
,
MOSQ_OPT_PROTOCOL_VERSION
,
MQTT_PROTOCOL_V5
);
mosquitto_int_option
(
mosq
,
MOSQ_OPT_PROTOCOL_VERSION
,
MQTT_PROTOCOL_V5
);
return
0
;
return
0
;
}
}
...
@@ -67,7 +66,7 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) {//回调函数
...
@@ -67,7 +66,7 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) {//回调函数
my_zlog_debug
(
"Connected to broker"
);
my_zlog_debug
(
"Connected to broker"
);
topic_middle_value
();
topic_middle_value
();
mosquitto_subscribe
(
mosq
,
NULL
,
TOPIC
,
0
);
mosquitto_subscribe
(
mosq
,
NULL
,
TOPIC
,
0
);
//mosquitto_subscribe(mosq, NULL, TOPIC_BACK
, 0);
mosquitto_subscribe
(
mosq
,
NULL
,
TOPIC4
,
0
);
}
else
{
}
else
{
fprintf
(
stderr
,
"Connection failed with code %d
\n
"
,
rc
);
fprintf
(
stderr
,
"Connection failed with code %d
\n
"
,
rc
);
my_zlog_fatal
(
"Connection failed"
);
my_zlog_fatal
(
"Connection failed"
);
...
@@ -156,7 +155,12 @@ void mqtt_wirte(){//心跳格式,每5s一次心跳
...
@@ -156,7 +155,12 @@ void mqtt_wirte(){//心跳格式,每5s一次心跳
}
}
void
message_3
(
cJSON
*
body
,
cJSON
*
pwm_ctrl
){
//message_type为3,控制pwm
void
message_3
(
cJSON
*
body
){
//message_type为3,控制pwm
cJSON
*
pwm_ctrl
=
cJSON_GetObjectItem
(
body
,
"pwm_ctrl"
);
cJSON
*
pin_setctrl
=
cJSON_GetObjectItem
(
body
,
"pin_setctrl"
);
if
(
pwm_ctrl
==
NULL
||
pin_setctrl
==
NULL
){
return
;
}
cJSON
*
mode
=
cJSON_GetObjectItem
(
pwm_ctrl
,
"mode"
);
//mode=1 速度,mode=2 转向(unsigned char)
cJSON
*
mode
=
cJSON_GetObjectItem
(
pwm_ctrl
,
"mode"
);
//mode=1 速度,mode=2 转向(unsigned char)
cJSON
*
type
=
cJSON_GetObjectItem
(
pwm_ctrl
,
"type"
);
cJSON
*
type
=
cJSON_GetObjectItem
(
pwm_ctrl
,
"type"
);
cJSON
*
val
=
cJSON_GetObjectItem
(
pwm_ctrl
,
"val"
);
//val为pwm的值 0~100(unsigned char)(unsigned char)
cJSON
*
val
=
cJSON_GetObjectItem
(
pwm_ctrl
,
"val"
);
//val为pwm的值 0~100(unsigned char)(unsigned char)
...
@@ -182,7 +186,13 @@ void message_3(cJSON *body,cJSON *pwm_ctrl){//message_type为3,控制pwm
...
@@ -182,7 +186,13 @@ void message_3(cJSON *body,cJSON *pwm_ctrl){//message_type为3,控制pwm
}
}
void
message_4
(
cJSON
*
body
,
cJSON
*
pin_setctrl
){
//message 为4时候
void
message_4
(
cJSON
*
body
){
//message 为4时候
cJSON
*
pwm_ctrl
=
cJSON_GetObjectItem
(
body
,
"pwm_ctrl"
);
cJSON
*
pin_setctrl
=
cJSON_GetObjectItem
(
body
,
"pin_setctrl"
);
if
(
pwm_ctrl
==
NULL
||
pin_setctrl
==
NULL
){
return
;
}
cJSON
*
pin
=
cJSON_GetObjectItem
(
pin_setctrl
,
"pin"
);
cJSON
*
pin
=
cJSON_GetObjectItem
(
pin_setctrl
,
"pin"
);
cJSON
*
val
=
cJSON_GetObjectItem
(
pin_setctrl
,
"val"
);
//val为pwm的值 0~100(unsigned char)(unsigned char)
cJSON
*
val
=
cJSON_GetObjectItem
(
pin_setctrl
,
"val"
);
//val为pwm的值 0~100(unsigned char)(unsigned char)
...
@@ -206,7 +216,46 @@ void message_4(cJSON *body, cJSON *pin_setctrl){//message 为4时候
...
@@ -206,7 +216,46 @@ void message_4(cJSON *body, cJSON *pin_setctrl){//message 为4时候
if
(
AppExit_pin_pwm
==
202
)
tank_shot_back_stop
(
gvalt
[
1
],
gvalt
[
2
]);
if
(
AppExit_pin_pwm
==
202
)
tank_shot_back_stop
(
gvalt
[
1
],
gvalt
[
2
]);
}
}
int
device_message
(
cJSON
*
json
){
//接收到的控制设备的mqtt消息
int
download_message
(
cJSON
*
body
){
//接收的下载的mqtt消息
if
(
cJSON_IsObject
(
body
))
{
// 获取 doby 对象
// 获取 site 中的 name 和 url
cJSON
*
name
=
cJSON_GetObjectItem
(
body
,
"name"
);
cJSON
*
url
=
cJSON_GetObjectItem
(
body
,
"url"
);
if
(
cJSON_IsString
(
name
)
&&
cJSON_IsString
(
url
))
{
my_zlog_debug
(
"Site Name: %s"
,
name
->
valuestring
);
my_zlog_debug
(
"Site URL: %s"
,
url
->
valuestring
);
}
// 获取 type 中的 suf、dir 和 sername
cJSON
*
down_suf
=
cJSON_GetObjectItem
(
body
,
"suf"
);
//cJSON *down_dir = cJSON_GetObjectItem(type, "dir");&& cJSON_IsString(down_dir)
cJSON
*
down_sername
=
cJSON_GetObjectItem
(
body
,
"sername"
);
if
(
cJSON_IsString
(
down_suf
)
&&
cJSON_IsString
(
down_sername
))
{
my_zlog_debug
(
"Type Suf: %s"
,
down_suf
->
valuestring
);
//my_zlog_debug("Type Dir: %s", down_dir->valuestring);
my_zlog_debug
(
"Type Sername: %s"
,
down_sername
->
valuestring
);
}
}
else
return
-
1
;
return
0
;
}
int
device_mqttchange_name
(
cJSON
*
device_id
){
cJSON
*
id
=
cJSON_GetObjectItem
(
device_id
,
"id"
);
// 提取ID
cJSON
*
date
=
cJSON_GetObjectItem
(
device_id
,
"date"
);
// 提取日期
char
*
device_change_id
=
id
->
valuestring
;
char
*
device_change_date
=
date
->
valuestring
;
device_changename_back
(
device_change_date
,
device_change_id
);
// 输出提取的数据
my_zlog_debug
(
"设备ID: %s"
,
device_change_id
);
my_zlog_debug
(
"日期: %s"
,
device_change_date
);
return
0
;
}
int
device_message_receive
(
cJSON
*
json
){
//接收到的控制设备的mqtt消息
cJSON
*
head
=
cJSON_GetObjectItem
(
json
,
"head"
);
cJSON
*
head
=
cJSON_GetObjectItem
(
json
,
"head"
);
cJSON
*
message_type
=
cJSON_GetObjectItem
(
head
,
"message_type"
);
cJSON
*
message_type
=
cJSON_GetObjectItem
(
head
,
"message_type"
);
...
@@ -218,12 +267,6 @@ int device_message(cJSON *json){//接收到的控制设备的mqtt消息
...
@@ -218,12 +267,6 @@ int device_message(cJSON *json){//接收到的控制设备的mqtt消息
return
1
;
return
1
;
}
}
cJSON
*
pwm_ctrl
=
cJSON_GetObjectItem
(
body
,
"pwm_ctrl"
);
cJSON
*
pin_setctrl
=
cJSON_GetObjectItem
(
body
,
"pin_setctrl"
);
if
(
pwm_ctrl
==
NULL
||
pin_setctrl
==
NULL
){
return
2
;
}
gmessage_type
=
message_type
->
valueint
;
gmessage_type
=
message_type
->
valueint
;
my_zlog_debug
(
"message_type: %d"
,
message_type
->
valueint
);
my_zlog_debug
(
"message_type: %d"
,
message_type
->
valueint
);
switch
(
gmessage_type
){
switch
(
gmessage_type
){
...
@@ -236,13 +279,29 @@ int device_message(cJSON *json){//接收到的控制设备的mqtt消息
...
@@ -236,13 +279,29 @@ int device_message(cJSON *json){//接收到的控制设备的mqtt消息
my_zlog_debug
(
"刷新成功"
);
my_zlog_debug
(
"刷新成功"
);
break
;
break
;
case
3
:
case
3
:
message_3
(
body
,
pwm_ctrl
);
message_3
(
body
);
my_zlog_debug
(
"进入pwm控制"
);
my_zlog_debug
(
"进入pwm控制"
);
break
;
break
;
case
4
:
case
4
:
message_4
(
body
,
pin_setctrl
);
message_4
(
body
);
my_zlog_debug
(
"进入引脚控制"
);
my_zlog_debug
(
"进入引脚控制"
);
break
;
break
;
case
2001
:
audioplay_mqtt_receive
(
body
);
my_zlog_debug
(
"进入音频播放"
);
break
;
case
2002
:
device_mqttchange_name
(
body
);
my_zlog_debug
(
"进入修改设备号"
);
break
;
case
2003
:
scan_wifi_json
();
my_zlog_debug
(
"进入查询WiFi"
);
break
;
case
2004
:
break
;
case
2005
:
download_message
(
body
);
break
;
default
:
default
:
break
;
break
;
}
}
...
@@ -251,53 +310,6 @@ int device_message(cJSON *json){//接收到的控制设备的mqtt消息
...
@@ -251,53 +310,6 @@ int device_message(cJSON *json){//接收到的控制设备的mqtt消息
return
0
;
return
0
;
}
}
int
download_message
(
cJSON
*
root
){
//接收的下载的mqtt消息
cJSON
*
down
=
cJSON_GetObjectItem
(
root
,
"down"
);
if
(
cJSON_IsObject
(
down
))
{
// 获取 site 对象
cJSON
*
site
=
cJSON_GetObjectItem
(
down
,
"site"
);
if
(
cJSON_IsObject
(
site
))
{
// 获取 site 中的 name 和 url
cJSON
*
name
=
cJSON_GetObjectItem
(
site
,
"name"
);
cJSON
*
url
=
cJSON_GetObjectItem
(
site
,
"url"
);
if
(
cJSON_IsString
(
name
)
&&
cJSON_IsString
(
url
))
{
my_zlog_debug
(
"Site Name: %s"
,
name
->
valuestring
);
my_zlog_debug
(
"Site URL: %s"
,
url
->
valuestring
);
}
}
// 获取 type 对象
cJSON
*
type
=
cJSON_GetObjectItem
(
down
,
"type"
);
if
(
cJSON_IsObject
(
type
))
{
// 获取 type 中的 suf、dir 和 sername
cJSON
*
down_suf
=
cJSON_GetObjectItem
(
type
,
"suf"
);
//cJSON *down_dir = cJSON_GetObjectItem(type, "dir");&& cJSON_IsString(down_dir)
cJSON
*
down_sername
=
cJSON_GetObjectItem
(
type
,
"sername"
);
if
(
cJSON_IsString
(
down_suf
)
&&
cJSON_IsString
(
down_sername
))
{
my_zlog_debug
(
"Type Suf: %s"
,
down_suf
->
valuestring
);
//my_zlog_debug("Type Dir: %s", down_dir->valuestring);
my_zlog_debug
(
"Type Sername: %s"
,
down_sername
->
valuestring
);
}
}
}
else
return
-
1
;
return
0
;
}
int
device_mqttchange_name
(
cJSON
*
device_id
){
cJSON
*
id
=
cJSON_GetObjectItem
(
device_id
,
"id"
);
// 提取ID
cJSON
*
date
=
cJSON_GetObjectItem
(
device_id
,
"date"
);
// 提取日期
char
*
device_change_id
=
id
->
valuestring
;
char
*
device_change_date
=
date
->
valuestring
;
device_changename_back
(
device_change_date
,
device_change_id
);
// 输出提取的数据
my_zlog_debug
(
"设备ID: %s"
,
device_change_id
);
my_zlog_debug
(
"日期: %s"
,
device_change_date
);
return
0
;
}
void
on_message
(
struct
mosquitto
*
mosq
,
void
*
obj
,
const
struct
mosquitto_message
*
message
)
{
//消息回环函数
void
on_message
(
struct
mosquitto
*
mosq
,
void
*
obj
,
const
struct
mosquitto_message
*
message
)
{
//消息回环函数
time_t
end
=
time
(
NULL
);
time_t
end
=
time
(
NULL
);
...
@@ -319,23 +331,10 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -319,23 +331,10 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
// 解析 JSON
// 解析 JSON
cJSON
*
json
=
cJSON_Parse
(
payload_str
);
cJSON
*
json
=
cJSON_Parse
(
payload_str
);
if
(
json
==
NULL
)
{
if
(
json
!=
NULL
){
//fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
if
(
device_message_receive
(
json
)
==
0
)
my_zlog_debug
(
"进入设备接收mqtt"
);
my_zlog_error
(
"Error before: [%s]"
,
cJSON_GetErrorPtr
());
}
}
else
if
(
cJSON_GetObjectItem
(
json
,
"device_id"
)
!=
NULL
)
{
cJSON
*
device_id
=
cJSON_GetObjectItem
(
json
,
"device_id"
);
device_mqttchange_name
(
device_id
);
my_zlog_debug
(
"接收到设备号更改"
);
}
else
if
(
cJSON_GetObjectItem
(
json
,
"type"
)
!=
NULL
)
{
cJSON
*
type
=
cJSON_GetObjectItem
(
json
,
"type"
);
if
(
type
&&
cJSON_IsString
(
type
)
&&
strcmp
(
type
->
valuestring
,
"audio"
)
==
0
)
{
audioplay_mqtt_receive
(
json
);
my_zlog_debug
(
"接收到音频播放"
);
}
}
else
{
// 提取 head 对象
if
(
download_message
(
json
)
==
0
)
my_zlog_debug
(
"接收到dowwnload下载"
);
if
(
device_message
(
json
)
!=
0
)
my_zlog_debug
(
"没有接收到控制信息,代码编号:%d"
,
device_message
(
json
));
}
// 释放 payload 字符串
// 释放 payload 字符串
free
(
payload_str
);
free
(
payload_str
);
cJSON_Delete
(
json
);
cJSON_Delete
(
json
);
...
@@ -343,27 +342,42 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -343,27 +342,42 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
}
}
void
mqtt_create
(
struct
mosquitto
*
mosq
)
{
//创建mqtt客服端
int
mqtt_create
(
struct
mosquitto
*
mosq
)
{
// 返回连接结果,0成功,非0失败
mosquitto_reconnect_delay_set
(
mosq
,
2
,
10
,
true
);
mosquitto_reconnect_delay_set
(
mosq
,
2
,
10
,
true
);
// 设置连接和消息回调
mosquitto_connect_callback_set
(
mosq
,
on_connect
);
mosquitto_connect_callback_set
(
mosq
,
on_connect
);
mosquitto_message_callback_set
(
mosq
,
on_message
);
mosquitto_message_callback_set
(
mosq
,
on_message
);
//设置用户名和密码
mosquitto_username_pw_set
(
mosq
,
USERNAME
,
PASSWORD
);
mosquitto_username_pw_set
(
mosq
,
USERNAME
,
PASSWORD
);
// 创建 mosquitto 客户端
rc
=
mosquitto_connect
(
mosq
,
BROKER_ADDRESS
,
BROKER_PORT
,
60
);
int
rc
=
mosquitto_connect
(
mosq
,
BROKER_ADDRESS
,
BROKER_PORT
,
60
);
if
(
rc
!=
MOSQ_ERR_SUCCESS
)
{
if
(
rc
!=
MOSQ_ERR_SUCCESS
)
{
//fprintf(stderr, "Failed to connect to broker: %s\n", mosquitto_strerror(rc));
my_zlog_warn
(
"Failed to connect to broker: %s"
,
mosquitto_strerror
(
rc
));
my_zlog_warn
(
"Failed to connect to broker: %s"
,
mosquitto_strerror
(
rc
));
mosquitto_destroy
(
mosq
);
// 不销毁 mosq,等待调用者处理重连
//return EXIT_FAILURE;
return
rc
;
}
return
0
;
// 成功
}
int
mqtt_cycle
(
struct
mosquitto
*
mosq
)
{
//阻塞型
// 这个函数会阻塞直到连接断开或错误返回
int
rc
=
mosquitto_loop_forever
(
mosq
,
-
1
,
1
);
if
(
rc
!=
MOSQ_ERR_SUCCESS
)
{
my_zlog_warn
(
"mqtt loop error: %s"
,
mosquitto_strerror
(
rc
));
return
rc
;
}
}
}
void
mqtt_cycle
(
struct
mosquitto
*
mosq
){
mosquitto_loop_forever
(
mosq
,
-
1
,
1
);
// -1 表示无限等待,1 表示处理消息的最大数量
}
}
// int mqtt_cycle(struct mosquitto *mosq) {//非阻塞型
// int rc;
// while (1) {
// rc = mosquitto_loop(mosq, 100, 1); // 每秒检查一次
// if (rc != MOSQ_ERR_SUCCESS) {
// my_zlog_warn("mosquitto loop error: %s,(code: %d)", mosquitto_strerror(rc),rc);
// return -1; // 跳出循环,重新连接
// }
// Delay_Ms(0, 3); // 防止空转
// }
// }
void
mqtt_clean
(
struct
mosquitto
*
mosq
){
void
mqtt_clean
(
struct
mosquitto
*
mosq
){
// 清理
// 清理
mosquitto_destroy
(
mosq
);
mosquitto_destroy
(
mosq
);
...
...
src/thread_main.c
View file @
6d5a6a99
...
@@ -13,6 +13,8 @@
...
@@ -13,6 +13,8 @@
pthread_t
thread
[
6
];
// 全局线程句柄数组(或传参)
pthread_t
thread
[
6
];
// 全局线程句柄数组(或传参)
int
grc
=
0
;
void
*
args
[
6
]
=
{
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
};
void
*
args
[
6
]
=
{
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
};
int
thread_start
(
ThreadFunc
AppExit
,
ThreadFunc
Mqttbeat
,
int
thread_start
(
ThreadFunc
AppExit
,
ThreadFunc
Mqttbeat
,
...
@@ -60,13 +62,14 @@ void *Mqttbeat(void *arg) {
...
@@ -60,13 +62,14 @@ void *Mqttbeat(void *arg) {
my_zlog_info
(
"Mqttbeat start"
);
my_zlog_info
(
"Mqttbeat start"
);
Delay_Ms
(
15
,
0
);
Delay_Ms
(
15
,
0
);
while
(
1
)
{
while
(
1
)
{
ipaddr
();
//获取ip
if
(
AppExit_pin_pwm
==
202
)
Delay_Ms
(
0
,
45
);
if
(
AppExit_pin_pwm
==
202
)
Delay_Ms
(
0
,
45
);
if
(
AppExit_pin_pwm
!=
202
)
Delay_Ms
(
0
,
100
);
if
(
AppExit_pin_pwm
!=
202
)
Delay_Ms
(
0
,
100
);
heartbeat_count
++
;
heartbeat_count
++
;
mqtt_wirte
();
//心跳,3s一个
if
(
grc
==
0
){
if
(
heartbeat_count
>=
30
)
heartbeat_count
=
0
;
ipaddr
();
//获取ip
mqtt_wirte
();
//心跳,3s一个
}
if
(
heartbeat_count
>=
30
)
heartbeat_count
=
0
;
}
}
return
NULL
;
return
NULL
;
}
}
...
@@ -81,11 +84,35 @@ void *opensh(void *arg) {
...
@@ -81,11 +84,35 @@ void *opensh(void *arg) {
void
*
Mqtt_onnect
(
void
*
arg
)
{
//mqtt异常处理,断开自动重连,简单粗暴
void
*
Mqtt_onnect
(
void
*
arg
)
{
//mqtt异常处理,断开自动重连,简单粗暴
Delay_Ms
(
5
,
0
);
Delay_Ms
(
5
,
0
);
mqtt_init
();
//mqtt初始化
mqtt_create
(
mosq
);
//创建mqtt客户端
if
(
mqtt_init
()
!=
0
)
{
while
(
1
){
my_zlog_fatal
(
"mqtt_init failed"
);
mqtt_create
(
mosq
);
//创建mqtt客户端
return
NULL
;
mqtt_cycle
(
mosq
);
//进入mqtt死循环,异常退出继续执行while
}
while
(
1
)
{
if
(
mosq
==
NULL
)
{
mqtt_init
();
if
(
!
mosq
)
{
mqtt_clean
(
mosq
);
my_zlog_error
(
"Failed to create mosquitto client"
);
continue
;
}
}
grc
=
mqtt_create
(
mosq
);
if
(
grc
!=
0
)
{
mqtt_clean
(
mosq
);
mosq
=
NULL
;
continue
;
}
grc
=
mqtt_cycle
(
mosq
);
// 阻塞直到断开
if
(
grc
!=
0
){
mqtt_clean
(
mosq
);
mosq
=
NULL
;
}
my_zlog_warn
(
"mqtt disconnected ..."
);
}
}
mqtt_clean
(
mosq
);
mqtt_clean
(
mosq
);
return
NULL
;
return
NULL
;
...
...
src/wifichange.c
0 → 100644
View file @
6d5a6a99
#include <cjson/cJSON.h>
#include "wifichange.h"
#include "mqtt.h"
#include "common.h"
#include "judg.h"
char
current_ssid
[
SSID_MAX_LEN
]
=
{
0
};
//用于存现在已连接WiFi
int
find_ssid
(
wifi_info_t
list
[],
int
count
,
const
char
*
ssid
)
{
//找重复的WiFi名称
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
if
(
strcmp
(
list
[
i
].
ssid
,
ssid
)
==
0
)
{
return
i
;
}
}
return
-
1
;
}
int
cmp_signal_desc
(
const
void
*
a
,
const
void
*
b
)
{
//进行qsort前,此未降序操作
wifi_info_t
*
wa
=
(
wifi_info_t
*
)
a
;
wifi_info_t
*
wb
=
(
wifi_info_t
*
)
b
;
return
wb
->
signal
-
wa
->
signal
;
}
void
get_current_wifi
()
{
//显示已连接代码
FILE
*
fp
=
popen
(
"nmcli -t -f ACTIVE,SSID dev wifi"
,
"r"
);
if
(
!
fp
)
{
my_zlog_error
(
"{
\"
error
\"
:
\"
无法执行扫描命令
\"
}"
);
return
;
}
char
line
[
256
];
while
(
fgets
(
line
,
sizeof
(
line
),
fp
))
{
char
*
active
=
strtok
(
line
,
":"
);
char
*
ssid
=
strtok
(
NULL
,
"
\n
"
);
if
(
active
&&
ssid
&&
strcmp
(
active
,
"yes"
)
==
0
&&
strlen
(
ssid
)
>
0
)
{
strncpy
(
current_ssid
,
ssid
,
SSID_MAX_LEN
-
1
);
current_ssid
[
SSID_MAX_LEN
-
1
]
=
'\0'
;
break
;
}
}
pclose
(
fp
);
}
void
scan_wifi_json
()
{
//显示以连接WiFi名称和周围已有的WiFi和过滤信号强度低于50的WiFi
get_current_wifi
();
char
line
[
100
];
//显示WiFi名称与信号强度
FILE
*
fp
=
popen
(
"nmcli -t -f SSID,SIGNAL dev wifi list"
,
"r"
);
if
(
!
fp
)
{
my_zlog_error
(
"{
\"
error
\"
:
\"
无法执行扫描命令
\"
}"
);
return
;
}
wifi_info_t
wifi_list
[
MAX_WIFI_LIST
];
int
wifi_count
=
0
;
while
(
fgets
(
line
,
sizeof
(
line
),
fp
))
{
char
*
ssid
=
strtok
(
line
,
":"
);
char
*
signal_str
=
strtok
(
NULL
,
"
\n
"
);
if
(
!
ssid
||
!
signal_str
)
continue
;
if
(
strlen
(
ssid
)
==
0
)
continue
;
int
signal
=
atoi
(
signal_str
);
int
idx
=
find_ssid
(
wifi_list
,
wifi_count
,
ssid
);
//去重 + 只保留信号最强的
if
(
idx
==
-
1
)
{
if
(
wifi_count
<
MAX_WIFI_LIST
)
{
strncpy
(
wifi_list
[
wifi_count
].
ssid
,
ssid
,
SSID_MAX_LEN
-
1
);
wifi_list
[
wifi_count
].
ssid
[
SSID_MAX_LEN
-
1
]
=
'\0'
;
wifi_list
[
wifi_count
].
signal
=
signal
;
wifi_count
++
;
}
}
else
{
if
(
signal
>
wifi_list
[
idx
].
signal
)
{
wifi_list
[
idx
].
signal
=
signal
;
}
}
}
pclose
(
fp
);
qsort
(
wifi_list
,
wifi_count
,
sizeof
(
wifi_info_t
),
cmp_signal_desc
);
//通过信号强度进行排序
//将WiFi信息转化为json格式
cJSON
*
root
=
cJSON_CreateObject
();
cJSON
*
body
=
cJSON_CreateObject
();
cJSON
*
head
=
cJSON_CreateObject
();
cJSON_AddStringToObject
(
body
,
"current_connected"
,
current_ssid
[
0
]
?
current_ssid
:
""
);
cJSON
*
array
=
cJSON_CreateArray
();
for
(
int
i
=
0
;
i
<
wifi_count
;
i
++
)
{
if
(
wifi_list
[
i
].
signal
<
50
)
continue
;
// 过滤信号小于50的
cJSON
*
item
=
cJSON_CreateObject
();
cJSON_AddStringToObject
(
item
,
"ssid"
,
wifi_list
[
i
].
ssid
);
cJSON_AddNumberToObject
(
item
,
"strength"
,
wifi_list
[
i
].
signal
);
cJSON_AddItemToArray
(
array
,
item
);
}
cJSON_AddItemToObject
(
body
,
"available_wifi"
,
array
);
cJSON_AddNumberToObject
(
head
,
"message_type"
,
3003
);
cJSON_AddItemToObject
(
root
,
"body"
,
body
);
cJSON_AddItemToObject
(
root
,
"head"
,
head
);
char
*
json_str
=
cJSON_Print
(
root
);
if
(
json_str
)
{
my_zlog_debug
(
"%s"
,
json_str
);
topic_middle_value
();
mosquitto_publish
(
mosq
,
NULL
,
TOPIC3
,
strlen
(
json_str
),
json_str
,
0
,
false
);
free
(
json_str
);
}
cJSON_Delete
(
root
);
}
int
connect_wifi
(
const
char
*
ssid
,
const
char
*
password
)
{
//连接WiFi
char
cmd
[
CMD_BUFFER_SIZE
];
snprintf
(
cmd
,
sizeof
(
cmd
),
"nmcli dev wifi connect '%s' password '%s' 2>&1"
,
ssid
,
password
);
my_zlog_debug
(
"🔌 正在连接 Wi-Fi:%s..."
,
ssid
);
FILE
*
fp
=
popen
(
cmd
,
"r"
);
if
(
!
fp
)
{
my_zlog_warn
(
"❌ 无法执行连接命令。"
);
return
-
1
;
}
char
output
[
512
]
=
{
0
};
fread
(
output
,
1
,
sizeof
(
output
)
-
1
,
fp
);
pclose
(
fp
);
if
(
strstr
(
output
,
"successfully activated"
))
{
my_zlog_debug
(
"✅ 已成功连接到:%s"
,
ssid
);
return
0
;
}
else
if
(
strstr
(
output
,
"No network with SSID"
))
{
my_zlog_warn
(
"❌ 连接失败:Wi-Fi '%s' 不存在。"
,
ssid
);
return
-
2
;
}
else
if
(
strstr
(
output
,
"Secrets were required"
)
||
strstr
(
output
,
"incorrect password"
))
{
my_zlog_warn
(
"❌ 连接失败:密码错误。"
);
return
-
3
;
}
else
{
my_zlog_error
(
"❌ 连接失败,未知错误:%s"
,
output
);
return
-
4
;
}
}
int
connect_wifi_with_fallback
(
const
char
*
ssid
,
const
char
*
password
,
const
char
*
fallback_ssid
)
{
//如果连接失败则回退
int
ret
=
connect_wifi
(
ssid
,
password
);
if
(
ret
!=
0
&&
fallback_ssid
&&
fallback_ssid
[
0
]
!=
0
)
{
my_zlog_warn
(
"⚠️ 连接失败,尝试回退连接原Wi-Fi:%s"
,
fallback_ssid
);
char
cmd
[
CMD_BUFFER_SIZE
];
snprintf
(
cmd
,
sizeof
(
cmd
),
"nmcli dev wifi connect '%s' 2>&1"
,
fallback_ssid
);
FILE
*
fp
=
popen
(
cmd
,
"r"
);
if
(
fp
)
{
char
output
[
512
]
=
{
0
};
fread
(
output
,
1
,
sizeof
(
output
)
-
1
,
fp
);
pclose
(
fp
);
if
(
strstr
(
output
,
"successfully activated"
))
{
my_zlog_debug
(
"✅ 回退连接成功:%s"
,
fallback_ssid
);
}
else
{
my_zlog_warn
(
"❌ 回退连接失败:%s"
,
output
);
}
}
else
{
my_zlog_error
(
"❌ 无法执行回退连接命令。"
);
}
}
return
ret
;
}
int
change_wifi_connect
(
const
char
*
wifi_ssid
,
const
char
*
wifi_password
)
{
//改变WiFi连接
get_current_wifi
(
current_ssid
,
sizeof
(
current_ssid
));
int
ret
=
connect_wifi_with_fallback
(
wifi_ssid
,
wifi_password
,
current_ssid
);
my_zlog_notice
(
"返回代码:%d"
,
ret
);
return
ret
;
}
int
can_access_internet
()
{
//测试网络是否连接成功
int
ret
=
system
(
"ping -c 1 -W 1 baidu.com > /dev/null 2>&1"
);
return
ret
==
0
;
// 返回 0 表示 ping 成功
}
int
delete_wifi_by_ssid
(
const
char
*
ssid
)
{
//删除对应wifi
if
(
ssid
==
NULL
||
strlen
(
ssid
)
==
0
)
{
fprintf
(
stderr
,
"SSID is empty."
);
return
-
1
;
}
char
command
[
256
];
// 构造 nmcli 删除连接命令
snprintf
(
command
,
sizeof
(
command
),
"nmcli -t -f NAME,TYPE connection show | grep '^%s:wifi$' | cut -d: -f1"
,
ssid
);
// 打开管道读取匹配到的连接名
FILE
*
fp
=
popen
(
command
,
"r"
);
if
(
!
fp
)
{
perror
(
"popen failed"
);
return
-
1
;
}
char
conn_name
[
128
];
if
(
fgets
(
conn_name
,
sizeof
(
conn_name
),
fp
)
==
NULL
)
{
my_zlog_debug
(
"No matching WiFi connection found for SSID: %s"
,
ssid
);
pclose
(
fp
);
return
-
1
;
}
pclose
(
fp
);
// 去除末尾换行符
conn_name
[
strcspn
(
conn_name
,
"
\n
"
)]
=
0
;
// 构造删除命令
snprintf
(
command
,
sizeof
(
command
),
"nmcli connection delete
\"
%s
\"
"
,
conn_name
);
int
ret
=
system
(
command
);
if
(
ret
==
0
)
{
my_zlog_debug
(
"Successfully deleted WiFi connection: %s"
,
conn_name
);
}
else
{
my_zlog_warn
(
"Failed to delete WiFi connection: %s"
,
conn_name
);
}
return
ret
;
}
// 检查文件是否存在,不存在则创建
int
check_or_create_wifi_conf
()
{
struct
stat
st
;
if
(
stat
(
WIFI_CONF_PATH
,
&
st
)
==
0
)
{
// 文件已存在
return
0
;
}
// 创建空配置文件
FILE
*
fp
=
fopen
(
WIFI_CONF_PATH
,
"w"
);
if
(
!
fp
)
{
perror
(
"Failed to create device_wifi.conf"
);
return
-
1
;
}
fclose
(
fp
);
return
0
;
}
// 写入 SSID 和密码到文件
int
write_wifi_conf
(
const
char
*
ssid
,
const
char
*
password
)
{
FILE
*
fp
=
fopen
(
WIFI_CONF_PATH
,
"w"
);
if
(
!
fp
)
{
perror
(
"Failed to open device_wifi.conf for writing"
);
return
-
1
;
}
fprintf
(
fp
,
"SSID=%s
\n
"
,
ssid
);
fprintf
(
fp
,
"PASSWORD=%s
\n
"
,
password
);
fclose
(
fp
);
return
0
;
}
// 从文件读取 SSID,比较当前连接的 WiFi 名称
int
compare_ssid_with_file
()
{
char
file_ssid
[
128
]
=
{
0
};
FILE
*
fp
=
fopen
(
WIFI_CONF_PATH
,
"r"
);
if
(
!
fp
)
{
perror
(
"Failed to open device_wifi.conf"
);
return
-
1
;
}
char
line
[
256
];
while
(
fgets
(
line
,
sizeof
(
line
),
fp
))
{
if
(
strncmp
(
line
,
"SSID="
,
5
)
==
0
)
{
strncpy
(
file_ssid
,
line
+
5
,
sizeof
(
file_ssid
)
-
1
);
file_ssid
[
strcspn
(
file_ssid
,
"
\n
"
)]
=
0
;
break
;
}
}
fclose
(
fp
);
get_current_wifi
();
my_zlog_debug
(
"File SSID: %s"
,
file_ssid
);
my_zlog_debug
(
"Current SSID: %s"
,
current_ssid
);
if
(
strcmp
(
file_ssid
,
current_ssid
)
==
0
)
{
my_zlog_debug
(
"WiFi matches the config file."
);
return
1
;
}
else
{
my_zlog_debug
(
"WiFi does NOT match the config file."
);
return
0
;
}
}
\ No newline at end of file
system/gpio/gpio_common.h
View file @
6d5a6a99
...
@@ -11,10 +11,6 @@ extern int device_delay_count;//延时计算函数,使用前必须置0
...
@@ -11,10 +11,6 @@ extern int device_delay_count;//延时计算函数,使用前必须置0
#define PWM_PIN_SPEED 21
#define PWM_PIN_SPEED 21
#define PWM_PIN_CHANGE 2
#define PWM_PIN_CHANGE 2
void
Device_exit_end
();
//main最后结束需要调用的函数
void
init_gpioWPi
(
int
*
values_pin
);
//gpio引脚初始化
void
init_gpioWPi
(
int
*
values_pin
);
//gpio引脚初始化
void
init_gpioPwm
(
int
*
values_pwm
);
void
init_gpioPwm
(
int
*
values_pwm
);
...
@@ -27,4 +23,6 @@ void pin_value(int pin,int value);
...
@@ -27,4 +23,6 @@ void pin_value(int pin,int value);
void
pwm_init_speed
();
void
pwm_init_speed
();
void
pwm_value
(
int
pin
,
int
value
);
//软件陪我们控制调速
void
pwm_value
(
int
pin
,
int
value
);
//软件陪我们控制调速
void
Device_exit_end
();
//main最后结束需要调用的函数
#endif
#endif
system/gpio/gpio_pwm_tank0202.c
View file @
6d5a6a99
...
@@ -212,7 +212,7 @@ long long get_current_time_millis() {
...
@@ -212,7 +212,7 @@ long long get_current_time_millis() {
return
(
long
long
)
tv
.
tv_sec
*
1000
+
tv
.
tv_usec
/
1000
;
return
(
long
long
)
tv
.
tv_sec
*
1000
+
tv
.
tv_usec
/
1000
;
}
}
void
tank_shot_back_stop_task_function
(
void
*
arg
)
{
void
tank_shot_back_stop_task_function
(
void
*
arg
)
{
//多线程处理坦克发射后退线程池
while
(
1
){
while
(
1
){
long
long
interval
=
shot_device_time_start
-
shot_device_time_end
;
long
long
interval
=
shot_device_time_start
-
shot_device_time_end
;
if
(
device_delay_count
>
10
&&
device_delay_count
<=
25
)
mode_right_back
(
0
);
if
(
device_delay_count
>
10
&&
device_delay_count
<=
25
)
mode_right_back
(
0
);
...
@@ -224,6 +224,7 @@ void tank_shot_back_stop_task_function(void *arg) {
...
@@ -224,6 +224,7 @@ void tank_shot_back_stop_task_function(void *arg) {
}
}
}
}
free
(
arg
);
}
}
ThreadPool
*
pool_tank
;
ThreadPool
*
pool_tank
;
...
@@ -231,6 +232,14 @@ void tank_shot_back_stop_task_end(){
...
@@ -231,6 +232,14 @@ void tank_shot_back_stop_task_end(){
thread_pool_destroy
(
pool_tank
);
thread_pool_destroy
(
pool_tank
);
}
}
void
tank_shot_pthrpoll_task_init
(){
int
*
arg
=
malloc
(
sizeof
(
int
));
*
arg
=
1
;
pool_tank
=
thread_pool_init
(
1
,
1
);
thread_pool_add_task
(
pool_tank
,
tank_shot_back_stop_task_function
,
&
arg
);
my_zlog_debug
(
"线程池打开"
);
}
int
tank_shot_back_stop
(
int
pin
,
int
val
){
int
tank_shot_back_stop
(
int
pin
,
int
val
){
static
int
shot_count
=
0
;
static
int
shot_count
=
0
;
shot_device_time_start
=
get_current_time_millis
();
shot_device_time_start
=
get_current_time_millis
();
...
@@ -246,10 +255,7 @@ int tank_shot_back_stop(int pin,int val){
...
@@ -246,10 +255,7 @@ int tank_shot_back_stop(int pin,int val){
}
}
if
(
val
!=
0
)
{
if
(
val
!=
0
)
{
if
(
shot_count
==
0
){
if
(
shot_count
==
0
){
int
arg
=
1
;
tank_shot_pthrpoll_task_init
();
pool_tank
=
thread_pool_init
(
1
,
1
);
thread_pool_add_task
(
pool_tank
,
tank_shot_back_stop_task_function
,
&
arg
);
my_zlog_debug
(
"线程池打开"
);
shot_count
=
1
;
shot_count
=
1
;
}
}
shot_count
=
1
;
shot_count
=
1
;
...
...
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