Test

Revision:
5:70b84276766a
Parent:
4:928351381957
Child:
6:b38819d5db73
--- a/main.cpp	Wed Mar 23 17:38:04 2022 +0000
+++ b/main.cpp	Fri Mar 25 21:06:34 2022 +0000
@@ -1,28 +1,25 @@
 #include "mbed.h"
+#include <string>
+#define DEBUG 1
 
 /* !!!Change pins according to your connection!!! */
-BufferedSerial uart(D10,D2, 115200); // UART TX, RX
+BufferedSerial uart(PD_5,PD_6, 115200); // UART TX, RX
 ATCmdParser module(&uart,"\r\n");
 
+bool modulInit(int simPin = 0);
+bool sendSMS(string phoneNum, string text);
 
 // setup
 int main() {
     printf("Mbed A9G test\n");
     module.debug_on(true);
-    bool result = false;
-    for(int i=0;i<5;i++){
-        module.send("AT");
-        thread_sleep_for(500);
-        result =  module.recv("OK");
-        if(result){
-            break;
+    module.set_timeout(1000);
+    
+    if(modulInit()){
+        if(sendSMS("+123123456789", "Testerino")){
+            printf("SMS sended\n");
         }
     }
-    if(result){
-        printf("Module is up!\n");  
-    } else {
-        printf("No communication with module!\n");  
-    }
 
     // loop
     while(true)
@@ -30,4 +27,88 @@
         //do something in loop
         thread_sleep_for(1000);
     }
+}
+
+bool modulInit(int simPin){
+    bool result = true;
+    // power on
+    for(int i=0;i<5;i++){
+        module.send("AT");
+        thread_sleep_for(500);
+        if(module.recv("OK")){
+            debug_if(DEBUG, "Module is up!\n");  
+            break;
+        } else {
+            debug_if(DEBUG, "No communication with module!\n");
+            return false;  
+        }
+    }
+    // SIM autentication
+    if(simPin != 0){
+        module.send("AT+CPIN=%d", simPin);
+        if(module.recv("OK")){
+            debug_if(DEBUG, "Sim unlocked\n");  
+        } else {
+            debug_if(DEBUG, "SIM locked!\n"); 
+            return false;
+        }
+    }
+    
+    // Wait for network registration
+    string network_status[] = { "not registered", 
+                                "not searching", 
+                                "registered, home network", 
+                                "not registered, but is searching", 
+                                "registration denied", 
+                                "unknown", 
+                                "registered, roaming"};
+    int state = 0;
+    debug_if(DEBUG, "Connecting to network...\n");
+    module.set_timeout(10000);
+    for(int i=0;i<5;i++){
+        module.recv("+CREG: %d",&state);
+        if(state == 1)break;
+        debug_if(DEBUG, "State: %s...\n",network_status[state].c_str());
+        thread_sleep_for(500);
+    }  
+    if(state == 1){
+        debug_if(DEBUG, "Connected\n");
+    }else{
+        debug_if(DEBUG, "Connection failed!\n");
+        return false;
+    }
+    thread_sleep_for(1000);
+    module.set_timeout(1000);    
+    return true;
+}
+
+bool sendSMS(string phoneNum, string text){
+    bool result = true;
+    // set mode to text mode
+    int mode = 1;
+    module.send("AT+CMGF=%d", mode);
+    if(module.recv("OK")){
+        debug_if(DEBUG, "Sms mode was set to %d\n", mode);  
+    } else {
+        debug_if(DEBUG, "Set mode failed!\n");  
+    }
+    
+    module.set_timeout(5000);
+    module.send("AT+CMGS=\"%s\"", phoneNum.c_str());
+    if(module.recv(">")){
+        debug_if(DEBUG, "Sms mode was set to %d\n", mode); 
+        module.send("%s\n%c", text.c_str(), 0x1A); 
+        int id = 0;
+        if(module.recv("+CMGS: %d", &id)){
+            debug_if(DEBUG, "Sms was send to %s under id: %d \n",phoneNum.c_str(), id); 
+        } else {
+            debug_if(DEBUG, "Sms send failed\n"); 
+            result = false;
+        }
+    } else {
+        debug_if(DEBUG, "Sms send failed\n");
+        result = false;
+    }
+    module.set_timeout(1000);  
+    return result;
 }
\ No newline at end of file