HOW TO EXACTLY USE CAYENNE_OUT / IN Funtions for field automation where they are used in nested form

Hello !!! We are final year students from Don Bosco College of engineering Fatorda, Goa, India.

Our Project is field Automation, where in we control irrigation via one the algorithm which we have developed.

The PROBLEM !!! Which we are encountering on Cayenne I am Explaining it up next:

  1. We are Keeping One AUTOMODE BUTTON on the Cayenne Dashboard.

  2. If that AUTOMODE BUTTON is PRESSED (state indicating Automode is on) Then Our Automatic Irrigation control Algorithm will run and will automatically Irrigate (Will Put ON the solonoid valve ie. water reaches the field ) the field at required times (Fully Automatic NO MANUAL interruption).

  3. W.r.t Point 2 we are doing one more thing simultaneously we are displaying the state also of the Water Valve Whether it is ON or OFF only applicable for Auto-mode algorithm, For that we are using state Button named STATE

  4. Now when That AUTOMODE BUTTON is NOT PRESSED we will Irrigate the Field Manually Ie There will be one more Button On the Dashboard Named MANUAL CONTROL BUTTON and if that Button is pressed that Valve will be ON (Water reaches Plant) and if that Button is released Valve will be OFF (Water does not reaches plant)

  5. Considering point 4 and 3 Manual state will not be displayed eg:

lets say ;

if we are in Manual Mode ie AUTOMODE Button is NOT PRESSED

and using MANUAL CONTROL BUTTON We are putting off the valve

but According to the Field Automation Algorithm Which is running simultaneosly if the valve has to ON then it will still display the state of the valve on STATE Button ie ON that does not clash with MANUAL OFF
SO This is our code: BUT IT GIVES ERROR AND WE FOUND OUT ALL POSSIBLE WAYS TO SOLVE OUR PROBLEMS

  //#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP32.h>

// WiFi network info.
char ssid[] = "huwai";
char wifiPassword[] = "12345678";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

void setup() {
  // put your setup code here, to run once:
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
   Cayenne.loop();
}

CAYENNE_IN(0)             // AUTO BUTTON CHANNEL IS 0

{
  if(getValue.asInt()==1)
  {
    while(getValue.asInt()==1)
    {
     digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
     
     CAYENNE_OUT(2)             //STATE BUTTON MADE ACTIVE
       {
        Cayenne.virtualWrite(2, 1);
       }
     delay(1000);                       // wait for a second
     digitalWrite(2, LOW);    // turn the LED off by making the voltage LOW
    
    CAYENNE_OUT(2)              //STATE BUTTON MADE DE ACTIVE
       {
           Cayenne.virtualWrite(2, 0);
       }
      delay(1000);
    }
  }
   
   
   else
   {
    CAYENNE_IN(1)            // MAN CONTROL BUTTON
    {
      digitalWrite(2, getValue.asInt()); 
    }
    
   }  

}
1 Like

I guess you are in the same project group of @sahillotlikar65
Regarding your project, if I understand properly you have two modes:

  1. Auto Mode:
    the solenoid turns ON and then OFF at a predetermined time interval. Next, you need to show the state of the solenoid whether it is ON or OFF.
  2. Manual Mode:
    You need to control the solenoid manually using a button on the cayenne dashboard.

regarding your code, does it compile at least?

yes we are in the same group

no it doesnot compile

we just want to know how to implement our idea using functions and keyword on ide along with cayenne

you have exactly understood our point

cause what logic we have used is not accepted that is nested cayenne in or out

you cannot have one function defined inside another. that is

Wrong

CAYENNE_IN(0) {

CAYENNE_IN(1) {    
}
}

Correct:

CAYENNE_IN(0) {
 }
CAYENNE_IN(1) {    

}
1 Like