#include <SoftwareSerial.h>
#include "Wire.h";
#include "LiquidCrystal_I2C.h";
// Define o endereço utilizado pelo Adaptador I2C
LiquidCrystal_I2C lcd(0x27, 20, 4);
SoftwareSerial serialGSM(3, 2); // RX, TX
bool temSMS = false;
String telefoneSMS;
String dataHoraSMS;
String mensagemSMS;
String comandoGSM = "";
String ultimoGSM = "";
const int motionPin = 13;
// Constantes:
const int REED = 6;
// Variáveis:
int val = 0;
int old_val = 0;
int REEDCOUNT = 0;
const int buttonPin = 2;
#define numeroComando "+55XXXXXXXXXXX" //numero para validacao e inicio dos comandos.
void leGSM();
void enviaSMS(String telefone, String mensagem);
void configuraGSM();
void setup() {
Serial.begin(9600);
serialGSM.begin(9600);
pinMode(motionPin, INPUT_PULLUP); //motion sensor
pinMode (REED, INPUT_PULLUP); //This activates the internal pull up resistor
Serial.println("Sketch Iniciado!");
configuraGSM();
//Inicializa o LCD e o backlight
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0); // Posiciona o cursor
lcd.print("Pulsos: "); // Escreve no LCD "Pulsos: "
lcd.setCursor(0, 1); // Posiciona o cursor
lcd.print("Chuva: "); // Escreve no LCD "Chuva:
}
void loop() {
val = digitalRead(REED); // Lê o Status do Reed Switch
int motion = digitalRead(motionPin);
if ((motion == HIGH) && (old_val == HIGH)) { // Verefica se o Status mudou
delay(10); // Atraso colocado para lidar com qualquer "salto" no switch.
REEDCOUNT = REEDCOUNT + 1; // Adiciona 1 à cntagem de pulsos
old_val = val; //Iguala o valor antigo com o atual
// Imprime no Monitor Serial
Serial.print("Medida de chuva (contagem): ");
Serial.print(REEDCOUNT);
Serial.println(" pulso");
Serial.print("Medida de chuva (calculado): ");
Serial.print(REEDCOUNT * 0.50);
Serial.println(" mm");
// Imprime as informações do Display
lcd.setCursor(8, 0); //Posiciona o cursor
lcd.print(REEDCOUNT); //Escreve o número de Pulsos
lcd.setCursor(8, 1); //Posiciona o cursor
lcd.print(REEDCOUNT * 0.50); //Escreve o valor em milímetros
lcd.setCursor(14, 1); //Posiciona o cursor
lcd.print("mm"); //Escreve no LCD "mm"
}
leGSM();
if (comandoGSM != "") {
Serial.println(comandoGSM);
ultimoGSM = comandoGSM;
comandoGSM = "";
}
if ((val == HIGH) && (old_val == LOW)) { // Verefica se o Status mudou
delay(10); // Atraso colocado para lidar com qualquer "salto" no switch.
REEDCOUNT = REEDCOUNT + 1; // Adiciona 1 à cntagem de pulsos
old_val = val; //Iguala o valor antigo com o atual
// Imprime no Monitor Serial
Serial.print("Medida de chuva (contagem): ");
Serial.print(REEDCOUNT);
Serial.println(" pulso");
Serial.print("Medida de chuva (calculado): ");
Serial.print(REEDCOUNT * 0.50);
Serial.println(" mm");
// Envia sms para o celular
enviaSMS(numeroComando, "Medida de chuva (contagem): ");
enviaSMS(numeroComando, "Medida de chuva (calculado): ");
delay(1000);
// Imprime as informações do Display
lcd.setCursor(8, 0); //Posiciona o cursor
lcd.print(REEDCOUNT); //Escreve o número de Pulsos
lcd.setCursor(8, 1); //Posiciona o cursor
lcd.print(REEDCOUNT * 0.50); //Escreve o valor em milímetros
lcd.setCursor(14, 1); //Posiciona o cursor
lcd.print("mm"); //Escreve no LCD "mm"
}
else {
old_val = val; //If the status hasn't changed then do nothing
}
}
void leGSM()
{
static String textoRec = "";
static unsigned long delay1 = 0;
static int count=0;
static unsigned char buffer[64];
if (serialGSM.available()) {
while(serialGSM.available()) {
buffer[count++] = serialGSM.read();
if(count == 64)break;
}
textoRec += (char*)buffer;
delay1 = millis();
for (int i=0; i<count; i++) {
buffer[i]=NULL;
}
count = 0;
}
if ( ((millis() - delay1) > 100) && textoRec != "" ) {
if ( textoRec.substring(2,7) == "+CMT:" ) {
temSMS = true;
}
if (temSMS) {
telefoneSMS = "";
dataHoraSMS = "";
mensagemSMS = "";
byte linha = 0;
byte aspas = 0;
for (int nL=1; nL < textoRec.length(); nL++) {
if (textoRec.charAt(nL) == '"') {
aspas++;
continue;
}
if ( (linha == 1) && (aspas == 1) ) {
telefoneSMS += textoRec.charAt(nL);
}
if ( (linha == 1) && (aspas == 5) ) {
dataHoraSMS += textoRec.charAt(nL);
}
if ( linha == 2 ) {
mensagemSMS += textoRec.charAt(nL);
}
if (textoRec.substring(nL - 1, nL + 1) == "\r\n") {
linha++;
}
}
} else {
comandoGSM = textoRec;
}
textoRec = "";
}
}
void enviaSMS(String telefone, String mensagem) {
serialGSM.print("AT+CMGS=\"" + telefone + "\"\n");
serialGSM.print(mensagem + "\n");
serialGSM.print((char)26);
}
void configuraGSM() {
serialGSM.print("AT+CMGF=1\n;AT+CNMI=2,2,0,0,0\n;ATX4\n;AT+COLP=1\n");
}