Commit c8e33e86 authored by 957dd's avatar 957dd

Your commit message

parent 5f7d710c
master @ 8215ad18
Subproject commit 8215ad1887f59c6c08ac10c631d84ccbbc0f41cc
......@@ -2,7 +2,8 @@
#include "mqtt.h"
#include "serial.h"
struct mosquitto *mosq;
//mosq = mosquitto_new(CLIENT_ID, true, NULL);
void Delay_Ms()
{
......@@ -54,21 +55,21 @@ int main(int argc, char *argv[]) {
gStart=time(NULL);
// 初始化 mosquitto 库
mosquitto_lib_init();
// 创建 mosquitto 客户端
mosq = mosquitto_new(CLIENT_ID, true, NULL);
if (!mosq) {
fprintf(stderr, "Failed to create mosquitto client\n");
return EXIT_FAILURE;
}
init_mqtt(mosq);
// 创建 mosquitto 客户端
//if (!mosq) {
// fprintf(stderr, "Failed to create mosquitto client\n");
// return EXIT_FAILURE;
// }
// 设置连接和消息回调
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
//mosquitto_connect_callback_set(mosq, on_connect);
// mosquitto_message_callback_set(mosq, on_message);
// 设置用户名和密码
mosquitto_username_pw_set(mosq, USERNAME, PASSWORD);
//mosquitto_username_pw_set(mosq, USERNAME, PASSWORD);
// 连接到 MQTT 代理
rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60);
......@@ -79,7 +80,7 @@ int main(int argc, char *argv[]) {
}
ipaddr();
mosquitto_publish(mosq, NULL, TOPIC, strlen(ip_address), ip_address, 0, false);//将ip发送给mqtt
send_mqtt_message(mosq, TOPIC,ip_address);//将ip发送给mqtt
pthread_t thread[2];
......
......@@ -3,6 +3,30 @@
time_t gStart;
int gPwmCount = 0; // 计数
struct mosquitto *mosq;
int init_mqtt(struct mosquitto *mosq)
{
if (!mosq) {
fprintf(stderr, "Failed to create Mosquitto client\n");
return -1;
}
// 设置连接和消息回调
mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message);
//设置用户名和密码
mosquitto_username_pw_set(mosq, USERNAME, PASSWORD);
}
void send_mqtt_message(struct mosquitto *mosq,const char *topic, const char *message)
{
mosquitto_publish(mosq, NULL, topic, strlen(message), message, 0, false) ;
}
void on_connect(struct mosquitto *mosq, void *obj, int rc)
{
......
......@@ -8,18 +8,25 @@
#include "ip.h"
#include "serial.h"
#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883
#define CLIENT_ID "device1car/carer54"
#define CLIENT_ID "device1car/carer5434"
#define TOPIC "devices/controcar03"
#define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码
extern struct mosquitto *mosq;
extern time_t gStart;
extern int gPwmCount; // 计数
int init_mqtt(struct mosquitto *mosq) ;
void send_mqtt_message(struct mosquitto *mosq,const char *topic, const char *message);
void on_connect(struct mosquitto *mosq, void *obj, int rc);
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) ;
......
#include "serial.h"
#include "mqtt.h"
int fd;
......@@ -31,20 +32,54 @@ int init_serial(const char *port, int baudrate) {
return fd;
}
void Delay_Ms1()
{
struct timespec ts1;
ts1.tv_sec = 1; // 秒
ts1.tv_nsec =0 ;//100000000; // 1毫秒 = 1000000纳秒
nanosleep(&ts1,NULL);
}
void serial_Receive() {
char buffer[254]="\0";
unsigned char buffer[256];
//mosq = mosquitto_new(CLIENT_ID, true, NULL);
while (1) {
int bytes_read = read(fd, buffer, sizeof(buffer)-1);
if (bytes_read > 0) {
//printf("Received chatdate: %s\n", buffer);
buffer[bytes_read] = '\0';
printf("Received chatdate:: %s\n", buffer);
//buffer[bytes_read] = '0xFF';
printf("Received chatdate:: %d\n", buffer[1]);
//int receivedNumber = atoi(buffer);
//printf("Received intdate: %d\n", receivedNumber);
//mosquitto_publish(mosq, NULL, TOPIC, strlen(buffer), buffer, 0, false);
//printf("%s\n",mosq);
int i=buffer[1];
if(i==1)
{
send_mqtt_message(mosq, TOPIC,"1");
i=0;
}
if(i==2)
{
send_mqtt_message(mosq, TOPIC,"2");
i=0;
}
if(i==3)
{
send_mqtt_message(mosq, TOPIC,"3");
i=0;
}
if(i==4)
{
send_mqtt_message(mosq, TOPIC,"4");
i=0;
}
}
Delay_Ms1();
}
close(fd);
//free(buffer);
......
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