如何在arduino uno中同时使用水流量传感器和GSM(SIM900)?

时间:2015-05-16 14:00:36

标签: c++ arduino-uno

#include <SoftwareSerial.h>

SoftwareSerial SIM900(7, 8);

String outMessage1 = "Hello Arduino";
String outMessage2 = "Arduino2";

volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;                               
int hallsensor = 2;    //The pin location of the sensor
int condition_1 = LOW; 
int condition_2 = LOW;
int gsm_condition_1 = HIGH; 
int gsm_condition_2 = HIGH;
String destinationNumber = "+6014681xxxx";

void rpm ()     //This is the function that the interupt calls 
{ 
    NbTopsFan++;  //This function measures the rising and falling edge of            the hall effect sensors signal
} 
// The setup() method runs once, when the sketch starts
void setup() //
{ 
    Serial.begin(9600); //This is the setup function where the serial port   is initialised,
    SIM900.begin(19200); 
    SIM900power();
    delay(20000);

    pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
    pinMode(13, OUTPUT);

     attachInterrupt(0, rpm, RISING); //and the interrupt is attached
} 
// the loop() method runs over and over again,
// as long as the Arduino has power

void SIM900power()
{
    digitalWrite(9,HIGH);
    delay(1000);
    digitalWrite(9,LOW);
    delay(5000);
}


void sendSMS1()
{
    gsm_condition_2 = HIGH;
    SIM900.print("AT+CMGF=1\r");    //AT command to send SMS message
    delay(100);
    SIM900.println("AT + CMGS = \"" + destinationNumber +"\"");    //recipient's mobile number, in international format
    delay(100);
    SIM900.println(outMessage1);    //message to send
    delay(100);
    SIM900.println((char)26);   //End AT command with a^Z, ASCII code 26 or  ctrl+z
    delay(100);
}

void sendSMS2()
{
    gsm_condition_1 = HIGH;
    SIM900.print("AT+CMGF=1\r");    //AT command to send SMS message
    delay(100);
    SIM900.println("AT + CMGS = \"" + destinationNumber +"\"");        //recipient's mobile number, in international format
    delay(100);
    SIM900.println(outMessage2);    //message to send
    delay(100);
    SIM900.println((char)26);   //End AT command with a^Z, ASCII code 26 or   ctrl+z
    delay(100);
}


void gsm_check()
{
    if (condition_1 == HIGH && gsm_condition_1 == HIGH) {

        condition_1 = LOW;
        gsm_condition_1 = LOW;
        sendSMS1();
    }
    else if ( condition_2 == HIGH && gsm_condition_2 == HIGH) {
        condition_2 = LOW;
        gsm_condition_2 = LOW;
        sendSMS2();
    }
}

void water_rate()
{
     NbTopsFan = 0;      //Set NbTops to 0 ready for calculations
     sei();            //Enables interrupts
     delay (1000);      //Wait 1 second
     cli();            //Disable interrupts
     Calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow   rate in L/hour 
     Serial.print (Calc, DEC); //Prints the number calculated above
     Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a  new line
}

void loop ()    
{
    water_rate();

    if ( Calc > 100 ) {
        digitalWrite(13, HIGH);
        condition_1 = HIGH;
    } 
    else {
        digitalWrite(13, LOW);
        condition_2 = HIGH;
    }
    gsm_check();
}

我不能一起使用水流量传感器和GSM(SIM900)。一旦我使用两者,那么GSM根本不起作用。我已经尝试了很多方法来解决它但仍未成功。希望你们能给我一些帮助。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。解决方案是停止运行时间2中断以发送消息。

编辑代码:

void sendSMS2()
{
 cli();
   gsm_condition_1 = HIGH;
   SIM900.print("AT+CMGF=1\r");    //AT command to send SMS message
   delay(100);
   SIM900.println("AT + CMGS = \"" + destinationNumber +"\"");           
   delay(100);
   SIM900.println(outMessage2);    //message to send
   delay(100);
   SIM900.println((char)26);   //End AT command with a^Z, ASCII code 26 or     ctrl+z
   delay(100);
   sei();
   }

问题是传感器只能使用中断读取,但是当中断激活时,sim900将不起作用。因此,您必须暂时禁用中断,然后继续。此外,SIM900.print("AT+CMGF=1\r");应该处于无效循环中。 最后一个问题是你的sim 900不会发送整数。所以你必须把它作为一个字符串发送。请按照以下示例:http://microembarcado.blogspot.com.au/p/wr-bridge-send-sms-with-temperature.html

我坐了大约4个小时才搞清楚。 希望这会让事情变得更加简单。