Test

Committer:
JohnnyK
Date:
Fri Apr 08 22:00:00 2022 +0000
Revision:
8:f5e79684b53c
sample library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 8:f5e79684b53c 1 #include "A9G.h"
JohnnyK 8:f5e79684b53c 2 #include <string>
JohnnyK 8:f5e79684b53c 3
JohnnyK 8:f5e79684b53c 4 A9G::A9G(PinName tx, PinName rx, bool atDebug, int baud):_uart(tx,rx,baud), _module(&_uart){
JohnnyK 8:f5e79684b53c 5 _module.debug_on(atDebug);
JohnnyK 8:f5e79684b53c 6 };
JohnnyK 8:f5e79684b53c 7
JohnnyK 8:f5e79684b53c 8 bool A9G::init(int simPin){
JohnnyK 8:f5e79684b53c 9 bool result = true;
JohnnyK 8:f5e79684b53c 10 // power on
JohnnyK 8:f5e79684b53c 11 for(int i=0;i<5;i++){
JohnnyK 8:f5e79684b53c 12 _module.send("AT");
JohnnyK 8:f5e79684b53c 13 thread_sleep_for(500);
JohnnyK 8:f5e79684b53c 14 if(_module.recv("OK")){
JohnnyK 8:f5e79684b53c 15 debug_if(DEBUG, "module is up!\n");
JohnnyK 8:f5e79684b53c 16 break;
JohnnyK 8:f5e79684b53c 17 } else {
JohnnyK 8:f5e79684b53c 18 debug_if(DEBUG, "No communication with _module!\n");
JohnnyK 8:f5e79684b53c 19 return false;
JohnnyK 8:f5e79684b53c 20 }
JohnnyK 8:f5e79684b53c 21 }
JohnnyK 8:f5e79684b53c 22 // SIM autentication
JohnnyK 8:f5e79684b53c 23 if(simPin != 0){
JohnnyK 8:f5e79684b53c 24 _module.send("AT+CPIN=%d", simPin);
JohnnyK 8:f5e79684b53c 25 if(_module.recv("OK")){
JohnnyK 8:f5e79684b53c 26 debug_if(DEBUG, "Sim unlocked\n");
JohnnyK 8:f5e79684b53c 27 } else {
JohnnyK 8:f5e79684b53c 28 debug_if(DEBUG, "SIM locked!\n");
JohnnyK 8:f5e79684b53c 29 return false;
JohnnyK 8:f5e79684b53c 30 }
JohnnyK 8:f5e79684b53c 31 }
JohnnyK 8:f5e79684b53c 32
JohnnyK 8:f5e79684b53c 33 // Wait for network registration
JohnnyK 8:f5e79684b53c 34 string network_status[] = { "not registered, not searching",
JohnnyK 8:f5e79684b53c 35 "registered, home network",
JohnnyK 8:f5e79684b53c 36 "not registered, but is searching",
JohnnyK 8:f5e79684b53c 37 "registration denied",
JohnnyK 8:f5e79684b53c 38 "unknown",
JohnnyK 8:f5e79684b53c 39 "registered, roaming"};
JohnnyK 8:f5e79684b53c 40 int state = 0;
JohnnyK 8:f5e79684b53c 41 debug_if(DEBUG, "Connecting to network...\n");
JohnnyK 8:f5e79684b53c 42 _module.set_timeout(10000);
JohnnyK 8:f5e79684b53c 43 for(int i=0;i<5;i++){
JohnnyK 8:f5e79684b53c 44 if(_module.recv("+CREG: %d",&state)){
JohnnyK 8:f5e79684b53c 45 if(state == 1)break;
JohnnyK 8:f5e79684b53c 46 debug_if(DEBUG, "State: %s...\n",network_status[state].c_str());
JohnnyK 8:f5e79684b53c 47 } else {
JohnnyK 8:f5e79684b53c 48 debug_if(DEBUG, "State: timeout\n");
JohnnyK 8:f5e79684b53c 49 }
JohnnyK 8:f5e79684b53c 50 thread_sleep_for(500);
JohnnyK 8:f5e79684b53c 51 }
JohnnyK 8:f5e79684b53c 52 if(state == 1){
JohnnyK 8:f5e79684b53c 53 debug_if(DEBUG, "Connected\n");
JohnnyK 8:f5e79684b53c 54 }else{
JohnnyK 8:f5e79684b53c 55 debug_if(DEBUG, "Connection failed!\n");
JohnnyK 8:f5e79684b53c 56 return false;
JohnnyK 8:f5e79684b53c 57 }
JohnnyK 8:f5e79684b53c 58 thread_sleep_for(1000);
JohnnyK 8:f5e79684b53c 59 _module.set_timeout(1000);
JohnnyK 8:f5e79684b53c 60 return true;
JohnnyK 8:f5e79684b53c 61 }
JohnnyK 8:f5e79684b53c 62
JohnnyK 8:f5e79684b53c 63 bool A9G::sendSMS(const char *number, const char *text){
JohnnyK 8:f5e79684b53c 64 bool result = true;
JohnnyK 8:f5e79684b53c 65 // set mode to text mode
JohnnyK 8:f5e79684b53c 66 int mode = 1;
JohnnyK 8:f5e79684b53c 67 _module.send("AT+CMGF=%d", mode);
JohnnyK 8:f5e79684b53c 68 if(_module.recv("OK")){
JohnnyK 8:f5e79684b53c 69 debug_if(DEBUG, "Sms mode was set to %d\n", mode);
JohnnyK 8:f5e79684b53c 70 } else {
JohnnyK 8:f5e79684b53c 71 debug_if(DEBUG, "Set mode failed!\n");
JohnnyK 8:f5e79684b53c 72 }
JohnnyK 8:f5e79684b53c 73
JohnnyK 8:f5e79684b53c 74 _module.set_timeout(10000);
JohnnyK 8:f5e79684b53c 75 _module.send("AT+CMGS=\"%s\"", number);
JohnnyK 8:f5e79684b53c 76 if(_module.recv(">")){
JohnnyK 8:f5e79684b53c 77 debug_if(DEBUG, "Sms mode was set to %d\n", mode);
JohnnyK 8:f5e79684b53c 78 _module.send("%s\n%c", text, 0x1A);
JohnnyK 8:f5e79684b53c 79 int id = 0;
JohnnyK 8:f5e79684b53c 80 if(_module.recv("+CMGS: %d", &id)){
JohnnyK 8:f5e79684b53c 81 debug_if(DEBUG, "Sms was send to %s under id: %d \n",number, id);
JohnnyK 8:f5e79684b53c 82 } else {
JohnnyK 8:f5e79684b53c 83 debug_if(DEBUG, "Sms send failed\n");
JohnnyK 8:f5e79684b53c 84 result = false;
JohnnyK 8:f5e79684b53c 85 }
JohnnyK 8:f5e79684b53c 86 } else {
JohnnyK 8:f5e79684b53c 87 debug_if(DEBUG, "Sms send failed\n");
JohnnyK 8:f5e79684b53c 88 result = false;
JohnnyK 8:f5e79684b53c 89 }
JohnnyK 8:f5e79684b53c 90 _module.set_timeout(1000);
JohnnyK 8:f5e79684b53c 91 return result;
JohnnyK 8:f5e79684b53c 92 }