Here it goes...
Main:
/*------------------------ Headers -----------------------*/
#include <Arduino.h>
#include "SmsMenu.h"
#include "GsmModuleFunctions.h"
#include "GasSensorFunctions.h"
#include "AlarmFunctions.h"
/*------------------------------------------------------- */
/*-------------------- Defining Pins -------------------- */
/*------------------------------------------------------- */
/*------------------ Defining Constants ----------------- */
/*------------------------------------------------------- */
/*---------------------- Variables ---------------------- */
/*------------------------------------------------------- */
/*------------------ Functions' Scopes ------------------ */
/*------------------------------------------------------- */
void setup()
{
delay(100);
Serial.begin(115200); //debug
delay(500);
gsm_config();
//gas_sensors_config();
}
void loop()
{
//sensors_reading();
//check_alarm();
sms_check();
}
This is the lib for GSM functions
/*-------------------------- Headers --------------------------*/
#define TINY_GSM_MODEM_SIM800
#include <Arduino.h>
#include "TinyGsmClient.h"
/*-------------------------------------------------------------*/
/*---------------------- PINS to be used ----------------------*/
#define LED_GSM_INIT_STATUS 19
/*-------------------------------------------------------------*/
/*------------------------- Constants -------------------------*/
#define MAX_PHONES 5
/*-------------------------------------------------------------*/
/*---------------------- Functions Scope ----------------------*/
void sms_check();
void gsm_config();
String send_AT_command(String AT_command);
bool check_if_sms_received_sent(String *sms_received, String *sender_phone);
void check_sms_sender_phone(String sms_text_received, String *sms_sender_phone);
void check_sms_text(String *sms_text_received);
void execution(String command, String command_sender, int *index, int *list_index, String list[]);
void send_sms(String command_response, String command_sender);
/*-------------------------------------------------------------*/
/*------------------------- Variables -------------------------*/
TinyGsm gsm_modem(Serial2);
String sms_text = "";
String phone = "";
String phone_list[MAX_PHONES] = {"","","","",""};
int menu_index = 0;
int phone_list_index = 0;
/*-------------------------------------------------------------*/
void sms_check() {
String gsm_module_response = "";
if(check_if_sms_received_sent(&sms_text, &phone))
{
Serial.println("\nSMS Received...");
Serial.println("\nSender: " + phone );
Serial.println("Message:\n'" + sms_text + "'\n");
execution(sms_text, phone, &menu_index, &phone_list_index, phone_list);
Serial.println("Deleting all messages in memory.\n");
gsm_module_response = send_AT_command("AT+CMGD=1,4");
while(gsm_module_response.indexOf("OK") < 0)
{
Serial.println("Could not delete the messages\n");
delay(100);
}
Serial.print("Module response:" + gsm_module_response);
Serial.println("All messages deleted.\n");
}
}
void gsm_config()
{
String gsm_module_response = "";
pinMode(LED_GSM_INIT_STATUS, OUTPUT);
digitalWrite(LED_GSM_INIT_STATUS, LOW);
Serial2.begin(9600);
delay(500);
Serial.println("Start GSM module configuration...\n");
gsm_module_response = send_AT_command("AT&F0");
Serial.print("Module response:" + gsm_module_response);
delay(500);
while(!gsm_modem.restart())
{
Serial.println("\nWait the GSM modem to restart.");
delay(100);
}
Serial.println("\nGSM modem restart successfully!\n");
Serial.println("Setting the baud rate.\n");
gsm_module_response = send_AT_command("AT+IPR=9600");
while(gsm_module_response.indexOf("OK") < 0)
{
Serial.println("Cannot set the fixed baud rate at 9600.\n");
delay(100);
}
Serial.print("Module response:" + gsm_module_response);
Serial.println("Baud rate set to 9600.\n");
Serial.println("Selecting not to display URC for timestamp.\n");
gsm_module_response = send_AT_command("AT+CLTS=0");
while(gsm_module_response.indexOf("OK") < 0)
{
Serial.println("Could not configurate to not display URC for timestamp.\n");
delay(100);
}
Serial.print("Module response:" + gsm_module_response);
Serial.println("No URC for timestamp will be displayed.\n");
Serial.println("Setting how new SMS will be indicated.\n");
gsm_module_response = send_AT_command("AT+CNMI=2,0,0,0,0");
while(gsm_module_response.indexOf("OK") < 0)
{
Serial.println("Could not configure how new SMS indications will be indicated.\n");
delay(100);
}
Serial.print("Module response:" + gsm_module_response);
Serial.println("New SMS indication configuration set.\n");
Serial.println("Selecting SMS mode as Text Mode.\n");
gsm_module_response = send_AT_command("AT+CMGF=1");
while(gsm_module_response.indexOf("OK") < 0)
{
Serial.println("Could not configurate to Text Mode.\n");
delay(100);
}
Serial.print("Module response:" + gsm_module_response);
Serial.println("SMS text mode selected.\n");
Serial.println("Deleting all messages in memory.\n");
gsm_module_response = send_AT_command("AT+CMGD=1,4");
while(gsm_module_response.indexOf("OK") < 0)
{
Serial.println("Could not delete the messages\n");
delay(100);
}
Serial.print("Module response:" + gsm_module_response);
Serial.println("All messages deleted.\n");
Serial.println("GSM Module configuration finished");
digitalWrite(LED_GSM_INIT_STATUS, HIGH);
}
String send_AT_command(String AT_command) //function to send via UART to GSM Module the string with the AT command
{
String gsm_module_response = ""; //string to store the value read in UART from GSM Module
Serial.println("Sending command " + AT_command);
Serial2.println(AT_command); //Send the AT command via UART to GSM module
delay(500);
while(!Serial2.available()); //wait until a data is available on serial buffer
if(AT_command.indexOf("CMGS") > 0)
{
while (gsm_module_response.indexOf("CMGS") < 0)
{
gsm_module_response = Serial2.readString(); //read the data from the buffer and stores in the string variable
}
//Serial.println(gsm_module_response);
delay(100);
return gsm_module_response;
}
gsm_module_response = Serial2.readString(); //read the data from the buffer and stores in the string variable
//Serial.println(gsm_module_response);
delay(100);
return gsm_module_response;
}
bool check_if_sms_received_sent (String *sms_received, String *sender_phone)
{
*sms_received = send_AT_command("AT+CMGL=\"ALL\""); //AT command syntax: "AT+CMGL=(stat)" -> stat = "ALL" -> list all messages
//returns "+CMTI: SM" if a SMS is arriving in the listing
if((*sms_received).indexOf("+CMTI:\"SM\"") >= 0) //if a sms is arriving in the reading, do the listing again.
{
*sms_received = send_AT_command("AT+CMGL=\"ALL\"");
}
/*if((*sms_received).indexOf("CMGS") >= 0)
{
*sms_received = send_AT_command("AT+CMGL=\"ALL\"");
}*/
Serial.println(*sms_received);
//Serial.println((*sms_received).length());
if((*sms_received).indexOf("OK") >= 0)
{
if((*sms_received).length() <= 6) //6
{
Serial.println("No new messages to be read.\n");
return false;
}
else
{
Serial.println("Reading message.\n");
check_sms_sender_phone(*sms_received, *&sender_phone);
check_sms_text(*&sms_received);
return true;
}
}
Serial.println("Error with AT command... Could not check if it has SMS.\n");
return false;
}
void check_sms_sender_phone(String sms_text_received, String *sms_sender_phone)
{
String text_extracted = "";
int i = 0;
*sms_sender_phone = "";
if(sms_text_received.indexOf("CMGL") >= 0)
{
//SMS line example: +CMGL: 1,"REC UNREAD","+5518999999999","","18/11/30,11:36:14-08"
text_extracted = sms_text_received.substring(sms_text_received.indexOf(",") + 1);
text_extracted = text_extracted.substring(text_extracted.indexOf(",") + 2);
for(i = 0; (i < text_extracted.length()) && (text_extracted.charAt(i) != '"'); i++)
{
*sms_sender_phone = (*sms_sender_phone) + text_extracted.charAt(i);
}
}
else if(sms_text_received.indexOf("CMT") >= 0)
{
//SMS line example: +CMT: "+5541999999999","","21/09/12,14:35:24-12"
text_extracted = sms_text_received.substring(sms_text_received.indexOf('"') + 1);
for(i = 0; (i < text_extracted.length()) && (text_extracted.charAt(i) != '"'); i++)
{
*sms_sender_phone = (*sms_sender_phone) + text_extracted.charAt(i);
}
}
}
void check_sms_text (String *sms_text_received)
{
String text_extracted = "";
int i = 0;
if((*sms_text_received).indexOf("CMGL") >= 0)
{
/*
SMS received example syntax:
[\n
+CMGL: 1,"REC UNREAD","+5518999999999","","18/11/30,11:36:14-08"\n
Hello\n
\n
OK\n
]
*/
*sms_text_received = (*sms_text_received).substring((*sms_text_received).indexOf("\n") + 1);
*sms_text_received = (*sms_text_received).substring((*sms_text_received).indexOf("\n") + 1);
text_extracted = *sms_text_received;
*sms_text_received = "";
for(i = 0; i < (text_extracted.length() - 8); i++)
{
*sms_text_received = *sms_text_received + text_extracted.charAt(i);
}
}
else if((*sms_text_received).indexOf("CMT") >= 0)
{
/*
SMS received example syntax:
[\n
OK\n
\n
+CMT: "+5541984339642","","21/09/13,19:25:26-12"\n
Testando\n
\n
]
*/
*sms_text_received = (*sms_text_received).substring((*sms_text_received).indexOf("\n") + 1);
*sms_text_received = (*sms_text_received).substring((*sms_text_received).indexOf("\n") + 1);
*sms_text_received = (*sms_text_received).substring((*sms_text_received).indexOf("\n") + 1);
*sms_text_received = (*sms_text_received).substring((*sms_text_received).indexOf("\n") + 1);
text_extracted = *sms_text_received;
*sms_text_received = "";
for(i = 0; i <= (text_extracted.length() - 3); i++)
{
*sms_text_received = *sms_text_received + text_extracted.charAt(i);
}
}
}
void execution(String command, String command_sender, int *index, int *list_index, String list[])
{
String command_response = "";
int i;
command.toUpperCase();
if((*index) == 0)
{
if(command == "MENU")
{
command_response = main_menu;
//command_response = "Menu OK";
//*index = 0;
}
else if((command == "REGISTER") || (command == "1"))
{
command_response = menu_opt_1;
*index = 1;
}
else if((command == "LIST") || (command == "2"))
{
if((*list_index) == 0)
{
command_response = menu_opt_22;
}
else
{
command_response = menu_opt_2 + "\n";
for(i = 0; i < (*list_index); i++)
{
if(i == 0)
{
command_response = command_response + (i + 1) + " - " + list[i] + "\n";
}
}
Serial.println(command_response);
//*index = 0;
}
}
else if((command == "VSTATE") || (command == "3"))
{
command_response = menu_opt_3;
*index = 3;
}
else if((command == "VCLOSE") || (command == "4"))
{
command_response = menu_opt_4;
*index = 4;
}
else if((command == "ASTATE") || (command == "5"))
{
command_response = menu_opt_5;
*index = 5;
}
else if((command == "ADEACTIVE") || (command == "6"))
{
command_response = menu_opt_6;
*index = 6;
}
/*else if((command == "SVALUE") || (command == "7"))
{
command_response = menu_opt_7;
*index = 7;
}*/
/*else if((command == "DELETE") || (command == "8"))
{
command_response = menu_opt_8;
*index = 8;
}*/
else
{
command_response = not_valid;
}
}
else if((*index) == 1)
{
if(command.length() == 11)
{
command_response = menu_opt_11 + "+55" + command;
list[*list_index] = "+55" + command;
Serial.print("List index: ");
Serial.println(*list_index);
*index = 11;
}
else if(command == "0")
{
command_response = menu_opt_11111;
*index = 0;
}
else
{
command_response = menu_opt_111;
*index = 1;
}
}
else if ((*index) == 11)
{
if(command == "SIM")
{
command_response = menu_opt_1111;
(*list_index)++;
/*Serial.print("List index: ");
Serial.println(*list_index);*/
*index = 0;
}
else if(command == "NAO")
{
command_response = menu_opt_1;
list[*list_index] = "";
*index = 1;
}
else
{
command_response = not_valid;
}
}
send_sms(command_response, command_sender);
}
void send_sms(String command_response, String command_sender)
{
String module_response = "";
module_response = send_AT_command("AT+CMGS=\"" + command_sender + "\"\n" + command_response + "\n" + (char)26);
Serial.println(module_response);
}
Menu Strings lib:
` String main_menu = "Menu \n 1 - Registrar Numero \n 2 - Listar Numeros \n 3 - Estado da Valvula \n 4 - Fechar Valvula \n 5 - Estado Alarme \n 6 - Desativar Alarme \n 7 - Valor dos Sensores \n 8 - Deletar Numero";
String menu_opt_1 = "Inserir Telefone (DDD + Numero). Exemplo:41987654321, ou '0'(zero) para cancelar";
String menu_opt_11 = "O numero esta correto (sim ou nao)? ";
String menu_opt_111 = "Formato de numero invalido. Insira novamente, ou '0'(zero) para cancelar.";
String menu_opt_1111 = "Ok, numero registrado.";
String menu_opt_11111 = "Registro cancelado.";
String menu_opt_2 = "Telefones Registrados:";
String menu_opt_22 = "Nenhum telefone registrado.";
String menu_opt_3 = "Estado da Valvula:";
String menu_opt_4 = "Fechando a Valvula... Aguarde resposta";
String menu_opt_5 = "Estado do Alarme:";
String menu_opt_6 = "Desantivando Alarme... Aguarde resposta";
String menu_opt_7_co = "Concentração de CO: ";
String menu_opt_7_lpg = "Concentração de LPG: ";
String not_valid = "Opcao desconhecida.";`