-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathKinetic2MQTT.ino
298 lines (247 loc) · 9.98 KB
/
Kinetic2MQTT.ino
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <Arduino.h>
#include <RadioLib.h>
#include <IotWebConf.h>
#include <IotWebConfUsing.h>
#include <PubSubClient.h>
#include <AceCRC.h>
// Select the type of CRC algorithm we'll be using
using namespace ace_crc::crc16ccitt_byte;
// Radio Config
#define PACKET_LENGTH 5 // bytes
#define MIN_RSSI -100 // dBm
#define CARRIER_FREQUENCY 433.3 // MHz
#define BIT_RATE 100.0 // kbps
#define FREQUENCY_DEVIATION 50.0 // kHz
#define RX_BANDWIDTH 135.0 // kHz
#define OUTPUT_POWER 10 // dBm
#define PREAMBLE_LENGTH 16 // bits
// IotWebConf Config
#define CONFIG_PARAM_MAX_LEN 128
#define CONFIG_VERSION "mqt2"
#define CONFIG_PIN 4 // D2
#define STATUS_PIN 16 // D0
// Kinetic2MQTT Config
#define DEBOUNCE_MILLIS 15
// CC1101 has the following connections:
// CS pin: 15
// GDO0 pin: 5
// RST pin: unused
// GDO2 pin: unused (optional)
CC1101 radio = new Module(15, 5, RADIOLIB_NC, RADIOLIB_NC);
// Set up IotWebConf
const char deviceName[] = "kinetic2mqtt";
const char apPassword[] = "EMWhP56Q"; // Default password for SSID and configuration page, can be changed after first boot
void configSaved();
bool formValidator(iotwebconf::WebRequestWrapper* webRequestWrapper);
DNSServer dnsServer;
WebServer server(80);
WiFiClient net;
PubSubClient mqttClient(net);
char mqttServerValue[CONFIG_PARAM_MAX_LEN];
// char mqttUserNameValue[CONFIG_PARAM_MAX_LEN];
// char mqttUserPasswordValue[CONFIG_PARAM_MAX_LEN];
char mqttTopicValue[CONFIG_PARAM_MAX_LEN];
IotWebConf iotWebConf(deviceName, &dnsServer, &server, apPassword, CONFIG_VERSION);
IotWebConfParameterGroup mqttGroup = IotWebConfParameterGroup("mqtt", "MQTT configuration");
IotWebConfTextParameter mqttServerParam = IotWebConfTextParameter("MQTT server", "mqttServer", mqttServerValue, CONFIG_PARAM_MAX_LEN);
// IotWebConfTextParameter mqttUserNameParam = IotWebConfTextParameter("MQTT user", "mqttUser", mqttUserNameValue, CONFIG_PARAM_MAX_LEN);
// IotWebConfPasswordParameter mqttUserPasswordParam = IotWebConfPasswordParameter("MQTT password", "mqttPass", mqttUserPasswordValue, CONFIG_PARAM_MAX_LEN);
IotWebConfTextParameter mqttTopicParam = IotWebConfTextParameter("MQTT Topic", "mqttTopic", mqttTopicValue, CONFIG_PARAM_MAX_LEN);
bool needReset = false;
// Global State Variables
char lastSentSwitchID[5] = "";
char lastSentButtonAction[8] = "";
unsigned long lastSentMillis = 0;
void setup() {
Serial.begin(115200);
pinMode(CONFIG_PIN, INPUT_PULLUP);
// Initialise and configure CC1101
Serial.print("Initializing Radio... ");
int state = radio.begin(CARRIER_FREQUENCY, BIT_RATE, FREQUENCY_DEVIATION, RX_BANDWIDTH, OUTPUT_POWER, PREAMBLE_LENGTH);
radio.setCrcFiltering(false);
radio.fixedPacketLengthMode(PACKET_LENGTH);
uint8_t syncWord[] = {0xA4, 0x23};
radio.setSyncWord(syncWord, 2);
radio.setGdo0Action(setFlag);
state = radio.startReceive();
if (state == RADIOLIB_ERR_NONE) {
Serial.println("done!");
} else {
Serial.print("failed, code ");
Serial.println(state);
while (true);
}
// Initialise IOTWebConf
Serial.println("Initialising IotWebConf... ");
mqttGroup.addItem(&mqttServerParam);
// mqttGroup.addItem(&mqttUserNameParam);
// mqttGroup.addItem(&mqttUserPasswordParam);
mqttGroup.addItem(&mqttTopicParam);
iotWebConf.setStatusPin(STATUS_PIN);
iotWebConf.setConfigPin(CONFIG_PIN);
iotWebConf.addParameterGroup(&mqttGroup);
iotWebConf.setConfigSavedCallback(&configSaved);
iotWebConf.setFormValidator(&formValidator);
bool validConfig = iotWebConf.init();
if (!validConfig) {
mqttServerValue[0] = '\0';
// mqttUserNameValue[0] = '\0';
// mqttUserPasswordValue[0] = '\0';
mqttTopicValue[0] = '\0';
}
// This will disable the device sitting in AP mode for 30s on startup
// Not requred due to presence of reset button to manually enable AP mode
iotWebConf.setApTimeoutMs(0);
server.on("/", []{ iotWebConf.handleConfig(); });
server.onNotFound([](){ iotWebConf.handleNotFound(); });
// Initialise MQTT
mqttClient.setServer(mqttServerValue, 1883);
}
// flag to indicate that a packet was received
volatile bool receivedFlag = false;
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
#endif
void setFlag(void) {
// we got a packet, set the flag
receivedFlag = true;
}
void loop() {
iotWebConf.doLoop();
if (needReset) {
Serial.println("Rebooting after 1 second.");
iotWebConf.delay(1000);
ESP.restart();
}
// If WiFi is connected but MQTT is not, establish MQTT connection
if ((iotWebConf.getState() == iotwebconf::OnLine) && !mqttClient.connected()) {
connectMqtt();
}
mqttClient.loop();
// If a message has been received and flag has been set by interrupt, process the message
if(receivedFlag) {
byte byteArr[PACKET_LENGTH];
int state = radio.readData(byteArr, PACKET_LENGTH);
if (state == RADIOLIB_ERR_NONE) {
// Only process the message if RSSI is high enough, this prevents uncessary calculating CRC for noise
if (radio.getRSSI() > MIN_RSSI) {
// Last 2 bytes are the CRC-16/AUG-CCITT of the first 3 bytes
// Verify CRC and only continue if valid
byte messageArr[] = {byteArr[0], byteArr[1], byteArr[2]};
unsigned long messageCRC = (byteArr[3] << 8) | byteArr[4];
crc_t crc = crc_init();
crc = crc_update(crc, messageArr, 3);
crc = crc_finalize(crc);
// Does calculted CRC match the CRC in the message?
if (((unsigned long) crc) == messageCRC) {
// First two bytes correspond to the switch ID
// Convert to hex string
char switchID[5] = "";
bytesToHexString(byteArr, 2, switchID);
// First bit of third byte indicates press/release
// Bit shift and compare the result
char buttonAction[] = "release";
if ((byteArr[2] >> 7) == 0) {
strcpy(buttonAction, "press");
}
// Do not send a message if this received message is the same as the previous one and the previous one was sent less than DEBOUNCE_MILLIS miliseconds ago
// This is because Kinetic switches continuously send messages every milisecond or so until the power runs out, this logic deduplicates these
if (!((strcmp(switchID, lastSentSwitchID) == 0) && (strcmp(buttonAction, lastSentButtonAction) == 0) && ((millis() - lastSentMillis) < DEBOUNCE_MILLIS))) {
Serial.print("Button pressed: ");
Serial.print(switchID);
Serial.print(", action: ");
Serial.println(buttonAction);
// Send the RSSI and button action message over MQTT
char topicLevel1[] = "status";
char topicLevel2[] = "rssi";
char messageValue[20] = "";
sprintf(messageValue, "%f dBm", radio.getRSSI());
publishMqtt(topicLevel1, topicLevel2, messageValue);
strcpy(topicLevel1, "action");
publishMqtt(topicLevel1, switchID, buttonAction);
strcpy(lastSentButtonAction, buttonAction);
strcpy(lastSentSwitchID, switchID);
lastSentMillis = millis();
}
} else {
// Message CRC was invalid
Serial.println("Error: CRC Mismatch!");
}
}
} else {
// An error occurred receiving the message
Serial.print("RadioLib error: ");
Serial.println(state);
}
// Reset flag and put module back into listening mode
// This should be the last thing in the loop
receivedFlag = false;
radio.startReceive();
}
}
// Publish [value] to MQTT at topic: [mqttTopicValue]/[topicLevel1]/[topicLevel2]
void publishMqtt(char topicLevel1[], char topicLevel2[], char value[]) {
char compiledTopic[strlen(mqttTopicValue) + strlen(topicLevel1) + strlen(topicLevel2) + 3] = "";
strcat(compiledTopic, mqttTopicValue);
strcat(compiledTopic, "/");
strcat(compiledTopic, topicLevel1);
strcat(compiledTopic, "/");
strcat(compiledTopic, topicLevel2);
mqttClient.publish(compiledTopic, value);
}
// Establish an MQTT connection, if connection fails this function will delay for 5 seconds and then return
// Connection will be re-attempted at next execution of loop()
void connectMqtt() {
// Loop until we're reconnected
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = iotWebConf.getThingName();
clientId += "-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (mqttClient.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
char topicLevel1[] = "status";
char topicLevel2[] = "connection";
char messageValue[] = "connected";
publishMqtt(topicLevel1, topicLevel2, messageValue);
} else {
Serial.print("MQTT Connection Failed:");
Serial.print(mqttClient.state());
Serial.println(". Trying again in 5 seconds");
// Wait 5 seconds before retrying
iotWebConf.delay(5000);
}
}
// Convert array of bytes into a string containing the HEX representation of the array
void bytesToHexString(byte array[], unsigned int len, char buffer[]) {
for (unsigned int i = 0; i < len; i++) {
byte nib1 = (array[i] >> 4) & 0x0F;
byte nib2 = (array[i] >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
}
buffer[len*2] = '\0';
}
// If configuration is saved in IOTWebConf, reboot the device
void configSaved() {
Serial.println("Configuration was updated.");
needReset = true;
}
// Validate the data entered into the IOTWebConf configuration page
bool formValidator(iotwebconf::WebRequestWrapper* webRequestWrapper) {
Serial.println("Validating form.");
bool valid = true;
int l = webRequestWrapper->arg(mqttServerParam.getId()).length();
if (l < 3) {
mqttServerParam.errorMessage = "Please provide at least 3 characters!";
valid = false;
}
l = webRequestWrapper->arg(mqttTopicParam.getId()).length();
if (l < 3) {
mqttTopicParam.errorMessage = "Please provide at least 3 characters!";
valid = false;
}
return valid;
}