0
votes

I'm trying to read or determine the water flow with the arduino. At this moment I haver connected:

  1. Arduino UNO.
  2. Arduino GSM SIM900 Module
  3. Arduino Water flow Sensor

And here is my code:

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8); // Configura el puerto serial para el SIM900

char incoming_char=0; //Variable que guarda los caracteres que envia el SIM900
int salir = 0;

byte statusLed    = 13;

byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin       = 2;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;

volatile byte pulseCount;  

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;

void setup()
{
SIM900.begin(19200); //Configura velocidad serial para el SIM900
delay(25000); //Retardo para que encuentra a una RED
Serial.begin(19200); //Configura velocidad serial para el Arduino
Serial.println("OK"); //Mensaje OK en el arduino, para saber que todo va bien.

// Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached

  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

void mensaje_sms()
//Funcion para mandar mensaje de texto
{

SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(100);
SIM900.println("AT+CMGS=\"3003043789\""); // recipient's mobile number, in international format
delay(100);
//SIM900.println("Saludos desde HetPro"); // message to send
// Print the flow rate for this second in litres / minute
    //SIM900.print("Flow rate: " + int(flowRate) + "L/min" + "\t");
    SIM900.print("Flow rate: ");
    delay(100);
    SIM900.print(int(flowRate));  // Print the integer part of the variable
    delay(100);
    SIM900.print("L/min");
    delay(100);
    SIM900.print("\t");       // Print tab space
delay(100);
    // Print the cumulative total of litres flowed since starting
    //SIM900.print("Output Liquid Quantity: " + totalMilliLitres + "mL" + "L" + totalMilliLitres/1000 + "L");
    SIM900.print("Output Liquid Quantity: ");
    delay(100);        
    SIM900.print(totalMilliLitres);
    delay(100);
    SIM900.println("mL"); 
    delay(100);
    SIM900.print("\t");       // Print tab space
    delay(100);
    SIM900.print(totalMilliLitres/1000);
    delay(100);
    SIM900.print("L");

delay(100);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26 //Comando de finalizacion
delay(100);
SIM900.println();
delay(5000); // Tiempo para que se envie el mensaje
Serial.println("SMS sent successfully");
}


void loop()
{
  if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
    detachInterrupt(sensorInterrupt);
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    oldTime = millis();

    flowMilliLitres = (flowRate / 60) * 1000;

    totalMilliLitres += flowMilliLitres;

    unsigned int frac;

  mensaje_sms(); //Envia mensaje
  pulseCount = 0;
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  } 
  while(1); // Esperate por tiempo indefinido
}

/*
Insterrupt Service Routine
 */
void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

I want to send every minute the value of the water flow sensor, but I just receive 1 SMS message, maybe something is wrong with the code.

I'll apreciate your help with this problem.

Thanks.

1

1 Answers

0
votes

Hi the problem is the last while in the code, delete the line.

while(1); // Esperate por tiempo indefinido