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