如何在每一段时间内读取arduino水流量传感器的流量?

时间:2016-11-15 15:45:29

标签: arduino-uno sim900

我正在尝试用arduino读取或确定水流量。此刻,我已经联系了:

  1. Arduino UNO。
  2. Arduino GSM SIM900模块
  3. Arduino水流量传感器
  4. 这是我的代码:

    #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++;
    }
    

    我想每分钟发送水流量传感器的值,但我只收到1条短信,可能是代码有问题。

    我会帮助解决这个问题。

    感谢。

1 个答案:

答案 0 :(得分:0)

嗨,问题是代码中的最后一次,删除该行。

,而(1); // Esperate por tiempo indefinido