-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mqtt.h
87 lines (71 loc) · 2.29 KB
/
Mqtt.h
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
#ifndef MQTT_H
#define MQTT_H
#include <Cbor.h>
#include <EventBus.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define HOST "limero.ddns.net"
//#define HOST "test.mosquitto.org"
#define ADDRESS "tcp://limero.ddns.net:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "stm32/alive"
#define PAYLOAD "true"
#define QOS 1
#define TIMEOUT 10000L
#define TOPIC_LENGTH 40
class Mqtt : public Actor
{
private:
WiFiClient* _wifi_client;
int _client_state;
Str _host;
uint32_t _port;
Str _clientId;
Str _user;
Str _password;
Str _willTopic;
Str _willMessage;
int _willQos;
bool _willRetain;
uint32_t _keepAlive;
int _cleanSession;
uint16_t _msgid;
uint16_t _lastSrc;
int _fd[2]; // pipe fd to wakeup in select
Str _prefix;
Str _topic;
Bytes _message;
uid_t _wifi;
static void callback(char* topic,byte* message,uint32_t length);
public:
static Mqtt* _thisMqtt;
PubSubClient* _pubSub;
void setLog(bool on);
Mqtt(const char* name,uint32_t maxSize);
virtual ~Mqtt();
void setup();
void onWifiEvent(Cbor& msg);
void onEvent(Cbor& cbor);
void onActorRegister(Cbor& cbor);
void log(char* start,uint32_t length);
void setWifi(uid_t wifi){_wifi=wifi;};
// static void onConnectionLost(void *context, char *cause);
// static int onMessage(void *context, char *topicName, int topicLen, MQTTAsync_message *message);
void disconnect(Cbor& cbor);
// static void onDisconnect(void* context, MQTTAsync_successData* response);
bool pubSubConnect();
// static void onConnectFailure(void* context, MQTTAsync_failureData* response);
// static void onConnectSuccess(void* context, MQTTAsync_successData* response);
void subscribe(Cbor& cbor);
// static void onSubscribeSuccess(void* context, MQTTAsync_successData* response);
// static void onSubscribeFailure(void* context, MQTTAsync_failureData* response);
void publish(Cbor& cbor);
// static void onPublishSuccess(void* context, MQTTAsync_successData* response);
// static void onPublishFailure(void* context, MQTTAsync_failureData* response);
void loadConfig(Cbor& cbor);
void isConnected(Cbor& cbor);
int fd();
void wakeup();
void loop();
};
#endif // MQTT_H