Benny Estes
Published © LGPL

All About Water Systems: Ultrasonic/WaterFlow/Rain Sensor

Sometimes when I take a bath, the water stops. I checked, the water tank is empty and I have to call the water engineer to fix the water.

EasyShowcase (no instructions)3 hours3,043
All About Water Systems: Ultrasonic/WaterFlow/Rain Sensor

Things used in this project

Story

Read more

Code

Code snippet #1

Plain text
//Remove This Someday when UnoWifiDev can connect to Cayenne (Can't Wait That Time Comes ^^)
//#include <UnoWiFiDevEd.h>

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

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "XXXXXXXX";

// Water Flow --> Only Works ON Pin 2 and Pin 3 for Uno, check https://www.arduino.cc/en/Reference/AttachInterrupt
#define VIRTUAL_PIN_WATERFLOW_1 V2
#define VIRTUAL_PIN_WATERFLOW_2 V3

int flowPin = 2;    //This is the input pin on the Arduino, Only Can be setup in Port 2 and 3
int flowPin2 = 3;
double flowRate;    //This is the value we intend to calculate. 
double flowRate2;
volatile int count;    //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.  
volatile int count2;
unsigned int getcount; //Getcount from count for flowRate
unsigned int getcount2;     

// Rain Sensor
float rain_level;
int rain_percentage;
int rain_bottom = 1022; 
int rain_top = 2;
int rain_distance =  rain_bottom - rain_top;
const int rainPin=A0; 
#define VIRTUAL_RAIN V10

// Ultrasonic
#define VIRTUAL_PIN_ULTRASONIC_1 V4
#define VIRTUAL_PIN_ULTRASONIC_PERCENT_1 V5
const int echoPin = 4; // Echo Pin 
const int trigPin = 5; // Trigger Pin 
float water_level;
float water_percentage;
int water_bottom = 42; 
int water_top = 32;
int water_distance = water_bottom - water_top;

#define VIRTUAL_PIN_ULTRASONIC_2 V6
#define VIRTUAL_PIN_ULTRASONIC_PERCENT_2 V7
const int echoPin2 = 6; // Echo Pin 
const int trigPin2 = 7; // Trigger Pin
float water_level2;
float water_percentage2;
int water_bottom2 = 75; 
int water_top2 = 19;
int water_distance2 = water_bottom2- water_top2;

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);

  // Water Flow
  pinMode(flowPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(flowPin), Flow, RISING);  

  pinMode(flowPin2, INPUT);
  attachInterrupt(digitalPinToInterrupt(flowPin2), Flow2, RISING);  

  // Ultrasonic 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);  

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);   

  // Rain
  pinMode(rainPin, INPUT);
  pinMode(VIRTUAL_RAIN, OUTPUT);
}

void loop()
{
  Cayenne.run();

  // Water Flow
  getWaterFlow();
  
  // Ultrasonic
  getDistance();    

  // Rain
  rain_level = analogRead(rainPin);
  rain_percentage = 100 * ((rain_distance - rain_level)/rain_distance);
}


// Water Flow
void Flow()
{
   count++; //Every time this function is called, increment "count" by 1
}

void Flow2()
{
   count2++; //Every time this function is called, increment "count" by 1
}


static unsigned long lastSecond;
void getWaterFlow()
{
  if (micros() - lastSecond >= 1000000L)
  {
	lastSecond += 1000000;

	noInterrupts(); //Disable the interrupts on the Arduino
	  getcount = count;
	  getcount2 = count2;
	  count = 0;      // Reset the counter so we start counting from 0 again
	  count2 = 0;       
	interrupts();   //Enables interrupts on the Arduino

	//Start the math for Blue Tank
	flowRate = (getcount * 2.25);        //Take counted pulses in the last second and multiply by 2.25mL 
	flowRate = flowRate * 60;         //Convert seconds to minutes, giving you mL / Minute
	flowRate = flowRate / 1000;       //Convert mL to Liters, giving you Liters / Minute
  
	//Start the math for Orange Tank
	flowRate2 = (getcount2 * 2.25);        //Take counted pulses in the last second and multiply by 2.25mL 
	flowRate2 = flowRate2 * 60;         //Convert seconds to minutes, giving you mL / Minute
	flowRate2 = flowRate2 / 1000;       //Convert mL to Liters, giving you Liters / Minute
  
	//Serial.println(flowRate);         //Print the variable flowRate to Serial  
	Cayenne.virtualWrite(VIRTUAL_PIN_WATERFLOW_1, flowRate);
  
	//Serial.println(flowRate);         //Print the variable flowRate to Serial  
	Cayenne.virtualWrite(VIRTUAL_PIN_WATERFLOW_2, flowRate2);  
  }
}


