summaryrefslogtreecommitdiff
path: root/platformio/relayctl/src/mqtt.h
blob: a769750e2603c1c7dcc26d3fadc38037fe1d7575 (plain)
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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Ticker.h>
#include "stopwatch.h"

namespace homekit::mqtt {

class MQTT {
private:
    WiFiClientSecure wifiClient;
    PubSubClient client;
    Ticker reconnectTimer;

    void callback(char* topic, uint8_t* payload, size_t length);
    void handleRelayPowerPayload(uint8_t* payload, uint32_t length);
    bool publish(const char* topic, uint8_t* payload, size_t length);
    bool subscribe(const char* topic);
    void sendInitialStat();

public:
    StopWatch statStopWatch;

    MQTT();
    void connect();
    void disconnect();
    void reconnect();
    bool loop();
    void sendStat();
};

struct StatFlags {
    uint8_t state: 1;
    uint8_t config_changed_value_present: 1;
    uint8_t config_changed: 1;
    uint8_t reserved: 5;
} __attribute__((packed));

struct InitialStatPayload {
    uint32_t ip;
    uint8_t fw_version;
    int8_t rssi;
    uint32_t free_heap;
    StatFlags flags;
} __attribute__((packed));

struct StatPayload {
    int8_t rssi;
    uint32_t free_heap;
    StatFlags flags;
} __attribute__((packed));

struct PowerPayload {
    char secret[12];
    uint8_t state;
} __attribute__((packed));

}