I am making aquarium automation project.
Since triggers is not I decided to write if else for my project but it is not working the relay not turning on if condition is satisfied, if I try to get it on through Cayenne dashboard it get off instantly but same code working if I write if else in offline mode I mean only if else for realy ,plzz tell me where I get wrong.here I code
#include <NewPing.h>
//#define CAYENNE_DEBUG
//#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>
// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";
char username[] = "";
char password[] = "";
char clientID[] = "";
// Set ESP8266 Serial object. In this example we use the Serial hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial
int sensor =A0;
int sens = analogRead(sensor);
#define VIRTUAL_CHANNEL 10
ESP8266 wifi(&EspSerial);
#define VIRTUAL_CHANNEL 2
#define VIRTUAL_CHANNEL 3
#define VIRTUAL_CHANNEL 4
#define VIRTUAL_CHANNEL 6
#define TRIGGER_PIN 11 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
#define VIRTUAL_CHANNEL 7
#define VIRTUAL_CHANNEL 8
#define ACTUATOR_PIN 3
int ledpin=7;
int ledpina=8;
int ledpinb=9;
int feedbutton;
int filterbutton;
int motor1;
int motor2;
unsigned int uS;
CAYENNE_IN(2)
{ feedbutton = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(7, getValue.asInt());
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(3)
{ filterbutton = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(3, getValue.asInt());
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(4){ motor1 = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(4, getValue.asInt());
}
CAYENNE_IN(5){ motor2 = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(5, getValue.asInt());
}
CAYENNE_OUT(8)
{
int sens = analogRead(sensor);
Serial.println("value ");
Serial.println(sens);
Cayenne.virtualWrite(8,sens);
}
void setup()
{
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(115200);
delay(10);
Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}
void loop()
{
if(sens>650)// I am talking about this if else
{
digitalWrite(4,HIGH);
}
else
{
digitalWrite(4,LOW);
}
Cayenne.loop();
}
CAYENNE_OUT(6)
{
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
Cayenne.virtualWrite(6,uS / US_ROUNDTRIP_CM);
}
CAYENNE_IN(7)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(3, HIGH);
}
else {
digitalWrite(3, LOW);
}
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
// Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
// 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");
}