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
b351dc32
Commit
b351dc32
authored
Jan 06, 2025
by
957dd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
88998a2a
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
18 additions
and
20 deletions
+18
-20
ip.c
ip.c
+0
-1
ip.h
ip.h
+6
-1
log.txt
log/log.txt
+0
-0
main
main
+0
-0
mqtt.c
mqtt.c
+12
-18
No files found.
ip.c
View file @
b351dc32
...
@@ -23,7 +23,6 @@ int ipaddr() {
...
@@ -23,7 +23,6 @@ int ipaddr() {
if
(
ifa
->
ifa_addr
!=
NULL
&&
ifa
->
ifa_addr
->
sa_family
==
AF_INET
)
{
if
(
ifa
->
ifa_addr
!=
NULL
&&
ifa
->
ifa_addr
->
sa_family
==
AF_INET
)
{
sa
=
(
struct
sockaddr_in
*
)
ifa
->
ifa_addr
;
sa
=
(
struct
sockaddr_in
*
)
ifa
->
ifa_addr
;
inet_ntop
(
AF_INET
,
&
sa
->
sin_addr
,
ip_address
,
sizeof
(
ip_address
));
inet_ntop
(
AF_INET
,
&
sa
->
sin_addr
,
ip_address
,
sizeof
(
ip_address
));
//printf("%s: %s ", ifa->ifa_name, ip_address);
}
}
}
}
...
...
ip.h
View file @
b351dc32
#ifndef __IP_H__
#ifndef __IP_H__
#define __IP_H__
#define __IP_H__
extern
char
ip_address
[];
//INET_ADDRSTRLEN
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
extern
char
ip_address
[
INET_ADDRSTRLEN
];
//
int
ipaddr
();
int
ipaddr
();
...
...
log/log.txt
100644 → 100755
View file @
b351dc32
File mode changed from 100644 to 100755
main
View file @
b351dc32
No preview for this file type
mqtt.c
View file @
b351dc32
...
@@ -206,12 +206,12 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -206,12 +206,12 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if
(
message
->
payload
&&
message
->
payloadlen
)
if
(
message
->
payload
&&
message
->
payloadlen
)
{
{
// 将消息内容转换为字符串
// 将消息内容转换为字符串
//
char *payload_str = (char *)malloc(message->payloadlen + 1);
char
*
payload_str
=
(
char
*
)
malloc
(
message
->
payloadlen
+
1
);
//
if (!payload_str)
if
(
!
payload_str
)
//
{
{
//
return;
return
;
//
}
}
static
char
payload_str
[
200
];
memcpy
(
payload_str
,
message
->
payload
,
message
->
payloadlen
);
memcpy
(
payload_str
,
message
->
payload
,
message
->
payloadlen
);
payload_str
[
message
->
payloadlen
]
=
'\0'
;
payload_str
[
message
->
payloadlen
]
=
'\0'
;
...
@@ -220,7 +220,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -220,7 +220,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if
(
json
==
NULL
)
if
(
json
==
NULL
)
{
{
fprintf
(
stderr
,
"Error before: [%s]
\n
"
,
cJSON_GetErrorPtr
());
fprintf
(
stderr
,
"Error before: [%s]
\n
"
,
cJSON_GetErrorPtr
());
//
free(payload_str);
free
(
payload_str
);
}
}
else
else
{
{
...
@@ -228,19 +228,20 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -228,19 +228,20 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
cJSON
*
head
=
cJSON_GetObjectItem
(
json
,
"head"
);
cJSON
*
head
=
cJSON_GetObjectItem
(
json
,
"head"
);
if
(
!
cJSON_IsObject
(
head
))
if
(
!
cJSON_IsObject
(
head
))
{
{
//
free(payload_str);
free
(
payload_str
);
return
;
return
;
}
}
// 提取 message_type
// 提取 message_type
cJSON
*
message_type
=
cJSON_GetObjectItem
(
head
,
"message_type"
);
cJSON
*
message_type
=
cJSON_GetObjectItem
(
head
,
"message_type"
);
if
(
!
cJSON_IsNumber
(
message_type
))
if
(
!
cJSON_IsNumber
(
message_type
))
{
{
//
free(payload_str);
free
(
payload_str
);
return
;
return
;
}
}
cJSON
*
body
=
cJSON_GetObjectItem
(
json
,
"body"
);
cJSON
*
body
=
cJSON_GetObjectItem
(
json
,
"body"
);
if
(
!
cJSON_IsObject
(
body
))
if
(
!
cJSON_IsObject
(
body
))
{
{
free
(
payload_str
);
return
;
return
;
}
}
cJSON
*
pwm_ctrl
=
cJSON_GetObjectItem
(
body
,
"pwm_ctrl"
);
cJSON
*
pwm_ctrl
=
cJSON_GetObjectItem
(
body
,
"pwm_ctrl"
);
...
@@ -248,7 +249,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -248,7 +249,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if
(
pwm_ctrl
==
NULL
||
pin_setctrl
==
NULL
)
if
(
pwm_ctrl
==
NULL
||
pin_setctrl
==
NULL
)
{
{
//
free(payload_str);
free
(
payload_str
);
return
;
return
;
}
}
gmessage_type
=
message_type
->
valueint
;
gmessage_type
=
message_type
->
valueint
;
...
@@ -275,7 +276,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -275,7 +276,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
}
}
// 释放 payload 字符串
// 释放 payload 字符串
//
free(payload_str);
free
(
payload_str
);
}
}
}
}
...
@@ -300,18 +301,11 @@ int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端
...
@@ -300,18 +301,11 @@ int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端
mosquitto_destroy
(
mosq
);
mosquitto_destroy
(
mosq
);
//return EXIT_FAILURE;
//return EXIT_FAILURE;
}
}
//mosquitto_subscribe_opt_set(mosq, TOPIC, strlen(TOPIC), 1, MOSQ_OPT_NO_LOCAL);
}
}
int
mqtt_cycle
(
struct
mosquitto
*
mosq
)
int
mqtt_cycle
(
struct
mosquitto
*
mosq
)
{
{
//mosquitto_message_callback_set(mosq, NULL);
// 开始循环,处理网络流量
//mosquitto_loop_start(mosq);//后台处理
mosquitto_loop_forever
(
mosq
,
-
1
,
1
);
// -1 表示无限等待,1 表示处理消息的最大数量
mosquitto_loop_forever
(
mosq
,
-
1
,
1
);
// -1 表示无限等待,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