如何在arduino uno中同时使用水流量传感器和GSM(SIM900)
How to use both water flow rate sensor and GSM(SIM900) in 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=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
相关文章:
- Android NDK传感器向事件队列报告奇怪的间隔
- 扩展光电二极管探测器以支持多个传感器
- 传感器值的阈值
- 无法获取webot::PositionSensor对象中位置传感器的值
- 具有非整数边容量的最大流量的Dinic算法
- 使用 pcap 捕获双 VLAN 流量
- 同步两个具有不同帧速率的传感器
- 环回中的嗅探会捕获外部流量吗?
- 如何将HTTPS流量重定向到本地主机:443上的代理?
- POST Arduino 传感器值通过 Flask API 发送到本地 Sqlite db
- Arduino Si7021 温度和湿度传感器代码中的错误
- 使用eBPF拦截Linux上的所有传出/传入流量
- 通过串行端口从GPS传感器读取
- 如何固定从传感器获得的变量值
- 当机器人车用超声波传感器(C++)检测到物体时,停止机器人车
- 为电容式触摸传感器设置延迟以防止"double press"
- 什么是英特尔实感的等效传感器
- 校准和归一化来自多个温度传感器的读数的算法
- 使用带有 PIR 传感器的米利斯检测连续运动
- 使用 Arduino 发送两种不同类型的传感器数据