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
c8e33e86
Commit
c8e33e86
authored
Nov 29, 2024
by
957dd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Your commit message
parent
5f7d710c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
84 additions
and
18 deletions
+84
-18
master
gitcar/master
+0
-1
main.c
main.c
+13
-12
mqtt.c
mqtt.c
+24
-0
mqtt.h
mqtt.h
+8
-1
serial.c
serial.c
+39
-4
No files found.
master
@
8215ad18
Subproject commit 8215ad1887f59c6c08ac10c631d84ccbbc0f41cc
main.c
View file @
c8e33e86
...
...
@@ -2,7 +2,8 @@
#include "mqtt.h"
#include "serial.h"
struct
mosquitto
*
mosq
;
//mosq = mosquitto_new(CLIENT_ID, true, NULL);
void
Delay_Ms
()
{
...
...
@@ -54,21 +55,21 @@ int main(int argc, char *argv[]) {
gStart
=
time
(
NULL
);
// 初始化 mosquitto 库
mosquitto_lib_init
();
// 创建 mosquitto 客户端
mosq
=
mosquitto_new
(
CLIENT_ID
,
true
,
NULL
);
if
(
!
mosq
)
{
fprintf
(
stderr
,
"Failed to create mosquitto client
\n
"
);
return
EXIT_FAILURE
;
}
init_mqtt
(
mosq
);
// 创建 mosquitto 客户端
//if (!mosq) {
// fprintf(stderr, "Failed to create mosquitto client\n");
// return EXIT_FAILURE;
// }
// 设置连接和消息回调
mosquitto_connect_callback_set
(
mosq
,
on_connect
);
mosquitto_message_callback_set
(
mosq
,
on_message
);
//
mosquitto_connect_callback_set(mosq, on_connect);
//
mosquitto_message_callback_set(mosq, on_message);
// 设置用户名和密码
mosquitto_username_pw_set
(
mosq
,
USERNAME
,
PASSWORD
);
//
mosquitto_username_pw_set(mosq, USERNAME, PASSWORD);
// 连接到 MQTT 代理
rc
=
mosquitto_connect
(
mosq
,
BROKER_ADDRESS
,
BROKER_PORT
,
60
);
...
...
@@ -79,7 +80,7 @@ int main(int argc, char *argv[]) {
}
ipaddr
();
mosquitto_publish
(
mosq
,
NULL
,
TOPIC
,
strlen
(
ip_address
),
ip_address
,
0
,
false
);
//将ip发送给mqtt
send_mqtt_message
(
mosq
,
TOPIC
,
ip_address
);
//将ip发送给mqtt
pthread_t
thread
[
2
];
...
...
mqtt.c
View file @
c8e33e86
...
...
@@ -3,6 +3,30 @@
time_t
gStart
;
int
gPwmCount
=
0
;
// 计数
struct
mosquitto
*
mosq
;
int
init_mqtt
(
struct
mosquitto
*
mosq
)
{
if
(
!
mosq
)
{
fprintf
(
stderr
,
"Failed to create Mosquitto client
\n
"
);
return
-
1
;
}
// 设置连接和消息回调
mosquitto_connect_callback_set
(
mosq
,
on_connect
);
mosquitto_message_callback_set
(
mosq
,
on_message
);
//设置用户名和密码
mosquitto_username_pw_set
(
mosq
,
USERNAME
,
PASSWORD
);
}
void
send_mqtt_message
(
struct
mosquitto
*
mosq
,
const
char
*
topic
,
const
char
*
message
)
{
mosquitto_publish
(
mosq
,
NULL
,
topic
,
strlen
(
message
),
message
,
0
,
false
)
;
}
void
on_connect
(
struct
mosquitto
*
mosq
,
void
*
obj
,
int
rc
)
{
...
...
mqtt.h
View file @
c8e33e86
...
...
@@ -8,18 +8,25 @@
#include "ip.h"
#include "serial.h"
#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883
#define CLIENT_ID "device1car/carer54"
#define CLIENT_ID "device1car/carer54
34
"
#define TOPIC "devices/controcar03"
#define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码
extern
struct
mosquitto
*
mosq
;
extern
time_t
gStart
;
extern
int
gPwmCount
;
// 计数
int
init_mqtt
(
struct
mosquitto
*
mosq
)
;
void
send_mqtt_message
(
struct
mosquitto
*
mosq
,
const
char
*
topic
,
const
char
*
message
);
void
on_connect
(
struct
mosquitto
*
mosq
,
void
*
obj
,
int
rc
);
void
on_message
(
struct
mosquitto
*
mosq
,
void
*
obj
,
const
struct
mosquitto_message
*
message
)
;
...
...
serial.c
View file @
c8e33e86
#include "serial.h"
#include "mqtt.h"
int
fd
;
...
...
@@ -31,20 +32,54 @@ int init_serial(const char *port, int baudrate) {
return
fd
;
}
void
Delay_Ms1
()
{
struct
timespec
ts1
;
ts1
.
tv_sec
=
1
;
// 秒
ts1
.
tv_nsec
=
0
;
//100000000; // 1毫秒 = 1000000纳秒
nanosleep
(
&
ts1
,
NULL
);
}
void
serial_Receive
()
{
char
buffer
[
254
]
=
"
\0
"
;
unsigned
char
buffer
[
256
];
//mosq = mosquitto_new(CLIENT_ID, true, NULL);
while
(
1
)
{
int
bytes_read
=
read
(
fd
,
buffer
,
sizeof
(
buffer
)
-
1
);
if
(
bytes_read
>
0
)
{
//printf("Received chatdate: %s\n", buffer);
buffer
[
bytes_read
]
=
'\0
'
;
printf
(
"Received chatdate:: %
s
\n
"
,
buffer
);
//buffer[bytes_read] = '0xFF
';
printf
(
"Received chatdate:: %
d
\n
"
,
buffer
[
1
]
);
//int receivedNumber = atoi(buffer);
//printf("Received intdate: %d\n", receivedNumber);
//mosquitto_publish(mosq, NULL, TOPIC, strlen(buffer), buffer, 0, false);
//printf("%s\n",mosq);
int
i
=
buffer
[
1
];
if
(
i
==
1
)
{
send_mqtt_message
(
mosq
,
TOPIC
,
"1"
);
i
=
0
;
}
if
(
i
==
2
)
{
send_mqtt_message
(
mosq
,
TOPIC
,
"2"
);
i
=
0
;
}
if
(
i
==
3
)
{
send_mqtt_message
(
mosq
,
TOPIC
,
"3"
);
i
=
0
;
}
if
(
i
==
4
)
{
send_mqtt_message
(
mosq
,
TOPIC
,
"4"
);
i
=
0
;
}
}
Delay_Ms1
();
}
close
(
fd
);
//free(buffer);
...
...
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