// Ultrasonic 
float duration;
long HR_dist = 0; // Calculated Distance
unsigned long previousMillisUltraSonic = 0;

void getDistance(){ 
  unsigned long currentMillisUltraSonic = millis();
  
  // Check sensor data every 250 milliseconds
  if (currentMillisUltraSonic - previousMillisUltraSonic >= 5000) {  
	
	digitalWrite(trigPin, LOW); 
	delayMicroseconds(2); 
	digitalWrite(trigPin, HIGH);
	delayMicroseconds(10); 
	digitalWrite(trigPin, LOW);
	duration = pulseIn(echoPin, HIGH);
	duration = duration/58.2;
	Cayenne.virtualWrite(VIRTUAL_PIN_ULTRASONIC_1, duration);
	
	water_percentage = (100 * (water_top + water_distance - duration)) / water_distance;
	Cayenne.virtualWrite(VIRTUAL_PIN_ULTRASONIC_PERCENT_1, water_percentage);
	

	digitalWrite(trigPin2, LOW); 
	delayMicroseconds(2); 
	digitalWrite(trigPin2, HIGH);
	delayMicroseconds(10); 
	digitalWrite(trigPin2, LOW);
	duration = pulseIn(echoPin2, HIGH);
	duration = duration/58.2;
	Cayenne.virtualWrite(VIRTUAL_PIN_ULTRASONIC_2, duration);

	water_percentage2 = (100 * (water_top2 + water_distance2 - duration)) / water_distance2;
	Cayenne.virtualWrite(VIRTUAL_PIN_ULTRASONIC_PERCENT_2, water_percentage2);
	
	previousMillisUltraSonic = currentMillisUltraSonic;
  }
}

// Rain
CAYENNE_OUT(VIRTUAL_RAIN)
{
  Cayenne.virtualWrite(VIRTUAL_RAIN, rain_percentage);
}

Code snippet #2

Plain text
//Remove This Someday when UnoWifiDev can connect to Cayenne (Can't Wait That Time Comes ^^)
//#include <UnoWiFiDevEd.h>

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

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "XXXXXXXX";

// Water Flow --> Only Works ON Pin 2 and Pin 3 for Uno, check https://www.arduino.cc/en/Reference/AttachInterrupt
#define VIRTUAL_PIN_WATERFLOW_1 V2
#define VIRTUAL_PIN_WATERFLOW_2 V3

int flowPin = 2;    //This is the input pin on the Arduino, Only Can be setup in Port 2 and 3
int flowPin2 = 3;
double flowRate;    //This is the value we intend to calculate. 
double flowRate2;
volatile int count;    //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.  
volatile int count2;
unsigned int getcount; //Getcount from count for flowRate
unsigned int getcount2;     

// Rain Sensor
float rain_level;
int rain_percentage;
int rain_bottom = 1022; 
int rain_top = 2;
int rain_distance =  rain_bottom - rain_top;
const int rainPin=A0; 
#define VIRTUAL_RAIN V10

// Ultrasonic
#define VIRTUAL_PIN_ULTRASONIC_1 V4
#define VIRTUAL_PIN_ULTRASONIC_PERCENT_1 V5
const int echoPin = 4; // Echo Pin 
const int trigPin = 5; // Trigger Pin 
float water_level;
float water_percentage;
int water_bottom = 42; 
int water_top = 32;
int water_distance = water_bottom - water_top;

#define VIRTUAL_PIN_ULTRASONIC_2 V6
#define VIRTUAL_PIN_ULTRASONIC_PERCENT_2 V7
const int echoPin2 = 6; // Echo Pin 
const int trigPin2 = 7; // Trigger Pin
float water_level2;
float water_percentage2;
int water_bottom2 = 75; 
int water_top2 = 19;
int water_distance2 = water_bottom2- water_top2;

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);

  // Water Flow
  pinMode(flowPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(flowPin), Flow, RISING);  

  pinMode(flowPin2, INPUT);
  attachInterrupt(digitalPinToInterrupt(flowPin2), Flow2, RISING);  

  // Ultrasonic 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);  

  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);   

  // Rain
  pinMode(rainPin, INPUT);
  pinMode(VIRTUAL_RAIN, OUTPUT);
}

