/*############################################################# Program Name : ex1_SIM808 Author : Grant Phillips Date Modified : 21/07/2016 Compiler : ARMmbed Tested On : STM32F4-Discovery Description : Example that demonstrate the use of a SIM808 GSM module to receive SMS instructions from another mobile device and also to send SMS information to another mobile device. With this example the user can send the following text SMS messages to the STM32 device: "Out1=1" - turns LED1 on STM32 device ON "Out1=0" - turns LED1 on STM32 device OFF "Out2=1" - turns LED2 on STM32 device ON "Out2=0" - turns LED12 on STM32 device OFF "Analog?" - requests the STM32 device to reply with a SMS to indicate the value of the analog input pin. The Serial Wire Viewer (SWV) is used to show the status of the communications and program, but is not required for the operation of the SIM808 module. See the SWV examples for an explanation of the use of this feature. Requirements : * STM32F4-Discovery Board Circuit : * a potentiometer (POT) connected to PC1 * LEDs connected to PD_12 and PD_13 * The SIM808 module is connected as follows: VCC/5V - 5V GND - GND RXD - PA2 (F4's TX pin) TXD - PA3 (F4's RX pin) * A wire link between PB3 and pin6 of the SWD connector (CN2) as explained in the SWV examples section ##############################################################*/ #include "mbed.h" #include "SWO.h" #include "Adafruit_FONA.h" Adafruit_FONA gsm(PA_2, PA_3, PD_15, PE_8); //Create a Adafruit_FONA object called gsm // TX RX RST RI //Not all SIM808-based boards have a RST (Reset) and RI (Ring Indicator) pin, //but when initializing the object one needs to specify them, even if they //are not there. In our case we just used two unused pins (PD_15 & PE_8) as "dummy" pins. SWO_Channel swo; //serial wire viewer object for debug information Ticker t; //Ticker object to create timer interrupt for checking SMSes DigitalOut OUT1(PD_12); //output pin for first output DigitalOut OUT2(PD_13); //output pin for second output AnalogIn ain(PC_1); //analog input pin int checkflag=0; //flag to indicate when to check for new SMSes //timer interrupt that instruct main to check the number of new SMSes every 2.5 seconds void timerinterruptroutine() { checkflag = 1; } int main() { int success, i, n; uint16_t analogval; uint16_t buflen; char buf[500]; char smsbuf[40]; swo.printf("Program Started...\n"); /* create connection to SIM808 */ swo.printf("Connecting to SIM808...\n"); success = gsm.begin(9600); //connect to SIM808 device at 9600 baud rate and return if successful(=1) or not (=0) if(success == 1) swo.printf("SIM808 detected successful...\n"); else { swo.printf("SIM808 not detected!\n"); while(1); //program stops/freezes at this point if SIM808 not detected } /* delete the first 10 received SMS messages currently stored on SIM808 to allow for new messages*/ swo.printf("Deleting stored SMS messages..."); for(i=1; i<=10;i++) { gsm.deleteSMS(i); } swo.printf("deleted\n"); t.attach(&timerinterruptroutine, 2.5); //call toggleroutine every 2.5 seconds (you can change this interval) while(1) { /* read the analog input */ analogval = ain.read_u16(); //read the analog input value /* check for new SMS messages */ if(checkflag==1) { //if 2.5 sec passed n = gsm.getNumSMS(); //get the number of new SMS messages checkflag = 0; //reset the flag } /* check if a new SMS or SMSes was received and then decode and apply the messages */ if(n > 0) { //if a new SMS was received for(i=1; i<=n; i++) { //start reading and decoding from first up to last SMS received gsm.readSMS(i, buf, 1000, &buflen); //read the SMS from slot i and store in string buf; buflen indicates the length of the stored SMS buf[buflen]='\0'; //terminate the string with NULL gsm.deleteSMS(i); //delete the SMS from the SIM808's memory swo.printf("SMS instruction received: %s\n", buf); if(strcmp(buf, "Out1=1") == 0) //if SMS instruction is equal to "Out1=1" OUT1=1; else if(strcmp(buf, "Out1=0") == 0) OUT1=0; else if(strcmp(buf, "Out2=1") == 0) OUT2=1; else if(strcmp(buf, "Out2=0") == 0) OUT2=0; else if(strcmp(buf, "Analog?") == 0) { //if SMS instruction is a request to get the analog value swo.printf("Sending SMS Reply to request: Analog value = %d\n", analogval); sprintf(smsbuf, "Analog value = %d", analogval); //print to a string smsbuf gsm.sendSMS("+27841234567", smsbuf); //sms the smsbuf string to a mobile number } else swo.printf("Invalid instruction!\n"); } n = 0; //reset the number of new SMS messages counter } } }