|
| 1 | +--- |
| 2 | +title: Triggers |
| 3 | +description: Learn how to use triggers that allow you to send notifications based on set conditions. |
| 4 | +author: Hannes Siebeneicher |
| 5 | +tags: [IoT Cloud, Triggers, Cloud Notification] |
| 6 | +--- |
| 7 | + |
| 8 | +Triggers react to certain conditions inside your IoT Cloud Thing, such as a boolean being true, or a string being assigned a value. As soon as a set condition is met a notification gets triggered and send to you. This is super useful when you monitor data and you need to know about any change as soon as it happens. This could be anything from different values in environmental monitoring or something security related such as movement detection. |
| 9 | + |
| 10 | +Triggers can be set up for any of your existing projects, and are found in the [cloud home section](https://cloud.arduino.cc/home/). |
| 11 | + |
| 12 | +## Hardware & Software Needed |
| 13 | + |
| 14 | +- [Arduino IoT Cloud](https://create.arduino.cc/iot/) (Maker plan). |
| 15 | +- Cloud-compatible boards, [see full list](https://docs.arduino.cc/arduino-cloud/getting-started/iot-cloud-getting-started#compatible-hardware). |
| 16 | + |
| 17 | +***In this tutorial, we use the [Nano 33 IoT](https://store.arduino.cc/products/arduino-nano-33-iot?queryID=undefined). This is not a requirement, you can use any IoT Cloud-compatible board for this tutorial.*** |
| 18 | + |
| 19 | +## Setup & Configuration |
| 20 | + |
| 21 | +## Limitations |
| 22 | + |
| 23 | +Currently the only variables supported by the trigger feature are: |
| 24 | + |
| 25 | +- Booleans |
| 26 | +- Strings |
| 27 | + |
| 28 | +## Setup & Configuration |
| 29 | + |
| 30 | +***If you are unfamiliar with how to set up a Thing and variables, head on over to the [Getting Started with the Arduino IoT Cloud](/arduino-cloud/getting-started/iot-cloud-getting-started) article.*** |
| 31 | + |
| 32 | +**1.** Head over to the Things tab and create a new Thing, create a variable, and set up your device including a working network connection. |
| 33 | + |
| 34 | +**2.** Upload your code to the board you want to use. For demonstration purposes, we'll use a [simple button](#example-code) sketch setting our button boolean equal to true each time a button connected to Pin D3 is pressed. You can of course set this up in whatever way you'd like. |
| 35 | + |
| 36 | +**3.** Go to [Arduino Cloud home](https://cloud.arduino.cc/home/) and click on **Triggers**. |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +**4.** Click **Add Trigger** |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +**5.** Click **Cloud Variable** |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + then select your **Thing**, your **Variable**, and finally press on **Link Variable**. |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +**6.** Next, select your **Action**, in our case **Email**. A new window will pop up in which we can create our personalized message, which is sent each time the trigger is activated. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +You can even include **dynamic data** such as `{variable.timestamp}` or `{variable.value}` by using the tags shown at the bottom, to include variable data from your sketch in the messages sent to your email. |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +**7.** Once you're finished press **Done**. The final step is to turn on the **State** switch and now your trigger is ready to be used. |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +**8.** Try it out! Press the button and within a couple of seconds, you should receive an email telling you that the trigger has been activated showing you the message that you create in the previous step. |
| 66 | + |
| 67 | +## Example Code |
| 68 | + |
| 69 | +```arduino |
| 70 | +#include "thingProperties.h" |
| 71 | +
|
| 72 | +const int btnPin = 3; |
| 73 | +
|
| 74 | +int buttonState = 0; |
| 75 | +
|
| 76 | +void setup() { |
| 77 | + // Initialize serial and wait for port to open: |
| 78 | + Serial.begin(9600); |
| 79 | + // This delay gives the chance to wait for a Serial Monitor without blocking if none is found |
| 80 | + delay(1500); |
| 81 | +
|
| 82 | + // Defined in thingProperties.h |
| 83 | + initProperties(); |
| 84 | +
|
| 85 | + // Connect to Arduino IoT Cloud |
| 86 | + ArduinoCloud.begin(ArduinoIoTPreferredConnection, false, "mqtts-sa.iot.oniudra.cc"); |
| 87 | + |
| 88 | + /* |
| 89 | + The following function allows you to obtain more information |
| 90 | + related to the state of network and IoT Cloud connection and errors |
| 91 | + the higher number the more granular information you’ll get. |
| 92 | + The default is 0 (only errors). |
| 93 | + Maximum is 4 |
| 94 | + */ |
| 95 | + setDebugMessageLevel(2); |
| 96 | + ArduinoCloud.printDebugInfo(); |
| 97 | + |
| 98 | + pinMode(btnPin, INPUT); |
| 99 | + pinMode(LED_BUILTIN, OUTPUT); |
| 100 | +} |
| 101 | +
|
| 102 | +void loop() { |
| 103 | + ArduinoCloud.update(); |
| 104 | + |
| 105 | + buttonState = digitalRead(btnPin); |
| 106 | + |
| 107 | + if (buttonState == HIGH) { |
| 108 | + digitalWrite(LED_BUILTIN, LOW); |
| 109 | + button = true; |
| 110 | + } else { |
| 111 | + digitalWrite(LED_BUILTIN, HIGH); |
| 112 | + button = false; |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Use Cases |
| 118 | + |
| 119 | +Here are some suggestions for potential projects that utilize **Triggers** in a project: |
| 120 | + |
| 121 | +- A PIR detecting motion, sending a notification as soon as the sensor is triggered. |
| 122 | +- An automated plant monitoring setup sending a notification as soon as your plants need water. |
| 123 | +- An environmental data collection setup notifying you as soon as the values reach a certain threshold. |
| 124 | + |
| 125 | + |
| 126 | +## Summary |
| 127 | +Triggers are a simple but extremely powerful feature allowing you to stay on track with all your Arduino IoT Cloud projects. Instead of constantly checking your dashboards to see the newest update just create a trigger notifying you as soon as changes occur. In this tutorial, we used just **one trigger** in combination with a simple demo sketch but you can create **as many Triggers as you need** to automate your projects. |
0 commit comments