@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");
}