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
2ee975a6
Commit
2ee975a6
authored
Dec 05, 2024
by
957dd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
241205
parent
71aebce6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
67 additions
and
14 deletions
+67
-14
main
main
+0
-0
main.c
main.c
+1
-1
mqttjson.c
mqttjson.c
+52
-8
mqttjson.h
mqttjson.h
+2
-1
serial.c
serial.c
+8
-4
serial.h
serial.h
+4
-0
No files found.
main
View file @
2ee975a6
No preview for this file type
main.c
View file @
2ee975a6
...
@@ -83,7 +83,7 @@ int main(int argc, char *argv[]) {
...
@@ -83,7 +83,7 @@ int main(int argc, char *argv[]) {
serial_Init
();
serial_Init
();
ipaddr
();
ipaddr
();
mqtt_wirte
(
ip_address
);
//
mqtt_wirte(ip_address);
//printf("%s\n",ip_address);
//printf("%s\n",ip_address);
//mosquitto_publish(mosq, NULL, "controlship/02", strlen(ip_address), ip_address, 0, false);//将ip发送给mqtt
//mosquitto_publish(mosq, NULL, "controlship/02", strlen(ip_address), ip_address, 0, false);//将ip发送给mqtt
...
...
mqttjson.c
View file @
2ee975a6
#include"mqttjson.h"
#include "mqttjson.h"
#include "ip.h"
time_t
gStart
;
time_t
gStart
;
int
gmqttindex
=
0
;
struct
mosquitto
*
mosq
;
struct
mosquitto
*
mosq
;
int
gLeftPwmPin
=
21
;
int
gLeftPwmPin
=
21
;
...
@@ -12,10 +15,37 @@ int gRightPwmPin = 2;
...
@@ -12,10 +15,37 @@ int gRightPwmPin = 2;
int
gRightPwm
=
0
;
int
gRightPwm
=
0
;
int
gRightPwmCount
=
0
;
// 计数
int
gRightPwmCount
=
0
;
// 计数
cJSON
*
root
;
char
*
payload
;
void
mqtt_wirte
(
char
*
message
)
void
mqtt_wirte
()
{
{
mosquitto_publish
(
mosq
,
NULL
,
TOPIC
,
strlen
(
message
),
message
,
0
,
false
);
root
=
cJSON_CreateObject
();
cJSON
*
body
=
cJSON_AddObjectToObject
(
root
,
"body"
);
cJSON
*
data
=
cJSON_AddArrayToObject
(
body
,
"data"
);
cJSON
*
data_item
=
cJSON_CreateObject
();
cJSON_AddStringToObject
(
data_item
,
"ip"
,
ip_address
);
cJSON_AddStringToObject
(
data_item
,
"ID"
,
TOPIC
);
cJSON_AddStringToObject
(
data_item
,
"N"
,
lat
);
cJSON_AddStringToObject
(
data_item
,
"E"
,
lon
);
//printf("%d\n",gmqttcount);
if
(
gmqttcount
>=
10
)
{
gmqttindex
=
0
;
gmqttcount
=
11
;
cJSON_AddStringToObject
(
data_item
,
"connectfail"
,
"-1"
);
}
cJSON_AddItemToArray
(
data
,
data_item
);
payload
=
cJSON_PrintUnformatted
(
root
);
if
(
gmqttindex
==
0
)
{
mosquitto_publish
(
mosq
,
NULL
,
TOPIC
,
strlen
(
payload
),
payload
,
0
,
false
);
}
}
}
int
mqtt_Init
()
int
mqtt_Init
()
...
@@ -49,7 +79,8 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc)
...
@@ -49,7 +79,8 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc)
{
{
printf
(
"Connected to broker
\n
"
);
printf
(
"Connected to broker
\n
"
);
mosquitto_subscribe
(
mosq
,
NULL
,
TOPIC
,
0
);
mosquitto_subscribe
(
mosq
,
NULL
,
TOPIC
,
0
);
time_t
start
=
time
(
NULL
);
//time_t start=time(NULL);
gmqttindex
=
0
;
}
}
else
else
{
{
...
@@ -66,14 +97,15 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -66,14 +97,15 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
return
;
return
;
}
}
// 确保消息有效
// 确保消息有效
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
)
{
memcpy
(
payload_str
,
message
->
payload
,
message
->
payloadlen
);
memcpy
(
payload_str
,
message
->
payload
,
message
->
payloadlen
);
payload_str
[
message
->
payloadlen
]
=
'\0'
;
payload_str
[
message
->
payloadlen
]
=
'\0'
;
// 解析 JSON
// 解析 JSON
cJSON
*
json
=
cJSON_Parse
(
payload_str
);
cJSON
*
json
=
cJSON_Parse
(
payload_str
);
if
(
json
==
NULL
)
{
if
(
json
==
NULL
)
{
...
@@ -93,10 +125,11 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -93,10 +125,11 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if
(
!
cJSON_IsObject
(
body
))
{
if
(
!
cJSON_IsObject
(
body
))
{
return
;
return
;
}
}
gmqttcount
=
0
;
//接收到消息,就表示在连接
printf
(
"message_type: %d
\n
"
,
message_type
->
valueint
);
printf
(
"message_type: %d
\n
"
,
message_type
->
valueint
);
if
(
message_type
->
valueint
==
3
){
//pwm控制
if
(
message_type
->
valueint
==
3
){
//pwm控制
// 提取 pin_ctrl_req 对象
// 提取 pin_ctrl_req 对象
cJSON
*
pwm_ctrl_req
=
cJSON_GetObjectItem
(
body
,
"p
wm
_ctrl_req"
);
cJSON
*
pwm_ctrl_req
=
cJSON_GetObjectItem
(
body
,
"p_ctrl_req"
);
if
(
!
cJSON_IsObject
(
pwm_ctrl_req
))
{
if
(
!
cJSON_IsObject
(
pwm_ctrl_req
))
{
return
;
return
;
}
}
...
@@ -145,7 +178,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -145,7 +178,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
}
}
else
if
(
message_type
->
valueint
==
4
){
else
if
(
message_type
->
valueint
==
4
){
// 提取 pin_ctrl_req 对象
// 提取 pin_ctrl_req 对象
cJSON
*
pin_ctrl_req
=
cJSON_GetObjectItem
(
body
,
"p
in
_ctrl_req"
);
cJSON
*
pin_ctrl_req
=
cJSON_GetObjectItem
(
body
,
"p_ctrl_req"
);
if
(
!
cJSON_IsObject
(
pin_ctrl_req
))
{
if
(
!
cJSON_IsObject
(
pin_ctrl_req
))
{
return
;
return
;
}
}
...
@@ -171,6 +204,17 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
...
@@ -171,6 +204,17 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
pinvalue
(
pinTemp
,
valTemp
);
pinvalue
(
pinTemp
,
valTemp
);
}
}
}
}
else
if
(
message_type
->
valueint
==
5
)
{
mqtt_wirte
();
mosquitto_publish
(
mosq
,
NULL
,
TOPIC
,
strlen
(
payload
),
payload
,
0
,
false
);
}
else
if
(
message_type
->
valueint
==
0
)
{
gmqttindex
=
1
;
}
}
}
// 释放 payload 字符串
// 释放 payload 字符串
...
...
mqttjson.h
View file @
2ee975a6
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
#include <cjson/cJSON.h>
#include <cjson/cJSON.h>
#include<time.h>
#include<time.h>
#include "pin.h"
#include "pin.h"
#include "serial.h"
extern
time_t
gStart
;
extern
time_t
gStart
;
...
@@ -32,6 +33,6 @@ extern struct mosquitto *mosq;
...
@@ -32,6 +33,6 @@ 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
();
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
);
void
mqtt_wirte
();
#endif
#endif
serial.c
View file @
2ee975a6
#include"serial.h"
#include"serial.h"
int
fd
;
int
fd
;
int
gmqttcount
=
0
;
//计算在第一次连接后规定时间是否获得消息
char
lat
[
20
],
lat_dir
,
lon
[
20
],
lon_dir
;
int
init_serial
(
const
char
*
port
,
int
baudrate
,
speed_t
baud
)
int
init_serial
(
const
char
*
port
,
int
baudrate
,
speed_t
baud
)
{
{
...
@@ -49,8 +51,8 @@ double convert_to_decimal(const char *coordinate, char direction) {
...
@@ -49,8 +51,8 @@ double convert_to_decimal(const char *coordinate, char direction) {
*/
*/
// 解析$GNRMC语句并提取经纬度
// 解析$GNRMC语句并提取经纬度
void
parse_gnrmc
(
const
char
*
nmea_sentence
)
{
void
parse_gnrmc
(
const
char
*
nmea_sentence
)
{
char
time
[
11
],
status
,
lat
[
20
],
lat_dir
,
lon
[
20
],
lon_dir
;
char
time
[
11
],
status
;
if
(
sscanf
(
nmea_sentence
,
"$GNRMC,%10[^,],%c,%[^,],%c,%[^,],%c"
,
time
,
&
status
,
lat
,
&
lat_dir
,
lon
,
&
lon_dir
)
==
6
)
if
(
sscanf
(
nmea_sentence
,
"$GNRMC,%10[^,],%c,%[^,],%c,%[^,],%c"
,
time
,
&
status
,
lat
,
&
lat_dir
,
lon
,
&
lon_dir
)
==
6
)
{
{
if
(
status
==
'A'
)
if
(
status
==
'A'
)
...
@@ -61,8 +63,8 @@ void parse_gnrmc(const char *nmea_sentence) {
...
@@ -61,8 +63,8 @@ void parse_gnrmc(const char *nmea_sentence) {
printf
(
"Time (UTC): %s
\n
"
,
time
);
printf
(
"Time (UTC): %s
\n
"
,
time
);
printf
(
"Latitude: %s
\n
"
,
lat
);
printf
(
"Latitude: %s
\n
"
,
lat
);
printf
(
"Longitude: %s
\n
"
,
lon
);
printf
(
"Longitude: %s
\n
"
,
lon
);
mqtt_wirte
(
lat
);
//
mqtt_wirte(lat);
mqtt_wirte
(
lon
);
//
mqtt_wirte(lon);
}
}
else
else
{
{
...
@@ -109,7 +111,9 @@ void serial_Receive() {
...
@@ -109,7 +111,9 @@ void serial_Receive() {
}
}
}
}
}
}
mqtt_wirte
();
sleep
(
3
);
sleep
(
3
);
gmqttcount
++
;
}
}
}
}
...
...
serial.h
View file @
2ee975a6
...
@@ -9,6 +9,9 @@
...
@@ -9,6 +9,9 @@
#define SERIAL_PORT "/dev/ttyS2" // 根据实际设备修改
#define SERIAL_PORT "/dev/ttyS2" // 根据实际设备修改
#define BUFFER_SIZE 256
#define BUFFER_SIZE 256
extern
char
lat
[
20
],
lat_dir
,
lon
[
20
],
lon_dir
;
extern
int
gmqttcount
;
void
serial_Receive
();
void
serial_Receive
();
int
serial_Init
();
int
serial_Init
();
#endif
#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