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.

269 lines
6.4 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. // if (!modem.init()) {
  95. DBG("Failed to restart modem, delaying 10s and retrying");
  96. delay(3000);
  97. // restart autobaud in case GSM just rebooted
  98. TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  99. delay(10000);
  100. return;
  101. }
  102. String modemInfo = modem.getModemInfo();
  103. DBG("Modem:", modemInfo);
  104. #if TINY_GSM_USE_GPRS
  105. // Unlock your SIM card with a PIN if needed
  106. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  107. modem.simUnlock(GSM_PIN);
  108. }
  109. #endif
  110. #if TINY_GSM_USE_WIFI
  111. SerialMon.print(F("Setting SSID/password..."));
  112. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  113. SerialMon.println(" fail");
  114. delay(10000);
  115. return;
  116. }
  117. SerialMon.println(" OK");
  118. #endif
  119. DBG("Waiting for network...");
  120. if (!modem.waitForNetwork()) {
  121. delay(10000);
  122. return;
  123. }
  124. if (modem.isNetworkConnected()) {
  125. DBG("Network connected");
  126. }
  127. #if TINY_GSM_USE_GPRS
  128. DBG("Connecting to", apn);
  129. if (!modem.gprsConnect(apn, user, pass)) {
  130. delay(10000);
  131. return;
  132. }
  133. bool res = modem.isGprsConnected();
  134. DBG("GPRS status:", res ? "connected" : "not connected");
  135. String ccid = modem.getSimCCID();
  136. DBG("CCID:", ccid);
  137. String imei = modem.getIMEI();
  138. DBG("IMEI:", imei);
  139. String cop = modem.getOperator();
  140. DBG("Operator:", cop);
  141. IPAddress local = modem.localIP();
  142. DBG("Local IP:", local);
  143. int csq = modem.getSignalQuality();
  144. DBG("Signal quality:", csq);
  145. // This is NOT supported on M590
  146. int battLevel = modem.getBattPercent();
  147. DBG("Battery lavel:", battLevel);
  148. // This is only supported on SIMxxx series
  149. float battVoltage = modem.getBattVoltage() / 1000.0F;
  150. DBG("Battery voltage:", battVoltage);
  151. // This is only supported on SIMxxx series
  152. String gsmLoc = modem.getGsmLocation();
  153. DBG("GSM location:", gsmLoc);
  154. // This is only supported on SIMxxx series
  155. // String gsmTime = modem.getGSMDateTime(DATE_TIME);
  156. // DBG("GSM Time:", gsmTime);
  157. // String gsmDate = modem.getGSMDateTime(DATE_DATE);
  158. // DBG("GSM Date:", gsmDate);
  159. String ussd_balance = modem.sendUSSD("*111#");
  160. DBG("Balance (USSD):", ussd_balance);
  161. String ussd_phone_num = modem.sendUSSD("*161#");
  162. DBG("Phone number (USSD):", ussd_phone_num);
  163. #endif
  164. #if defined(TINY_GSM_MODEM_HAS_GPS)
  165. modem.enableGPS();
  166. String gps_raw = modem.getGPSraw();
  167. modem.disableGPS();
  168. DBG("GPS raw data:", gps_raw);
  169. #endif
  170. #if TINY_GSM_USE_SMS && defined(SMS_TARGET)
  171. res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
  172. DBG("SMS:", res ? "OK" : "fail");
  173. // This is only supported on SIMxxx series
  174. res = modem.sendSMS_UTF16(SMS_TARGET, u"Привіііт!", 9);
  175. DBG("UTF16 SMS:", res ? "OK" : "fail");
  176. #endif
  177. #if TINY_GSM_USE_CALL && defined(CALL_TARGET)
  178. DBG("Calling:", CALL_TARGET);
  179. // This is NOT supported on M590
  180. res = modem.callNumber(CALL_TARGET);
  181. DBG("Call:", res ? "OK" : "fail");
  182. if (res) {
  183. delay(1000L);
  184. // Play DTMF A, duration 1000ms
  185. modem.dtmfSend('A', 1000);
  186. // Play DTMF 0..4, default duration (100ms)
  187. for (char tone='0'; tone<='4'; tone++) {
  188. modem.dtmfSend(tone);
  189. }
  190. delay(5000);
  191. res = modem.callHangup();
  192. DBG("Hang up:", res ? "OK" : "fail");
  193. }
  194. #endif
  195. #if TINY_GSM_USE_GPRS
  196. modem.gprsDisconnect();
  197. if (!modem.isGprsConnected()) {
  198. DBG("GPRS disconnected");
  199. } else {
  200. DBG("GPRS disconnect: Failed.");
  201. }
  202. #endif
  203. #if TINY_GSM_USE_WIFI
  204. modem.networkDisconnect();
  205. DBG("WiFi disconnected");
  206. #endif
  207. #if TINY_GSM_POWERDOWN
  208. // Try to power-off (modem may decide to restart automatically)
  209. // To turn off modem completely, please use Reset/Enable pins
  210. modem.poweroff();
  211. DBG("Poweroff.");
  212. #endif
  213. // Do nothing forevermore
  214. while (true) {
  215. modem.maintain();
  216. }
  217. }