|
1 | 1 | /*
|
2 |
| -Cayenne SendData Example |
3 |
| -
|
4 |
| -This example sketch shows how a value can be sent from Arduino to the Cayenne Dashboard on a Virtual Channel. |
| 2 | +This example sketch shows how a value can be sent from the Arduino to the Cayenne Dashboard at automatic intervals. |
5 | 3 |
|
6 | 4 | The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
|
7 | 5 |
|
8 | 6 | Steps:
|
9 | 7 | 1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
|
10 | 8 | 2. Compile and upload the sketch.
|
11 | 9 | 3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
|
12 |
| -
|
13 |
| -NOTE: |
14 |
| - For this example you'll need SimpleTimer library: |
15 |
| - https://github.com/jfturcot/SimpleTimer |
16 |
| - Visit this page for more information: |
17 |
| - http://playground.arduino.cc/Code/SimpleTimer |
18 | 10 | */
|
19 | 11 |
|
20 |
| -#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space |
21 |
| -#include <CayenneMQTTEthernet.h> // Change this to use a different communication device. See Communications examples. |
22 |
| -#include <SimpleTimer.h> |
| 12 | +#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space |
| 13 | +#include <CayenneMQTTEthernet.h> // Change this to use a different communication device. See Communications examples. |
23 | 14 |
|
24 | 15 | // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
|
25 | 16 | char username[] = "MQTT_USERNAME";
|
26 | 17 | char password[] = "MQTT_PASSWORD";
|
27 | 18 | char clientID[] = "CLIENT_ID";
|
28 | 19 |
|
29 |
| -SimpleTimer timer; |
30 |
| - |
31 |
| -// This function sends Arduino's up time to Virtual Channel 5. |
32 |
| -void sendUptime() |
33 |
| -{ |
34 |
| - // Send values using the virtualWrite function. Cayenne currently accepts int and float values. |
35 |
| - // Please don't send more that 10 values per second. |
36 |
| - Cayenne.virtualWrite(5, millis() / 1000); |
37 |
| -} |
| 20 | +// Use Virtual Channel 5 for uptime display. |
| 21 | +#define VIRTUAL_CHANNEL 5 |
38 | 22 |
|
39 | 23 | void setup()
|
40 | 24 | {
|
41 |
| - Serial.begin(9600); // See the connection status in Serial Monitor |
| 25 | + Serial.begin(9600); |
42 | 26 | Cayenne.begin(username, password, clientID);
|
43 |
| - |
44 |
| - // Set up a function to be called every 5 seconds |
45 |
| - timer.setInterval(5000L, sendUptime); |
46 | 27 | }
|
47 | 28 |
|
48 | 29 | void loop()
|
49 | 30 | {
|
50 |
| - Cayenne.loop(); // Runs main loop |
51 |
| - timer.run(); // Initiates SimpleTimer |
| 31 | + Cayenne.loop(); |
52 | 32 | }
|
53 | 33 |
|
| 34 | +// This function is called at intervals to send data to Cayenne. |
| 35 | +CAYENNE_OUT(VIRTUAL_CHANNEL) |
| 36 | +{ |
| 37 | + CAYENNE_LOG("Send data for Virtual Channel %d", VIRTUAL_CHANNEL); |
| 38 | + // This command writes the device's uptime in seconds to the Virtual Channel. |
| 39 | + Cayenne.virtualWrite(VIRTUAL_CHANNEL, millis() / 1000); |
| 40 | +} |
0 commit comments