CAYENNE_OUT_DEFAULT----Widget adding errors

Hello Everyone out there, I need help!!

I am using Arduino Uno with W5100 Ethernet Shield
I am using a Web-based dashboard:

I am experiencing a Serious problem Creating the Cayenne Dashboard:-

In my specific project, I send data to cayenne through the serial port on pins 0 (Rx)and 1(Tx) to the cayenne. Sensors are NOT connected directly to Arduino or the ethernet shield. Its only data transmitted to the cayenne.

The data, however, is copied to the "Cayenne.virtualWrite(VIRTUAL_CHANNEL_HUMIDITY, humidity) " where “humidity” is the —value obtained and stored----

(This data is actually coming from XBee device(coordinator) which has been collected wirelessly from the other XBee device(Router))

Bug: The pic displays my cayenne dashboard. It can be seen that the top 5 Widgets has only icons but there are no data values shown there. I added the 5 widgets, using Custom Widgets options, giving the exact channel number as in my Sketch, whereas the remaining 12 channels(starting at sixth position with a +sign on top-right corner) are automatically appearing on the dashboard timely manner because they are to be used in the new MQTT coding method function, CAYENNE_OUT_DEFAULT() ).

I would love to use the automatically generated Widget values( by “clicking the +sign” to dashboard) but the problem is with the widgets DURING the addition. All the options of the widgets do not appear, which makes it difficult and only data is displayed along with small icon on the left. This is not helpful, for example, when adding a Gauge, the indicator is not used in place of the value itself. Please see the image below where it shows, all the options not showing up during addition.

look at the Gauge for example in the pic

It’s not the case when "Custom widgets are added like for example in the pic given below.

This problem is appearing now as it says to mandatorily update the Cayenne work to MQTT.

Earlier it was not the case. My earlier working Cayenne Dashboard with Authorization tokens (and without MQTT) was like this…as shown below.

The solution needed:
Please help me in using the …channels data …so that they can be used like…custom widgets.

Regards,

Everything was better before mqtt update :exploding_head:
Try using virtual write instead out_default for adding widgets in your code.
Out_default writes widgets in pin to widget manner, example on pin 1 you have temperature sensor, out_default writes widget 1 for pin 1 and so on.

Yes I did try to add using VirtualWrite alone by commenting out the Out_default …
still I have same errors

I think the issue can be resolved by letting the add …look like …add custom widget…

@tu.smartcontrol have a look at the different data types Data types for Cayenne MQTT API

@shramik_salgaonkar
Sure, will read and try on that. But i am doubtful of the “appearance” of the widgets.
Let me get back ASAP.

Still a Concern:the data is not translated to Widget

can you post the code you are using,

Using the MQTT data types…(without OUT_default)

Better …but still …not good
Using the data types…digital values are showing up(like o or 1)…(but not good for differentiating)
High is shown with a BOLD black color…LOW is shown in Gray color(earlier, there was a clear color difference to indicate the change of state)

Analog Values are still not shown using the scale indicators of the Widgets

Stay tuned…to help me out, please… I am still testing…
Regards