void loop()
{
  Cayenne.run();

  // Water Flow
  getWaterFlow();
  
  // Ultrasonic
  getDistance();    

  // Rain
  rain_level = analogRead(rainPin);
  rain_percentage = 100 * ((rain_distance - rain_level)/rain_distance);
}


// Water Flow
void Flow()
{
   count++; //Every time this function is called, increment "count" by 1
}

void Flow2()
{
   count2++; //Every time this function is called, increment "count" by 1
}


static unsigned long lastSecond;
void getWaterFlow()
{
  if (micros() - lastSecond >= 1000000L)
  {
	lastSecond += 1000000;

	noInterrupts(); //Disable the interrupts on the Arduino
	  getcount = count;
	  getcount2 = count2;
	  count = 0;      // Reset the counter so we start counting from 0 again
	  count2 = 0;       
	interrupts();   //Enables interrupts on the Arduino

	//Start the math for Blue Tank
	flowRate = (getcount * 2.25);        //Take counted pulses in the last second and multiply by 2.25mL 
	flowRate = flowRate * 60;         //Convert seconds to minutes, giving you mL / Minute
	flowRate = flowRate / 1000;       //Convert mL to Liters, giving you Liters / Minute
  
	//Start the math for Orange Tank
	flowRate2 = (getcount2 * 2.25);        //Take counted pulses in the last second and multiply by 2.25mL 
	flowRate2 = flowRate2 * 60;         //Convert seconds to minutes, giving you mL / Minute
	flowRate2 = flowRate2 / 1000;       //Convert mL to Liters, giving you Liters / Minute
  
	//Serial.println(flowRate);         //Print the variable flowRate to Serial  
	Cayenne.virtualWrite(VIRTUAL_PIN_WATERFLOW_1, flowRate);
  
	//Serial.println(flowRate);         //Print the variable flowRate to Serial  
	Cayenne.virtualWrite(VIRTUAL_PIN_WATERFLOW_2, flowRate2);  
  }
}


// Ultrasonic 
float duration;
long HR_dist = 0; // Calculated Distance
unsigned long previousMillisUltraSonic = 0;

void getDistance(){ 
  unsigned long currentMillisUltraSonic = millis();
  
  // Check sensor data every 250 milliseconds
  if (currentMillisUltraSonic - previousMillisUltraSonic >= 5000) {  
	
	digitalWrite(trigPin, LOW); 
	delayMicroseconds(2); 
	digitalWrite(trigPin, HIGH);
	delayMicroseconds(10); 
	digitalWrite(trigPin, LOW);
	duration = pulseIn(echoPin, HIGH);
	duration = duration/58.2;
	Cayenne.virtualWrite(VIRTUAL_PIN_ULTRASONIC_1, duration);
	
	water_percentage = (100 * (water_top + water_distance - duration)) / water_distance;
	Cayenne.virtualWrite(VIRTUAL_PIN_ULTRASONIC_PERCENT_1, water_percentage);
	

	digitalWrite(trigPin2, LOW); 
	delayMicroseconds(2); 
	digitalWrite(trigPin2, HIGH);
	delayMicroseconds(10); 
	digitalWrite(trigPin2, LOW);
	duration = pulseIn(echoPin2, HIGH);
	duration = duration/58.2;
	Cayenne.virtualWrite(VIRTUAL_PIN_ULTRASONIC_2, duration);

	water_percentage2 = (100 * (water_top2 + water_distance2 - duration)) / water_distance2;
	Cayenne.virtualWrite(VIRTUAL_PIN_ULTRASONIC_PERCENT_2, water_percentage2);
	
	previousMillisUltraSonic = currentMillisUltraSonic;
  }
}

// Rain
CAYENNE_OUT(VIRTUAL_RAIN)
{
  Cayenne.virtualWrite(VIRTUAL_RAIN, rain_percentage);
}

Credits

willy

Posted by Matrix Benny Estes

Comments