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