Skip to content

Commit db41304

Browse files
committed
Add actuator example sketches. Update default channel numbers.
1 parent afd8c82 commit db41304

File tree

14 files changed

+428
-6
lines changed

14 files changed

+428
-6
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Cayenne Generic Digital Output Example
3+
4+
This sketch shows how to set up a Generic Digital Output with Cayenne.
5+
6+
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+
8+
Steps:
9+
1. In the Cayenne Dashboard add a new Generic Digital Output widget.
10+
2. Select a virtual channel number for the widget.
11+
3. Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
12+
4. Attach an output actuator (e.g. an LED) to a digital pin on your Arduino.
13+
5. Set the ACTUATOR_PIN value below to the pin number you used when connecting your output actuator.
14+
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
15+
7. Compile and upload this sketch.
16+
8. Once the Arduino connects to the Dashboard you can use the widget button to send digital values.
17+
*/
18+
19+
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
20+
#include <CayenneMQTTEthernet.h>
21+
22+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
23+
char username[] = "MQTT_USERNAME";
24+
char password[] = "MQTT_PASSWORD";
25+
char clientID[] = "CLIENT_ID";
26+
27+
#define VIRTUAL_CHANNEL 1
28+
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
29+
30+
void setup()
31+
{
32+
Serial.begin(9600);
33+
pinMode(ACTUATOR_PIN, OUTPUT);
34+
Cayenne.begin(username, password, clientID);
35+
}
36+
37+
void loop()
38+
{
39+
Cayenne.loop();
40+
}
41+
42+
// This function is called when data is sent from Cayenne.
43+
CAYENNE_IN(VIRTUAL_CHANNEL)
44+
{
45+
int value = getValue.asInt();
46+
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
47+
// Write the value received to the digital pin.
48+
digitalWrite(ACTUATOR_PIN, value);
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Cayenne Generic PWM Output Example
3+
4+
This sketch shows how to set up a Generic PWM Output with Cayenne.
5+
6+
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+
8+
Steps:
9+
1. In the Cayenne Dashboard add a new Generic PWM Output widget.
10+
2. Select a virtual channel number.
11+
3. Select a slider widget.
12+
4. Set the slider widget min value to 0 and max value to 255.
13+
5. Set VIRTUAL_CHANNEL to the virtual channel number you selected.
14+
6. Attach an output device to a digital PWM pin (3, 5, 6, 9, 10, and 11 on most Arduino boards).
15+
7. Set ACTUATOR_PIN to the digital PWM pin number you selected.
16+
8. Set the Cayenne authentication info to match the authentication info from the Dashboard.
17+
9. Compile and upload this sketch.
18+
10. Once the Arduino connects to the Dashboard you can use the slider to change PWM values.
19+
*/
20+
21+
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
22+
#include <CayenneMQTTEthernet.h>
23+
24+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
25+
char username[] = "MQTT_USERNAME";
26+
char password[] = "MQTT_PASSWORD";
27+
char clientID[] = "CLIENT_ID";
28+
29+
#define VIRTUAL_CHANNEL 1
30+
#define ACTUATOR_PIN 3
31+
32+
void setup()
33+
{
34+
Serial.begin(9600);
35+
Cayenne.begin(username, password, clientID);
36+
}
37+
38+
void loop()
39+
{
40+
Cayenne.loop();
41+
}
42+
43+
// This function is called when data is sent from Cayenne.
44+
CAYENNE_IN(VIRTUAL_CHANNEL)
45+
{
46+
int value = getValue.asInt(); // 0 to 255
47+
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
48+
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
49+
analogWrite(ACTUATOR_PIN, value);
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Cayenne Light Switch Example
3+
4+
This sketch shows how to set up a Light Switch with Cayenne.
5+
6+
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+
8+
Steps:
9+
1. In the Cayenne Dashboard add a new Light Switch Widget.
10+
2. Select a virtual channel number for the widget.
11+
3. Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
12+
4. Attach the negative leg of an LED to ground and the other leg to the selected digital pin.
13+
Schematic:
14+
[Ground] -- [LED] -- [Resistor] -- [Digital Pin]
15+
5. Set the ACTUATOR_PIN value below to the pin number you used when connecting your LED.
16+
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
17+
7. Compile and upload this sketch.
18+
8. Once the Arduino connects to the Dashboard you can use the widget button to turn the LED on and off.
19+
*/
20+
21+
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
22+
#include <CayenneMQTTEthernet.h>
23+
24+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
25+
char username[] = "MQTT_USERNAME";
26+
char password[] = "MQTT_PASSWORD";
27+
char clientID[] = "CLIENT_ID";
28+
29+
#define VIRTUAL_CHANNEL 1
30+
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
31+
32+
void setup()
33+
{
34+
Serial.begin(9600);
35+
pinMode(ACTUATOR_PIN, OUTPUT);
36+
Cayenne.begin(username, password, clientID);
37+
}
38+
39+
void loop()
40+
{
41+
Cayenne.loop();
42+
}
43+
44+
// This function is called when data is sent from Cayenne.
45+
CAYENNE_IN(VIRTUAL_CHANNEL)
46+
{
47+
int value = getValue.asInt();
48+
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
49+
// Write the value received to the digital pin.
50+
digitalWrite(ACTUATOR_PIN, value);
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Cayenne Luminosity Example
3+
4+
This sketch sample file shows how to change the brightness on an LED using Cayenne Dashboard.
5+
6+
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+
8+
Steps:
9+
1. In the Cayenne Dashboard add a new Luminosity widget.
10+
2. Select a virtual channel number.
11+
3. Select a slider widget.
12+
4. Set the slider widget min value to 0 and max value to 255.
13+
5. Set VIRTUAL_CHANNEL to the virtual channel number you selected.
14+
6. Connect the LED's legs to GND and to a PWM pin (3, 5, 6, 9, 10, and 11 on most Arduino boards).
15+
Use a 1k resistor if possible.
16+
Schematic:
17+
[Ground] -- [LED] -- [1k-resistor] -- [PWM Pin]
18+
7. Set ACTUATOR_PIN to the digital PWM pin number you selected.
19+
8. Set the Cayenne authentication info to match the authentication info from the Dashboard.
20+
9. Compile and upload this sketch.
21+
10. Once the Arduino connects to the Dashboard you can use the slider to change LED brightness.
22+
*/
23+
24+
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
25+
#include <CayenneMQTTEthernet.h>
26+
27+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
28+
char username[] = "MQTT_USERNAME";
29+
char password[] = "MQTT_PASSWORD";
30+
char clientID[] = "CLIENT_ID";
31+
32+
#define VIRTUAL_CHANNEL 1
33+
#define ACTUATOR_PIN 3
34+
35+
void setup()
36+
{
37+
Serial.begin(9600);
38+
Cayenne.begin(username, password, clientID);
39+
}
40+
41+
void loop()
42+
{
43+
Cayenne.loop();
44+
}
45+
46+
// This function is called when data is sent from Cayenne.
47+
CAYENNE_IN(VIRTUAL_CHANNEL)
48+
{
49+
int value = getValue.asInt(); // 0 to 255
50+
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
51+
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
52+
analogWrite(ACTUATOR_PIN, value);
53+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Cayenne Motor Switch Actuator Example
3+
4+
This sketch shows how to set up a Motor Switch with Cayenne. The Arduino cannot
5+
drive a motor because it does not output the needed current. As a result, in order
6+
to make this example work, various electronic components are necessary to drive
7+
the DC motor. To keep it simple, you will need an external power source, transistor (eg. PN2222),
8+
diode (eg. 1N4001), and a 270 ohm resistor.
9+
10+
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.
11+
12+
Steps:
13+
1. In the Cayenne Dashboard add a new Motor Switch widget.
14+
2. Select a virtual channel number for the widget.
15+
3. Set the VIRTUAL_CHANNEL value below to the virtual channel you selected.
16+
4. Set up your motor schematic and attach it to a PWM pin (3, 5, 6, 9, 10, and 11 on most Arduino boards).
17+
5. Set ACTUATOR_PIN to the PWM pin number you selected.
18+
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
19+
7. Compile and upload this sketch.
20+
8. Once the Arduino connects to the Dashboard you can use the widget button to turn the motor on and off.
21+
*/
22+
23+
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
24+
#include <CayenneMQTTEthernet.h>
25+
26+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
27+
char username[] = "MQTT_USERNAME";
28+
char password[] = "MQTT_PASSWORD";
29+
char clientID[] = "CLIENT_ID";
30+
31+
#define VIRTUAL_CHANNEL 1
32+
#define ACTUATOR_PIN 3
33+
34+
void setup()
35+
{
36+
Serial.begin(9600);
37+
Cayenne.begin(username, password, clientID);
38+
}
39+
40+
void loop()
41+
{
42+
Cayenne.loop();
43+
}
44+
45+
// Enable or disable the motor based on value received on virtual channel.
46+
CAYENNE_IN(VIRTUAL_CHANNEL)
47+
{
48+
int speed = 155;
49+
// Read the integer value which should be 0 or 1.
50+
int enabled = getValue.asInt();
51+
if (enabled == 1) {
52+
// Turn on the motor at the specified speed.
53+
analogWrite(ACTUATOR_PIN, speed);
54+
}
55+
else {
56+
// Turn off the motor.
57+
analogWrite(ACTUATOR_PIN, 0);
58+
}
59+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Cayenne Relay Switch Example
3+
4+
This sketch sample file shows how to set up a Relay Switch with Cayenne.
5+
6+
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+
8+
Steps:
9+
1. In the Cayenne Dashboard add a new Relay Switch widget.
10+
2. Select a virtual channel number for the widget.
11+
3. Set the VIRTUAL_CHANNEL value below to the virtual channel you selected.
12+
4. Connect your relay switch to a digital pin.
13+
5. Set ACTUATOR_PIN to the digital pin number you selected.
14+
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
15+
7. Compile and upload this sketch.
16+
8. Once the Arduino connects to the Dashboard you can use the widget button to turn the relay switch on and off.
17+
*/
18+
19+
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
20+
#include <CayenneMQTTEthernet.h>
21+
22+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
23+
char username[] = "MQTT_USERNAME";
24+
char password[] = "MQTT_PASSWORD";
25+
char clientID[] = "CLIENT_ID";
26+
27+
#define VIRTUAL_CHANNEL 1
28+
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
29+
30+
void setup()
31+
{
32+
Serial.begin(9600);
33+
pinMode(ACTUATOR_PIN, OUTPUT);
34+
Cayenne.begin(username, password, clientID);
35+
}
36+
37+
void loop()
38+
{
39+
Cayenne.loop();
40+
}
41+
42+
// This function is called when data is sent from Cayenne.
43+
CAYENNE_IN(VIRTUAL_CHANNEL)
44+
{
45+
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
46+
if (getValue.asInt() == 0) {
47+
digitalWrite(ACTUATOR_PIN, HIGH);
48+
}
49+
else {
50+
digitalWrite(ACTUATOR_PIN, LOW);
51+
}
52+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Cayenne Servo Motor Example
3+
4+
This sketch sample file shows how to connect a Servo Motor with CayenneMQTT Library.
5+
6+
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+
8+
Steps:
9+
1. Install the Servo library from the Arduino Library Manager.
10+
2. In the Cayenne Dashboard add a new Servo Motor widget.
11+
3. Select a virtual channel number for the widget.
12+
4. Set min value to 0 and max value to 1.
13+
5. Set the VIRTUAL_CHANNEL value below to the virtual channel you selected.
14+
6. Connect the Servo's legs to GND, VCC (5.0), and a digital pin.
15+
7. Set ACTUATOR_PIN to the pin number you selected.
16+
8. Set the Cayenne authentication info to match the authentication info from the Dashboard.
17+
9. Compile and upload this sketch.
18+
10. Once the Arduino connects to the Dashboard you can use the slider to change the servo position.
19+
*/
20+
21+
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
22+
#include <CayenneMQTTEthernet.h>
23+
#include <Servo.h>
24+
25+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
26+
char username[] = "MQTT_USERNAME";
27+
char password[] = "MQTT_PASSWORD";
28+
char clientID[] = "CLIENT_ID";
29+
30+
#define VIRTUAL_CHANNEL 1
31+
#define ACTUATOR_PIN 4
32+
33+
Servo s1;
34+
35+
void setup()
36+
{
37+
Serial.begin(9600);
38+
Cayenne.begin(username, password, clientID);
39+
s1.attach(ACTUATOR_PIN);
40+
}
41+
42+
void loop()
43+
{
44+
Cayenne.loop();
45+
}
46+
47+
// This function is called when data is sent from Cayenne.
48+
CAYENNE_IN(VIRTUAL_CHANNEL)
49+
{
50+
// Determine angle to set the servo.
51+
int position = getValue.asDouble() * 180;
52+
// Move the servo to the specified position.
53+
s1.write(position);
54+
}

0 commit comments

Comments
 (0)