Example code to do an action based on a received SMS. WARNING: this code has been written in a hurry during an hackathon. It's total crap.
Dependencies: GPS_CanSat_gen mbed
Example code to do an action based on a received SMS. WARNING: this code has been written in a hurry during an hackathon. It's total crap.
main.cpp
- Committer:
- gipmad
- Date:
- 2015-09-19
- Revision:
- 0:14bb407a4bdf
File content as of revision 0:14bb407a4bdf:
#include "mbed.h" #include "GPS.h" //#include "gsm.h" Serial pc(SERIAL_TX, SERIAL_RX); //v0.2 Serial GSM(PC_10, PC_11); GPS g1(PB_6, PB_7); DigitalOut myled(PB_2); DigitalOut GSMsw(PA_15); DigitalOut DET(PB_13); float LAT; float LON; int flag_gsm_get = 0; int flag_gsm_getend = 0; int count = 0; char msg[128]; void getline() { char temp; temp = GSM.getc(); pc.putc(temp); if((temp == '+') || (temp == 'D')){ flag_gsm_get = 1; count = 0; } if(flag_gsm_get) { // pc.printf(".%d%c.",count,temp); msg[count] = temp; if(temp == '\r'){ msg[count] = '\0'; flag_gsm_getend = 1; //pc.printf(".%s.",msg); flag_gsm_get = 0; } count ++; } } int SMSid = -1; int Bum = 0; void GSMin() { getline(); if(flag_gsm_getend == 1) { if(sscanf(msg, "+CMTI: \"SM\",%d", &SMSid) >= 1) { pc.printf("Got SMS! ID: %d\n", SMSid); } else if(strstr(msg, "DETONATE") != NULL) { pc.printf("Scoppia!\n"); Bum = 55; } flag_gsm_getend = 0; } } int main() { DET = 0; /*while(1) { DET = 1; wait(5); DET = 0; wait(1); } */ pc.baud(115200); //GPS.baud(9600); GSM.baud(115200); GSM.attach(&GSMin); int i = 1; pc.printf("Hello World !\n"); ///INIT myled = 1; GSMsw = 1; wait(15); myled = 0; pc.printf("ON\n"); //init GSM.printf("AT+CMGF=1\n"); wait(1); while(1) { //if(GSM.readable()) // pc.putc(GSM.getc()); if(pc.readable()) GSM.putc(pc.getc()); if(SMSid != -1) { GSM.printf("AT+CMGR=%d\n",SMSid); wait(1); GSM.printf("AT+CMGD=%d\n",SMSid); wait(1); SMSid = -1; } if(Bum == 55) { pc.printf("DETON ON\n"); myled = 1; DET = 1; wait(10); DET = 0; myled = 0; Bum = 0; } /*if(GPS.readable()) pc.putc(GPS.getc()); if(pc.readable()) GPS.putc(pc.getc());*/ /*wait(1); pc.printf("ns: %d\r\n", g1.ns()); pc.printf("ew: %d\r\n", g1.ew()); pc.printf("lock: %d\r\n", g1.lock()); pc.printf("t: %f\r\n", g1.time()); if(g1.lock() == 0) { pc.printf("No fix; faking it...\r\n"); LAT = 45.564411; LON = 12.428018; } else { LAT = g1.latitude(); LON = g1.longitude(); } pc.printf("lat: %f\r\n", LAT); pc.printf("lon: %f\r\n", LON);*/ } }