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.

193 lines
4.3 KiB

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