Test

Files at this revision

API Documentation at this revision

Comitter:
JohnnyK
Date:
Fri Apr 08 22:00:00 2022 +0000
Parent:
7:90bb5b736fe4
Commit message:
sample library

Changed in this revision

A9G/A9G.cpp Show annotated file Show diff for this revision Revisions of this file
A9G/A9G.h Show annotated file Show diff for this revision Revisions of this file
diff -r 90bb5b736fe4 -r f5e79684b53c A9G/A9G.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/A9G/A9G.cpp	Fri Apr 08 22:00:00 2022 +0000
@@ -0,0 +1,92 @@
+#include "A9G.h"
+#include <string>
+
+A9G::A9G(PinName tx, PinName rx, bool atDebug, int baud):_uart(tx,rx,baud), _module(&_uart){
+    _module.debug_on(atDebug);
+};
+
+bool A9G::init(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++){
+        if(_module.recv("+CREG: %d",&state)){
+            if(state == 1)break;
+            debug_if(DEBUG, "State: %s...\n",network_status[state].c_str());
+        } else {
+            debug_if(DEBUG, "State: timeout\n");
+        }
+        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 A9G::sendSMS(const char *number, const char *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(10000);
+    _module.send("AT+CMGS=\"%s\"", number);
+    if(_module.recv(">")){
+        debug_if(DEBUG, "Sms mode was set to %d\n", mode); 
+        _module.send("%s\n%c", text, 0x1A); 
+        int id = 0;
+        if(_module.recv("+CMGS: %d", &id)){
+            debug_if(DEBUG, "Sms was send to %s under id: %d \n",number, 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
diff -r 90bb5b736fe4 -r f5e79684b53c A9G/A9G.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/A9G/A9G.h	Fri Apr 08 22:00:00 2022 +0000
@@ -0,0 +1,40 @@
+#ifndef A9G_H
+#define A9G_H
+#define DEBUG 1
+#include <stdio.h>
+#include "mbed.h"
+
+#define DEFAULT_TIMEOUT 5
+#define SMS_MAX_LENGTH 16
+
+class A9G
+{
+    public:
+        /** Create GSM instance
+        * @param tx uart transmit pin to communicate with GSM module
+        * @param rx uart receive pin to communicate with GSM module
+        * @param atDebug enable/disable debug output of ATCmdParser - default false
+        * @param baudRate baud rate of uart communication - default 115200
+        */
+        A9G(PinName tx, PinName rx, bool atDebug = false, int baudRate = 115200) ;
+
+        /** It Initializes/establish communication with module, unlock Sim card if it is necessary and wait for Network connection
+        *  @param  simPin  Pin code of Sim card for its unlocking if it necessary
+        *  @returns
+        *      true on success,
+        *      false on error
+        */
+        bool init(int simPin = 0);
+
+        /** It set text mode and send specific text to specific phone number
+        *  @param  *number     phone number as destination of SMS
+        *  @returns *text      text to be send via SMS
+        *      true on success,
+        *      false on error
+        */
+        bool sendSMS(const char *number, const char *text);
+    private:
+        BufferedSerial _uart;
+        ATCmdParser _module;
+};
+ #endif
\ No newline at end of file