my code:
#include <Wire.h>
// #include <LiquidCrystal_I2C.h> // This for non I2C LCD, Remove with Replacement of I2C LCD
#include <LiquidCrystal.h>
#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>
#include "DHT.h"
#define unitID 001
//#define FONA_RX 10 //reset after testing is complete
//#define FONA_TX 11 //reset after testing is complete
//#define FONA_RST 4 //reset after testing is complete
#define FONA_RX A1 //remove after testing is complete
#define FONA_TX A2 //remove after testing is complete
#define FONA_RST A3 //remove after testing is complete
const int pulsePin = 2;
// int pushButton = 2; // for testing purposes
#define pulsePin2 3
#define DHTPIN 6
#define DHTTYPE DHT22
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
// Use this for FONA 800 and 808s
// Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
// Use this one for FONA 3G (use this one)
Adafruit_FONA_3G fona = Adafruit_FONA_3G(FONA_RST); // Using FONA 3G
//LiquidCrystal_I2C lcd(0x27,16,2);
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8; //This for non I2C LCD, Remove with Replacement of I2C LCD
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //This for non I2C LCD, Remove with Replacement of I2C LCD
DHT dht(DHTPIN, DHTTYPE);
int noOfContacts = 3;
String no[] = {"0012345678901", "0012345678902", "0012345678903"}; //set phone #s to send sms here, deault is 3 change previous variable for more
int pipeDiameter = 1;//inches
float flowRateThreshold = 1;//gal/s //This is flow rate threshold. When flow rate higher than this, program detect it as leak
float flowRateThreshold2 = 1;//gal/s //This is flow rate threshold2. When flow rate of pipe 2 higher than this, program detect it as leak
volatile unsigned int pulseCount = 0;
unsigned int lastPulseCount = 0;
volatile unsigned int pulseCount2 = 0;
unsigned int lastPulseCount2 = 0;
long startTime = 0;
long startTime2 = 0;
float FlowRate = 0;
float FlowRate2 = 0;
int flag = 0;
int flag2 = 0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
fonaSerial->begin(4800);
if (!fona.begin(*fonaSerial))
{
Serial.println(F("Couldn't find FONA"));
//while (1);
}
// Print module IMEI number.
char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
uint8_t imeiLen = fona.getIMEI(imei);
if (imeiLen > 0)
{
Serial.print("Module IMEI: "); Serial.println(imei);
}
dht.begin();
//lcd.init();
//lcd.init();
//lcd.backlight();
//set up the non i2c LCD's number of colums and rows: Remove for I2C LCD
lcd.begin(16, 2);
lcd.setCursor(3, 0);
lcd.print("No Leak...");
attachInterrupt(digitalPinToInterrupt(pulsePin), pulseCount, RISING); //pulseCount or pulseCounter?
attachInterrupt(digitalPinToInterrupt(pulsePin2), pulseCount2, RISING); //pluseCount2 or pulseCounter2?
startTime = millis();
startTime2 = millis();
delay(60000);
}
void loop() {
// put your main code here, to run repeatedly:
//read button input for testing
int pulsePin = digitalRead(pulsePin);
// print out the state of the button:
Serial.println(pulsePin);
delay(1); // delay in between reads for stabiltiy
float h = dht.readHumidity();
float f = dht.readTemperature(true);
float FlowRate(pipeDiameter);
float FlowRate2(pipeDiameter);
if (FlowRate > flowRateThreshold); { // leak
flag++;
}
else if (FlowRate = < flowRateThreshold); { // no leak
flag = 0;
lcd.clear();
//lcd.setCursor(0,0);
//lcd.print("No Leak...");
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.setCursor(5, 0);
lcd.print(f, 1);
lcd.setCursor(10, 0);
lcd.print("F");
lcd.setCursor(0, 1);
lcd.print("Humidity:");
lcd.setCursor(9, 1);
lcd.print(h, 1);
lcd.setCursor(14, 1);
lcd.print("%");
}
else if (flag == 5); //flag raised 5 times continuesly LEAK!!!
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LEAK PIPE..1...");
lcd.setCursor(0, 1);
lcd.print("Tech. Notified! ");
// for (int i = 0; i < noOfContacts; i++) {
// sendSMS(no[i], "LEAK Detected in unit " + String(unitID) + "pipe 1.");
delay(5000);
}
if (FlowRate2 > flowRateThreshold2); //leak
{
flag2++;
}
else if (FlowRate2 = < flowRateThreshold2); { //no leak
flag2 = 0;
lcd.clear();
//lcd.setCursor(0,0);
//lcd.print("No Leak...");
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.setCursor(5, 0);
lcd.print(f, 1);
lcd.setCursor(10, 0);
lcd.print("F");
lcd.setCursor(0, 1);
lcd.print("Humidity:");
lcd.setCursor(9, 1);
lcd.print(h, 1);
lcd.setCursor(14, 1);
lcd.print("%");
}
else (flag2 == 5); { //flag raised 5 times continuesly LEAK!!!
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LEAK PIPE..2...");
lcd.setCursor(0, 1);
lcd.print("Tech. Notified! ");
for (int i = 0; i < noOfContacts; i++);
{
sendSMS(no[i], "LEAK Detected in unit " + String(unitID) + "pipe 2.");
delay(5000);
}
}
delay(60000);
}
void getFlowRate(int pipeDiameter)
{
float pulsePerUSgal = 0.0;
long elapsedTime;
if (pipeDiameter == 2)
pulsePerUSgal = 6.095;
else if (pipeDiameter == 3)
pulsePerUSgal = 2.890;
else if (pipeDiameter == 4)
pulsePerUSgal = 1.590;
else if (pipeDiameter == 6)
pulsePerUSgal = 0.464;
else if (pipeDiameter == 1) // for testing
pulsePerUSgal = 0.001;
}
if (pulsePerUSgal > 0.0)
{
elapsedTime = millis() - startTime;
FlowRate = (float) (pulseCount - lastPulseCount);
startTime = millis();
lastPulseCount = pulseCount;
FlowRate = (FlowRate * pulsePerUSgal) / ((float)elapsedTime / 1000.0); //gal/s
}
void getFlowRate2(int pipeDiameter)
{
float pulsePerUSgal = 0.0;
long elapsedTime;
{
if (pipeDiameter == 1) // for testing
pulsePerUSgal = 0.001;
else if (pipeDiameter == 2)
pulsePerUSgal = 6.095;
else if (pipeDiameter == 3)
pulsePerUSgal = 2.890;
else if (pipeDiameter == 4)
pulsePerUSgal = 1.590;
else if (pipeDiameter == 6)
pulsePerUSgal = 0.464;
}
if (pulsePerUSgal > 0.0)
{
elapsedTime = millis() - startTime2;
FlowRate = (float) (pulseCount2 - lastPulseCount2);
startTime2 = millis();
lastPulseCount2 = pulseCount2;
FlowRate2 = (FlowRate2 * pulsePerUSgal) / ((float)elapsedTime / 1000.0); //gal/s
}
}
void sendSMS(String to, String msg)
{
char sendto[21], message[141];
to.toCharArray(sendto, to.length());
to.toCharArray(message, msg.length());
if (!fona.sendSMS(sendto, message))
{
Serial.println(F("Failed"));
}
else
{
Serial.println(F("Sent!"));
}
}
void pulsecounter()
{
pulseCount++;
}
void pulsecounter2()
{
pulseCount2++;
}
}