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.

202 lines
4.7 KiB

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