Commit 0a832f17 authored by orangepi's avatar orangepi

优化了文件管理

parent 03c67e1c
#include "common.h"
#include "thread_main.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "mqtt.h"
#include "car.h"
void car_Init_0101() {
pwm_speed(); //pwm初始化,车为停止
pin_init();
AppExit_pin_pwm=1;//车的异常停止值
}
\ No newline at end of file
#ifndef __CAR_H__
#define __CAR_H__
void car_Init_0101();
#endif
\ No newline at end of file
#include "common.h"
#include "thread_main.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "mqtt.h"
#include "pao.h"
void ptr_Init_0401() {
pin_init();
pwm_PTZ_hz();
PTZ_pwm_init();
AppExit_pin_pwm=4;//车的异常停止值
}
\ No newline at end of file
#ifndef __PAO_H__
#define __PAO_H__
void ptr_Init_0401();
#endif
\ No newline at end of file
#include "common.h"
#include "thread_main.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "mqtt.h"
#include "ship.h"
void ship_Init_0301() {
pwm_speed();
pin_ship_init(); //pwm初始化,车为停止
AppExit_pin_pwm=3;//车的异常停止值
}
\ No newline at end of file
#ifndef __SHIP_H__
#define __SHIP_H__
void ship_Init_0301();
#endif
\ No newline at end of file
#include "common.h"
#include "thread_main.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "mqtt.h"
#include "tank.h"
void tank_Init_0201() {
pin_init();
pinMode(2, OUTPUT);//pwm引脚改为普通引脚
pinMode(21, OUTPUT);//pwm引脚改为普通引脚
digitalWrite(2, LOW);
digitalWrite(21, LOW);
AppExit_pin_pwm=2;//坦克的异常停止值
}
\ No newline at end of file
#ifndef __TANK_H__
#define __TANK_H__
void tank_Init_0201();
#endif
\ No newline at end of file
#include "common.h"
#include "thread_main.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "mqtt.h"
#include "judg.h"
#include "delay.h"
#include "car.h"
#include "tank.h"
#include "ship.h"
#include "pao.h"
int Device_File_Init() {
const char *readbuf=device_inspect();
//将第3个和第4个字符提取出来
char *sub_str=malloc(5);
sub_str[0]=readbuf[2];
sub_str[1]=readbuf[3];
sub_str[2]=readbuf[4];
sub_str[3]=readbuf[5];
sub_str[4]='\0';
printf("开始初始化了");
if(strcmp(sub_str,"0101")==0){//车的编码
car_Init_0101() ;
printf("使用型号%s\n",sub_str);
free(sub_str) ;
}else if(strcmp(sub_str,"0201")==0){//坦克的编码
tank_Init_0201();
printf("使用型号%s\n",sub_str);
free(sub_str);
}else if(strcmp(sub_str,"0301")==0){//船的编码
ship_Init_0301();
printf("使用型号%s\n",sub_str);
free(sub_str);
}else if(strcmp(sub_str,"0401")==0){//炮的编码
ptr_Init_0401();
printf("使用型号%s\n",sub_str);
free(sub_str);
}
Delay_Ms(20,0);
TOPIC=malloc(24);
TOPIC2=malloc(24);
TOPIC3= malloc(16);
sprintf(TOPIC2,"dev2app/%s",readbuf);
sprintf(TOPIC,"app2dev/%s",readbuf);
sprintf(TOPIC3,"%s",readbuf);
printf("1:%s,2:%s,3:%s\n",TOPIC,TOPIC2,TOPIC3);
return 0;
}
#ifndef __JUDG_H__
#define __JUDG_H__
int Device_File_Init();
#endif
\ No newline at end of file
#ifndef COMMON_H
#define COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
#include <pthread.h>
#include <errno.h>
#include <wiringPi.h>
#include <stdbool.h>
// 其他公共声明
#endif
#ifndef __DELAY_H__
#define __DELAY_H__
#include <stdio.h>
#include <time.h>
void Delay_Ms(int sec,int msec);
#endif
\ No newline at end of file
#ifndef __IP_H__
#define __IP_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
extern char ip_address[INET_ADDRSTRLEN];//读到的ip地址
int ipaddr();//ip地址函数
#endif
#ifndef __MQTT_H__
#define __MQTT_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include<time.h>
#include "ip.h"
#include "opensh.h"
#include "gpio_pwm.h"
#include "INA226.h"
#include "pthrpoll.h"
#include "gpio_pwm_2.h"
#include "heat.h"
extern char* TOPIC ;//="app2dev/controlcar0004"
extern char* TOPIC2 ;//="dev2app/controlcar0004"
extern char* TOPIC3;//= "controlcar0004"
extern ThreadPool *pool;
extern char* TOPIC ;
extern char* TOPIC2 ;
extern char* TOPIC3 ;
extern uint8_t AppExit_pin_pwm;
//extern ThreadPool *pool;
#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883
extern uint8_t AppExit_pin_pwm;
#define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码
extern struct mosquitto *mosq;//创建客服端
extern time_t gStart;//开始时间戳
extern int gPwmCount; // 计数
extern int gmessage_type;//message消息值
int mqtt_init();//mqtt初始化
......
#ifndef __opensh_H__
#define __opensh_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mqtt.h"
int opencamsh();//打开摄像头函数
......
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include "common.h"
// 任务结构体
typedef struct Task {
void (*function)(void *);
......
#ifndef __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)) ;
void *AppExit(void *arg) ;
void *Mqttbeat(void *arg) ;
void *opensh(void *arg) ;
void *Mqtt_onnect(void *arg) ;
char *device_inspect() ;
void thread_end() ;
#endif
\ No newline at end of file
No preview for this file type
# 编译器
CC = gcc
# 编译选项
CFLAGS = -Wall -g -Ilib
# 编译选项-Ilib
CFLAGS = -Wall -g -Iinclude -Isystem/gpio -Isystem/sensor -Iusr -Idevice_judg/judg \
-Idevice_judg/device
# 链接库
LDFLAGS = -lwiringPi -lmosquitto -lcjson -lm
# 目标可执行文件
TARGET = main
# 源文件目录
SRC_DIR = usr src
SRC_DIR = usr src system/gpio system/sensor device_judg/judg device_judg/device \
# 头文件目录
INC_DIR = lib
INC_DIR = usr include system/gpio system/sensor device_judg/judg device_judg/device \
# 查找所有 .c 文件
SRCS = $(foreach dir,$(SRC_DIR),$(wildcard $(dir)/*.c))
# 目标文件
......
#include "common.h"
#include "delay.h"
void Delay_Ms(int sec,int msec)
{
void Delay_Ms(int sec,int msec) {
struct timespec ts;
ts.tv_sec = sec; // 秒
ts.tv_nsec = msec*1000000; // 1毫秒 = 1000000纳秒
......
#include <sys/socket.h>
#include <netinet/in.h>
#include <ifaddrs.h>
#include "common.h"
#include "ip.h"
struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;
char ip_address[INET_ADDRSTRLEN];
int ipaddr() {//获取ip
......
#include "common.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "ip.h"
#include "opensh.h"
#include "INA226.h"
#include "heat.h"
#include "mqtt.h"
struct mosquitto *mosq;
char* TOPIC;
char* TOPIC2;
char* TOPIC3;
......@@ -12,59 +21,63 @@ int message;//存放gmessage_type的值发送心跳,2,3消息为1;
uint8_t AppExit_pin_pwm=0;//判断坦克或者车的退出
int rc=0;//判断mqtt是否成功创建
ThreadPool *pool;//线程池线程函数
//ThreadPool *pool;//线程池线程函数
unsigned char modeTemp=0;
unsigned char typeTemp=0;
unsigned char pinTemp=0;
unsigned char valTemp=0;
unsigned char gvalt[4];//存放mqtt接收的tpye,mode等
struct mosquitto *mosq;
time_t gStart;
char *glat=NULL;//加入gps后删除,心跳预留,不更改
char *glon=NULL;//加入gps后删除,心跳预留,不更改
time_t gStart;
int mqtt_init() {
// 初始化 mosquitto 库
mosquitto_lib_init();
mosq = mosquitto_new(NULL, true, NULL);
if (!mosq) {
fprintf(stderr, "Failed to create Mosquitto client\n");
return -1;
}
mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
return 0;
}
void on_connect(struct mosquitto *mosq, void *obj, int rc) {//回调函数
if (rc == 0)
{
if (rc == 0) {
printf("Connected to broker\n");
mosquitto_subscribe(mosq, NULL, TOPIC, 0);
}
else
{
} else {
fprintf(stderr, "Connection failed with code %d\n", rc);
}
}
void mqtt_wirte(){//心跳格式
float voltage = INA226_readBusVoltage(sda_pin, scl_pin);
float current = INA226_readCurrent(sda_pin, scl_pin);
char voltage_str[20]; // 足够存储转换后的字符串的缓冲区
sprintf(voltage_str, "%.2f", voltage);
char current_str[20]; // 足够存储转换后的字符串的缓冲区
sprintf(current_str, "%.2f", current);
heat_tem();//获取CPU温度
message=1;
cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject();
cJSON *head = cJSON_CreateObject();
cJSON_AddStringToObject(body, "ip", ip_address);//发送设备id
cJSON_AddStringToObject(body, "ID", TOPIC);//发送设备号
cJSON_AddStringToObject(body, "V:", voltage_str);//心跳发送电压
......@@ -72,9 +85,12 @@ void mqtt_wirte(){//心跳格式
cJSON_AddStringToObject(body,"Tem:",temperature);//发送温度
cJSON_AddStringToObject(body, "N", glat);//gps
cJSON_AddStringToObject(body, "E", glon);//gps
cJSON_AddNumberToObject(head, "message_type",message);
cJSON_AddItemToObject(root, "body", body);
cJSON_AddItemToObject(root, "head",head);
char *payload = cJSON_PrintUnformatted(root);
printf("%s\n",payload);
mosquitto_publish(mosq, NULL, TOPIC2, strlen(payload), payload, 0, false);
......@@ -82,72 +98,80 @@ void mqtt_wirte(){//心跳格式
}
void message_3(cJSON *body,cJSON *pwm_ctrl){//message_type为3,控制pwm
cJSON *mode = cJSON_GetObjectItem(pwm_ctrl, "mode"); //mode=1 速度,mode=2 转向(unsigned char)
cJSON *type = cJSON_GetObjectItem(pwm_ctrl, "type");
cJSON *val = cJSON_GetObjectItem(pwm_ctrl, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char)
modeTemp= mode->valueint;
typeTemp=type->valueint;
cJSON *mode = cJSON_GetObjectItem(pwm_ctrl, "mode"); //mode=1 速度,mode=2 转向(unsigned char)
cJSON *type = cJSON_GetObjectItem(pwm_ctrl, "type");
cJSON *val = cJSON_GetObjectItem(pwm_ctrl, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char)
modeTemp= mode->valueint;
typeTemp=type->valueint;
valTemp= val->valueint;
if(typeTemp==3){
valTemp= val->valueint;
if(typeTemp==3){
valTemp= val->valueint;
}
gvalt[0]=typeTemp;
gvalt[1]=modeTemp;
gvalt[2]=valTemp;
gPwmCount = 0;
printf("typeTemp:%d\n",gvalt[0]);
printf("modeTemp:%d\n",gvalt[1]);
printf("valTemp:%d\n",gvalt[2]);
if(AppExit_pin_pwm==1)speed_change(gvalt);
if(AppExit_pin_pwm==3) {
if (thread_pool_add_task(pool, ship_speed_change, gvalt) != 0) {
printf("Failed to add task\n");
}
}
if(AppExit_pin_pwm == 4) {
PTZ_pwm_change(gvalt);
}
}
gvalt[0]=typeTemp;
gvalt[1]=modeTemp;
gvalt[2]=valTemp;
gPwmCount = 0;
printf("typeTemp:%d\n",gvalt[0]);
printf("modeTemp:%d\n",gvalt[1]);
printf("valTemp:%d\n",gvalt[2]);
if(AppExit_pin_pwm==1) speed_change(gvalt) ;
if(AppExit_pin_pwm==3) ship_speed_change(gvalt) ;
if(AppExit_pin_pwm == 4) PTZ_pwm_change(gvalt);
}
void message_4(cJSON *body, cJSON *pin_setctrl){//message 为4时候
cJSON *pin = cJSON_GetObjectItem(pin_setctrl, "pin");
cJSON *val = cJSON_GetObjectItem(pin_setctrl, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char)
pinTemp=pin->valueint;
valTemp= val->valueint;
if(valTemp>=1){
valTemp=1;
}
gPwmCount=0;
gvalt[0]=0;
gvalt[1]=pinTemp;
gvalt[2]=valTemp;
printf("pinTemp:%d\n",gvalt[1]);
printf("valTemp:%d\n",gvalt[2]);
pin_value(gvalt[1],gvalt[2]);
}
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) {//消息回环函数
time_t end = time(NULL);
if(gStart+5>end){
printf("正在处理过期消息\n");
return;
}
// 确保消息有效
if (message->payload && message->payloadlen) { // 将消息内容转换为字符串
char *payload_str = (char *)malloc(message->payloadlen + 1);
if (!payload_str) {
return;
}
memcpy(payload_str, message->payload, message->payloadlen);
payload_str[message->payloadlen] = '\0';
// 解析 JSON
cJSON *json = cJSON_Parse(payload_str);
if (json == NULL) {
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
free(payload_str);
}
else {// 提取 head 对象
} else {// 提取 head 对象
cJSON *head = cJSON_GetObjectItem(json, "head");
if (!cJSON_IsObject(head)) {
free(payload_str);
......@@ -159,21 +183,24 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
free(payload_str);
return;
}
//提取 body数据段
cJSON *body = cJSON_GetObjectItem(json, "body");
if (!cJSON_IsObject(body)) {
free(payload_str);
return;
}
cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl");
cJSON *pin_setctrl = cJSON_GetObjectItem(body, "pin_setctrl");
if(pwm_ctrl==NULL||pin_setctrl==NULL){
free(payload_str);
return;
}
gmessage_type=message_type->valueint;
printf("message_type: %d\n",message_type->valueint);
switch(gmessage_type){
case 1:
system("sudo reboot");//重启香橙派
......
#include "common.h"
#include "mqtt.h"
#include "opensh.h"
char gwebcam[254];//存放启动火狐网站命令
int opencamsh(){
const char* url = "https://jywy.yd-ss.com?dev=";
char urls[50];
......@@ -11,7 +11,7 @@ int opencamsh(){
sprintf(urls,"%s%s",url,TOPIC3);
//setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004 --new-window sudo
sprintf(gwebcam,"su - orangepi -c \"chromium-browser --use-fake-ui-for-media-stream %s\"",urls);
sprintf(gwebcam,"su - orangepi -c \"chromium-browser --use-fake-ui-for-media-stream %s\"",urls);
system(gwebcam);
printf("%s\n",gwebcam);
printf("open cam\n");
......@@ -24,18 +24,27 @@ int opencamsh(){
void refresh_cam() {//刷新页面
setenv("XAUTHORITY", "/home/orangepi/.Xauthority", 1);//加入授权
const char *search_command = "xdotool search --class \"chromium-browser\"";//获取窗口id
FILE *fp = popen(search_command, "r");
if (!fp) {
perror("Failed to execute search command");
return;
}
char window_id[32];
while (fgets(window_id, sizeof(window_id), fp) != NULL) {
window_id[strcspn(window_id, "\n")] = '\0';
char activate_command[128];
snprintf(activate_command, sizeof(activate_command), "xdotool windowactivate --sync %s key Ctrl+r", window_id);
pid_t pid = fork();
if (pid == 0) {
// 子进程执行命令
execl("/bin/sh", "sh", "-c", activate_command, (char *)NULL);
......@@ -48,7 +57,9 @@ void refresh_cam() {//刷新页面
perror("Failed to fork");
}
}
pclose(fp);
printf("recam\n");
}
#include "pthrpoll.h"
// 初始化任务队列
static int task_queue_init(TaskQueue *queue) {
queue->head = NULL;
......
#ifndef __MAIN_H__
#define __MAIN_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <termios.h>
#include <wiringPi.h>
#include "gpio_pwm.h"
#include "gpio_pwm_2.h"
#include "ip.h"
#include "mqtt.h"
#include "delay.h"
#include "opensh.h"
#include "INA226.h"
#include "pthrpoll.h"
#include "heat.h"
#define filename "/home/orangepi/car/master/Deviceld.txt"
pthread_t thread[4];
char buffer[30]; // 用于存储文件内容
//开启线程
int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg)){
if(pthread_create(&thread[0],NULL,AppExit,NULL)!=0) {
perror("Failed to create thread 0");
return 0;
}
if(pthread_create(&thread[1],NULL,Mqttbeat,NULL)!=0) {
perror("Failed to create thread 1");
return 1;
}
if(pthread_create(&thread[2],NULL,opensh,NULL)!=0){
perror("Failed to create thread 2");
return 2;
}
if(pthread_create(&thread[3],NULL,Mqtt_onnect,NULL)!=0) {
perror("Failed to create thread 3");
return 3;
}
return 10086;
}
void *AppExit(void *arg){ //出现意外自动停止
while(1){
Delay_Ms(0,100);
gPwmCount++;
if(gPwmCount>=5) {
if(AppExit_pin_pwm==1) {//车异常问题处理
midde_pwm();
pin_all_default();
}
if(AppExit_pin_pwm==2) {//坦克异常问题处理
midde_pwm();
pin_all_default();
digitalWrite(2, LOW);
digitalWrite(21, LOW);
}
if(AppExit_pin_pwm==3) {//船异常问题处理
ship_stop_pwm();
pin_all_ship_default();
}
gPwmCount=6;
}
}
return NULL;
}
void *Mqttbeat(void *arg) {
printf("Mqttbeat start\n");
Delay_Ms(15,0);
while(1) {
ipaddr();//获取ip
Delay_Ms(3,0);
mqtt_wirte();//心跳,3s一个
}
return NULL;
}
void *opensh(void *arg) {
Delay_Ms(5,0);
printf("open cam\n");
opencamsh();//10s后打开游览器并且进入网址
return NULL;
}
void *Mqtt_onnect(void *arg) {//mqtt异常处理,断开自动重连,简单粗暴
Delay_Ms(5,0);
mqtt_init();//mqtt初始化
mqtt_create(mosq);//创建mqtt客户端
while(1){
mqtt_create(mosq);//创建mqtt客户端
mqtt_cycle(mosq); //进入mqtt死循环,异常退出继续执行while
}
mqtt_clean(mosq);
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() {
pthread_join(thread[0], NULL);
pthread_join(thread[1], NULL);
pthread_join(thread[2], NULL);
pthread_join(thread[3], NULL);
}
#endif
\ No newline at end of file
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "ip.h"
#include "mqtt.h"
#include "delay.h"
#include "opensh.h"
#include "INA226.h"
#include "pthrpoll.h"
#include "heat.h"
#include "common.h"
#include "thread_main.h"
char buffer[30]; // 用于存储文件内容
pthread_t thread[4];
//开启线程
int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg)){
if(pthread_create(&thread[0],NULL,AppExit,NULL)!=0) {
perror("Failed to create thread 0");
return 0;
}
if(pthread_create(&thread[1],NULL,Mqttbeat,NULL)!=0) {
perror("Failed to create thread 1");
return 1;
}
if(pthread_create(&thread[2],NULL,opensh,NULL)!=0){
perror("Failed to create thread 2");
return 2;
}
if(pthread_create(&thread[3],NULL,Mqtt_onnect,NULL)!=0) {
perror("Failed to create thread 3");
return 3;
}
return -1;
}
void *AppExit(void *arg) { //出现意外自动停止
while(1){
Delay_Ms(0,100);
gPwmCount++;
if(gPwmCount>=5) {
if(AppExit_pin_pwm==1) {//车异常问题处理
midde_pwm();
pin_all_default();
}
if(AppExit_pin_pwm==2) {//坦克异常问题处理
midde_pwm();
pin_all_default();
digitalWrite(2, LOW);
digitalWrite(21, LOW);
}
if(AppExit_pin_pwm==3) {//船异常问题处理
ship_stop_pwm();
pin_all_ship_default();
}
gPwmCount=6;
}
}
return NULL;
}
void *Mqttbeat(void *arg) {
printf("Mqttbeat start\n");
Delay_Ms(15,0);
while(1) {
ipaddr();//获取ip
Delay_Ms(3,0);
mqtt_wirte();//心跳,3s一个
}
return NULL;
}
void *opensh(void *arg) {
Delay_Ms(5,0);
printf("open cam\n");
opencamsh();//10s后打开游览器并且进入网址
return NULL;
}
void *Mqtt_onnect(void *arg) {//mqtt异常处理,断开自动重连,简单粗暴
Delay_Ms(5,0);
mqtt_init();//mqtt初始化
mqtt_create(mosq);//创建mqtt客户端
while(1){
mqtt_create(mosq);//创建mqtt客户端
mqtt_cycle(mosq); //进入mqtt死循环,异常退出继续执行while
}
mqtt_clean(mosq);
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() {
pthread_join(thread[0], NULL);
pthread_join(thread[1], NULL);
pthread_join(thread[2], NULL);
pthread_join(thread[3], NULL);
}
#include "gpio_pwm.h"
#include <stdbool.h>
#include "gpio_pwm_carship.h"
#include "common.h"
// 定义 PWM 引脚的 WiringPi 编号
#define PWM_PIN_SPEED 21
......@@ -8,7 +8,6 @@
int count_mode = 0;
const int gpioWPi[] = {5, 6, 7, 10, 16, 20, 22, 23, 24, 25, 26, 27};//车能使用高低引脚
const int gpioWPi_ship[] = {5, 6, 7,10, 16, 20, 22, 23, 24,25,26, 27};//船能使用高低引脚
void pin_init() {//初始化引脚,车和坦克共用
......
#ifndef __GPIO_PWM_H__
#define __GPIO_PWM_H__
#ifndef __GPIO_PWM_CARSHIP_H__
#define __GPIO_PWM_CARSHIP_H__
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
// 定义 PWM 引脚的 WiringPi 编号
void pwm_speed();//pwm控制函数
void midde_pwm();//车pwm停止函数
void speed_change(unsigned char *buf);//速度和转向值处理mqtt函数
......
#include "gpio_pwm_2.h"
#include "common.h"
#include "gpio_pwm_ptz.h"
#define PWM_PIN_up 21
#define PWM_PIN_down 2
// 舵机最小脉冲宽度(ms)
#define MIN_PULSE_WIDTH 0.5
// 舵机最大脉冲宽度(ms)
#define MAX_PULSE_WIDTH 2.5
......
#ifndef __GPIO_PWM_2_H__
#define __GPIO_PWM_2_H__
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef __GPIO_PWM_PTZ_H__
#define __GPIO_PWM_PTZ_H__
void pwm_PTZ_hz();
void PTZ_pwm_init();
void PTZ_pwm_change(unsigned char *buf) ;
#endif
\ No newline at end of file
#include <math.h>
#include "common.h"
#include "INA226.h"
float currentLSB ;
......
#ifndef INA226_H
#define INA226_H
#include <stdint.h>
#include <stdbool.h>
#include <wiringPi.h>
#include <stdio.h>
#include <math.h>
extern int sda_pin; // GPIO0
extern int scl_pin; // GPIO1
......
#include "common.h"
#include "heat.h"
#define MAX_LINE_LENGTH 20
......
#ifndef __HEAT_H__
#define __HEAT_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int heat_tem();//获取CPU温度
extern char temperature[];//温度存储
......
#include "main.h"
int Device_File_Init() {
const char *readbuf=device_inspect();
//将第3个和第4个字符提取出来
char *sub_str=malloc(3);
sub_str[0]=readbuf[2];
sub_str[1]=readbuf[3];
sub_str[2]='\0';
printf("开始初始化了");
if(strcmp(sub_str,"01")==0){//车的编码
pwm_speed(); //pwm初始化,车为停止
pin_init();
AppExit_pin_pwm=1;//车的异常停止值
free(sub_str);
}else if(strcmp(sub_str,"02")==0){//坦克的编码
pin_init();
pinMode(2, OUTPUT);//pwm引脚改为普通引脚
pinMode(21, OUTPUT);//pwm引脚改为普通引脚
digitalWrite(2, LOW);
digitalWrite(21, LOW);
AppExit_pin_pwm=2;//坦克的异常停止值
free(sub_str);
}else if(strcmp(sub_str,"03")==0){//船的编码
pwm_speed();
pin_ship_init(); //pwm初始化,车为停止
pool = thread_pool_init(1, 1);
AppExit_pin_pwm=3;//车的异常停止值
free(sub_str);
}else if(strcmp(sub_str,"04")==0){//船的编码
pin_init();
pwm_PTZ_hz();
PTZ_pwm_init();
AppExit_pin_pwm=4;//车的异常停止值
free(sub_str);
}
Delay_Ms(20,0);
TOPIC=malloc(24);
TOPIC2=malloc(24);
TOPIC3= malloc(16);
sprintf(TOPIC2,"dev2app/%s",readbuf);
sprintf(TOPIC,"app2dev/%s",readbuf);
sprintf(TOPIC3,"%s",readbuf);
printf("1:%s,2:%s,3:%s\n",TOPIC,TOPIC2,TOPIC3);
return 0;
}
int main(int argc, char *argv[]) {
if (wiringPiSetup() == -1) {
printf("WiringPi setup failed!\n");
return 1;
}
}
if (!INA226_begin(sda_pin, scl_pin)) {
printf("INA226 初始化失败!\n");
return 2;
}
if(Device_File_Init()!=0) {
printf("设备文件读取初始化失败!\n");
return 3;
}
printf("识别初始化成功\n");
system("pkill chromium");
// 配置 INA226
INA226_configure(sda_pin, scl_pin);
// 校准 INA226
float rShuntValue = 0.1; // 分流电阻值(单位:欧姆)
float iMaxExpected = 5; // 最大预期电流(单位:安培)
INA226_calibrate(sda_pin, scl_pin, rShuntValue, iMaxExpected);
gStart=time(NULL);//开始时间戳
ipaddr();//获取ip
thread_start(AppExit,Mqttbeat,opensh,Mqtt_onnect);//开启线程
thread_end();
return EXIT_SUCCESS;
}
\ No newline at end of file
#ifndef __HAIN_H__
#define __HAIN_H__
#include "common.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "ip.h"
#include "mqtt.h"
#include "delay.h"
#include "opensh.h"
#include "INA226.h"
#include "pthrpoll.h"
#include "heat.h"
#include "thread_main.h"
#include "judg.h"
#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