|
7 | 7 | This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License:
|
8 | 8 | http://creativecommons.org/licenses/by-sa/4.0/
|
9 | 9 |
|
10 |
| - Version 2.7.2 |
| 10 | + Version 2.7.3 |
11 | 11 | Changelog:
|
12 | 12 |
|
| 13 | + Version 2.7.3: Added support to set your own ID when using API key |
13 | 14 | Version 2.7.2: Bug fixes for aREST.io
|
14 | 15 | Version 2.7.1: Additional fixes & optimisations by @eykamp
|
15 | 16 | Version 2.7.0: Several fixes & optimisations by @eykamp
|
@@ -304,23 +305,51 @@ void publish(PubSubClient& client, const String& eventName, T data) {
|
304 | 305 |
|
305 | 306 | }
|
306 | 307 |
|
307 |
| -void setKey(char* proKey, PubSubClient& client) { |
| 308 | +void setKey(char* api_key) { |
308 | 309 |
|
309 |
| - // Assign MQTT server |
310 |
| - // mqtt_server = "104.131.78.157"; |
311 |
| - // client.setServer(mqtt_server, 1883); |
| 310 | + // Set |
| 311 | + proKey = String(api_key); |
312 | 312 |
|
313 |
| - // Generate MQTT random ID |
314 |
| - id = gen_random(6); |
| 313 | + if (id.length() == 0) { |
| 314 | + |
| 315 | + // Generate MQTT random ID |
| 316 | + id = gen_random(6); |
| 317 | + |
| 318 | + } |
| 319 | + |
| 320 | + // Build topics IDs |
| 321 | + String inTopic = id + String(api_key) + String("_in"); |
| 322 | + String outTopic = id + String(api_key) + String("_out"); |
| 323 | + |
| 324 | + strcpy(in_topic, inTopic.c_str()); |
| 325 | + strcpy(out_topic, outTopic.c_str()); |
| 326 | + |
| 327 | + // Build client ID |
| 328 | + client_id = id + String(api_key); |
| 329 | + |
| 330 | +} |
| 331 | + |
| 332 | +void setKey(char* api_key, PubSubClient& client) { |
| 333 | + |
| 334 | + // Set |
| 335 | + proKey = String(api_key); |
| 336 | + |
| 337 | + if (id.length() == 0) { |
| 338 | + |
| 339 | + // Generate MQTT random ID |
| 340 | + id = gen_random(6); |
| 341 | + |
| 342 | + } |
315 | 343 |
|
316 | 344 | // Build topics IDs
|
317 |
| - String inTopic = id + String(proKey) + String("_in"); |
318 |
| - String outTopic = id + String(proKey) + String("_out"); |
| 345 | + String inTopic = id + String(api_key) + String("_in"); |
| 346 | + String outTopic = id + String(api_key) + String("_out"); |
319 | 347 |
|
320 | 348 | strcpy(in_topic, inTopic.c_str());
|
321 | 349 | strcpy(out_topic, outTopic.c_str());
|
322 | 350 |
|
323 | 351 | // Build client ID
|
| 352 | + client_id = id + String(api_key); |
324 | 353 | client_id = id + String(proKey);
|
325 | 354 |
|
326 | 355 | }
|
@@ -1515,31 +1544,34 @@ void set_id(const String& device_id) {
|
1515 | 1544 |
|
1516 | 1545 | #if defined(PubSubClient_h)
|
1517 | 1546 |
|
1518 |
| - // Generate MQTT random ID |
1519 |
| - String randomId = gen_random(6); |
| 1547 | + if (proKey.length() == 0) { |
1520 | 1548 |
|
1521 |
| - // Build topics IDs |
1522 |
| - String inTopic = randomId + id + String("_in"); |
1523 |
| - String outTopic = randomId + id + String("_out"); |
| 1549 | + // Generate MQTT random ID |
| 1550 | + String randomId = gen_random(6); |
1524 | 1551 |
|
1525 |
| - strcpy(in_topic, inTopic.c_str()); |
1526 |
| - strcpy(out_topic, outTopic.c_str()); |
| 1552 | + // Build topics IDs |
| 1553 | + String inTopic = randomId + id + String("_in"); |
| 1554 | + String outTopic = randomId + id + String("_out"); |
1527 | 1555 |
|
1528 |
| - // inTopic.toCharArray(in_topic, inTopic.length()); |
1529 |
| - // outTopic.toCharArray(out_topic, outTopic.length()); |
| 1556 | + strcpy(in_topic, inTopic.c_str()); |
| 1557 | + strcpy(out_topic, outTopic.c_str()); |
1530 | 1558 |
|
1531 |
| - // Build client ID |
1532 |
| - client_id = randomId + id; |
| 1559 | + // Build client ID |
| 1560 | + client_id = randomId + id; |
1533 | 1561 |
|
1534 |
| - if (DEBUG_MODE) { |
1535 |
| - Serial.print("Input MQTT topic: "); |
1536 |
| - Serial.println(in_topic); |
| 1562 | + } |
| 1563 | + else { |
1537 | 1564 |
|
1538 |
| - Serial.print("Output MQTT topic: "); |
1539 |
| - Serial.println(out_topic); |
| 1565 | + // Build topics IDs |
| 1566 | + String inTopic = id + String(proKey) + String("_in"); |
| 1567 | + String outTopic = id + String(proKey) + String("_out"); |
1540 | 1568 |
|
1541 |
| - Serial.print("Client ID: "); |
1542 |
| - Serial.println(client_id); |
| 1569 | + strcpy(in_topic, inTopic.c_str()); |
| 1570 | + strcpy(out_topic, outTopic.c_str()); |
| 1571 | + |
| 1572 | + // Build client ID |
| 1573 | + client_id = id + String(proKey); |
| 1574 | + |
1543 | 1575 | }
|
1544 | 1576 |
|
1545 | 1577 | #endif
|
@@ -1894,6 +1926,7 @@ void setMQTTServer(char* new_mqtt_server){
|
1894 | 1926 |
|
1895 | 1927 | char name[NAME_SIZE];
|
1896 | 1928 | String id;
|
| 1929 | + String proKey; |
1897 | 1930 | String arguments;
|
1898 | 1931 |
|
1899 | 1932 | // Output uffer
|
@@ -1971,6 +2004,4 @@ void aREST::addToBuffer(char toAdd[], bool quotable) {
|
1971 | 2004 | addStringToBuffer(toAdd, quotable); // Strings must be quoted
|
1972 | 2005 | }
|
1973 | 2006 |
|
1974 |
| - |
1975 |
| - |
1976 | 2007 | #endif
|
0 commit comments