Commit 0ae069ac authored by 957dd's avatar 957dd

优化了bug,减少了内存泄漏

parent dd1ee7c8
...@@ -9,9 +9,23 @@ ...@@ -9,9 +9,23 @@
#include "tank.h" #include "tank.h"
#include "ship.h" #include "ship.h"
#include "pao.h" #include "pao.h"
#include "fileopen.h"
char TOPIC_middle[23];
char TOPIC2_middle[23];
char TOPIC3_middle[15];
void topic_middle_value() {
TOPIC=TOPIC_middle;
TOPIC2=TOPIC2_middle;
TOPIC3=TOPIC3_middle;
}
int Device_File_Init() { int Device_File_Init() {
const char *readbuf=device_inspect(); const char *readbuf=device_inspect();
//将第3个和第4个字符提取出来 //将第3个和第4个字符提取出来
char *sub_str=malloc(5); char *sub_str=malloc(5);
sub_str[0]=readbuf[2]; sub_str[0]=readbuf[2];
...@@ -39,12 +53,14 @@ int Device_File_Init() { ...@@ -39,12 +53,14 @@ int Device_File_Init() {
free(sub_str); free(sub_str);
} }
Delay_Ms(20,0); Delay_Ms(20,0);
TOPIC=malloc(24);
TOPIC2=malloc(24); sprintf(TOPIC2_middle,"dev2app/%s",readbuf);
TOPIC3= malloc(16); sprintf(TOPIC_middle,"app2dev/%s",readbuf);
sprintf(TOPIC2,"dev2app/%s",readbuf); sprintf(TOPIC3_middle,"%s",readbuf);
sprintf(TOPIC,"app2dev/%s",readbuf);
sprintf(TOPIC3,"%s",readbuf); topic_middle_value();//指针传值声明
printf("1:%s,2:%s,3:%s\n",TOPIC,TOPIC2,TOPIC3); printf("1:%s,2:%s,3:%s\n",TOPIC,TOPIC2,TOPIC3);
return 0; return 0;
} }
#ifndef __JUDG_H__ #ifndef __JUDG_H__
#define __JUDG_H__ #define __JUDG_H__
int Device_File_Init(); int Device_File_Init();//指针传值声明
#endif #endif
\ No newline at end of file
#ifndef __FILEOPEN_H__
#define __FILEOPEN_H__
#define filename "/home/orangepi/car/master/Deviceld.txt"//设备名存放文件
#define file_version "/home/orangepi/car/master/version.conf"
char *device_inspect() ;//打开设备号文件
char *program_version() ;//打开版本文件
#endif
\ No newline at end of file
#ifndef __THREAD_MAIN_H__ #ifndef __THREAD_MAIN_H__
#define __THREAD_MAIN_H__ #define __THREAD_MAIN_H__
#define filename "/home/orangepi/car/master/Deviceld.txt"
int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg)) ; int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg)) ;
void *AppExit(void *arg) ; void *AppExit(void *arg) ;
void *Mqttbeat(void *arg) ; void *Mqttbeat(void *arg) ;
void *opensh(void *arg) ; void *opensh(void *arg) ;
void *Mqtt_onnect(void *arg) ; void *Mqtt_onnect(void *arg) ;
char *device_inspect() ;
void thread_end() ; void thread_end() ;
#endif #endif
\ No newline at end of file
#include "fileopen.h"
#include "common.h"
#include "delay.h"
char buffer_device[30]; // 用于存储文件内容
char buffer_version[30];// 用于存储版本号内容
char *device_inspect() {//读出文件函数
FILE *file;
while (1) {
file = fopen(filename, "r"); // 以只读模式打开文件
if (file == NULL) {
printf("文件 %s 打开失败,等待中...\n", filename);
}
else {
// 尝试读取文件内容
if (fgets(buffer_device, sizeof(buffer_device), file) != NULL) {
// 如果文件内容不为空
fclose(file);
printf("读取到文件内容: %s\n", buffer_device);
return buffer_device;
} else {
printf("文件为空,等待中...\n");
}
fclose(file);
}
Delay_Ms(1,0); // 等待1秒后再次检查
}
}
char *program_version() {//打开版本文件
FILE *file;
file = fopen(file_version, "r"); // 以只读模式打开文件
if (file == NULL) {
printf("文件无");
} else {
// 尝试读取文件内容
if (fgets(buffer_version, sizeof(buffer_version), file) != NULL) {
// 如果文件内容不为空
fclose(file);
printf("读取到文件内容: %s\n", buffer_version);
return buffer_version;
} else {
printf("文件为空,等待中...\n");
}
fclose(file);
}
}
\ No newline at end of file
...@@ -7,12 +7,16 @@ ...@@ -7,12 +7,16 @@
#include "heat.h" #include "heat.h"
#include "mqtt.h" #include "mqtt.h"
#include "warm.h" #include "warm.h"
#include "fileopen.h"
#include "judg.h"
struct mosquitto *mosq; struct mosquitto *mosq;
char* TOPIC; char* TOPIC = NULL;
char* TOPIC2; char* TOPIC2 = NULL;
char* TOPIC3; char* TOPIC3 = NULL;
char *version_num;//版本号指针
int gPwmCount = 0; // 计数 int gPwmCount = 0; // 计数
int gmessage_type=10086; int gmessage_type=10086;
...@@ -53,7 +57,7 @@ int mqtt_init() { ...@@ -53,7 +57,7 @@ int mqtt_init() {
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) {
printf("Connected to broker\n"); printf("Connected to broker\n");
topic_middle_value();
mosquitto_subscribe(mosq, NULL, TOPIC, 0); mosquitto_subscribe(mosq, NULL, TOPIC, 0);
} else { } else {
...@@ -61,7 +65,7 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) {//回调函数 ...@@ -61,7 +65,7 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) {//回调函数
} }
} }
void mqtt_wirte(){//心跳格式 void mqtt_wirte(){//心跳格式,每5s一次心跳
float voltage = INA226_readBusVoltage(sda_pin, scl_pin); float voltage = INA226_readBusVoltage(sda_pin, scl_pin);
float current = INA226_readCurrent(sda_pin, scl_pin); float current = INA226_readCurrent(sda_pin, scl_pin);
...@@ -73,10 +77,16 @@ void mqtt_wirte(){//心跳格式 ...@@ -73,10 +77,16 @@ void mqtt_wirte(){//心跳格式
if(AppExit_pin_pwm/100 != 2) alarm_control(voltage);//判断电压警报 if(AppExit_pin_pwm/100 != 2) alarm_control(voltage);//判断电压警报
/*读取程序版本号*/
version_num = malloc(16);
version_num = program_version();
heat_tem();//获取CPU温度 heat_tem();//获取CPU温度
message=1; message=1;
topic_middle_value();
cJSON *root = cJSON_CreateObject(); cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject(); cJSON *body = cJSON_CreateObject();
cJSON *head = cJSON_CreateObject(); cJSON *head = cJSON_CreateObject();
...@@ -88,6 +98,7 @@ void mqtt_wirte(){//心跳格式 ...@@ -88,6 +98,7 @@ void mqtt_wirte(){//心跳格式
cJSON_AddStringToObject(body,"Tem:",temperature);//发送温度 cJSON_AddStringToObject(body,"Tem:",temperature);//发送温度
cJSON_AddStringToObject(body, "N", glat);//gps cJSON_AddStringToObject(body, "N", glat);//gps
cJSON_AddStringToObject(body, "E", glon);//gps cJSON_AddStringToObject(body, "E", glon);//gps
cJSON_AddStringToObject(body, "version", version_num);//gps
cJSON_AddNumberToObject(head, "message_type",message); cJSON_AddNumberToObject(head, "message_type",message);
...@@ -98,6 +109,10 @@ void mqtt_wirte(){//心跳格式 ...@@ -98,6 +109,10 @@ void mqtt_wirte(){//心跳格式
printf("%s\n",payload); printf("%s\n",payload);
mosquitto_publish(mosq, NULL, TOPIC2, strlen(payload), payload, 0, false); mosquitto_publish(mosq, NULL, TOPIC2, strlen(payload), payload, 0, false);
mosquitto_publish(mosq, NULL, TOPIC3, strlen(payload), payload, 0, false); mosquitto_publish(mosq, NULL, TOPIC3, strlen(payload), payload, 0, false);
cJSON_Delete(root); // 释放 cJSON 对象
return ;
} }
void message_3(cJSON *body,cJSON *pwm_ctrl){//message_type为3,控制pwm void message_3(cJSON *body,cJSON *pwm_ctrl){//message_type为3,控制pwm
......
#include "common.h" #include "common.h"
#include "mqtt.h" #include "mqtt.h"
#include "opensh.h" #include "opensh.h"
#include "judg.h"
char gwebcam[254];//存放启动火狐网站命令 char gwebcam[254];//存放启动火狐网站命令
...@@ -8,6 +9,8 @@ int opencamsh(){ ...@@ -8,6 +9,8 @@ int opencamsh(){
const char* url = "https://jywy.yd-ss.com?dev="; const char* url = "https://jywy.yd-ss.com?dev=";
char urls[50]; char urls[50];
topic_middle_value();
sprintf(urls,"%s%s",url,TOPIC3); sprintf(urls,"%s%s",url,TOPIC3);
//setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004 --new-window sudo //setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004 --new-window sudo
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#include <cjson/cJSON.h> #include <cjson/cJSON.h>
#include "request.h" #include "request.h"
#include "mqtt.h" #include "mqtt.h"
#include "judg.h"
const char *post = "http://47.119.190.60/api/v1/device/config"; const char *post = "http://47.119.190.60/api/v1/device/config";
int errCodeValue; int errCodeValue;
...@@ -82,6 +84,8 @@ int analysis_request_json(char *payload_str) { ...@@ -82,6 +84,8 @@ int analysis_request_json(char *payload_str) {
char *wirte_json() { char *wirte_json() {
cJSON *body = cJSON_CreateObject(); cJSON *body = cJSON_CreateObject();
topic_middle_value();
cJSON_AddStringToObject(body, "device_id", TOPIC3); // 发送设备id cJSON_AddStringToObject(body, "device_id", TOPIC3); // 发送设备id
char *payload = cJSON_PrintUnformatted(body); char *payload = cJSON_PrintUnformatted(body);
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "common.h" #include "common.h"
#include "thread_main.h" #include "thread_main.h"
char buffer[30]; // 用于存储文件内容
pthread_t thread[4]; pthread_t thread[4];
//开启线程 //开启线程
int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg)){ int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg)){
...@@ -89,30 +88,6 @@ void *Mqtt_onnect(void *arg) {//mqtt异常处理,断开自动重连,简单 ...@@ -89,30 +88,6 @@ void *Mqtt_onnect(void *arg) {//mqtt异常处理,断开自动重连,简单
return NULL; return NULL;
} }
char *device_inspect() {//读出文件函数
FILE *file;
while (1) {
file = fopen(filename, "r"); // 以只读模式打开文件
if (file == NULL) {
printf("文件 %s 打开失败,等待中...\n", filename);
}
else {
// 尝试读取文件内容
if (fgets(buffer, sizeof(buffer), file) != NULL) {
// 如果文件内容不为空
fclose(file);
printf("读取到文件内容: %s\n", buffer);
return buffer;
} else {
printf("文件为空,等待中...\n");
}
fclose(file);
}
Delay_Ms(1,0); // 等待1秒后再次检查
}
}
// 等待线程结束 // 等待线程结束
void thread_end() { void thread_end() {
......
...@@ -7,7 +7,10 @@ ...@@ -7,7 +7,10 @@
int alarm_highest(int index) {//最高警报,最低电压报警 int alarm_highest(int index) {//最高警报,最低电压报警
if(index == 1) { if(index == 1) {
pin_value(20,1);//紫灯 pin_value(20,1);//紫灯
if(enable_buzzer_value)
{
pin_value(22,1);//蜂鸣器 pin_value(22,1);//蜂鸣器
}
}else if(index == 0) { }else if(index == 0) {
pin_value(20,0);//紫灯 pin_value(20,0);//紫灯
pin_value(22,0);//蜂鸣器 pin_value(22,0);//蜂鸣器
......
...@@ -14,5 +14,6 @@ ...@@ -14,5 +14,6 @@
#include "thread_main.h" #include "thread_main.h"
#include "judg.h" #include "judg.h"
#include "request.h" #include "request.h"
#include "fileopen.h"
#endif #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