Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
ship-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
ship-controlserver
Commits
71aebce6
Commit
71aebce6
authored
Dec 04, 2024
by
957dd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
45
parent
1568ed46
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
206 additions
and
32 deletions
+206
-32
log.txt
log/log.txt
+8
-0
main
main
+0
-0
main.c
main.c
+23
-28
mqttjson.c
mqttjson.c
+31
-0
mqttjson.h
mqttjson.h
+5
-4
serial.c
serial.c
+124
-0
serial.h
serial.h
+15
-0
No files found.
log/log.txt
View file @
71aebce6
Failed to connect to broker: Network is unreachable
Failed to connect to broker: Network is unreachable
Failed to connect to broker: Network is unreachable
Failed to connect to broker: Network is unreachable
Failed to connect to broker: Network is unreachable
Failed to connect to broker: Network is unreachable
Failed to connect to broker: Network is unreachable
Failed to connect to broker: Network is unreachable
main
0 → 100755
View file @
71aebce6
File added
main.c
View file @
71aebce6
#include"mqttjson.h"
#include"mqttjson.h"
#include "ip.h"
#include "ip.h"
#include "serial.h"
int
keepRunning
=
1
;
int
keepRunning
=
1
;
const
int
gmiddlepwm
=
0
;
const
int
gmiddlepwm
=
0
;
...
@@ -22,6 +23,13 @@ void sleepmsv2(int val){
...
@@ -22,6 +23,13 @@ void sleepmsv2(int val){
}
}
void
*
usar_Receive
(
void
*
arg
)
{
printf
(
"serial receive runing"
);
serial_Receive
();
return
NULL
;
}
void
*
left_function
(
void
*
arg
)
void
*
left_function
(
void
*
arg
)
{
{
printf
(
"Thread speed is runing
\n
"
);
printf
(
"Thread speed is runing
\n
"
);
...
@@ -63,41 +71,21 @@ void *right_function(void *arg)
...
@@ -63,41 +71,21 @@ void *right_function(void *arg)
}
}
int
main
(
int
argc
,
char
*
argv
[])
{
int
main
(
int
argc
,
char
*
argv
[])
{
struct
mosquitto
*
mosq
;
int
rc
,
ret
;
gStart
=
time
(
NULL
);
gStart
=
time
(
NULL
);
// 初始化 mosquitto 库
// 初始化 mosquitto 库
mosquitto_lib_init
();
mosquitto_lib_init
();
mqtt_Init
();
// 创建 mosquitto 客户端
mosq
=
mosquitto_new
(
CLIENT_ID
,
true
,
NULL
);
serial_Init
();
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_username_pw_set
(
mosq
,
USERNAME
,
PASSWORD
);
// 连接到 MQTT 代理
rc
=
mosquitto_connect
(
mosq
,
BROKER_ADDRESS
,
BROKER_PORT
,
60
);
if
(
rc
!=
MOSQ_ERR_SUCCESS
)
{
fprintf
(
stderr
,
"Failed to connect to broker: %s
\n
"
,
mosquitto_strerror
(
rc
));
mosquitto_destroy
(
mosq
);
return
EXIT_FAILURE
;
}
ipaddr
();
ipaddr
();
mqtt_wirte
(
ip_address
);
//printf("%s\n",ip_address);
//printf("%s\n",ip_address);
mosquitto_publish
(
mosq
,
NULL
,
"controlship/01
"
,
strlen
(
ip_address
),
ip_address
,
0
,
false
);
//将ip发送给mqtt
//mosquitto_publish(mosq, NULL, "controlship/02
", strlen(ip_address), ip_address, 0, false);//将ip发送给mqtt
if
(
wiringPiSetup
()
==
-
1
)
{
if
(
wiringPiSetup
()
==
-
1
)
{
printf
(
"pin set fail
\n
"
);
printf
(
"pin set fail
\n
"
);
...
@@ -115,7 +103,7 @@ int main(int argc, char *argv[]) {
...
@@ -115,7 +103,7 @@ int main(int argc, char *argv[]) {
pwmSetClock
(
gRightPwmPin
,
328
);
//=19200*1000/(hz*2000)
pwmSetClock
(
gRightPwmPin
,
328
);
//=19200*1000/(hz*2000)
pwmSetRange
(
gRightPwmPin
,
1000
);
pwmSetRange
(
gRightPwmPin
,
1000
);
pthread_t
thread
[
2
];
pthread_t
thread
[
3
];
if
(
pthread_create
(
&
thread
[
0
],
NULL
,
left_function
,
NULL
)
!=
0
)
if
(
pthread_create
(
&
thread
[
0
],
NULL
,
left_function
,
NULL
)
!=
0
)
{
{
...
@@ -129,6 +117,13 @@ int main(int argc, char *argv[]) {
...
@@ -129,6 +117,13 @@ int main(int argc, char *argv[]) {
return
1
;
return
1
;
}
}
if
(
pthread_create
(
&
thread
[
2
],
NULL
,
usar_Receive
,
NULL
)
!=
0
)
{
perror
(
"Failed to create thread 1"
);
return
1
;
}
// 开始循环,处理网络流量
// 开始循环,处理网络流量
mosquitto_loop_forever
(
mosq
,
-
1
,
1
);
// -1 表示无限等待,1 表示处理消息的最大数量
mosquitto_loop_forever
(
mosq
,
-
1
,
1
);
// -1 表示无限等待,1 表示处理消息的最大数量
...
...
mqttjson.c
View file @
71aebce6
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
time_t
gStart
;
time_t
gStart
;
struct
mosquitto
*
mosq
;
int
gLeftPwmPin
=
21
;
int
gLeftPwmPin
=
21
;
int
gLeftPwm
=
0
;
int
gLeftPwm
=
0
;
...
@@ -12,6 +13,36 @@ int gRightPwm = 0;
...
@@ -12,6 +13,36 @@ int gRightPwm = 0;
int
gRightPwmCount
=
0
;
// 计数
int
gRightPwmCount
=
0
;
// 计数
void
mqtt_wirte
(
char
*
message
)
{
mosquitto_publish
(
mosq
,
NULL
,
TOPIC
,
strlen
(
message
),
message
,
0
,
false
);
}
int
mqtt_Init
()
{
int
rc
;
// 创建 mosquitto 客户端
mosq
=
mosquitto_new
(
CLIENT_ID
,
true
,
NULL
);
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_username_pw_set
(
mosq
,
USERNAME
,
PASSWORD
);
// 连接到 MQTT 代理
rc
=
mosquitto_connect
(
mosq
,
BROKER_ADDRESS
,
BROKER_PORT
,
60
);
if
(
rc
!=
MOSQ_ERR_SUCCESS
)
{
fprintf
(
stderr
,
"Failed to connect to broker: %s
\n
"
,
mosquitto_strerror
(
rc
));
mosquitto_destroy
(
mosq
);
return
EXIT_FAILURE
;
}
}
void
on_connect
(
struct
mosquitto
*
mosq
,
void
*
obj
,
int
rc
)
void
on_connect
(
struct
mosquitto
*
mosq
,
void
*
obj
,
int
rc
)
{
{
if
(
rc
==
0
)
if
(
rc
==
0
)
...
...
mqttjson.h
View file @
71aebce6
...
@@ -23,14 +23,15 @@ extern int gRightPwmCount; // 计数
...
@@ -23,14 +23,15 @@ extern int gRightPwmCount; // 计数
#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883
#define BROKER_PORT 1883
#define CLIENT_ID "device/tatalship"
#define CLIENT_ID "device/tatalship
2
"
#define TOPIC "controlship/0
1
"
#define TOPIC "controlship/0
2
"
#define USERNAME "admin" // 替换为你的用户名
#define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码
#define PASSWORD "admin" // 替换为你的密码
extern
struct
mosquitto
*
mosq
;
void
on_connect
(
struct
mosquitto
*
mosq
,
void
*
obj
,
int
rc
);
void
on_connect
(
struct
mosquitto
*
mosq
,
void
*
obj
,
int
rc
);
int
mqtt_Init
();
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
(
char
*
message
);
#endif
#endif
serial.c
0 → 100644
View file @
71aebce6
#include"serial.h"
int
fd
;
int
init_serial
(
const
char
*
port
,
int
baudrate
,
speed_t
baud
)
{
int
fd
=
open
(
port
,
O_RDWR
|
O_NOCTTY
|
O_NDELAY
);
if
(
fd
==
-
1
)
{
perror
(
"open serial port"
);
return
-
1
;
}
struct
termios
options
;
tcgetattr
(
fd
,
&
options
);
// 设置波特率
cfsetispeed
(
&
options
,
baud
);
cfsetospeed
(
&
options
,
baud
);
// 配置串口
options
.
c_cflag
&=
~
PARENB
;
// 无校验
options
.
c_cflag
&=
~
CSTOPB
;
// 1停止位
options
.
c_cflag
&=
~
CSIZE
;
// 清除数据位设置
options
.
c_cflag
|=
CS8
;
// 8数据位
options
.
c_cflag
|=
CLOCAL
|
CREAD
;
// 忽略调制解调器状态,开启接收器
options
.
c_lflag
&=
~
(
ICANON
|
ECHO
|
ECHOE
|
ISIG
);
// 原始模式
options
.
c_iflag
&=
~
(
IXON
|
IXOFF
|
IXANY
);
// 禁用流控
options
.
c_oflag
&=
~
OPOST
;
// 原始输出
tcsetattr
(
fd
,
TCSANOW
,
&
options
);
return
fd
;
}
// 将NMEA格式的经纬度转换为十进制度
/*
double convert_to_decimal(const char *coordinate, char direction) {
double degrees, minutes;
sscanf(coordinate, "%15lf", °rees);
minutes=degrees%100;
degrees=int(degrees/100);
degrees += minutes / 60.0;
if (direction == 'S' || direction == 'W') {
degrees = -degrees;
}
return degrees;
}
*/
// 解析$GNRMC语句并提取经纬度
void
parse_gnrmc
(
const
char
*
nmea_sentence
)
{
char
time
[
11
],
status
,
lat
[
20
],
lat_dir
,
lon
[
20
],
lon_dir
;
if
(
sscanf
(
nmea_sentence
,
"$GNRMC,%10[^,],%c,%[^,],%c,%[^,],%c"
,
time
,
&
status
,
lat
,
&
lat_dir
,
lon
,
&
lon_dir
)
==
6
)
{
if
(
status
==
'A'
)
{
// 定位有效
//double latitude = convert_to_decimal(lat, lat_dir);
//double longitude = convert_to_decimal(lon, lon_dir);
printf
(
"Time (UTC): %s
\n
"
,
time
);
printf
(
"Latitude: %s
\n
"
,
lat
);
printf
(
"Longitude: %s
\n
"
,
lon
);
mqtt_wirte
(
lat
);
mqtt_wirte
(
lon
);
}
else
{
printf
(
"GNRMC data is invalid (status: %c)
\n
"
,
status
);
}
}
else
{
printf
(
"Failed to parse GNRMC sentence.
\n
"
);
}
}
void
serial_Receive
()
{
char
buffer
[
BUFFER_SIZE
];
char
nmea_sentence
[
BUFFER_SIZE
];
int
index
=
0
;
//mosq = mosquitto_new(CLIENT_ID, true, NULL);
while
(
1
)
{
int
bytes_read
=
read
(
fd
,
buffer
,
BUFFER_SIZE
-
1
);
if
(
bytes_read
>
0
)
{
buffer
[
bytes_read
]
=
'\0'
;
for
(
int
i
=
0
;
i
<
bytes_read
;
i
++
)
{
if
(
buffer
[
i
]
==
'$'
)
{
index
=
0
;
// 新语句开始
}
if
(
index
<
BUFFER_SIZE
-
1
)
{
nmea_sentence
[
index
++
]
=
buffer
[
i
];
nmea_sentence
[
index
]
=
'\0'
;
}
if
(
buffer
[
i
]
==
'\n'
)
{
// 一条语句结束
if
(
strstr
(
nmea_sentence
,
"$GNRMC"
))
{
parse_gnrmc
(
nmea_sentence
);
// 解析$GNRMC语句
}
}
}
}
sleep
(
3
);
}
}
int
serial_Init
()
{
fd
=
init_serial
(
SERIAL_PORT
,
9600
,
B9600
);
if
(
fd
==
-
1
)
return
-
1
;
printf
(
"Serial communication started...
\n
"
);
}
serial.h
0 → 100644
View file @
71aebce6
#ifndef __SERIAL_H__
#define __SERIAL_H__
#include <stdint.h>
#include <fcntl.h>
#include <termios.h>
#include"mqttjson.h"
#define SERIAL_PORT "/dev/ttyS2" // 根据实际设备修改
#define BUFFER_SIZE 256
void
serial_Receive
();
int
serial_Init
();
#endif
\ No newline at end of file
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