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

How to use both water flow rate sensor and GSM(SIM900) in arduino uno?

本文关键字:传感器 流量 GSM SIM900 arduino uno      更新时间:2023-10-16
#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=1r");    //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=1r");    //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/hourrn"); //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将无法工作。我尝试了很多方法来解决它,但仍然没有成功。

我也有同样的问题。解决方案是停止运行time2中断来发送消息。

编辑你的代码:

void sendSMS2()
{
 cli();
   gsm_condition_1 = HIGH;
   SIM900.print("AT+CMGF=1r");    //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=1r");应该在无效循环。最后一个问题是,你的sim 900将不发送整数。所以你需要把它作为字符串发送。请遵循以下示例:http://microembarcado.blogspot.com.au/p/wr-bridge-send-sms-with-temperature.html

我坐了大约4个小时才弄明白。