Commit b351dc32 authored by 957dd's avatar 957dd

update

parent 88998a2a
...@@ -23,7 +23,6 @@ int ipaddr() { ...@@ -23,7 +23,6 @@ int ipaddr() {
if (ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == AF_INET) { if (ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == AF_INET) {
sa = (struct sockaddr_in *) ifa->ifa_addr; sa = (struct sockaddr_in *) ifa->ifa_addr;
inet_ntop(AF_INET, &sa->sin_addr, ip_address, sizeof(ip_address)); inet_ntop(AF_INET, &sa->sin_addr, ip_address, sizeof(ip_address));
//printf("%s: %s ", ifa->ifa_name, ip_address);
} }
} }
......
#ifndef __IP_H__ #ifndef __IP_H__
#define __IP_H__ #define __IP_H__
extern char ip_address[];//INET_ADDRSTRLEN #include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
extern char ip_address[INET_ADDRSTRLEN];//
int ipaddr(); int ipaddr();
......
File mode changed from 100644 to 100755
No preview for this file type
...@@ -206,12 +206,12 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -206,12 +206,12 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if (message->payload && message->payloadlen) if (message->payload && message->payloadlen)
{ {
// 将消息内容转换为字符串 // 将消息内容转换为字符串
//char *payload_str = (char *)malloc(message->payloadlen + 1); char *payload_str = (char *)malloc(message->payloadlen + 1);
//if (!payload_str) if (!payload_str)
//{ {
// return; return;
//} }
static char payload_str[200];
memcpy(payload_str, message->payload, message->payloadlen); memcpy(payload_str, message->payload, message->payloadlen);
payload_str[message->payloadlen] = '\0'; payload_str[message->payloadlen] = '\0';
...@@ -220,7 +220,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -220,7 +220,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if (json == NULL) if (json == NULL)
{ {
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr()); fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
//free(payload_str); free(payload_str);
} }
else else
{ {
...@@ -228,19 +228,20 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -228,19 +228,20 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
cJSON *head = cJSON_GetObjectItem(json, "head"); cJSON *head = cJSON_GetObjectItem(json, "head");
if (!cJSON_IsObject(head)) if (!cJSON_IsObject(head))
{ {
//free(payload_str); free(payload_str);
return; return;
} }
// 提取 message_type // 提取 message_type
cJSON *message_type = cJSON_GetObjectItem(head, "message_type"); cJSON *message_type = cJSON_GetObjectItem(head, "message_type");
if (!cJSON_IsNumber(message_type)) if (!cJSON_IsNumber(message_type))
{ {
//free(payload_str); free(payload_str);
return; return;
} }
cJSON *body = cJSON_GetObjectItem(json, "body"); cJSON *body = cJSON_GetObjectItem(json, "body");
if (!cJSON_IsObject(body)) if (!cJSON_IsObject(body))
{ {
free(payload_str);
return; return;
} }
cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl"); cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl");
...@@ -248,7 +249,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -248,7 +249,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if(pwm_ctrl==NULL||pin_setctrl==NULL) if(pwm_ctrl==NULL||pin_setctrl==NULL)
{ {
//free(payload_str); free(payload_str);
return; return;
} }
gmessage_type=message_type->valueint; gmessage_type=message_type->valueint;
...@@ -275,7 +276,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -275,7 +276,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
} }
// 释放 payload 字符串 // 释放 payload 字符串
//free(payload_str); free(payload_str);
} }
} }
...@@ -300,18 +301,11 @@ int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端 ...@@ -300,18 +301,11 @@ int mqtt_create(struct mosquitto *mosq) //创建mqtt客服端
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);
} }
int mqtt_cycle(struct mosquitto *mosq) int mqtt_cycle(struct mosquitto *mosq)
{ {
//mosquitto_message_callback_set(mosq, NULL);
// 开始循环,处理网络流量
//mosquitto_loop_start(mosq);//后台处理
mosquitto_loop_forever(mosq, -1, 1); // -1 表示无限等待,1 表示处理消息的最大数量 mosquitto_loop_forever(mosq, -1, 1); // -1 表示无限等待,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