-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmqttclient.cpp
86 lines (78 loc) · 1.98 KB
/
mqttclient.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
/**
* Copyright (c) 2020 panStamp <[email protected]>
*
* This file is part of the RESPIRA project.
*
* panStamp is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* panStamp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with panStamp; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* Author: Daniel Berenguer
* Creation date: Oct 9 2020
*/
#include "mqttclient.h"
/**
* Pointer to custom subscription function
*/
void (*subscriptionFunct)(char *topic, char *payload) = NULL;
/**
* mqttReceive
*
* Function called whenever a MQTT packet is received
*
* @param topic MQTT topic
* @param payload message
* @param len message length
*/
void mqttReceive(char* topic, uint8_t* payload, unsigned int len)
{
if (subscriptionFunct != NULL)
{
payload[len] = 0;
subscriptionFunct(topic, (char*)payload);
}
}
/**
* begin
*
* Start client
*
* @param id : client ID
*
* @return True in case of connection success
*/
bool MQTTCLIENT::begin(char *id)
{
strcpy(clientId, id);
// Connect to MQTT broker
client.setServer(broker, port);
if (subscriptionFunct != NULL)
{
// Call this function whenever a MQTT message is received
client.setCallback(mqttReceive);
}
// Handle connection
return handle();
}
/**
* attachInterrupt
*
* Declare custom ISR, to be called whenever a MQTT packet is received
*
* @param funct pointer to the custom function
*/
void MQTTCLIENT::attachInterrupt(void (*funct)(char*, char*))
{
subscriptionFunct = funct;
}