Jan Kamidra
/
Mbed_A9G_test
Test
Diff: main.cpp
- Revision:
- 7:90bb5b736fe4
- Parent:
- 6:b38819d5db73
--- a/main.cpp Tue Mar 29 18:34:56 2022 +0000 +++ b/main.cpp Fri Apr 08 21:59:26 2022 +0000 @@ -1,25 +1,17 @@ #include "mbed.h" -#include <string> -#define DEBUG 1 - -/* !!!Change pins according to your connection!!! */ -BufferedSerial uart(PD_5,PD_6, 115200); // UART TX, RX -ATCmdParser module(&uart,"\r\n"); - -bool modulInit(int simPin); -bool sendSMS(string phoneNum, string text); +#include "A9G.h" // setup int main() { printf("Mbed A9G test\n"); - module.debug_on(true); - module.set_timeout(1000); - - 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. - if(sendSMS("+123123456789", "Testerino")){ // replace +123123456789 with destination Phone number - printf("SMS sended\n"); - } + + A9G a9g(PD_5,PD_6,true); + if(a9g.init()){ + if(a9g.sendSMS("+123123456789", "Hello!")) printf("sendSms success\n"); + else printf("sendSms failed\n"); } + else printf("module init failed\n"); + // loop while(true) @@ -27,90 +19,4 @@ //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++){ - 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 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(10000); - 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