Skip to content

Commit c78b281

Browse files
committed
possible to set device ID with API key
1 parent 9a55cba commit c78b281

File tree

16 files changed

+101
-37
lines changed

16 files changed

+101
-37
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ For Bluetooth Low Energy communications, the library has been tested with the Ad
4747

4848
To use the library with Arduino boards you will need the latest version of the Arduino IDE:
4949

50-
- [Arduino IDE 1.6.8](http://arduino.cc/en/main/software)
50+
- [Arduino IDE 1.8.5](http://arduino.cc/en/main/software)
5151

5252
### For WiFi using the ESP8266 chip
5353

@@ -189,6 +189,14 @@ You can also define your own functions in your sketch that can be called using t
189189
* `rest.function("led",ledControl);` declares the function in the Arduino sketch
190190
* `/led?params=0` executes the function
191191

192+
### Log data to the cloud
193+
194+
You can also directly tell your board to log data on our cloud server, to be stored there & retrieved later or displayed on the [aREST cloud dashboard](https://dashboard.arest.io/). This is useful when you want for example to record the data coming from a sensor at regular intervals. The data is then stored along with the current date, the ID of the device sending the data, and also an event name that is used to identifiy the data. This can be done via the following commands:
195+
* `rest.publish(client, "temperature", data);` logs the value of `data` with the event name `temperature`
196+
* `https://cloud.arest.io/47fd9g/events` retrieves the last events logged by the device `47fd9g`
197+
* You can also use the [aREST cloud dashboard](https://dashboard.arest.io/) to then display or plot this data in real-time on your dashboards
198+
* Note that for devices not protected by an API key, the server will only store the last 10 measurements
199+
192200
### Get data about the board
193201

194202
You can also access a description of all the variables that were declared on the board with a single command. This is useful to automatically build graphical interfaces based on the variables exposed to the API. This can be done via the following calls:

aREST.h

+60-29
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License:
88
http://creativecommons.org/licenses/by-sa/4.0/
99
10-
Version 2.7.2
10+
Version 2.7.3
1111
Changelog:
1212
13+
Version 2.7.3: Added support to set your own ID when using API key
1314
Version 2.7.2: Bug fixes for aREST.io
1415
Version 2.7.1: Additional fixes & optimisations by @eykamp
1516
Version 2.7.0: Several fixes & optimisations by @eykamp
@@ -304,23 +305,51 @@ void publish(PubSubClient& client, const String& eventName, T data) {
304305

305306
}
306307

307-
void setKey(char* proKey, PubSubClient& client) {
308+
void setKey(char* api_key) {
308309

309-
// Assign MQTT server
310-
// mqtt_server = "104.131.78.157";
311-
// client.setServer(mqtt_server, 1883);
310+
// Set
311+
proKey = String(api_key);
312312

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+
}
315343

316344
// 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");
319347

320348
strcpy(in_topic, inTopic.c_str());
321349
strcpy(out_topic, outTopic.c_str());
322350

323351
// Build client ID
352+
client_id = id + String(api_key);
324353
client_id = id + String(proKey);
325354

326355
}
@@ -1515,31 +1544,34 @@ void set_id(const String& device_id) {
15151544

15161545
#if defined(PubSubClient_h)
15171546

1518-
// Generate MQTT random ID
1519-
String randomId = gen_random(6);
1547+
if (proKey.length() == 0) {
15201548

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);
15241551

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");
15271555

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());
15301558

1531-
// Build client ID
1532-
client_id = randomId + id;
1559+
// Build client ID
1560+
client_id = randomId + id;
15331561

1534-
if (DEBUG_MODE) {
1535-
Serial.print("Input MQTT topic: ");
1536-
Serial.println(in_topic);
1562+
}
1563+
else {
15371564

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");
15401568

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+
15431575
}
15441576

15451577
#endif
@@ -1894,6 +1926,7 @@ void setMQTTServer(char* new_mqtt_server){
18941926

18951927
char name[NAME_SIZE];
18961928
String id;
1929+
String proKey;
18971930
String arguments;
18981931

18991932
// Output uffer
@@ -1971,6 +2004,4 @@ void aREST::addToBuffer(char toAdd[], bool quotable) {
19712004
addStringToBuffer(toAdd, quotable); // Strings must be quoted
19722005
}
19732006

1974-
1975-
19762007
#endif

examples/BLE/BLE.ino

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RD
2525
int temperature;
2626
int humidity;
2727

28+
// Declare functions to be exposed to the API
29+
int ledControl(String command);
30+
2831
void setup(void)
2932
{
3033
// Start Serial
@@ -87,4 +90,4 @@ int ledControl(String command) {
8790

8891
digitalWrite(7,state);
8992
return 1;
90-
}
93+
}

examples/ESP8266_cloud_pro/ESP8266_cloud_pro.ino examples/ESP8266_cloud_api_key/ESP8266_cloud_api_key.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ PubSubClient client(espClient);
1818
// Create aREST instance
1919
aREST rest = aREST(client);
2020

21-
// aREST Pro key (that you can get at dashboard.arest.io)
21+
// aREST API key (that you can get at dashboard.arest.io)
2222
char * key = "your_arest_key";
2323

2424
// WiFi parameters

examples/Ethernet/Ethernet.ino

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ aREST rest = aREST();
2828
int temperature;
2929
int humidity;
3030

31+
// Declare functions to be exposed to the API
32+
int ledControl(String command);
33+
3134
void setup(void)
3235
{
3336
// Start Serial
@@ -78,4 +81,6 @@ int ledControl(String command) {
7881

7982
digitalWrite(6,state);
8083
return 1;
84+
8185
}
86+

examples/MKR1000/MKR1000.ino

+1
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,5 @@ int ledControl(String command) {
9494

9595
digitalWrite(6,state);
9696
return 1;
97+
9798
}

examples/MKR1000_cloud_and_local/MKR1000_cloud_and_local.ino

-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ int ledControl(String command) {
116116
return 1;
117117
}
118118

119-
120119
// Handles message arrived on subscribed topic(s)
121120
void callback(char* topic, byte* payload, unsigned int length) {
122121

examples/MKR1000_cloud_pro/MKR1000_cloud_pro.ino examples/MKR1000_cloud_api_key/MKR1000_cloud_api_key.ino

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PubSubClient client(wifiClient);
2121
// Create aREST instance
2222
aREST rest = aREST(client);
2323

24-
// aREST Pro key (that you can get at dashboard.arest.io)
24+
// aREST API key (that you can get at dashboard.arest.io)
2525
char * key = "your_arest_key";
2626

2727
// WiFi parameters
@@ -91,7 +91,6 @@ int ledControl(String command) {
9191
return 1;
9292
}
9393

94-
9594
// Handles message arrived on subscribed topic(s)
9695
void callback(char* topic, byte* payload, unsigned int length) {
9796

examples/Serial/Serial.ino

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ aREST rest = aREST();
1717
int temperature;
1818
int humidity;
1919

20+
// Declare functions to be exposed to the API
21+
int ledControl(String command);
22+
2023
void setup(void)
2124
{
2225
// Start Serial

examples/WiFi/WiFi.ino

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ WiFiServer server(80);
2626
int temperature;
2727
int humidity;
2828

29+
// Declare functions to be exposed to the API
30+
int ledControl(String command);
31+
2932
void setup() {
3033

3134
// Start Serial

examples/WiFi_CC3000/WiFi_CC3000.ino

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ MDNSResponder mdns;
4141
int temperature;
4242
int humidity;
4343

44+
// Declare functions to be exposed to the API
45+
int ledControl(String command);
46+
4447
void setup(void)
4548
{
4649
// Start Serial

examples/WiFi_CC3000_Due/WiFi_CC3000_Due.ino

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ MDNSResponder mdns;
4040
int temperature;
4141
int humidity;
4242

43+
// Declare functions to be exposed to the API
44+
int ledControl(String command);
45+
4346
void setup(void)
4447
{
4548
// Start Serial

examples/Yun/Yun.ino

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ YunServer server(80);
2121
int temperature;
2222
int humidity;
2323

24+
// Declare functions to be exposed to the API
25+
int ledControl(String command);
26+
2427
void setup(void)
2528
{
2629
// Start Serial
@@ -62,4 +65,6 @@ int ledControl(String command) {
6265

6366
digitalWrite(7,state);
6467
return 1;
68+
6569
}
70+

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=aREST
2-
version=2.7.2
2+
version=2.7.3
33
author=Marco Schwartz
44
maintainer=Marco Schwartz <[email protected]>
55
sentence=RESTful API for the Arduino platform.

license.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014-2016 Marco Schwartz
3+
Copyright (c) 2014-2018 Marco Schwartz
44
Authors: Marco Schwartz
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy

0 commit comments

Comments
 (0)