-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmqtt.cpp
138 lines (120 loc) · 4.06 KB
/
mqtt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "mqtt.h"
#define PUBLISH_TOPIC "EXAMPLE_TOPIC"
#ifdef DEBUG
#include <iostream>
#endif
#include "tools-utils.h"
mqtt_client::mqtt_client(const char *id, const char *host, int port) : mosquittopp(id)
{
int keepalive = DEFAULT_KEEP_ALIVE;
connect(host, port, keepalive);
}
mqtt_client::~mqtt_client()
{
}
void mqtt_client::on_connect(int rc)
{
if (!rc)
{
subscribe(NULL, "/iot/zolertia/cmd");
#ifdef DEBUG
std::cout << "Connected - code " << rc << std::endl;
#endif
}
}
void mqtt_client::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
#ifdef DEBUG
std::cout << "Subscription succeeded." << std::endl;
#endif
}
void mqtt_client::publishmsg(const char* topic,const char* messagedata)
{
publish(NULL, topic, strlen(messagedata), messagedata);
#ifdef DEBUG
std::cout << "publish succeeded." << std::endl;
#endif
}
void mqtt_client::on_message(const struct mosquitto_message *message)
{
int payload_size = MAX_PAYLOAD + 1;
char buf[payload_size];
if(!strcmp(message->topic, "/iot/zolertia/cmd"))
{
memset(buf, 0, payload_size * sizeof(char));
/* Copy N-1 bytes to ensure always 0 terminated. */
memcpy(buf, message->payload, MAX_PAYLOAD * sizeof(char));
#ifdef DEBUG
std::cout << buf << std::endl;
#endif
/*// Examples of messages for M2M communications...
if (!strcmp(buf, "STATUS"))
{
snprintf(buf, payload_size, "This is a Status Message...");
publish(NULL, PUBLISH_TOPIC, strlen(buf), buf);
#ifdef DEBUG
std::cout << "Status Request Recieved." << std::endl;
#endif
}
if (!strcmp(buf, "ON"))
{
snprintf(buf, payload_size, "Turning on...");
publish(NULL, PUBLISH_TOPIC, strlen(buf), buf);
#ifdef DEBUG
std::cout << "Request to turn on." << std::endl;
#endif
}
if (!strcmp(buf, "OFF"))
{
snprintf(buf, payload_size, "Turning off...");
publish(NULL, PUBLISH_TOPIC, strlen(buf), buf);
#ifdef DEBUG
std::cout << "Request to turn off." << std:: endl;
#endif
}*/
//MQTT::Message &message = md.message;
char reply_command[500];
char json_str[500];
char topic_str[500];
int ret=0;
//pthread_mutex_lock( &mqtt_mutex );
//printf("Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n",
// ++arrivedcount, message.qos, message.retained, message.dup, message.id);
//printf("Payload %.*s\n", (int)message.payloadlen, (char*)message.payload);
//stringlog("Message arrived: Payload %.*s\n", 0,(int)message.payloadlen, (char*)message.payload);
if(strcmp("on",(char *)buf)==0){
system("sudo /share/yepkit-USB-hub/ykushcmd -u 1 &");
sleep(3);
publishmsg("/iot/zolertia/reply","{\"Node_id\": \"0\",\"Command\": \"on\",\"Value\": \"on\",\"Result\": \"Success\"}");
}
if(strcmp("off",(char *)buf)==0){
system("sudo /share/yepkit-USB-hub/ykushcmd -d 1 &");
sleep(3);
publishmsg("/iot/zolertia/reply","{\"Node_id\": \"0\",\"Command\": \"off\",\"Value\": \"off\",\"Result\": \"Success\"}");
}
if(strncmp("SETPANID=",(char *)buf,strlen("SETPANID="))==0 || strncmp("SETCH=",(char *)buf,strlen("SETCH="))==0 || strncmp("SETTX=",(char *)buf,strlen("SETTX="))==0){
strcpy(json_str,"{");
memset(reply_command,0,sizeof(char)*500);
ret=SerialCommand((char *)buf,reply_command);
//strcat(json_str,"\"Temperature\":\"");
if(strlen(reply_command)>0){
strcat(json_str,reply_command);
strcpy(topic_str,"/iot/zolertia/reply");
}
else{
strcpy(topic_str,"/iot/zolertia/error");
if(ret == FAILEDONACCESS){
strcat(json_str,"SERIALACCESSERROR");
}
else
strcat(json_str,"SERIALREADWRITEERROR");
}
//strcat(json_str,"\"");
//printf("temperature %s\n",reply_command);
strcat(json_str,"}");
printf("publishing message %s\n",json_str);
publishmsg(topic_str,json_str);
}
//pthread_mutex_unlock( &mqtt_mutex );
}
}