我正在尝试使用 arduino 读取或确定水流。此刻我已经连接:
- Arduino UNO。
- Arduino GSM SIM900 模块
- Arduino水流传感器
这是我的代码:
#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 条短信,可能是代码有问题。
我会感谢您对这个问题的帮助。
谢谢。