Commit 2ee975a6 authored by 957dd's avatar 957dd

241205

parent 71aebce6
No preview for this file type
......@@ -83,7 +83,7 @@ int main(int argc, char *argv[]) {
serial_Init();
ipaddr();
mqtt_wirte(ip_address);
//mqtt_wirte(ip_address);
//printf("%s\n",ip_address);
//mosquitto_publish(mosq, NULL, "controlship/02", strlen(ip_address), ip_address, 0, false);//将ip发送给mqtt
......
#include"mqttjson.h"
#include "mqttjson.h"
#include "ip.h"
time_t gStart;
int gmqttindex=0;
struct mosquitto *mosq;
int gLeftPwmPin = 21;
......@@ -12,10 +15,37 @@ int gRightPwmPin = 2;
int gRightPwm = 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()
......@@ -49,7 +79,8 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc)
{
printf("Connected to broker\n");
mosquitto_subscribe(mosq, NULL, TOPIC, 0);
time_t start=time(NULL);
//time_t start=time(NULL);
gmqttindex=0;
}
else
{
......@@ -66,14 +97,15 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
return;
}
// 确保消息有效
if (message->payload && message->payloadlen) {
if (message->payload && message->payloadlen) {
// 将消息内容转换为字符串
char *payload_str = (char *)malloc(message->payloadlen + 1);
if (payload_str) {
memcpy(payload_str, message->payload, message->payloadlen);
payload_str[message->payloadlen] = '\0';
// 解析 JSON
cJSON *json = cJSON_Parse(payload_str);
if (json == NULL) {
......@@ -93,10 +125,11 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if (!cJSON_IsObject(body)) {
return;
}
gmqttcount=0;//接收到消息,就表示在连接
printf("message_type: %d\n", message_type->valueint);
if (message_type->valueint==3){ //pwm控制
// 提取 pin_ctrl_req 对象
cJSON *pwm_ctrl_req = cJSON_GetObjectItem(body, "pwm_ctrl_req");
cJSON *pwm_ctrl_req = cJSON_GetObjectItem(body, "p_ctrl_req");
if (!cJSON_IsObject(pwm_ctrl_req)) {
return;
}
......@@ -145,7 +178,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
}
else if (message_type->valueint==4){
// 提取 pin_ctrl_req 对象
cJSON *pin_ctrl_req = cJSON_GetObjectItem(body, "pin_ctrl_req");
cJSON *pin_ctrl_req = cJSON_GetObjectItem(body, "p_ctrl_req");
if (!cJSON_IsObject(pin_ctrl_req)) {
return;
}
......@@ -171,6 +204,17 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
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 字符串
......
......@@ -10,6 +10,7 @@
#include <cjson/cJSON.h>
#include<time.h>
#include "pin.h"
#include "serial.h"
extern time_t gStart;
......@@ -32,6 +33,6 @@ extern struct mosquitto *mosq;
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 mqtt_wirte(char *message);
void mqtt_wirte();
#endif
#include"serial.h"
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)
{
......@@ -49,8 +51,8 @@ double convert_to_decimal(const char *coordinate, char direction) {
*/
// 解析$GNRMC语句并提取经纬度
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 (status == 'A')
......@@ -61,8 +63,8 @@ void parse_gnrmc(const char *nmea_sentence) {
printf("Time (UTC): %s\n", time);
printf("Latitude: %s\n", lat);
printf("Longitude: %s\n", lon);
mqtt_wirte(lat);
mqtt_wirte(lon);
//mqtt_wirte(lat);
//mqtt_wirte(lon);
}
else
{
......@@ -109,7 +111,9 @@ void serial_Receive() {
}
}
}
mqtt_wirte();
sleep(3);
gmqttcount++;
}
}
......
......@@ -9,6 +9,9 @@
#define SERIAL_PORT "/dev/ttyS2" // 根据实际设备修改
#define BUFFER_SIZE 256
extern char lat[20], lat_dir, lon[20], lon_dir;
extern int gmqttcount;
void serial_Receive();
int serial_Init();
#endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment