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.

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