Commit 56aba81f authored by 学习的菜鸟's avatar 学习的菜鸟

加入了mqtt接收音频播放

parent b9f8aa0f
...@@ -352,6 +352,30 @@ device_judg/judg/judg.c.s: ...@@ -352,6 +352,30 @@ device_judg/judg/judg.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/device_judg/judg/judg.c.s $(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/device_judg/judg/judg.c.s
.PHONY : device_judg/judg/judg.c.s .PHONY : device_judg/judg/judg.c.s
src/audioplay.o: src/audioplay.c.o
.PHONY : src/audioplay.o
# target to build an object file
src/audioplay.c.o:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/src/audioplay.c.o
.PHONY : src/audioplay.c.o
src/audioplay.i: src/audioplay.c.i
.PHONY : src/audioplay.i
# target to preprocess a source file
src/audioplay.c.i:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/src/audioplay.c.i
.PHONY : src/audioplay.c.i
src/audioplay.s: src/audioplay.c.s
.PHONY : src/audioplay.s
# target to generate assembly for a file
src/audioplay.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/src/audioplay.c.s
.PHONY : src/audioplay.c.s
src/delay.o: src/delay.c.o src/delay.o: src/delay.c.o
.PHONY : src/delay.o .PHONY : src/delay.o
...@@ -1443,6 +1467,9 @@ help: ...@@ -1443,6 +1467,9 @@ help:
@echo "... device_judg/judg/judg.o" @echo "... device_judg/judg/judg.o"
@echo "... device_judg/judg/judg.i" @echo "... device_judg/judg/judg.i"
@echo "... device_judg/judg/judg.s" @echo "... device_judg/judg/judg.s"
@echo "... src/audioplay.o"
@echo "... src/audioplay.i"
@echo "... src/audioplay.s"
@echo "... src/delay.o" @echo "... src/delay.o"
@echo "... src/delay.i" @echo "... src/delay.i"
@echo "... src/delay.s" @echo "... src/delay.s"
......
...@@ -2,5 +2,5 @@ ...@@ -2,5 +2,5 @@
#define PROJECT_VERSION_MINOR 1 #define PROJECT_VERSION_MINOR 1
#define PROJECT_VERSION_PATCH 3 #define PROJECT_VERSION_PATCH 3
#define GIT_HASH "" #define GIT_HASH ""
#define BUILD_TIMESTAMP "2025-05-28T09:22:22" #define BUILD_TIMESTAMP "2025-05-29T10:56:53"
#define BUILD_USER "orangepi" #define BUILD_USER "orangepi"
No preview for this file type
...@@ -236,7 +236,7 @@ char *device_changename_back_fail(void) { ...@@ -236,7 +236,7 @@ char *device_changename_back_fail(void) {
return full_path; return full_path;
} }
//读取失败备份内容 //读取失败,读取备份内容
char *read_device_back_fail() { char *read_device_back_fail() {
FILE *file; FILE *file;
char *device_back_fail =device_changename_back_fail(); char *device_back_fail =device_changename_back_fail();
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
#define DEVICE_NAME_DIR "/home/orangepi/car/master/deviceback" #define DEVICE_NAME_DIR "/home/orangepi/car/master/deviceback"
#define DEVICE_NAME_FILE "/home/orangepi/car/master/Deviceld.txt" #define DEVICE_NAME_FILE "/home/orangepi/car/master/Deviceld.txt"
int device_changename_back(const char *device_date, const char *device_id);//设备号备份和写新设备号 int device_changename_back(const char *device_date, const char *device_id);//设备号备份和写新设备号
int file_exists(const char *path);//文件已经存在 int file_exists(const char *path);//文件已经存在
int create_directory_if_not_exists(const char *path);//创建目录 int create_directory_if_not_exists(const char *path);//创建目录
char *read_device_back_fail();//下载失败回退版本 char *read_device_back_fail();//读取失败,读取备份内容
#endif #endif
\ No newline at end of file
#ifndef AUDIOPLAY_H
#define AUDIOPLAY_H
#include <cjson/cJSON.h>
extern int audio_status;
void audioplay_mqtt_receive(cJSON *json) ;
void audioplay_cycle();
#endif
\ No newline at end of file
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "log.h" #include "log.h"
#include "delay.h" #include "delay.h"
#include "softiic.h" #include "softiic.h"
#include "audioplay.h"
// 其他公共声明 // 其他公共声明
......
#ifndef THREAD_MAIN_H__ #ifndef THREAD_MAIN_H__
#define THREAD_MAIN_H__ #define THREAD_MAIN_H__
int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg),void *delay_count(void *arg)) ; typedef void *(*ThreadFunc)(void *);
int thread_start(ThreadFunc AppExit, ThreadFunc Mqttbeat,
ThreadFunc opensh, ThreadFunc Mqtt_onnect,
ThreadFunc delay_count, ThreadFunc play_mp3_thread);
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) ;//mqtt自带重连线程 void *Mqtt_onnect(void *arg) ;//mqtt自带重连线程
void *delay_count(void *arg) ;//延时计算延迟 void *delay_count(void *arg) ;//延时计算延迟
void *play_mp3_thread(void* arg);
void thread_end() ; void thread_end() ;
#endif #endif
\ No newline at end of file
#include <common.h>
#include "audioplay.h"
#include "mqtt.h"
#include "judg.h"
int audio_status=3;
char urlbuf[512];
char keybuf[256];
double audio_volume=3.0;
void audioplay_mqtt_receive(cJSON *json) {
// 解析"audioLink"字段(修正了原始JSON中的拼写错误)
cJSON *audio_link = cJSON_GetObjectItemCaseSensitive(json, "audioLink");
if (cJSON_IsString(audio_link) && (audio_link->valuestring != NULL)) {
my_zlog_debug("音频链接: %s\n", audio_link->valuestring);
char *url=audio_link->valuestring;
snprintf(urlbuf, sizeof(urlbuf), "%s", url);
} else {
my_zlog_warn("错误:无法解析音频链接字段\n");
}
// 解析"key"字段
cJSON *key_char = cJSON_GetObjectItemCaseSensitive(json, "key");
if (cJSON_IsString(key_char) && (key_char->valuestring != NULL)) {
my_zlog_debug("音频链接: %s\n", key_char->valuestring);
char *key=key_char->valuestring;
snprintf(keybuf, sizeof(keybuf), "%s", key);
} else {
my_zlog_warn("错误:无法解析密钥字段\n");
}
// 解析"status"字段
cJSON *audio_status_val = cJSON_GetObjectItemCaseSensitive(json, "status");
if (cJSON_IsNumber(audio_status_val)) {
my_zlog_debug("标志: %d\n", audio_status_val->valueint);
audio_status=audio_status_val->valueint;
} else {
my_zlog_warn("错误:无法解析标志字段\n");
}
// 解析"volume"字段
cJSON *volume = cJSON_GetObjectItemCaseSensitive(json, "volume");
if (cJSON_IsNumber(volume)) {
my_zlog_debug("声量大小: %d\n", volume->valueint);
audio_volume=volume->valueint*1.0;
} else {
my_zlog_warn("错误:无法解析声量字段\n");
}
}
void audioplay_send_mqtt() {
cJSON* root = cJSON_CreateObject();
// 添加各个字段到 JSON 对象
cJSON_AddStringToObject(root, "type", "audio");
cJSON_AddStringToObject(root, "audioLink", urlbuf);
cJSON_AddStringToObject(root, "key", keybuf);
cJSON_AddNumberToObject(root, "status", audio_status);
cJSON_AddNumberToObject(root, "volume", (int)audio_volume);
// 将 JSON 对象转换为字符串
char* json_string = cJSON_PrintUnformatted(root);
my_zlog_debug("%s",json_string);
topic_middle_value();
mosquitto_publish(mosq, NULL, TOPIC3, strlen(json_string), json_string, 0, false);
// 释放 JSON 对象内存
cJSON_Delete(root);
}
void audioplay_cycle(){
char command[512];
int ret;
while(1){
if(audio_status==0){
char *urlmoddle=urlbuf;
snprintf(command, sizeof(command),
"sudo ffplay -nodisp -autoexit -loglevel quiet -af \"volume=%f\" \"%s\"",
audio_volume,urlmoddle);
my_zlog_debug("播放地址: %s\n", urlbuf);
ret = system(command);
if (ret == -1) {
my_zlog_error("播放失败");
audio_status=2;
}
if (WIFEXITED(ret) && WEXITSTATUS(ret) == 0) {
my_zlog_debug("播放已成功完成 : %s \n", urlbuf);
audio_status=1;
audioplay_send_mqtt();
} else {
my_zlog_warn("播放失败或中断: %s \n", urlbuf);
audio_status=2;
}
audioplay_send_mqtt();
}
}
}
\ No newline at end of file
...@@ -77,11 +77,11 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) {//回调函数 ...@@ -77,11 +77,11 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) {//回调函数
void heartbeat_send() {//心跳发送格式*5/2 void heartbeat_send() {//心跳发送格式*5/2
float voltage = INA226_readBusVoltage(); float voltage = INA226_readBusVoltage();
float current = INA226_readCurrent(); float current = INA226_readCurrent();
my_zlog_debug("%.2f\n", voltage); //my_zlog_debug("%.2f\n", voltage);
if(voltage<=0.5){ if(voltage<=0.5){
voltage=ads1115_read_channel(0)*5/2; voltage=ads1115_read_channel(0)*5/2;
current=ads1115_read_channel(1)*5/2; current=ads1115_read_channel(1)*5/2;
my_zlog_debug("%.2f\n", voltage); //my_zlog_debug("%.2f\n", voltage);
current=(current-voltage)*100; current=(current-voltage)*100;
} }
char voltage_str[20]; // 足够存储转换后的字符串的缓冲区 char voltage_str[20]; // 足够存储转换后的字符串的缓冲区
...@@ -326,7 +326,13 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -326,7 +326,13 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
cJSON *device_id=cJSON_GetObjectItem(json, "device_id"); cJSON *device_id=cJSON_GetObjectItem(json, "device_id");
device_mqttchange_name(device_id); device_mqttchange_name(device_id);
my_zlog_debug("接收到设备号更改"); my_zlog_debug("接收到设备号更改");
} else {// 提取 head 对象 } else if(cJSON_GetObjectItem(json, "type") != NULL) {
cJSON *type = cJSON_GetObjectItem(json, "type");
if (type && cJSON_IsString(type) && strcmp(type->valuestring, "audio") == 0) {
audioplay_mqtt_receive(json);
my_zlog_debug("接收到音频播放");
}
}else {// 提取 head 对象
if(download_message(json) == 0) my_zlog_debug("接收到dowwnload下载"); if(download_message(json) == 0) my_zlog_debug("接收到dowwnload下载");
if(device_message(json) != 0) my_zlog_debug("没有接收到控制信息,代码编号:%d",device_message(json)); if(device_message(json) != 0) my_zlog_debug("没有接收到控制信息,代码编号:%d",device_message(json));
} }
......
...@@ -11,39 +11,32 @@ ...@@ -11,39 +11,32 @@
#include "device_exit.h" #include "device_exit.h"
#include "thread_main.h" #include "thread_main.h"
pthread_t thread[5]; pthread_t thread[6]; // 全局线程句柄数组(或传参)
//开启线程
int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg), void* args[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
void *opensh(void *arg),void *Mqtt_onnect(void *arg),void *delay_count(void *arg)){
if(pthread_create(&thread[0],NULL,AppExit,NULL)!=0) { int thread_start(ThreadFunc AppExit, ThreadFunc Mqttbeat,
//perror("Failed to create thread 0"); ThreadFunc opensh, ThreadFunc Mqtt_onnect,
my_zlog_error("Failed to create thread 0"); ThreadFunc delay_count, ThreadFunc play_mp3_thread) {
return 1;
} ThreadFunc thread_funcs[] = {
if(pthread_create(&thread[1],NULL,Mqttbeat,NULL)!=0) { AppExit,
//perror("Failed to create thread 1"); Mqttbeat,
my_zlog_error("Failed to create thread 1"); opensh,
return 2; Mqtt_onnect,
} delay_count,
if(pthread_create(&thread[2],NULL,opensh,NULL)!=0){ play_mp3_thread
//perror("Failed to create thread 2"); };
my_zlog_error("Failed to create thread 2");
return 3; for (int i = 0; i < 6; i++) {
} if (pthread_create(&thread[i], NULL, thread_funcs[i], args[i]) != 0) {
if(pthread_create(&thread[3],NULL,Mqtt_onnect,NULL)!=0) { my_zlog_error("创建线程 %d 失败", i);
//perror("Failed to create thread 3"); return i + 1;
my_zlog_error("Failed to create thread 3"); }
return 4;
}
if(pthread_create(&thread[4],NULL,delay_count,NULL)!=0) {
//perror("Failed to create thread 3");
my_zlog_error("Failed to create thread 4");
return 5;
} }
return 0; return 0;
} }
void *AppExit(void *arg) { //出现意外自动停止 void *AppExit(void *arg) { //出现意外自动停止
while(1){ while(1){
Delay_Ms(0,100); Delay_Ms(0,100);
...@@ -104,8 +97,13 @@ void *delay_count(void *arg) {//专门用于计时的线程 ...@@ -104,8 +97,13 @@ void *delay_count(void *arg) {//专门用于计时的线程
device_delay_count++; device_delay_count++;
if(device_delay_count >= 5000) device_delay_count = 5000; if(device_delay_count >= 5000) device_delay_count = 5000;
} }
return NULL;
} }
void *play_mp3_thread(void* arg) {//专门处理MP3播放
audioplay_cycle();
return NULL;
}
// 等待线程结束 // 等待线程结束
void thread_end() { void thread_end() {
...@@ -114,5 +112,6 @@ void thread_end() { ...@@ -114,5 +112,6 @@ void thread_end() {
pthread_join(thread[2], NULL); pthread_join(thread[2], NULL);
pthread_join(thread[3], NULL); pthread_join(thread[3], NULL);
pthread_join(thread[4], NULL); pthread_join(thread[4], NULL);
pthread_join(thread[5], NULL);
} }
#include "main.h" #include "main.h"
void audio_Init() {
int adino_jude = system("pactl load-module module-alsa-source device=hw:2,0");
int adino_play_jude =system("pactl load-module module-alsa-sink device=hw:2,0");
if (adino_jude == 0 && adino_play_jude==0) {
my_zlog_debug("命令执行成功!");
} else {
my_zlog_warn("麦命令执行失败,返回状态码: %d", adino_jude );
my_zlog_warn("喇叭命令执行失败,返回状态码: %d", adino_play_jude );
}
}
int main_start_error_return() { int main_start_error_return() {
if(logconf_create() != 0) { if(logconf_create() != 0) {
return 1; return 1;
...@@ -25,14 +39,7 @@ int main_start_error_return() { ...@@ -25,14 +39,7 @@ int main_start_error_return() {
return 6; return 6;
} }
int adino_jude = system("pactl load-module module-alsa-source device=hw:2,0"); audio_Init();//音频初始化
if (adino_jude == 0) {
my_zlog_debug("命令执行成功!");
} else {
my_zlog_warn("命令执行失败,返回状态码: %d", adino_jude );
}
my_zlog_info("程序初始化成功"); my_zlog_info("程序初始化成功");
system("pkill chromium"); system("pkill chromium");
...@@ -54,7 +61,7 @@ int main_start_error_return() { ...@@ -54,7 +61,7 @@ int main_start_error_return() {
my_zlog_fatal("Ip获取失败"); my_zlog_fatal("Ip获取失败");
return -1; return -1;
} }
if(thread_start(AppExit,Mqttbeat,opensh,Mqtt_onnect,delay_count) !=0 ) {//开启线程) if(thread_start(AppExit,Mqttbeat,opensh,Mqtt_onnect,delay_count,play_mp3_thread) !=0 ) {//开启线程)
my_zlog_fatal("开启线程失败"); my_zlog_fatal("开启线程失败");
return -1 ; return -1 ;
} }
......
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