You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
5.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /**************************************************************
  2. *
  3. * TinyGSM Getting Started guide:
  4. * http://tiny.cc/tiny-gsm-readme
  5. *
  6. * NOTE:
  7. * Some of the functions may be unavailable for your modem.
  8. * Just comment them out.
  9. *
  10. **************************************************************/
  11. // Select your modem:
  12. #define TINY_GSM_MODEM_SIM800
  13. // #define TINY_GSM_MODEM_SIM808
  14. // #define TINY_GSM_MODEM_SIM900
  15. // #define TINY_GSM_MODEM_A6
  16. // #define TINY_GSM_MODEM_A7
  17. // #define TINY_GSM_MODEM_M590
  18. // #define TINY_GSM_MODEM_MC60
  19. // #define TINY_GSM_MODEM_MC60E
  20. // Set serial for debug console (to the Serial Monitor, speed 115200)
  21. #define SerialMon Serial
  22. // Set serial for AT commands (to the module)
  23. // Use Hardware Serial on Mega, Leonardo, Micro
  24. #define SerialAT Serial1
  25. // or Software Serial on Uno, Nano
  26. //#include <SoftwareSerial.h>
  27. //SoftwareSerial SerialAT(2, 3); // RX, TX
  28. //#define DUMP_AT_COMMANDS
  29. #define TINY_GSM_DEBUG SerialMon
  30. #define GSM_AUTOBAUD_MIN 9600
  31. #define GSM_AUTOBAUD_MAX 38400
  32. /*
  33. * Test enabled
  34. */
  35. #define TINY_GSM_USE_GPRS true
  36. #define TINY_GSM_USE_CALL true
  37. #define TINY_GSM_USE_SMS true
  38. #define TINY_GSM_USE_USSD true
  39. // powerdown modem after tests
  40. #define TINY_GSM_POWERDOWN false
  41. // set GSM PIN, if any
  42. #define GSM_PIN ""
  43. // Set phone numbers, if you want to test SMS and Calls
  44. //#define SMS_TARGET "+380xxxxxxxxx"
  45. //#define CALL_TARGET "+380xxxxxxxxx"
  46. // Your GPRS credentials
  47. // Leave empty, if missing user or pass
  48. const char apn[] = "YourAPN";
  49. const char user[] = "";
  50. const char pass[] = "";
  51. #include <TinyGsmClient.h>
  52. #ifdef DUMP_AT_COMMANDS
  53. #include <StreamDebugger.h>
  54. StreamDebugger debugger(SerialAT, SerialMon);
  55. TinyGsm modem(debugger);
  56. #else
  57. TinyGsm modem(SerialAT);
  58. #endif
  59. void setup() {
  60. // Set console baud rate
  61. SerialMon.begin(115200);
  62. delay(10);
  63. // Set your reset, enable, power pins here
  64. delay(3000);
  65. // Set GSM module baud rate
  66. TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  67. }
  68. void loop() {
  69. // Restart takes quite some time
  70. // To skip it, call init() instead of restart()
  71. DBG("Initializing modem...");
  72. if (!modem.restart()) {
  73. DBG("Failed to restart modem, delayin 10s and retring");
  74. delay(3000);
  75. // restart autobaud in case GSM just rebooted
  76. TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  77. delay(10000);
  78. return;
  79. }
  80. String modemInfo = modem.getModemInfo();
  81. DBG("Modem:", modemInfo);
  82. // Unlock your SIM card with a PIN if needed
  83. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  84. modem.simUnlock(GSM_PIN);
  85. }
  86. DBG("Waiting for network...");
  87. if (!modem.waitForNetwork()) {
  88. delay(10000);
  89. return;
  90. }
  91. if (modem.isNetworkConnected()) {
  92. DBG("Network connected");
  93. }
  94. #if TINY_GSM_USE_GPRS
  95. DBG("Connecting to", apn);
  96. if (!modem.gprsConnect(apn, user, pass)) {
  97. delay(10000);
  98. return;
  99. }
  100. #endif
  101. bool res;
  102. String ccid = modem.getSimCCID();
  103. DBG("CCID:", ccid);
  104. String imei = modem.getIMEI();
  105. DBG("IMEI:", imei);
  106. String cop = modem.getOperator();
  107. DBG("Operator:", cop);
  108. IPAddress local = modem.localIP();
  109. DBG("Local IP:", local);
  110. #endif
  111. int csq = modem.getSignalQuality();
  112. DBG("Signal quality:", csq);
  113. // This is NOT supported on M590
  114. int battLevel = modem.getBattPercent();
  115. DBG("Battery lavel:", battLevel);
  116. // This is only supported on SIMxxx series
  117. float battVoltage = modem.getBattVoltage() / 1000.0F;
  118. DBG("Battery voltage:", battVoltage);
  119. // This is only supported on SIMxxx series
  120. String gsmLoc = modem.getGsmLocation();
  121. DBG("GSM location:", gsmLoc);
  122. // This is only supported on SIMxxx series
  123. // String gsmTime = modem.getGSMDateTime(DATE_TIME);
  124. // DBG("GSM Time:", gsmTime);
  125. // String gsmDate = modem.getGSMDateTime(DATE_DATE);
  126. // DBG("GSM Date:", gsmDate);
  127. String ussd_balance = modem.sendUSSD("*111#");
  128. DBG("Balance (USSD):", ussd_balance);
  129. String ussd_phone_num = modem.sendUSSD("*161#");
  130. DBG("Phone number (USSD):", ussd_phone_num);
  131. #endif
  132. #if defined(TINY_GSM_MODEM_HAS_GPS)
  133. modem.enableGPS();
  134. String gps_raw = modem.getGPSraw();
  135. modem.disableGPS();
  136. DBG("GPS raw data:", gps_raw);
  137. #endif
  138. #if TINY_GSM_USE_SMS && defined(SMS_TARGET)
  139. res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
  140. DBG("SMS:", res ? "OK" : "fail");
  141. // This is only supported on SIMxxx series
  142. res = modem.sendSMS_UTF16(SMS_TARGET, u"Привіііт!", 9);
  143. DBG("UTF16 SMS:", res ? "OK" : "fail");
  144. #endif
  145. #if TINY_GSM_USE_CALL && defined(CALL_TARGET)
  146. DBG("Calling:", CALL_TARGET);
  147. // This is NOT supported on M590
  148. res = modem.callNumber(CALL_TARGET);
  149. DBG("Call:", res ? "OK" : "fail");
  150. if (res) {
  151. delay(1000L);
  152. // Play DTMF A, duration 1000ms
  153. modem.dtmfSend('A', 1000);
  154. // Play DTMF 0..4, default duration (100ms)
  155. for (char tone='0'; tone<='4'; tone++) {
  156. modem.dtmfSend(tone);
  157. }
  158. delay(5000);
  159. res = modem.callHangup();
  160. DBG("Hang up:", res ? "OK" : "fail");
  161. }
  162. #endif
  163. #if TINY_GSM_USE_GPRS
  164. modem.gprsDisconnect();
  165. if (!modem.isGprsConnected()) {
  166. DBG("GPRS disconnected");
  167. } else {
  168. DBG("GPRS disconnect: Failed.");
  169. }
  170. #endif
  171. #if TINY_GSM_POWERDOWN
  172. // Try to power-off (modem may decide to restart automatically)
  173. // To turn off modem completely, please use Reset/Enable pins
  174. modem.poweroff();
  175. DBG("Poweroff.");
  176. #endif
  177. // Do nothing forevermore
  178. while (true) {
  179. modem.maintain();
  180. }
  181. }