Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed DebouncedIn
main.cpp
- Committer:
- omatorressan
- Date:
- 2014-12-05
- Revision:
- 0:1230a968d925
File content as of revision 0:1230a968d925:
#include "mbed.h"
#include "DebouncedIn.h"
#include "stdio.h"
#include "string.h"
Timer t;
DigitalOut LedVerde(LED2);
DigitalOut LedRojo(LED1);
DigitalOut LedAzul(LED3);
DebouncedIn button1(PTC12); //inicia el envio
Serial GSM(PTE0,PTE1); //Configura puerto UART de la FRDMKL25Z
Serial pc(USBTX,USBRX);//Configura puerto USB a la consola serial del PC conectado.
void Rx_interrupt();
int position=0;
int lenpack=6;
int longi=0;
char buffer1[13];
char mensaje[100];
char NUMBER[13];
char tel[10];
char DE[50];
char buffer[512];
char buffermsg[72];
int index;
int count;
int i = 0;
int c=0;
int cont=0;
unsigned char CtrlZ = 0x1A; // comodin
bool Flag = false; // Bandera
char r[]=""; //Cadena de recepción
char msg[256];
char char1; //Flush serial para el buffer
void FlushGSM(void) {
char1 = 0;
while (GSM.readable()){
char1 = GSM.getc();}
return;}
void callback() {
pc.printf("%c\n", GSM.getc());
}
int readBuffer(char *buffer,int count)
{
int i=0;
t.start(); // inicia el timmer
while(1) {
while (GSM.readable()) {
char c = GSM.getc();
if (c == '\r' || c == '\n') c = '$';
buffer[i++] = c;
if(i > count)break;
}
if(i > count)break;
if(t.read() > 3) {
t.stop();
t.reset();
break;
}
}
wait(0.5);
while(GSM.readable()) { // display the other thing..
char c = GSM.getc();
}
return 0;
}
void cleanBuffer(char *buffer, int count)
{
for(int i=0; i < count; i++) {
buffer[i] = '\0';
}
}
/* la funcion envia un comando parametrizado
*/
void sendCmd(char *cmd)
{
GSM.puts(cmd);
}
/* la funcion espera la respuesta de un comando que debe ser identica a la cadena "resp" y un tiempo timeout"
si todo sale bien retorna un cero que en la programacion hay que validar
si algo sale mal ( no se parece o se demora mucho )retorna -1 que debera validarse con alguna expresion logica
*/
//
int waitForResp(char *resp, int timeout)
{
int len = strlen(resp);
int sum=0;
t.start();
while(1) {
if(GSM.readable()) {
char c = GSM.getc();
sum = (c==resp[sum]) ? sum+1 : 0;//sum se incrementa o se hace cero segun c
if(sum == len)break; //termina
}
if(t.read() > timeout) { // time out revisa el tiempo minimo antes de salir perdiendo
t.stop();
t.reset();
return -1;
}
}
t.stop(); // stop timer antes de retornar
t.reset(); // clear timer
while(GSM.readable()) { // display the other thing..
char c = GSM.getc();
}
return 0;
}
/* esta funcion de abajo es muy completa e util se encarga de enviar el comando y esperar la respuesta
si todo sale bien retorna un cero(herencia de las funciones contenedoras) que en la programacion hay que validar
con alguna expresion logica
*/
//
int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout)
{
sendCmd(cmd);
return waitForResp(resp,timeout);
}
/* esta funcion de abajo chequea que el modem este vivo envia AT y le contesta con OK y espera 2 segundos
*/
//
int powerCheck(void)// este comando se manda para verificar si el modem esta vivo o conectado
{
return sendCmdAndWaitForResp("AT\r\n", "OK", 2);
}
/* esta funcion de abajo chequea el estado de la sim card
y si todo sale bien retorna un cero que en la programacion hay que validar
con alguna expresion logica
*/
//
int checkSIMStatus(void)
{
char gprsBuffer[30];
int count = 0;
cleanBuffer(gprsBuffer,30);
while(count < 3) {
sendCmd("AT+CPIN?\r\n");
readBuffer(gprsBuffer,30);
if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
break;
}
count++;
wait(1);
}
if(count == 3) {
return -1;
}
return 0;
}
/* esta funcion de abajo chequea la calidad de la señal
y si todo sale bien retorna cun el valor de señal util o un -1 si nop es aceptable, en la programacion hay que validar
con alguna expresion logica
*/
//
int checkSignalStrength(void)
{
char gprsBuffer[100];
int index,count = 0;
cleanBuffer(gprsBuffer,100);
while(count < 3) {
sendCmd("AT+CSQ\r\n");
readBuffer(gprsBuffer,25);
if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) {
break;
}
count++;
wait(1);
}
if(count == 3) {
return -1;
}
return index;
}
/* la funcion configura el modem con comandas AT
y si se completa el proceso retorna un cero que en la programacion hay que validar
con alguna expresion logica pero si no, retorna un -1
*/
//
int settingSMS() //esta funcion se invoca para configurar el modem al principio
{
if(0 != sendCmdAndWaitForResp("AT\r\n", "OK", 1)) {
return -1;
}
if(0 != sendCmdAndWaitForResp("AT+CNMI=1,1\r\n", "OK", 1)) {
return -1;
}
if(0 != sendCmdAndWaitForResp("AT+CMGF=0\r\n", "OK", 1)) {
return -1;
}
if(0 != sendCmdAndWaitForResp("ATE\r\n", "OK", 1)) {
return -1;
}
if(0 != sendCmdAndWaitForResp("CBST=0,0,1\r\n", "OK", 1)) {
return -1;
}
return 0;
}
/* la funcion que inicaliza el modem se compone de un grupo de subfunciones
primero chequea que este "vivo"
segundo chequea el estado de la simcard
tercero chequea la intencidad de señal celular
cuarto aplica la configuracion
y si todo sale bien retorna un cero que en la programacion hay que validar
con alguna expresion logica
*/
//
int init()
{
for(int i = 0; i < 3; i++){
sendCmdAndWaitForResp("AT\r\n", "OK", 1);
wait(0.5);
}
if(0 != checkSIMStatus()) {
return -1;
}
if(checkSignalStrength()<1) {
return -1;
}
GSM.attach(&Rx_interrupt, Serial::RxIrq);
return 0;
}
/* la funcion intenta leer un mensaje de texto en formato PDU o HEX
y si todo sale bien retorna un cero que en la programacion hay que validar
con alguna expresion logica
*/
//
int readSMSpdu(char *message, int index)
{
int i = 0;
char gprsBuffer[100];
char *p,*s;
GSM.printf("AT+CMGR=%d\r\n",index);
cleanBuffer(gprsBuffer,100);
readBuffer(gprsBuffer,100);
if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) {
return -1;
}
if(NULL != ( s = strstr(gprsBuffer,"+32"))) {
p = s + 6;
while((*p != '$')&&(i < 5)) {
message[i++] = *(p++);
}
message[i] = '\0';
}
return 0;
}
/* la funcion borra mensajes SMS del modem
*/
//
int deleteSMS(int index)
{
char cmd[32];
snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
sendCmd(cmd);
return 0;
}
//
//
void Rx_interrupt(){
}
/*
readBuffer(buffer,6);
if(strncmp(buffer, "+CMTI", 5)){
wait(1);
GSM.printf("AT+CMGL=%d\r\n",0);
readBuffer(buffer,100);}
if(strncmp(buffer, "+CMGL:", 5)){
for (i=0;i<31;i++){
buffer[5+i]=c; // 32 de basura
}
}
for (i=0;i<10;i++){
buffer[36+i]=tel[i]; // numero telefonico
}
for (i=0;i<18;i++){
buffer[46+i]=c; // 18 de basura
}
for (i=0;i<2;i++){
buffer[64+i]=tel[i]; // tamaño de septetos
}
for (i=0;i<30;i++){
buffer[66+i]=DE[i]; // datos en octetos hex
if (buffer[i]=='\0'){
}
*/
int main(void)
{
//NVIC_DisableIRQ(UART1_IRQn);
//apagamos los 3 leds
LedVerde=1;
LedRojo=1;
LedAzul=1;
lenpack=6; //tamaño de "ALARMA"
GSM.baud(9600);//actualiza los baudios de la FRDMKL25Z en 9600
GSM.format(8,Serial::None,1); //actualiza el formato de los datos de la UART
//se configura el modem GSM
GSM.printf("AT\r\n");
wait(0.5);
GSM.printf("AT+CNMI=1,1\r\n");
wait(0.5);
GSM.printf("AT+CMGF=0\r\n");
wait(0.5);
GSM.printf("ATE\r\n");
wait(0.5);
GSM.printf("CBST=0,0,1\r\n");
wait(0.5);
LedVerde=0;
//if(0!= init()){ //se configura y chequea el modem GSM
// LedRojo=0;
//}
while(1){
if (button1.falling())
{ LedVerde=1;
wait(2);
if (!button1)
{LedVerde=0;
index=20;
//GSM.printf("AT+CNMI=1,1\r\n"); //configuracion inicial del MODEM!
GSM.printf("AT+CMGS=%d\r\n",index);
wait(0.2);
GSM.printf("0011000A91037130590900AA08416650DA0C8262"); //esto es "ALARMA 1 "
wait(0.5);
GSM.putc((char)0x1A); //esto es controlZ
LedVerde=1;
LedRojo=0; //se prende
wait(3);
LedRojo=1; // se apaga
LedVerde=0;
}
}
if (GSM.readable()) {
readBuffer(buffer,100);
pc.printf("buffer= %s\n\r ",buffer);
pc.printf("buffer= %c %c\n\r ",buffer[10],buffer[11]);
if(buffer[67]=='A'){for(i=0;i<86;i++)
{buffermsg[i]=buffer[i];}
pc.printf("mensaje= %s\n\r ",buffermsg);
pc.printf("mensaje[72]= %c mensaje[73]=%c\n\r ",buffermsg[72],buffermsg[73]);
buffer[67]='c';
}
if(buffer[10]=='S'&& buffer[11]=='M'){
for(i=0;i<5;i++)
{buffer1[i]=buffer[2+i];}
pc.printf("buffer1= %s\n\r ",buffer1);
buffer[10]='c';
buffer[11]='c';
}
if(buffer1[3]=='T'){pc.printf("AT+CMGL=0\n\r");
wait(0.5);
GSM.printf("AT+CMGL=0\r\n");
buffer1[3]='p';
}
if(buffermsg[70]=='6' && buffermsg[71]=='5'){LedAzul=0;}
if(buffermsg[70]=='6' && buffermsg[71]=='F'){LedAzul=1;}
}
}
}