Getting Started with Arduino Cloud Trigger

It's not preferred to continuously monitor the value from an IoT Device but rather get precise alerts whenever particular condition gets awake. 
We will see how to add Trigger in Arduino Cloud.


Hardware Setup:

1. MQ4 (Gas Sensor)

Gas Sensor


2. ESP32:
ESP32
Connections: 
  1. VCC Pin of Gas Sensor -> 3V3 Pin of ESP32
  2. GND Pin of Gas Sensor -> GND pin of ESP32
  3. A0 Pin of Gas Sensor ->  34 Pin of ESP32
(NOTE: ESP32 may vary in Models make sure to install specific Drivers if needed)


Generally there are 2 variables supported in Arduino Cloud for Trigger.

1. Boolean Variable

2. String Variable

Here we are going to see Email alerts based on Boolean Variable Condition.

If you are new to Arduino Cloud please watch this video.


If you are familiar to setup and create dashboard you can move further.


Step 1: Go to Triggers and click -> create trigger

Arduino Cloud Trigger setup


Step 2: Add Trigger Name and click -> Rename

Rename Trigger


Step 3: Click -> Done (We will come back to this page again) 

Trigger setup complete


Step 4: Go to -> Things. 

  1. Click -> Add Variable
  2. Name it -> gasValue
  3. Declaration -> (select) int
  4. Click -> Add Variable
  •  This will read Gas Value from sensor and display on Arduino Cloud Dashboard to get live value of Gas.
  • Similarly we have to create Boolean variable for Trigger (as shown in figure below).
  1. Click -> Add Variable
  2. Name it -> isGasFound
  3. Declaration -> (select) bool
  4. Variable Permission ->  (select) Read Only

Adding Variable


Step 5: (Read Only)
  • You will notice there are 2 variables in comments generated automatically.
  • int gasValue and bool isGasFound.
  • They are same as we created earlier.
  • NOTE: Please do not change anything here.

Understanding Code (1)


Step 6: (Writing Code logic)
  • There are 2 Functions created by Arduino Cloud by Default.
  • void setup() and void loop().
  • Do not delete or change it.
   Write this in void setup()
  1. pinMode(34, INPUT);
  2. isGasFound = false
  • We have connected Gas Sensor analog pin to -> ESP32's pin 34.
  • As we are reading value it should be as INPUT.
  • isGasFound was automatically created so we will keep its value as false, saying that at initial gas leakage is not occurring.
Understanding Code (2)


Step 7: (Main Logic)

Write this in void loop()

  1. isGasFound = false
  2. int gasData = analogRead(34);
  3. gasValue = gasData;
  4. if (gasData > 1000){ isGasFound = true; }
  • Line 1: again we have at initial kept isGasFound as false to saying there is no Leakage of Gas.
  • Line 2: we created new variable which will take gas data from sensor.
  • Line 3: We pass the gasData variable (which reads gas data) to gasValue (as gasValue will display it on Dashboard)
  • Line 4: (MOST IMP)
  • If the value of gas is > 1000 then isGasFound is true.
  • Which means gas is leaked.
  • NOTE: Just for demo I have used 1000 value you may change it as per requirement.
  • If value is < 1000 then gas is not leaked and isGasFound will remain false so no email would be sent.


Understanding Code (3)


Step 8: Return to Trigger page:

  • Click -> Link Variable.
  • You will see list of Things (device) you created.
  • Click on Thing name for this project (here I named it as Gas Detect Trigger).
  • You will find isGasFound variable here click on it 
  • Finally click on -> Link Variable

Arduino Trigger Link Variable


Step 9: (Add Email)

  1. Click -> Email.
  2. Add Subject and Message of choice.
  3. Click -> Done
  • NOTE: Here you would see Email as well as Push Notification option but we will use Email here.

Email added


Step 10: (Enable Trigger)

  1. Enable the state of Trigger you created.
  • Success now you would start receiving Email as Gas is found.

Enable Email

Final Output:

Email Alert

Blog by: Aadi

Comments

Popular posts from this blog

Technical 1 Solved: A network-related or instance-specific error occurred while establishing a connection to SQL Server

Flutter-Fire Setup Demo Tutorial with Code