Commit b8278eba authored by 957dd's avatar 957dd

update mqtt

parent 67263504
This source diff could not be displayed because it is too large. You can view the blob instead.
#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];
//char gsite[18];//网址后缀,使用mac地址
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]);
//sprintf(gsite,"https://jywy.yd-ss.com?dev=%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];
//extern char gsite[18];
void get_mac_address(const char *iface) ;
#endif
No preview for this file type
......@@ -4,19 +4,19 @@
#include "serial.h"
#include "delay.h"
#include "opensh.h"
#include "mac.h"
//#include "mac.h"
void *serial_usart1(void *arg)
{
printf("serial_usart 1 start\n");
serial_Receive_1();
serial_Receive_1();//串口接收1,用于以后读取引脚高低电平
return NULL;
}
void *AppExit(void *arg)
void *AppExit(void *arg)//2s程序,出现意外2s自动停止
{
while(1)
......@@ -25,11 +25,7 @@ void *AppExit(void *arg)
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;
}
//unsigned char bufpwm5[3]={gtypeTemp,5,15};
Delay_Ms(0,100);
gPwmCount++;
//printf( "%d\n",gtypeTemp);
......@@ -37,11 +33,13 @@ void *AppExit(void *arg)
{
//printf( "%d\n",gtypeTemp);
gPwmCount=21;
//serial_Write(bufpwm5);
serial_Write(bufpwm1);
serial_Write(bufpwm2);
serial_Write(bufpwm3);
serial_Write(bufpwm4);
serial_Write(bufpwm5);
}
}
......@@ -52,10 +50,10 @@ void *Mqttbeat(void *arg)
{
printf("Mqttbeat start\n");
Delay_Ms(5,0);
while(1)
{
mqtt_wirte();
mqtt_wirte();//心跳,3s一个
Delay_Ms(3,0);
}
......@@ -67,7 +65,7 @@ void *opensh(void *arg)
{
Delay_Ms(10,0);
printf("open cam\n");
opencamsh();
opencamsh();//10s后打开火狐游览器并且进入网址
return NULL;
}
......@@ -75,39 +73,49 @@ void *serial_usart2(void *arg)
{
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);
gStart=time(NULL);//开始时间戳
//mqtt_wirte();//mqtt心跳首次发送
while(1)
{
mqtt_init();//mqtt初始化
mqtt_create(mosq);//创建mqtt客户端
mqtt_create(mosq);
if(mqtt_create(mosq)!=-1)
{
mqtt_cycle(mosq);
}
mqtt_clean(mosq);
}
return NULL;
}
int main(int argc, char *argv[]) {
serial_Init();//串口初始化
mqtt_init();//mqtt初始化
gStart=time(NULL);//开始时间戳
mqtt_create(mosq);//创建mqtt客户端
serial_Init();//串口初始化
get_mac_address("eth0");
// get_mac_address("eth0");//获取mac地址
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);
thread_start(serial_usart1,AppExit,Mqttbeat,serial_usart2,opensh,Mqtt_onnect);
//mqtt_cycle(mosq);
mqtt_clean(mosq);
thread_end();
return EXIT_SUCCESS;
......
......@@ -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
......@@ -46,7 +46,7 @@ int mqtt_init()
}
//int init_mqtt(struct mosquitto *mosq)
/*
void mqtt_MAC_wirte()
{
cJSON *root = cJSON_CreateObject();
......@@ -57,7 +57,7 @@ void mqtt_MAC_wirte()
cJSON_AddItemToObject(root, "body", body);
char *payload = cJSON_PrintUnformatted(root);
mosquitto_publish(mosq, NULL, "macmqtt", strlen(payload), payload, 0, false);
}
}*/
void mqtt_wirte()
{
......@@ -83,7 +83,7 @@ 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);
}
......@@ -199,7 +199,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if(gStart+5>end)
{
printf("正在处理过期消息");
printf("正在处理过期消息\n");
return;
}
......
......@@ -7,14 +7,14 @@
#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 "controlcar0001"
#define TOPIC "IEP/controlcar0001"
#define TOPIC2 "IEQ/controlcar0001"
#define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码
......
#include <stdio.h>
#include <stdlib.h>
#include "opensh.h"
#include "mac.h"
#include <string.h>
char gwebcam[100];
......@@ -11,7 +10,7 @@ int opencamsh()
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 &",gsite);
system("firefox https://jywy.yd-ss.com?dev=controlcar0001");
system("sudo firefox https://jywy.yd-ss.com?dev=controlcar0001");
printf("%s\n",gwebcam);
printf("close cam\n");
......
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