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
+ }
0 commit comments