Code issues - FONA linked Water Meter Project

Hello,

I'm working on a project to monitor a water meter (two sensors on this meter) and receive an SMS message via an Adafruit FONA if a preset flow rate is exceeded.

I keep having different code issues, so I'm pulling my hair out!!

I'm sure there are some syntax errors, but they're mixed in with control structure errors too, so I keep chasing my tail. I think I fix one, then another pops up and then the previous error returns.

My code appears to work up to approx. line 96 because I can see on my serial monitor that its trying to initialize the FONA (which is not currently attached) and then it moves on to providing the temp and humidity data from the DHT22 to the LCD.

After that, I cannot trigger a high flow alarm with my test button, that takes the place of the "pulseCount".

My code will be in the next post due to the character limit.

Thank you for any help you guys can provide.

Erich

Here's my error message:
Arduino: 1.8.12 (Windows 10), Board: "Arduino Uno"

C:\Users\schaa\Documents\Arduino\Fiverr_Fona_Smart_Meter_Sketch\Fiverr_Fona_Smart_Meter_Sketch.ino: In function 'void loop()':

Fiverr_Fona_Smart_Meter_Sketch:103:3: error: 'else' without a previous 'if'

else if (FlowRate = < flowRateThreshold); { // no leak
^~~~
Fiverr_Fona_Smart_Meter_Sketch:103:23: error: expected primary-expression before '<' token

else if (FlowRate = < flowRateThreshold); { // no leak
^
Fiverr_Fona_Smart_Meter_Sketch:123:3: error: 'else' without a previous 'if'

else if (flag == 5); //flag raised 5 times continuesly LEAK!!!
^~~~
Fiverr_Fona_Smart_Meter_Sketch:139:3: error: 'else' without a previous 'if'

else if (FlowRate2 = < flowRateThreshold2); { //no leak
^~~~
Fiverr_Fona_Smart_Meter_Sketch:139:24: error: expected primary-expression before '<' token

else if (FlowRate2 = < flowRateThreshold2); { //no leak
^
Fiverr_Fona_Smart_Meter_Sketch:158:3: error: 'else' without a previous 'if'

else (flag2 == 5); { //flag raised 5 times continuesly LEAK!!!
^~~~
Fiverr_Fona_Smart_Meter_Sketch:167:18: error: 'i' was not declared in this scope
sendSms(no*, "LEAK Detected in unit " + String(unitID) + "pipe 2.");*

  • ^*
    C:\Users\schaa\Documents\Arduino\Fiverr_Fona_Smart_Meter_Sketch\Fiverr_Fona_Smart_Meter_Sketch.ino: At global scope:
    Fiverr_Fona_Smart_Meter_Sketch:190:1: error: expected unqualified-id before 'if'
    if (pulsePerUSgal > 0.0)
    ^~
    C:\Users\schaa\Documents\Arduino\Fiverr_Fona_Smart_Meter_Sketch\Fiverr_Fona_Smart_Meter_Sketch.ino: In function 'void sendSms(String, String)':
    Fiverr_Fona_Smart_Meter_Sketch:231:13: error: 'class Adafruit_FONA_3G' has no member named 'sendSms'; did you mean 'sendSMS'?
  • if (!fona.sendSms(sendto, message))*
  • ^~~~~~~*
  • sendSMS*
    C:\Users\schaa\Documents\Arduino\Fiverr_Fona_Smart_Meter_Sketch\Fiverr_Fona_Smart_Meter_Sketch.ino: At global scope:
    Fiverr_Fona_Smart_Meter_Sketch:250:1: error: expected declaration before '}' token
    }
    ^
    exit status 1
    'else' without a previous 'if'
    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.

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++;
}
}
   else if (FlowRate = < flowRateThreshold); { // no leak
   ^~~~

remove the ; between the ) & the {

-jim lee

Done,

Here's the updated error code:

Arduino: 1.8.12 (Windows 10), Board: "Arduino Uno"

Fiverr_Fona_Smart_Meter_Sketch:190:3: error: expected unqualified-id before 'if'

   if (pulsePerUSgal > 0.000){

   ^~

Fiverr_Fona_Smart_Meter_Sketch:250:1: error: expected declaration before '}' token

 }

 ^

exit status 1
expected unqualified-id before 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Well, go find more. I just showed you an example bug.

-jim lee

jimLee:
Well, go find more. I just showed you an example bug.

-jim lee

Jim,

Thanks so much for your help! I was able to figure out the last error and it's working now!
Thanks again!

Erich

Good on ya!

-jim lee

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.