Test

Committer:
JohnnyK
Date:
Tue Mar 29 18:34:56 2022 +0000
Revision:
6:b38819d5db73
Parent:
5:70b84276766a
Child:
7:90bb5b736fe4
Corrections

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 0:aaec057956ca 1 #include "mbed.h"
JohnnyK 5:70b84276766a 2 #include <string>
JohnnyK 5:70b84276766a 3 #define DEBUG 1
JohnnyK 0:aaec057956ca 4
JohnnyK 0:aaec057956ca 5 /* !!!Change pins according to your connection!!! */
JohnnyK 5:70b84276766a 6 BufferedSerial uart(PD_5,PD_6, 115200); // UART TX, RX
JohnnyK 0:aaec057956ca 7 ATCmdParser module(&uart,"\r\n");
JohnnyK 0:aaec057956ca 8
JohnnyK 6:b38819d5db73 9 bool modulInit(int simPin);
JohnnyK 5:70b84276766a 10 bool sendSMS(string phoneNum, string text);
JohnnyK 0:aaec057956ca 11
JohnnyK 0:aaec057956ca 12 // setup
JohnnyK 0:aaec057956ca 13 int main() {
JohnnyK 0:aaec057956ca 14 printf("Mbed A9G test\n");
JohnnyK 0:aaec057956ca 15 module.debug_on(true);
JohnnyK 5:70b84276766a 16 module.set_timeout(1000);
JohnnyK 5:70b84276766a 17
JohnnyK 6:b38819d5db73 18 if(modulInit(1234)){ // replace 1234 with your Pin code of your SIM card or replace it with 0, when your SIM have no Pin code.
JohnnyK 6:b38819d5db73 19 if(sendSMS("+123123456789", "Testerino")){ // replace +123123456789 with destination Phone number
JohnnyK 5:70b84276766a 20 printf("SMS sended\n");
JohnnyK 4:928351381957 21 }
JohnnyK 4:928351381957 22 }
JohnnyK 0:aaec057956ca 23
JohnnyK 0:aaec057956ca 24 // loop
JohnnyK 0:aaec057956ca 25 while(true)
JohnnyK 0:aaec057956ca 26 {
JohnnyK 0:aaec057956ca 27 //do something in loop
JohnnyK 0:aaec057956ca 28 thread_sleep_for(1000);
JohnnyK 0:aaec057956ca 29 }
JohnnyK 5:70b84276766a 30 }
JohnnyK 5:70b84276766a 31
JohnnyK 5:70b84276766a 32 bool modulInit(int simPin){
JohnnyK 5:70b84276766a 33 bool result = true;
JohnnyK 5:70b84276766a 34 // power on
JohnnyK 5:70b84276766a 35 for(int i=0;i<5;i++){
JohnnyK 5:70b84276766a 36 module.send("AT");
JohnnyK 5:70b84276766a 37 thread_sleep_for(500);
JohnnyK 5:70b84276766a 38 if(module.recv("OK")){
JohnnyK 5:70b84276766a 39 debug_if(DEBUG, "Module is up!\n");
JohnnyK 5:70b84276766a 40 break;
JohnnyK 5:70b84276766a 41 } else {
JohnnyK 5:70b84276766a 42 debug_if(DEBUG, "No communication with module!\n");
JohnnyK 5:70b84276766a 43 return false;
JohnnyK 5:70b84276766a 44 }
JohnnyK 5:70b84276766a 45 }
JohnnyK 5:70b84276766a 46 // SIM autentication
JohnnyK 5:70b84276766a 47 if(simPin != 0){
JohnnyK 5:70b84276766a 48 module.send("AT+CPIN=%d", simPin);
JohnnyK 5:70b84276766a 49 if(module.recv("OK")){
JohnnyK 5:70b84276766a 50 debug_if(DEBUG, "Sim unlocked\n");
JohnnyK 5:70b84276766a 51 } else {
JohnnyK 5:70b84276766a 52 debug_if(DEBUG, "SIM locked!\n");
JohnnyK 5:70b84276766a 53 return false;
JohnnyK 5:70b84276766a 54 }
JohnnyK 5:70b84276766a 55 }
JohnnyK 5:70b84276766a 56
JohnnyK 5:70b84276766a 57 // Wait for network registration
JohnnyK 6:b38819d5db73 58 string network_status[] = { "not registered, not searching",
JohnnyK 5:70b84276766a 59 "registered, home network",
JohnnyK 5:70b84276766a 60 "not registered, but is searching",
JohnnyK 5:70b84276766a 61 "registration denied",
JohnnyK 5:70b84276766a 62 "unknown",
JohnnyK 5:70b84276766a 63 "registered, roaming"};
JohnnyK 5:70b84276766a 64 int state = 0;
JohnnyK 5:70b84276766a 65 debug_if(DEBUG, "Connecting to network...\n");
JohnnyK 5:70b84276766a 66 module.set_timeout(10000);
JohnnyK 5:70b84276766a 67 for(int i=0;i<5;i++){
JohnnyK 6:b38819d5db73 68 if(module.recv("+CREG: %d",&state)){
JohnnyK 6:b38819d5db73 69 if(state == 1)break;
JohnnyK 6:b38819d5db73 70 debug_if(DEBUG, "State: %s...\n",network_status[state].c_str());
JohnnyK 6:b38819d5db73 71 } else {
JohnnyK 6:b38819d5db73 72 debug_if(DEBUG, "State: timeout\n");
JohnnyK 6:b38819d5db73 73 }
JohnnyK 5:70b84276766a 74 thread_sleep_for(500);
JohnnyK 5:70b84276766a 75 }
JohnnyK 5:70b84276766a 76 if(state == 1){
JohnnyK 5:70b84276766a 77 debug_if(DEBUG, "Connected\n");
JohnnyK 5:70b84276766a 78 }else{
JohnnyK 5:70b84276766a 79 debug_if(DEBUG, "Connection failed!\n");
JohnnyK 5:70b84276766a 80 return false;
JohnnyK 5:70b84276766a 81 }
JohnnyK 5:70b84276766a 82 thread_sleep_for(1000);
JohnnyK 5:70b84276766a 83 module.set_timeout(1000);
JohnnyK 5:70b84276766a 84 return true;
JohnnyK 5:70b84276766a 85 }
JohnnyK 5:70b84276766a 86
JohnnyK 5:70b84276766a 87 bool sendSMS(string phoneNum, string text){
JohnnyK 5:70b84276766a 88 bool result = true;
JohnnyK 5:70b84276766a 89 // set mode to text mode
JohnnyK 5:70b84276766a 90 int mode = 1;
JohnnyK 5:70b84276766a 91 module.send("AT+CMGF=%d", mode);
JohnnyK 5:70b84276766a 92 if(module.recv("OK")){
JohnnyK 5:70b84276766a 93 debug_if(DEBUG, "Sms mode was set to %d\n", mode);
JohnnyK 5:70b84276766a 94 } else {
JohnnyK 5:70b84276766a 95 debug_if(DEBUG, "Set mode failed!\n");
JohnnyK 5:70b84276766a 96 }
JohnnyK 5:70b84276766a 97
JohnnyK 6:b38819d5db73 98 module.set_timeout(10000);
JohnnyK 5:70b84276766a 99 module.send("AT+CMGS=\"%s\"", phoneNum.c_str());
JohnnyK 5:70b84276766a 100 if(module.recv(">")){
JohnnyK 5:70b84276766a 101 debug_if(DEBUG, "Sms mode was set to %d\n", mode);
JohnnyK 5:70b84276766a 102 module.send("%s\n%c", text.c_str(), 0x1A);
JohnnyK 5:70b84276766a 103 int id = 0;
JohnnyK 5:70b84276766a 104 if(module.recv("+CMGS: %d", &id)){
JohnnyK 5:70b84276766a 105 debug_if(DEBUG, "Sms was send to %s under id: %d \n",phoneNum.c_str(), id);
JohnnyK 5:70b84276766a 106 } else {
JohnnyK 5:70b84276766a 107 debug_if(DEBUG, "Sms send failed\n");
JohnnyK 5:70b84276766a 108 result = false;
JohnnyK 5:70b84276766a 109 }
JohnnyK 5:70b84276766a 110 } else {
JohnnyK 5:70b84276766a 111 debug_if(DEBUG, "Sms send failed\n");
JohnnyK 5:70b84276766a 112 result = false;
JohnnyK 5:70b84276766a 113 }
JohnnyK 5:70b84276766a 114 module.set_timeout(1000);
JohnnyK 5:70b84276766a 115 return result;
JohnnyK 0:aaec057956ca 116 }