int main() { char * AT = "AT\r\n";//Setting up a char variable AT for the a long string char response[20]; //A character buffer called response that can hold 20 characters int result = 0; //int result to check the value of the response sent back int transmit_send = 0; volatile int CurrentTick; //Volitile interger to hold the current tick count of the current time char * ptr; //Character char * stat; enum STATES {INIT}; enum STATES CurrentState = INIT; buffer_init(); hardware_init(); enable_UART0_receive_interrupt(); PIT_Configure_interrupt_mode(1); while(1) { switch(CurrentState) { case INIT: //Check connection to MODEM by sending AT. Expected response is OK result = send_command(AT, response, sizeof(response), 2000); if(result == SUCCESS || result == ERROR) { printf_response(response); } if(result == SUCCESS) //"OK" was returned by MODEM PRINTF("It Worked"); else //incorrect response or timeout. Delay and try again { CurrentTick = tick_count; while((tick_count - CurrentTick) < 5) {} } break; default: break; }//end switch-case } }
int main (){ int x; //char * ptr; char * AT = "\r\nAT\r\n"; char * PIN_CHECK = "\r\nAT+CPIN?\r\n"; char * ENTER_PIN = "\r\nAT+CPIN=\"1234\"\r\n"; char * CREG = "\r\nAT+CREG?\r\n"; char response[20]; char * ptr; enum STATES {INIT, CHECK_PIN, SEND_PIN, CHECK_NETWORK_REG, SEND_SMS, CONNECTED}; enum STATES CurrentState = INIT; hardware_init(); enable_UART0_receive_interrupt(); buffer_init(); PIT_Configure_interrupt_mode(1); //PIT interrupt every second enable_UART0_receive_interrupt(); PRINTF("\nGSM Modem Control\n"); //Configuration while(1) { switch(CurrentState) { case INIT: //Check connection to MODEM by sending AT. Expected response is OK PRINTF("Testing Modem Connection\n"); result = send_command(AT, response, sizeof(response), 10); if(result == SUCCESS || result == ERROR) { printf_response(response); } if(result == SUCCESS) //"OK" was returned by MODEM CurrentState = CHECK_PIN; else //incorrect response or timeout. Delay and try again { CurrentTick = tick_count; while((tick_count - CurrentTick) < 5) {} } break; case CHECK_PIN: //Check if SIM card is ready result = send_command(PIN_CHECK, response, sizeof(response), 10); if(result == SUCCESS || result == ERROR) { printf_response(response); } if(result == SUCCESS) //"OK" returned, check response string for "READY" or "SIM_PIN" { if(strstr(response, "READY")) //if ready ok is typed into terminal { CurrentState = CHECK_NETWORK_REG; //move to check_network_reg } else if(strstr(response, "SIM_PIN")) // sim_pin ok entered { CurrentState = SEND_PIN; //move to send_pin } } else CurrentState = INIT; break; case SEND_PIN: //Send PIN code. "OK" response if PIN is correct result = send_command(ENTER_PIN, response, sizeof(response), 10); if(result == SUCCESS || result == ERROR) { printf_response(response); } if(result==SUCCESS) //if logic '0' { //if(strstr(response, "")) CurrentState = CHECK_NETWORK_REG; //move to check_network_reg } else { CurrentState = CHECK_PIN; //if '-1' move back to check_pin again } break; case CHECK_NETWORK_REG: //check if registered on mobile network result = send_command(CREG, response, sizeof(response), 20); if(result == SUCCESS || result == ERROR) { printf_response(response); } if(result==SUCCESS) { if(strstr(response,"+CREG")) //if +creg entered { ptr = (char*)strchr(response,':');//point to the colon ptr+=4; //move 4 places switch(*ptr)//switch contents of address pointed to { case '1'://if +creg: 0,1 entered CurrentState = INIT;// move back to init PRINTF("Invalid;Try again"); break; case '2'://if +creg: 0,2 entered CurrentState = SEND_SMS;//move to send_sms PRINTF("Proceed to send SMS"); break; case '3'://if +creg: 0,3 entered CurrentTick = tick_count;//wait for "network" while((tick_count - CurrentTick) < 5) {} PRINTF("Checking for Network, Please Wait"); break; case '4'://if +creg: 0,4 entered CurrentState = INIT;//move back to init PRINTF("Invalid,Try Again"); break; case '5': //if +creg: 0,5 entered CurrentState = SEND_SMS;//send sms PRINTF("Proceed to send SMS"); break; default: CurrentState = INIT; PRINTF("Invalid,Try Again"); break; } } else { CurrentState = INIT; } printf_response(response); break; case SEND_SMS: //Send a text message send_sms("\"0871234567\"","\"Testing 123\""); //send number send_sms("\"0171112233\"","\"Success\""); // the message if(result==SUCCESS) // if logic '0' { PRINTF("Message has been sent"); CurrentState = CONNECTED;//move to connected break; } else { CurrentTick = tick_count;// wait for input while((tick_count - CurrentTick) < 5) {} break; } case CONNECTED: while(1) //dummy loop {} break; default: break; }//end switch-case }//end while(1) } }