Water level control using Ultrasoic sensor

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial

#define TRIGGER 13
#define ECHO 12
#define PUMP 14
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid = “XXXXXXXXX”;
char wifiPassword = "XXXXXXXXX

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “MQTT_USERNAME”;
char password = “MQTT_PASSWORD”;
char clientID = “CLIENT_ID”;

unsigned long lastMillis = 0;
int VIRTUAL_CHANNEL = 0;

void setup() {
Serial.begin(9600);
pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(PUMP, OUTPUT);
pinMode(BUILTIN_LED, OUTPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
Cayenne.loop();
}

// 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.
CAYENNE_OUT_DEFAULT()
{

long duration, distance;
digitalWrite(TRIGGER, LOW);
delayMicroseconds(2);

digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);

digitalWrite(TRIGGER, LOW);
duration = pulseIn(ECHO, HIGH);
distance = (duration/2) / 29.1;

if (distance >= 150) {
Cayenne.virtualWrite(0, 255);
}
else {Cayenne.virtualWrite(0,0);
}
if (distance <= 15) {
digitalWrite(PUMP, LOW);
Cayenne.virtualWrite(2,0);
delay(1000);
}
else {
if (distance >= 100){
digitalWrite(PUMP, HIGH);
Cayenne.virtualWrite(2,255);
} else {delay(1000) ;

Serial.print(distance);

Serial.println(“Centimeter:”);
Cayenne.virtualWrite(5, distance);
delay(300);
}

CAYENNE_IN(VIRTUAL_CHANNEL)
int value = getValue.asInt();
if (getValue.asInt() == 1) { // NOTE: Channel = Virtual Pin
digitalWrite(PUMP, LOW);
}
else {
digitalWrite(PUMP, HIGH);
}

while compiling showing error - Expected Inititializer before Int

CAYENNE_IN , CAYENNE_IN(VIRTUAL_CHANNEL)
int value = getValue.asInt();
if (getValue.asInt() == 1) { // NOTE: Channel = Virtual Pin
digitalWrite(PUMP, LOW);
}
else {
digitalWrite(PUMP, HIGH);
}

any mistkae in my sketch Please help

you are missing many } in your code.

replace this in your code:

// 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.
CAYENNE_OUT_DEFAULT()
{

  long duration, distance;
  digitalWrite(TRIGGER, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);

  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration / 2) / 29.1;

  if (distance >= 150) 
  {
    Cayenne.virtualWrite(0, 255);
  }
  else 
  {
    Cayenne.virtualWrite(0, 0);
  }
  if (distance <= 15) 
  {
    digitalWrite(PUMP, LOW);
    Cayenne.virtualWrite(2, 0);
  }
  else 
  {
    if (distance >= 100) 
    {
      digitalWrite(PUMP, HIGH);
      Cayenne.virtualWrite(2, 255);
    } 
    else 
    {
      Serial.print(distance);
      Serial.println("Centimeter: ");
      Cayenne.virtualWrite(5, distance);
    }
  }
}

CAYENNE_IN(VIRTUAL_CHANNEL)
{
  int value = getValue.asInt();
  if (getValue.asInt() == 1) 
  { 
    digitalWrite(PUMP, LOW);
  }
  else 
  {
    digitalWrite(PUMP, HIGH);
  }
}
1 Like

Thank you very much Shramik
Great Wish good day

2 Likes