Here is the code for your reference

 /*This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

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.

Steps:
1. Set the Cayenne authentication info to match the authentication info from the Dashboard.
2. Compile and upload the sketch.
3. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
//#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

#include <XBee.h>
#include <string.h>

#define VIRTUAL_doorlock_state V1
#define VIRTUAL_HUM V2
#define VIRTUAL_HUMIDITY V3   // for current value
#define VIRTUAL_TEMP V4
#define VIRTUAL_TEMPERATURE V5 // for current temp value 
#define VIRTUAL_smokeValue_gauge V6
#define VIRTUAL_led_beyond_threshold V7
#define VIRTUAL_motiondetected V8
#define VIRTUAL_rainindicator V9
#define VIRTUAL_rain_value_slider V10
#define VIRTUAL_LDR_val_read V11
#define VIRTUAL_led_switch V12

#define LED_VIRTUAL_PIN V14
#define LED_RGB_PIN 9

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

//  Coordinator node parameter variables
int switch_pin_read;              // manual switch on router(pin 7)
boolean door_flag = false;        // initially the door is open 

int h = 0;
int t = 0;

int h1 = 0;
int t1 = 0;

int mq2_threshold = 120;
boolean mq2_flag =false;

uint8_t mq2_analogMSB ;
uint8_t mq2_analogLSB ;

int mq2_val; 

int pir_ledPin = 6;               // choose the pin 6 on Arduino for the LED...for output
int pir_State = LOW;              // start, assuming no motion detected
int pir_val = 0;                  // variable for reading the pin status
int pir_pinSpeaker = 4;           // using pin 4  (Set up a speaker on a PWM pin (digital 9, 10, or 11))

int LDR_pin_out = 5;              // pin 5 for action indicator of ldr
int LDR_val_read = 0;             //ldr value on router side(pin 3) 

int light_is_dim=0;
int nRain_valRead=0;

boolean bIsRaining = false;       //intially no rain
String strRaining;

//initialises the payload array with 0...0-7 for temp and hum....1 for switch..2 bytes for mq2(smoke)...1 for PIR sensor//
// the payload is the data received from the router node( same size as the payload that was sent by the router)//

uint8_t payload[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 

XBee xbee = XBee();

ZBRxResponse zbRx = ZBRxResponse();
XBeeAddress64 rx_senderAddr; // recieved(rx) mac address of sender node(router) 
//00 13 A2 00 40 E9 16 50
XBeeAddress64 known_nodeAddress1 = XBeeAddress64(0x0013A200, 0x40E91650); 
// The known NODE(Router1) is written in XBeeAddress64 datatype

// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
  duration *= 1000;
  int period = (1.0 / freq) * 1000000;
  long elapsed_time = 0;
  while (elapsed_time < duration) {
    digitalWrite(pir_pinSpeaker, HIGH);
    delayMicroseconds(period / 2);
    digitalWrite(pir_pinSpeaker, LOW);
    delayMicroseconds(period / 2);
    elapsed_time += (period);
  }
}

////////////////////setup ////////////////
void setup() {
  //delay(100);
  Serial.begin(9600);
  xbee.begin(Serial);
  
  Cayenne.begin(username, password, clientID);
  delay(1000);                // for cayenne to settle down

  pinMode(pir_ledPin, OUTPUT);
  pinMode(pir_pinSpeaker, OUTPUT);
  pinMode(LDR_pin_out, OUTPUT);
  //pinMode(LED_RGB_PIN, OUTPUT);
}

////////////////////main loop /////////////
void loop() {
  Cayenne.loop();


////////////////////////////////////////////////////////////
//// commented out 3 lines below: to be used as VirtualWrite

//}
//CAYENNE_OUT_DEFAULT()
//{

//// uncomment 3 lines above: to be used in Out_default 
///////////////////////////////////////////////////////////  


  // this code runs on the xbee coordinator node (xbee + arduinouno + ethernetshield)
  // read the packet incoming from the router

    xbee.readPacket();

   if (xbee.getResponse().isAvailable()) {
   if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
     xbee.getResponse().getZBRxResponse(zbRx);
     for (int i = 0; i < zbRx.getDataLength(); i++) {
       payload[i] = zbRx.getData(i);
     }
     //Serial.print("Received I/O Sample from: ");
     rx_senderAddr = zbRx.getRemoteAddress64();
     if (rx_senderAddr == known_nodeAddress1) {
       //       Serial.print("Node #1");
       // Cayenne.virtualWrite(TEXT_node, "#1");
     } else {
       //     Serial.print("Unkown");
       // Cayenne.virtualWrite(TEXT_node, "Unknown Node");
     }
     //Serial.print("\t");
   }

   //---------- DHT11 Temp and Hum Sensor ------ //
//   int h = 0;
//   int t = 0;
//
//   int h1 = 0;
//   int t1 = 0;

   //uint8_t rxload[] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; // payload[0]---payload[7]data bits for temp and hum
   
   union floatbyte {
     float rx_fval;
     uint8_t rx_bytes[4];
   }
   fb;

   for(int i = 0; i < 4; i++) {
     fb.rx_bytes[i] = payload[i];
   }
   h = fb.rx_fval;

   h1 = h;

   for(int i = 0; i < 4; i++) {
     fb.rx_bytes[i] = payload[i + 4];
   }
   t = fb.rx_fval;
   t1 = t;

   /////////////////////////////////////////////////

   Cayenne.virtualWrite(VIRTUAL_HUM, h, "rel_hum","Percent (%)");
   Cayenne.virtualWrite(VIRTUAL_HUMIDITY, h1, "rel_hum", "Percent (%)");
   
   Cayenne.virtualWrite(VIRTUAL_TEMP, t, "temp", "c");
   Cayenne.virtualWrite(VIRTUAL_TEMPERATURE, t1,"temp", "c");

   
   //---------- Door Switch Pin  ------ // payload[8]
   switch_pin_read = payload[8];
                                                          
   if (switch_pin_read == HIGH) {
   //Serial.print("Closed");
                                                          
   } else if (switch_pin_read == LOW) {
    
   //Serial.print("Open");
   }
  Cayenne.virtualWrite(VIRTUAL_doorlock_state, switch_pin_read, "digital_sensor", "d");
                                                           
   //Serial.print("\t");

   //---------- MQ-2 Smoke Detector  ------ // payload[9] and payload[10]
   
   mq2_threshold = 120;
   boolean mq2_flag =false;
 
   uint8_t mq2_analogMSB = zbRx.getData(9);
   uint8_t mq2_analogLSB = zbRx.getData(10);

    mq2_val = mq2_analogLSB + (mq2_analogMSB * 256);

    //Serial.print(mq2_val);
   Cayenne.virtualWrite(VIRTUAL_smokeValue_gauge, mq2_val);
   

   if (mq2_val > mq2_threshold) {
   mq2_flag=true;
   // Serial.print("Yes!");
   } 
   else
   {
   mq2_flag=false;
   // Serial.print("No!");
   }
   
   Cayenne.virtualWrite(VIRTUAL_led_beyond_threshold, mq2_flag, "digital_sensor", "d");
      
   /////Rain Sensor ////

   //payload[12],payload[13],payload[14];

   uint8_t nRain_valMSB = payload[12];
   uint8_t nRain_valLSB = payload[13];

   int nRain_valRead = nRain_valLSB + (nRain_valMSB * 256);

   bIsRaining = payload[14];

   if (!bIsRaining) {
     strRaining = "YES";
   }
   else
   {
    strRaining = "NO";
   }
   
   Cayenne.virtualWrite(VIRTUAL_rainindicator, bIsRaining, "digital_sensor", "d");
   
   Cayenne.virtualWrite(VIRTUAL_rain_value_slider, nRain_valRead, "analog_sensor", "null");


   //------LDR Sensor------//

   uint8_t LDR_msb = payload[15];
   uint8_t LDR_lsb = payload[16];
   int light_is_dim=0;

   LDR_val_read = LDR_lsb + (LDR_msb * 256);

   Cayenne.virtualWrite(VIRTUAL_LDR_val_read, LDR_val_read, "analog_sensor", "null");
  
   if (LDR_val_read < 250) {
     light_is_dim=1;
    Cayenne.virtualWrite(VIRTUAL_led_switch, light_is_dim, "digital_sensor", "d");
  
    digitalWrite(LDR_pin_out, HIGH);
    delay(1000);
   }
   digitalWrite(LDR_pin_out, LOW);
   delay(LDR_val_read);

   
   
////////////---------- PIR Sensor  ------ ///////payload[11]

   pir_val = payload[11];
   
      Cayenne.virtualWrite(VIRTUAL_motiondetected, pir_val, "digital_sensor", "d");
 
// turn LED ON if motion detected or LED OFF if not detected

   if (pir_val == HIGH) {
   digitalWrite(pir_ledPin, HIGH); // turn LED ON
   playTone(300, 160);
     delay(150);

     if (pir_State == LOW) {
      // we have just turned on
      //Serial.print("Detected!");
      // We only want to print on the output change, not state
      pir_State = HIGH;
     }
   } else {
     digitalWrite(pir_ledPin, LOW); // turn LED OFF
     playTone(0, 0);
     delay(300);
     if (pir_State == HIGH) {
       // we have just turned off
       // Serial.print("Ended!");
       // We only want to print on the output change, not state
       pir_State = LOW;
     }
   }
   //Serial.print("\t");
 }
  
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

Please ignore the Serial.print
(that was for my reference and other usage)
The serial does not work…as the pins 0 and 1 are used up by xbee connection

// ---- means comment out

anyone out there…

please help in resolving these

Hi not sure but you may be having similar issues to those I had after the upgrade, firstly you MUST use the auto generated channel widgets first, and you must only have 1 widget per channel, if you add 2 widgets to the same channel you can have the issue you describe with not getting the option to change widget type. I found that even when I thought I hadn’t got widgets on some channels I had but they were not visible - They were in the navigation tree but not on the dashboard, to resolve this you may need to reset the dashboard.

From a clean dashboard, when you add widgets (from the auto suggested ones) the only time I have found I CAN NOT change the widget type is if I haven’t specified a correct datatype in the virtualwrite. So in the uptime/millis for example, if you don’t specify the datatype you will not be able to change the widget type from value display.

Make sure you use only 1 widget per channel.

The colours of the Digital widgets can be changed depending on the icon chosen, I have seen brighter colours.

I know nothing about the default out, I have only used specific virtualWrite

Let me know if it gets you anywhere

Looking again, I’m sure you have widgets that are not on the dashboard - you seem to have 5 on the dashboard and 8 in the navigation tree - Reset the dashboard first, delete all of the widgets you have customised ad then re add them from the suggestions.

Keep in mind what I already said about 1 widget per channel and correctly adding data types to virtual write

@paulminize
Thank you for the response. I am, indeed using the auto-generated channel widgets and only 1 widget per channel.

For digital data, the widget looks to be working well(except identifying the change of state. it is either grey color for low and black color for high values)

For analog values, the problem still exists…the generated values are not changing into widget icons. The values are shown as it is without the indicators.

Hi I’m a little confused, maybe I have it wrong, I am just a noob, but have you tried removing the Vfrom the channel in your virtual writes at the Moment you specify V6 for example, I think with MQTT, you need just the digit 6…

I see on that particular channel (6) for example you have no icon on the widget, I think this is because you have not specified as valid datatype on the virtualwrite and this I think prevents the widgets loading in correctly.

For the digital widgets are you saying you can’t change the icon from the setting menu on each widget to something less generic and more colorful?

Did you reset the dashboard and delete all widgets and start over?

You definitely had some widgets that need ed clearing in one of your screenshots.

@paulminize

Apologies for the delay. Got stuck into things.

I understand what you mean. And I can correct the data type for channel 6.
I still have to try and work …by removing ‘V’ and use only channel numbers.

But, please look at the channels 10 and 11, which already have datatypes mentioned but still do not work for me.

before_after

This is before and after works of Cayenne (without MQTT and with MQTT update)

The icons I am talking about is not the small icons, but the indicators itself which get updated to the new value. Like the “Gauge” in this picture.

Experts of Cayenne…please. This does not seem to be my problem alone.
So, please help!

It doesn’t look like you went in to the widget settings and changed the widget from a value to gauge.

Before and after:


1 Like

@adam

I did apply the same to the Digital data…“Digital Motion Sensor” in the picture from my “first message” for example and IT DID give 2 states.

For analog values too, let me look into it again, as I remember ‘very little’ to have done similar.

Thank you, Adam.
Let me get back here ASAP.

1 Like

@adam

Thank you for the help. It seems that was the case, Initially I failed to use the drop down to see other options available in there. Guage was in there. I used that one (which was my main concern to begin with). I wanted to represent analog values…which wasn’t working for me earlier.

For my program… I had to write ‘all’ the Cayenne.virtualWrite 's outside the main Loop function and within Cayenne_out_default { }.

I am facing other issues now…may be a technical glitch from my side or the cayenne protocol.

Firstly, I want to demonstrate two charts…one for humidity and the other for temperature.
I am able to see chart for Humidity (with Live date and time) but for Temperature, I am getting a message like…data unavailable for this period.

I am using two widgets to represent the ‘same’ humdity values as well as two widgets to represent the 'same ’ temperature values.

Secondly,(a major concern, which may tell me if i am working correctly or incorrectly), Cayenne gets disconnected very often and I get connect failed, error -1 after just a less than a minute working…

" [409992] MQTT connect failed, error -1
[411046] MQTT connect failed, error -1
[412101] MQTT connect failed, error -1
[413156] MQTT connect failed, error -1
…"

Any help on these queries please?

Appreciate your help. Thanks

can you share the code you are using.

@shramik_salgaonkar

Here is the code for your reference.

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

#include <XBee.h>
#include <string.h>

#define VIRTUAL_doorlock_state V1
#define VIRTUAL_HUM V2
#define VIRTUAL_HUMIDITY V3   // for current value
#define VIRTUAL_TEMP V4
#define VIRTUAL_TEMPERATURE V5 // for current temp value 
#define VIRTUAL_smokeValue_gauge V6
#define VIRTUAL_motiondetected V7
#define VIRTUAL_rainindicator V8
#define VIRTUAL_rain_value_slider V9
#define VIRTUAL_LDR_val_read V10

//#define VIRTUAL_ldr_gauge V11
//#define VIRTUAL_light_is_dim V11 
//#define VIRTUAL_led_beyond_threshold V13

#define LED_VIRTUAL_PIN V14
#define LED_RGB_PIN 9


XBee xbee = XBee();

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


unsigned long lastMillis = 0;


//  Coordinator node parameter variables
int switch_pin_read;              // manual switch on router(pin 7)
int door_flag = HIGH ;        // initially the door is closed

int h;// = 0;
int t;// = 0;

int h1;// = 0;
int t1;// = 0;

int mq2_threshold = 120;
boolean mq2_flag =false;

uint8_t mq2_analogMSB ;
uint8_t mq2_analogLSB ;

int mq2_val; 

int pir_ledPin = 6;               // choose the pin 6 on Arduino for the LED...for output
int pir_State = LOW;              // start, assuming no motion detected
int pir_val = 0;                  // variable for reading the pin status
int pir_pinSpeaker = 4;           // using pin 4  (Set up a speaker on a PWM pin (digital 9, 10, or 11))

int LDR_pin_out = 5;              // pin 5 for action indicator of ldr
int LDR_val_read = 0;             //ldr value on router side(pin 3) 

int light_is_dim=0;
int nRain_valRead=0;

boolean bIsRaining = false;       //intially no rain
String strRaining;

//initialises the payload array with 0...0-7 for temp and hum....1 for switch..2 bytes for mq2(smoke)...1 for PIR sensor
// the payload is the data received from the router node( same size as the payload that was sent by the router)
uint8_t payload[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 

ZBRxResponse zbRx = ZBRxResponse();
XBeeAddress64 rx_senderAddr; // recieved(rx) mac address of sender node(router) //00 13 A2 00 40 E9 16 50
XBeeAddress64 known_nodeAddress1 = XBeeAddress64(0x0013A200, 0x40E91650); // The known NODE(Router1) is written in XBeeAddress64 datatype

// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
  duration *= 1000;
  int period = (1.0 / freq) * 1000000;
  long elapsed_time = 0;
  while (elapsed_time < duration) {
    digitalWrite(pir_pinSpeaker, HIGH);
    delayMicroseconds(period / 2);
    digitalWrite(pir_pinSpeaker, LOW);
    delayMicroseconds(period / 2);
    elapsed_time += (period);
  }
}

////////////////////setup ////////////////
void setup() {
  
  //delay(100);
  Serial.begin(9600);
  xbee.begin(Serial);
  
  pinMode(pir_ledPin, OUTPUT);
  pinMode(pir_pinSpeaker, OUTPUT);
  pinMode(LDR_pin_out, OUTPUT);
  //pinMode(LED_RGB_PIN, OUTPUT);

  
  Cayenne.begin(username, password, clientID);
  delay(200);                // for cayenne to settle down

}

////////////////////main loop /////////////
void loop() {

     Cayenne.loop();

  delay(10);


  // this code runs on the xbee coordinator node (xbee + arduinouno + ethernetshield)
  // read the packet incoming from the router
    xbee.readPacket();

   if (xbee.getResponse().isAvailable()) {
   if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
     xbee.getResponse().getZBRxResponse(zbRx);
     for (int i = 0; i < zbRx.getDataLength(); i++) {
       payload[i] = zbRx.getData(i);
     }
     //Serial.print("Received I/O Sample from: ");
     rx_senderAddr = zbRx.getRemoteAddress64();
     if (rx_senderAddr == known_nodeAddress1) {
       
       
       // Serial.print("Node #1");
       
       // Cayenne.virtualWrite(TEXT_node, "#1");
     } else {
       
       //Serial.print("Unkown");
       
       // Cayenne.virtualWrite(TEXT_node, "Unknown Node");
     }
     // Serial.print("\t");
   }

   //---------- DHT11 Temp and Hum Sensor ------ //
//   int h = 0;
//   int t = 0;
//
//   int h1 = 0;
//   int t1 = 0;

   //uint8_t rxload[] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; // payload[0]---payload[7]data bits for temp and hum
   
   union floatbyte {
     float rx_fval;
     uint8_t rx_bytes[4];
   }
   fb;

   for(int i = 0; i < 4; i++) {
     fb.rx_bytes[i] = payload[i];
   }
   h = fb.rx_fval;

   h1 = h;

   for(int i = 0; i < 4; i++) {
     fb.rx_bytes[i] = payload[i + 4];
   }
   t = fb.rx_fval;
   t1 = t;

   /////////////////////////////////////////////////

//   Cayenne.virtualWrite(VIRTUAL_HUM, h, "rel_hum","Percent (%)");
//   Cayenne.virtualWrite(VIRTUAL_HUMIDITY, h1, "rel_hum", "Percent (%)");
//   
//   Cayenne.virtualWrite(VIRTUAL_TEMP, t, "temp", "c");
//   Cayenne.virtualWrite(VIRTUAL_TEMPERATURE, t1,"temp", "c");
   
   //---------- Door Switch Pin  ------ // payload[8]
   switch_pin_read = payload[8];

   //Serial.print(switch_pin_read);
   
   if (switch_pin_read == HIGH) {
  door_flag=LOW;

//    Cayenne.virtualWrite(VIRTUAL_doorlock_state, '0', "digital_sensor", "d");

   
   //Serial.print("Closed");
                                                          
   } else if (switch_pin_read == LOW) {
    door_flag=HIGH;
   //Serial.print("Open");

//    Cayenne.virtualWrite(VIRTUAL_doorlock_state, '1', "digital_sensor", "d");

   
   }
  
  //Cayenne.virtualWrite(VIRTUAL_doorlock_state, switch_pin_read, "digital_sensor", "d");
                                                           
   //Serial.print("\t");

   //---------- MQ-2 Smoke Detector  ------ // payload[9] and payload[10]
   
   mq2_threshold = 120;
   boolean mq2_flag =false;
 
   uint8_t mq2_analogMSB = zbRx.getData(9);
   uint8_t mq2_analogLSB = zbRx.getData(10);

    mq2_val = mq2_analogLSB + (mq2_analogMSB * 256);


    //Serial.print(mq2_val);
    
//   Cayenne.virtualWrite(VIRTUAL_smokeValue_gauge, mq2_val,"analog_sensor", "null");



   if (mq2_val > mq2_threshold) {
   mq2_flag=true;
   // Serial.print("Yes!");
   } 
   else
   {
   mq2_flag=false;
   // Serial.print("No!");
   }
   
  
  //%% //Cayenne.virtualWrite(VIRTUAL_led_beyond_threshold, mq2_flag, "digital_sensor", "d");
      
   ////////////////////////////////%%%%%%%%//////////////////////////////////////
   /////Rain Sensor ////

   //payload[12],payload[13],payload[14];

   uint8_t nRain_valMSB = payload[12];
   uint8_t nRain_valLSB = payload[13];

   int nRain_valRead = nRain_valLSB + (nRain_valMSB * 256);

   bIsRaining = payload[14];

   if (!bIsRaining) {
     strRaining = "YES";
   }
   else
   {
    strRaining = "NO";
   }

////////////////////
//   Cayenne.virtualWrite(VIRTUAL_rainindicator, bIsRaining, "digital_sensor", "d");
//
//   Cayenne.virtualWrite(VIRTUAL_rain_value_slider, nRain_valRead, "analog_sensor", "null");
   //////////////
   
   //------LDR Sensor------//

   uint8_t LDR_msb = payload[15];
   uint8_t LDR_lsb = payload[16];
   int light_is_dim=0;

   LDR_val_read = LDR_lsb + (LDR_msb * 256);

//   Cayenne.virtualWrite(VIRTUAL_LDR_val_read, LDR_val_read, "analog_sensor", "null");
   
   if (LDR_val_read < 250) {
     light_is_dim=1;

//    //Cayenne.virtualWrite(VIRTUAL_light_is_dim, light_is_dim, "digital_sensor", "d");
  
    digitalWrite(LDR_pin_out, HIGH);
    delay(1000);
   }
   digitalWrite(LDR_pin_out, LOW);
   delay(LDR_val_read);

   
   
   //---------- PIR Sensor  ------ //..........payload earlier.....or change in router code

   pir_val = payload[11];
   
//   Cayenne.virtualWrite(VIRTUAL_motiondetected, pir_val, "digital_sensor", "d"); // turn LED ON if motion detected or LED OFF if not detected

   if (pir_val == HIGH) { // check if the input is HIGH...motion detected
   digitalWrite(pir_ledPin, HIGH); // turn LED ON
   playTone(300, 160);
     delay(150);

     if (pir_State == LOW) {
      // we have just turned on
      //Serial.print("Detected!");
      // We only want to print on the output change, not state
      pir_State = HIGH;
     }
   } else {
     digitalWrite(pir_ledPin, LOW); // turn LED OFF
     playTone(0, 0);
     delay(300);
     if (pir_State == HIGH) {
       // we have just turned off
       // Serial.print("Ended!");
       // We only want to print on the output change, not state
       pir_State = LOW;
     }
   }
   //Serial.print("\t");
 }

 
}

CAYENNE_OUT_DEFAULT(){

 
   Cayenne.virtualWrite(VIRTUAL_HUM, h, "rel_hum","Percent (%)");
   Cayenne.virtualWrite(VIRTUAL_HUMIDITY, h1, "rel_hum", "Percent (%)");
   
   Cayenne.virtualWrite(VIRTUAL_TEMP, t, "temp", "c");
   Cayenne.virtualWrite(VIRTUAL_TEMPERATURE, t1,"temp", "c");

   Cayenne.virtualWrite(VIRTUAL_doorlock_state,door_flag,"digital_sensor", "d");

   Cayenne.virtualWrite(VIRTUAL_smokeValue_gauge, mq2_val,"analog_sensor", "null");

   Cayenne.virtualWrite(VIRTUAL_rainindicator, bIsRaining, "digital_sensor", "d");

   //Cayenne.virtualWrite(VIRTUAL_rain_value_slider, nRain_valRead);
   
   Cayenne.virtualWrite(VIRTUAL_rain_value_slider, nRain_valRead, "analog_sensor", "null");
  
   Cayenne.virtualWrite(VIRTUAL_LDR_val_read, LDR_val_read, "analog_sensor", "null");
  
   Cayenne.virtualWrite(VIRTUAL_motiondetected, pir_val, "digital_sensor", "d"); // turn LED ON if motion detected or LED OFF if not detected

  }


// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}