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
9a95bc90
Commit
9a95bc90
authored
Jul 18, 2025
by
学习的菜鸟
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
3
parent
5ad47074
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
357 additions
and
0 deletions
+357
-0
main
build/main
+0
-0
gpio_init.c
drivers/gpio/gpio_init.c
+117
-0
gpio_init.h
drivers/gpio/gpio_init.h
+28
-0
http_request.c
modules/http/http_request.c
+172
-0
http_request.h
modules/http/http_request.h
+20
-0
mqtt_infor_handle.c
modules/mqtt/mqtt_infor_handle.c
+20
-0
No files found.
build/main
View file @
9a95bc90
No preview for this file type
drivers/gpio/gpio_init.c
View file @
9a95bc90
#include "common.h"
#include "gpio_init.h"
#include "mqtt.h"
#include "request.h"
#define MIN_DUTY 0 // 最小占空比
#define MAX_DUTY 100 // 最大占空比
// int device_delay_count =0;//设备计算函数
int
g_gpioPwm
[
30
];
//软件控制
int
g_gpiowpi
[
40
];
//能使用高低引脚和其他引脚
int
g_gpiocount
=
0
;
int
g_gpio_softpwmcount
=
0
;
void
init_gpioWPi
(
int
*
values_pin
)
{
while
(
values_pin
[
g_gpiocount
]
!=
-
1
)
{
g_gpiocount
++
;
}
for
(
int
i
=
0
;
i
<
g_gpiocount
;
i
++
)
{
g_gpiowpi
[
i
]
=
values_pin
[
i
];
}
// 设置所有GPIO为输出模式
for
(
int
i
=
0
;
i
<
g_gpiocount
;
i
++
)
{
pinMode
(
g_gpiowpi
[
i
],
OUTPUT
);
digitalWrite
(
g_gpiowpi
[
i
],
LOW
);
// 初始化所有引脚为低电平
}
}
void
init_gpioPwm
(
int
*
values_pwm
)
{
while
(
values_pwm
[
g_gpio_softpwmcount
]
!=
-
1
)
{
g_gpio_softpwmcount
++
;
}
for
(
int
i
=
0
;
i
<
g_gpio_softpwmcount
;
i
++
)
{
g_gpioPwm
[
i
]
=
values_pwm
[
i
];
}
for
(
int
i
=
0
;
i
<
g_gpio_softpwmcount
;
i
++
)
{
softPwmCreate
(
g_gpioPwm
[
i
],
MIN_DUTY
,
MAX_DUTY
);
}
}
void
pin_all_default
()
{
//全部至低电平,车和坦克共用
for
(
int
i
=
0
;
i
<
g_gpiocount
;
i
++
)
{
if
(
g_gpiowpi
[
i
]
==
16
||
g_gpiowpi
[
i
]
==
20
||
g_gpiowpi
[
i
]
==
22
)
continue
;
digitalWrite
(
g_gpiowpi
[
i
],
LOW
);
}
}
void
pwm_all_default
()
{
//全部至低电平,车和坦克共用
for
(
int
i
=
0
;
i
<
g_gpio_softpwmcount
;
i
++
)
{
softPwmWrite
(
g_gpioPwm
[
i
],
0
);
}
}
void
pwm_value
(
int
pin
,
int
value
)
{
//软件陪我们控制调速
for
(
int
i
=
0
;
i
<=
g_gpio_softpwmcount
;
i
++
)
{
if
(
pin
==
g_gpioPwm
[
i
])
{
break
;
}
if
(
i
==
g_gpio_softpwmcount
)
{
return
;
}
}
int
shot_speed
=
(
int
)(
shot_speed_value
*
100
);
if
(
value
==
1
)
{
if
(
pin
==
27
){
softPwmWrite
(
pin
,
shot_speed
);
my_zlog_debug
(
"pin:%d,%d"
,
pin
,
shot_speed
);
}
else
{
if
(
AppExit_pin_pwm
==
202
)
softPwmWrite
(
pin
,
30
);
if
(
AppExit_pin_pwm
!=
202
)
softPwmWrite
(
pin
,
shot_speed
);
}
}
else
if
(
value
==
0
)
{
softPwmWrite
(
pin
,
0
);
my_zlog_debug
(
"pin:%d,0"
,
pin
);
}
}
void
pin_value
(
int
pin
,
int
value
)
{
//引脚控制
for
(
int
i
=
0
;
i
<=
g_gpiocount
;
i
++
)
{
if
(
pin
==
g_gpiowpi
[
i
])
{
break
;
}
if
(
i
==
g_gpiocount
)
{
return
;
}
}
if
(
value
==
1
)
{
digitalWrite
(
pin
,
HIGH
);
my_zlog_debug
(
"pin:%d,%d"
,
pin
,
HIGH
);
}
else
if
(
value
==
0
)
{
digitalWrite
(
pin
,
LOW
);
my_zlog_debug
(
"pin:%d,%d"
,
pin
,
LOW
);
}
}
void
pwm_init_speed
()
{
int
pwm_clock
=
24000000
/
(
50
*
1000
);
// 定义 PWM 频率为 50Hz
pinMode
(
PWM_PIN_SPEED
,
PWM_OUTPUT
);
pinMode
(
PWM_PIN_CHANGE
,
PWM_OUTPUT
);
pwmSetClock
(
PWM_PIN_SPEED
,
pwm_clock
);
//=19200*1000/(hz*2000)
pwmSetRange
(
PWM_PIN_SPEED
,
1000
);
//占空比范围
pwmSetClock
(
PWM_PIN_CHANGE
,
pwm_clock
);
//=19200*1000/(hz*2000)
pwmSetRange
(
PWM_PIN_CHANGE
,
1000
);
//占空比范围
}
// void Device_exit_end(){//main最后结束需要调用的函数
// if(AppExit_pin_pwm == 202) tank_shot_back_stop_task_end();
// }
\ No newline at end of file
drivers/gpio/gpio_init.h
View file @
9a95bc90
/*
此文件为通用文件,一般用于50hz的驱动等和引脚高低,适合车和船使用,
如果有其他需求,此文件就不需要使用
*/
#ifndef GPIO_COMMON_H__
#define GPIO_COMMON_H__
extern
int
device_delay_count
;
//延时计算函数,使用前必须置0
// 定义 PWM 引脚的 WiringPi 编号
#define PWM_PIN_SPEED 21
#define PWM_PIN_CHANGE 2
void
init_gpioWPi
(
int
*
values_pin
);
//gpio引脚初始化
void
init_gpioPwm
(
int
*
values_pwm
);
void
pin_value
(
int
pin
,
int
value
);
//控制引脚高低
// 定义 PWM 引脚的 WiringPi 编号
void
pin_all_default
();
//拉低车的控制引脚
void
pwm_all_default
();
//软件pwm置0引脚
void
pin_value
(
int
pin
,
int
value
);
void
pwm_init_speed
();
void
pwm_value
(
int
pin
,
int
value
);
//软件陪我们控制调速
void
Device_exit_end
();
//main最后结束需要调用的函数
#endif
modules/http/http_request.c
0 → 100644
View file @
9a95bc90
#include<curl/curl.h>
#include <cjson/cJSON.h>
#include "http_request.h"
const
char
*
post
=
"http://47.119.190.60/api/v1/device/config"
;
int
errCodeValue
;
char
*
errMsgptr
=
NULL
;
bool
enable_buzzer_value
=
0
;
char
*
force_versionptr
=
NULL
;
double
warn_voltage_value
;
double
err_voltage_value
;
double
shot_speed_value
=
0
.
7
;
// 回调函数,用于处理接收到的数据
static
size_t
WriteMemoryCallback
(
void
*
contents
,
size_t
size
,
size_t
nmemb
,
void
*
userp
)
{
size_t
realsize
=
size
*
nmemb
;
struct
MemoryStruct
*
mem
=
(
struct
MemoryStruct
*
)
userp
;
char
*
ptr
=
realloc
(
mem
->
memory
,
mem
->
size
+
realsize
+
1
);
if
(
!
ptr
)
{
printf
(
"not enough memory (realloc returned NULL)
\n
"
);
return
0
;
}
mem
->
memory
=
ptr
;
memcpy
(
&
(
mem
->
memory
[
mem
->
size
]),
contents
,
realsize
);
mem
->
size
+=
realsize
;
mem
->
memory
[
mem
->
size
]
=
0
;
return
realsize
;
}
//分析接口拉的json分析
int
analysis_request_json
(
char
*
payload_str
)
{
cJSON
*
json
=
cJSON_Parse
(
payload_str
);
if
(
json
==
NULL
)
{
fprintf
(
stderr
,
"Error before: [%s]
\n
"
,
cJSON_GetErrorPtr
());
return
1
;
}
// 提取 data 对象
cJSON
*
data
=
cJSON_GetObjectItem
(
json
,
"data"
);
if
(
data
==
NULL
)
{
my_zlog_warn
(
"date数据段为空"
);
return
2
;
}
// 提取 errCode
cJSON
*
errCode
=
cJSON_GetObjectItem
(
json
,
"errCode"
);
if
(
errCode
==
NULL
)
{
my_zlog_warn
(
"errCode数据段为空"
);
return
3
;
}
// 提取 errMsg
cJSON
*
errMsg
=
cJSON_GetObjectItem
(
json
,
"errMsg"
);
if
(
errMsg
==
NULL
)
{
my_zlog_warn
(
"errMsg数据段为空"
);
}
// 提取 data 内部的键值对
cJSON
*
enable_buzzer
=
cJSON_GetObjectItem
(
data
,
"enable_buzzer"
);
cJSON
*
force_version
=
cJSON_GetObjectItem
(
data
,
"force_version"
);
cJSON
*
warn_voltage
=
cJSON_GetObjectItem
(
data
,
"warn_voltage"
);
cJSON
*
err_voltage
=
cJSON_GetObjectItem
(
data
,
"err_voltage"
);
cJSON
*
shot_speed
=
cJSON_GetObjectItem
(
data
,
"shot_speed"
);
if
(
cJSON_IsNumber
(
errCode
))
{
errCodeValue
=
errCode
->
valueint
;
if
(
errCodeValue
!=
0
){
cJSON_Delete
(
json
);
return
6
;
}
}
if
(
cJSON_IsString
(
errMsg
))
{
errMsgptr
=
errMsg
->
valuestring
;
}
if
(
cJSON_IsBool
(
enable_buzzer
))
{
enable_buzzer_value
=
cJSON_IsTrue
(
enable_buzzer
);
my_zlog_debug
(
"enable_buzzer: %s"
,
enable_buzzer_value
?
"true"
:
"false"
);
}
if
(
cJSON_IsString
(
force_version
))
{
force_versionptr
=
force_version
->
valuestring
;
my_zlog_debug
(
"force_versionptr: %s"
,
force_versionptr
);
}
if
(
cJSON_IsNumber
(
warn_voltage
))
{
warn_voltage_value
=
warn_voltage
->
valuedouble
;
my_zlog_debug
(
"warn_voltage_value: %.2f"
,
warn_voltage_value
);
}
if
(
cJSON_IsNumber
(
err_voltage
))
{
err_voltage_value
=
err_voltage
->
valuedouble
;
my_zlog_debug
(
"err_voltage_value: %.2f"
,
err_voltage_value
);
}
if
(
cJSON_IsNumber
(
shot_speed
))
{
if
(
0
<=
shot_speed_value
&&
shot_speed_value
<=
1
)
{
shot_speed_value
=
shot_speed
->
valuedouble
;
}
my_zlog_debug
(
"shot_speed_value: %.2f"
,
shot_speed_value
);
}
cJSON_Delete
(
json
);
return
0
;
}
//像接口发送设备id
char
*
wirte_json
()
{
cJSON
*
body
=
cJSON_CreateObject
();
topic_middle_value
();
cJSON_AddStringToObject
(
body
,
"device_id"
,
TOPIC3
);
// 发送设备id
char
*
payload
=
cJSON_PrintUnformatted
(
body
);
my_zlog_debug
(
"%s"
,
payload
);
cJSON_Delete
(
body
);
// 释放 cJSON 对象
return
payload
;
}
int
request_date
()
{
//请求数据
CURL
*
curl
;
CURLcode
res
;
// 初始化libcurl
curl_global_init
(
CURL_GLOBAL_DEFAULT
);
curl
=
curl_easy_init
();
if
(
!
curl
)
{
return
1
;
}
struct
MemoryStruct
chunk
;
chunk
.
memory
=
malloc
(
1
);
// 初始化为空字符串
chunk
.
size
=
0
;
// 设置请求URL
curl_easy_setopt
(
curl
,
CURLOPT_URL
,
post
);
// 设置POST请求
curl_easy_setopt
(
curl
,
CURLOPT_POST
,
1L
);
char
*
post_data
=
wirte_json
();
curl_easy_setopt
(
curl
,
CURLOPT_POSTFIELDS
,
post_data
);
// 设置HTTP头
struct
curl_slist
*
headers
=
NULL
;
headers
=
curl_slist_append
(
headers
,
"Content-Type: application/json"
);
curl_easy_setopt
(
curl
,
CURLOPT_HTTPHEADER
,
headers
);
// 设置接收数据的回调函数
curl_easy_setopt
(
curl
,
CURLOPT_WRITEFUNCTION
,
WriteMemoryCallback
);
curl_easy_setopt
(
curl
,
CURLOPT_WRITEDATA
,
(
void
*
)
&
chunk
);
// 执行请求
res
=
curl_easy_perform
(
curl
);
// 检查错误
if
(
res
!=
CURLE_OK
)
{
fprintf
(
stderr
,
"curl_easy_perform() failed: %s
\n
"
,
curl_easy_strerror
(
res
));
}
else
{
// 打印响应数据
my_zlog_debug
(
"%s"
,
chunk
.
memory
);
analysis_request_json
(
chunk
.
memory
);
}
// 清理
free
(
post_data
);
free
(
chunk
.
memory
);
curl_slist_free_all
(
headers
);
curl_easy_cleanup
(
curl
);
curl_global_cleanup
();
return
0
;
}
\ No newline at end of file
modules/http/http_request.h
0 → 100644
View file @
9a95bc90
#ifndef HTTP_REQUEST_H
#define HTTP_REQUEST_H
#include"common.h"// 用于存储HTTP响应数据的结构体
#define HTTP_REQUEST_INDEX 1
struct
MemoryStruct
{
char
*
memory
;
size_t
size
;
};
//初始化拉取接口,接收结构体
extern
int
errCodeValue
;
//是否错误,一般不会发生
extern
bool
enable_buzzer_value
;
//是否打开蜂鸣器
extern
double
shot_speed_value
;
//射击的pwm占空比
extern
double
warn_voltage_value
;
//警告电压
extern
double
err_voltage_value
;
//错误电压
int
request_date
();
//请求数据
#endif
modules/mqtt/mqtt_infor_handle.c
View file @
9a95bc90
...
...
@@ -8,6 +8,8 @@
#include "common.h"
#include "mylog.h"
bool
g_verified_mode
=
TRUE
;
//控制是否需要验证
int
g_heartbeat_count
=
0
;
unsigned
char
g_valt
[
4
];
//存放mqtt接收的tpye,mode等
...
...
@@ -167,6 +169,12 @@ void message_4(cJSON *body){//message 为4时候
//当接收到2时候验证
void
message_2_judyverify
(
cJSON
*
body
){
if
(
g_verified_mode
==
FALSE
)
{
//refresh_cam();
my_zlog_warn
(
"不使用验证"
);
return
;
}
receive_jwt
(
body
);
if
(
g_verify_index
==
0
)
{
//refresh_cam();
...
...
@@ -177,6 +185,12 @@ void message_2_judyverify(cJSON *body){
//当接收到3时候验证
void
message_3_judyverify
(
cJSON
*
body
){
if
(
g_verified_mode
==
FALSE
)
{
message_3
(
body
);
my_zlog_warn
(
"不使用验证"
);
return
;
}
receive_jwt
(
body
);
if
(
g_verify_index
==
0
)
{
message_3
(
body
);
...
...
@@ -187,6 +201,12 @@ void message_3_judyverify(cJSON *body){
//当接收到4时候验证
void
message_4_judyverify
(
cJSON
*
body
){
if
(
g_verified_mode
==
FALSE
)
{
message_4
(
body
);
my_zlog_warn
(
"不使用验证"
);
return
;
}
receive_jwt
(
body
);
if
(
g_verify_index
==
0
)
{
message_4
(
body
);
...
...
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