Commit 8ea141de authored by 957dd's avatar 957dd

update mqtt

parents f996c02d 263b27a2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include "mqtt.h"
char TOPIC[18];
void get_mac_address(const char *iface) {
int sock;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, 0); // 创建套接字
if (sock < 0) {
perror("Socket creation failed");
return;
}
strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1); // 指定网络接口
if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) { // 获取硬件地址
perror("ioctl failed");
close(sock);
//return;
}
unsigned char *mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
printf("MAC address of %s: %02X:%02X:%02X:%02X:%02X:%02X\n",
iface, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(TOPIC,"%02X:%02X:%02X:%02X:%02X:%02X",mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
close(sock); // 关闭套接字
}
\ No newline at end of file
#ifndef __MAC_H__
#define __MAC_H__
extern char TOPIC[18];
void get_mac_address(const char *iface) ;
#endif
No preview for this file type
#include "main.h"
#include "ip.h"
#include "mqtt.h"
#include "serial.h"
#include "delay.h"
#include "opensh.h"
#include "mac.h"
void *serial_usart1(void *arg)
{
printf("serial_usart 1 start\n");
serial_Receive_1();
return NULL;
}
void *AppExit(void *arg)
{
while(1)
{
unsigned char bufpwm1[3]={gtypeTemp,1,0};
unsigned char bufpwm2[3]={gtypeTemp,2,0};
unsigned char bufpwm3[3]={gtypeTemp,3,0};
unsigned char bufpwm4[3]={gtypeTemp,4,0};
unsigned char bufpwm5[3]={gtypeTemp,5,15};
if(gtypeTemp==3)
{
gPwmCount=0;
}
Delay_Ms(0,100);
gPwmCount++;
//printf( "%d\n",gtypeTemp);
if(gPwmCount>20)
{
//printf( "%d\n",gtypeTemp);
gPwmCount=21;
serial_Write(bufpwm1);
serial_Write(bufpwm2);
serial_Write(bufpwm3);
serial_Write(bufpwm4);
serial_Write(bufpwm5);
}
}
return NULL;
}
void *Mqttbeat(void *arg)
{
printf("Mqttbeat start\n");
while(1)
{
mqtt_wirte();
Delay_Ms(3,0);
}
return NULL;
}
void *opensh(void *arg)
{
Delay_Ms(10,0);
printf("open cam\n");
opencamsh();
return NULL;
}
void *serial_usart2(void *arg)
{
printf("serial_usart 2 start\n");
serial_Receive_2();
}
int main(int argc, char *argv[]) {
gStart=time(NULL);//开始时间戳
get_mac_address("eth0");
serial_Init();//串口初始化
mqtt_init();//mqtt初始化
mqtt_create(mosq);//创建mqtt客户端
ipaddr();//获取ip
mqtt_wirte();//mqtt心跳首次发送
//mosquitto_publish(mosq, NULL, "macmqtt", strlen(TOPIC), TOPIC, 0, false);
mqtt_MAC_wirte();
Delay_Ms(1,0);
mqtt_MAC_wirte();
Delay_Ms(1,0);
mqtt_MAC_wirte();
thread_start(serial_usart1,AppExit,Mqttbeat,serial_usart2,opensh);
mqtt_cycle(mosq);
mqtt_clean(mosq);
thread_end();
return EXIT_SUCCESS;
#include "main.h"
#include "ip.h"
#include "mqtt.h"
#include "serial.h"
#include "delay.h"
#include "opensh.h"
//#include "mac.h"
void *serial_usart1(void *arg)
{
printf("serial_usart 1 start\n");
serial_Receive_1();//串口接收1,用于以后读取引脚高低电平
return NULL;
}
void *AppExit(void *arg)//2s程序,出现意外2s自动停止
{
while(1)
{
unsigned char bufpwm1[3]={gtypeTemp,1,0};
unsigned char bufpwm2[3]={gtypeTemp,2,0};
unsigned char bufpwm3[3]={gtypeTemp,3,0};
unsigned char bufpwm4[3]={gtypeTemp,4,0};
//unsigned char bufpwm5[3]={gtypeTemp,5,15};
Delay_Ms(0,100);
gPwmCount++;
//printf( "%d\n",gtypeTemp);
if(gPwmCount>20)
{
//printf( "%d\n",gtypeTemp);
gPwmCount=21;
//serial_Write(bufpwm5);
serial_Write(bufpwm1);
serial_Write(bufpwm2);
serial_Write(bufpwm3);
serial_Write(bufpwm4);
}
}
return NULL;
}
void *Mqttbeat(void *arg)
{
printf("Mqttbeat start\n");
Delay_Ms(10,0);
while(1)
{
mqtt_wirte();//心跳,3s一个
Delay_Ms(3,0);
}
return NULL;
}
void *opensh(void *arg)
{
Delay_Ms(10,0);
printf("open cam\n");
opencamsh();//10s后打开火狐游览器并且进入网址
return NULL;
}
void *serial_usart2(void *arg)
{
printf("serial_usart 2 start\n");
serial_Receive_2();//启动串口二
return NULL;
}
void *Mqtt_onnect(void *arg)
{
Delay_Ms(5,0);
mqtt_init();//mqtt初始化
mqtt_create(mosq);//创建mqtt客户端
//mqtt_wirte();//mqtt心跳首次发送
while(1)
{
mqtt_create(mosq);
mqtt_cycle(mosq);
}
mqtt_clean(mosq);
return NULL;
}
int main(int argc, char *argv[]) {
Delay_Ms(20,0);
gStart=time(NULL);//开始时间戳
serial_Init();//串口初始化
// get_mac_address("eth0");//获取mac地址
ipaddr();//获取ip
//mosquitto_publish(mosq, NULL, "macmqtt", strlen(TOPIC), TOPIC, 0, false);
thread_start(serial_usart1,AppExit,Mqttbeat,serial_usart2,opensh,Mqtt_onnect);
//mqtt_cycle(mosq);
thread_end();
return EXIT_SUCCESS;
}
\ No newline at end of file
......@@ -11,7 +11,8 @@ pthread_t thread[5];
int thread_start(void *serial_usart1(void *arg),void *AppExit(void *arg),void *Mqttbeat(void *arg),void *serial_usart2(void *arg),void *opensh(void *arg))
int thread_start(void *serial_usart1(void *arg),void *AppExit(void *arg),
void *Mqttbeat(void *arg),void *serial_usart2(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg))
{
......@@ -35,15 +36,20 @@ int thread_start(void *serial_usart1(void *arg),void *AppExit(void *arg),void *M
if(pthread_create(&thread[3],NULL,serial_usart2,NULL)!=0)
{
perror("Failed to create thread 1");
perror("Failed to create thread 4");
return 4;
}
if(pthread_create(&thread[4],NULL,opensh,NULL)!=0)
{
perror("Failed to create thread 1");
perror("Failed to create thread 5");
return 5;
}
if(pthread_create(&thread[5],NULL,Mqtt_onnect,NULL)!=0)
{
perror("Failed to create thread 6");
return 6;
}
}
void thread_end()
......@@ -60,6 +66,8 @@ void thread_end()
pthread_join(thread[3], NULL);
pthread_join(thread[4], NULL);
pthread_join(thread[5], NULL);
}
#endif
\ No newline at end of file
......@@ -16,9 +16,22 @@ unsigned char gpinTemp=0;
unsigned char gvalTemp=0;
unsigned char gvalt[4];
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);
}
void on_connect(struct mosquitto *mosq, void *obj, int rc) //回调函数
{
if (rc == 0)
{
printf("Connected to broker\n");
......@@ -32,30 +45,19 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) //回调函数
}
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);
}
//int init_mqtt(struct mosquitto *mosq)
/*
void mqtt_MAC_wirte()
{
cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject();
cJSON_AddStringToObject(body, "TOPIC", TOPIC);
cJSON_AddStringToObject(body, "device", "controlcar0001");
cJSON_AddStringToObject(body, "http", "https://jywy.yd-ss.com?dev=controlcar0001");//gsite
cJSON_AddItemToObject(root, "body", body);
char *payload = cJSON_PrintUnformatted(root);
mosquitto_publish(mosq, NULL, "macmqtt", strlen(payload), payload, 0, false);
}
}*/
void mqtt_wirte()
{
......@@ -81,8 +83,8 @@ void mqtt_wirte()
char *payload = cJSON_PrintUnformatted(root);
printf("%s\n",payload);
mosquitto_publish(mosq, NULL, TOPIC, 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);
}
......@@ -197,7 +199,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if(gStart+5>end)
{
printf("正在处理过期消息");
printf("正在处理过期消息\n");
return;
}
......@@ -280,7 +282,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端
{
mosquitto_reconnect_delay_set(mosq, 2, 10, true);
// 设置连接和消息回调
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
......@@ -294,7 +296,7 @@ int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端
if (rc != MOSQ_ERR_SUCCESS) {
fprintf(stderr, "Failed to connect to broker: %s\n", mosquitto_strerror(rc));
mosquitto_destroy(mosq);
return EXIT_FAILURE;
//return EXIT_FAILURE;
}
//mosquitto_subscribe_opt_set(mosq, TOPIC, strlen(TOPIC), 1, MOSQ_OPT_NO_LOCAL);
......
......@@ -7,14 +7,15 @@
#include <pthread.h>
#include "ip.h"
#include "serial.h"
#include "mac.h"
#define CLIENT_ID "carorship123"
#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883
//#define TOPIC "controcar0004"
#define TOPIC "app2dev/controlcar0003"
#define TOPIC2 "dev2app/controlcar0003"
#define TOPIC3 "controlcar0003"
#define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码
......
#include <stdio.h>
#include <stdlib.h>
#include "opensh.h"
#include "mac.h"
#include <string.h>
#include "mqtt.h"
char webcam[100];
char gwebcam[100];
int opencamsh()
{
setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004
sprintf(webcam,"sudo firefox --new-window https://jywy.yd-ss.com?dev=%s &",TOPIC);
system(webcam);
printf("%s\n",webcam);
setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004 --new-window sudo
sprintf(gwebcam,"sudo firefox --new-window https://jywy.yd-ss.com?dev=%s &",TOPIC3);
system(gwebcam);
printf("%s\n",gwebcam);
printf("close cam\n");
}
void refresh_cam()
{
system(SHELLFILECLOSE);
//system(SHELLFILECLOSE);
const char *search_command = "xdotool search --name \"Mozilla Firefox\"";
FILE *fp = popen(search_command, "r");
if (!fp) {
......
......@@ -2,8 +2,10 @@
#define __opensh_H__
#define SHELLFILE "/home/kickpi/car/master/camopen.sh"
#define SHELLFILECLOSE "/home/kickpi/car/master/recam.sh"
//#define SHELLFILE "/home/kickpi/car/master/camopen.sh"
//#define SHELLFILECLOSE "/home/kickpi/car/master/recam.sh"
extern char gwebcam[100];
int opencamsh();
......
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