forked from mysensors/Raspberry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyMQTT.h
95 lines (77 loc) · 3.41 KB
/
MyMQTT.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
88
89
90
91
92
93
94
95
/*
The MySensors library adds a new layer on top of the RF24 library.
It handles radio network routing, relaying and ids.
Created by Henrik Ekblad <henrik.ekblad@gmail.com>
Modified by Daniel Wiegert
06.Dez.14 Ported to Parpberry Pi by busa <busa@gmx.ch>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#ifndef MyMQTT_h
#define MyMQTT_h
#include "MySensor.h"
//////////////////////////////////////////////////////////////////
#ifdef DEBUG
#define TCPDUMP // Dump TCP packages.
#endif
#define MQTT_FIRST_SENSORID 20 // If you want manually configured nodes below this value. 255 = Disable
#define MQTT_LAST_SENSORID 254 // 254 is max! 255 reserved.
#define MQTT_BROKER_PREFIX "MyMQTT" // First prefix in MQTT tree, keep short!
#define MQTT_SEND_SUBSCRIPTION 1 // Send empty payload (request) to node upon MQTT client subscribe request.
// NOTE above : Beware to check if there is any length on payload in your incommingMessage code:
// Example: if (msg.type==V_LIGHT && strlen(msg.getString())>0) otherwise the code might do strange things.
//////////////////////////////////////////////////////////////////
#define EEPROM_LATEST_NODE_ADDRESS ((uint8_t)EEPROM_LOCAL_CONFIG_ADDRESS)
#define MQTT_MAX_PACKET_SIZE 100
#define MQTTPROTOCOLVERSION 3
#define MQTTCONNECT 1 // Client request to connect to Server
#define MQTTCONNACK 2 // Connect Acknowledgment
#define MQTTPUBLISH 3 // Publish message
#define MQTTPUBACK 4 // Publish Acknowledgment
#define MQTTPUBREC 5 // Publish Received (assured delivery part 1)
#define MQTTPUBREL 6 // Publish Release (assured delivery part 2)
#define MQTTPUBCOMP 7 // Publish Complete (assured delivery part 3)
#define MQTTSUBSCRIBE 8 // Client Subscribe request
#define MQTTSUBACK 9 // Subscribe Acknowledgment
#define MQTTUNSUBSCRIBE 10 // Client Unsubscribe request
#define MQTTUNSUBACK 11 // Unsubscribe Acknowledgment
#define MQTTPINGREQ 12 // PING Request
#define MQTTPINGRESP 13 // PING Response
#define MQTTDISCONNECT 14 // Client is Disconnecting
#define MQTTReserved 15 // Reserved
#define MQTTQOS0 (0 << 1)
#define MQTTQOS1 (1 << 1)
#define MQTTQOS2 (2 << 1)
class MyMQTT :
public MySensor {
public:
#ifdef __Raspberry_Pi
MyMQTT(uint8_t _cepin, uint8_t _cspin, uint32_t spispeed, uint8_t _inclusion_time );
void begin(rf24_pa_dbm_e paLevel=RF24_PA_LEVEL_GW, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE, void (*dataCallback)(char *)=NULL);
#else
MyMQTT(uint8_t _cepin=5, uint8_t _cspin=6);
void begin(rf24_pa_dbm_e paLevel=RF24_PA_LEVEL_GW, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE, void (*dataCallback)
(const char *, uint8_t *)=NULL, uint8_t _rx=6, uint8_t _tx=5, uint8_t _er=4 );
#endif
void processRadioMessage();
void processMQTTMessage(char *inputString, uint8_t inputPos);
private:
#ifdef __Raspberry_Pi
void (*dataCallback)(char *);
#else
void (*dataCallback)(const char *, uint8_t *);
#endif
void SendMQTT(MyMessage &msg);
void ledTimers();
void rxBlink(uint8_t cnt);
void txBlink(uint8_t cnt);
void errBlink(uint8_t cnt);
bool MQTTClientConnected;
char buffer[MQTT_MAX_PACKET_SIZE];
char convBuf[MAX_PAYLOAD*2+1];
uint8_t buffsize;
char *getType(char *b, const char **index);
};
extern void ledTimersInterrupt();
#endif