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.

235 lines
5.6 KiB

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