Skip to content

Commit 9b163d2

Browse files
authored
Merge pull request #24 from shramik49/master
Create SendDataOnTrigger.ino
2 parents d219e75 + e528c88 commit 9b163d2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
This example is to be used when using Cayenne triggers.
3+
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.
4+
Steps:
5+
1. Set the trigger and threshold values in the sketch below.
6+
2. Set the Cayenne authentication info to match the authentication info from the Dashboard.
7+
3. Compile and upload the sketch.
8+
4. Two temporary widget (DATA_CHANNEL and TRIGGER_CHANNEL) will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
9+
5. Add trigger on your cayenne dashboard for the TRIGGER_CHANNEL widget when it becomes 1.
10+
*/
11+
12+
//#define CAYENNE_DEBUG
13+
#define CAYENNE_PRINT Serial
14+
#include <CayenneMQTTEthernet.h> // Change this to use a different communication device. See Communications examples.
15+
16+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
17+
char username[] = "MQTT_USERNAME";
18+
char password[] = "MQTT_PASSWORD";
19+
char clientID[] = "CLIENT_ID";
20+
21+
#define DATA_CHANNEL 0 //Virtual channel for publishing sensor data.
22+
#define TRIGGER_CHANNEL 1 //Virtual channel for publishing the trigger value.
23+
#define THRESHOLD 200 //Threshold for the trigger.
24+
bool sendBelowThreshold = true; //Set to true if the trigger should happen when the data value is below the threshold,
25+
//false if it should happen when the data value is above or equal to the threshold.
26+
bool crossedThreshold = false;
27+
28+
void setup() {
29+
Serial.begin(9600);
30+
Cayenne.begin(username, password, clientID);
31+
}
32+
33+
void sendTriggerValue(int channel, int value, int threshold, bool sendBelowThreshold) {
34+
if (((value >= threshold) && !sendBelowThreshold) || ((value < threshold) && sendBelowThreshold)) {
35+
if (!crossedThreshold) {
36+
Cayenne.virtualWrite(channel, 1, "digital_sensor", "d"); //set trigger two-state widget to 1
37+
crossedThreshold = true;
38+
}
39+
}
40+
else
41+
{
42+
Cayenne.virtualWrite(channel, 0, "digital_sensor", "d"); //set trigger two-state widget to 0
43+
crossedThreshold = false;
44+
}
45+
}
46+
47+
void loop() {
48+
Cayenne.loop();
49+
}
50+
51+
CAYENNE_OUT_DEFAULT()
52+
{
53+
int sensor_value = analogRead(A0);
54+
sendTriggerValue(TRIGGER_CHANNEL, sensor_value, THRESHOLD, sendBelowThreshold);
55+
Cayenne.virtualWrite(DATA_CHANNEL, sensor_value , "analog_sensor", "null"); //widget to display sensor data.
56+
}

0 commit comments

Comments
 (0)