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.

172 lines
3.9 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 GSM module baud rate
  49. TinyGsmAutoBaud(SerialAT);
  50. delay(3000);
  51. }
  52. void loop() {
  53. // Restart takes quite some time
  54. // To skip it, call init() instead of restart()
  55. DBG("Initializing modem...");
  56. if (!modem.restart()) {
  57. delay(10000);
  58. return;
  59. }
  60. String modemInfo = modem.getModemInfo();
  61. DBG("Modem:", modemInfo);
  62. // Unlock your SIM card with a PIN
  63. //modem.simUnlock("1234");
  64. DBG("Waiting for network...");
  65. if (!modem.waitForNetwork()) {
  66. delay(10000);
  67. return;
  68. }
  69. DBG("Connecting to", apn);
  70. if (!modem.gprsConnect(apn, user, pass)) {
  71. delay(10000);
  72. return;
  73. }
  74. bool res;
  75. String ccid = modem.getSimCCID();
  76. DBG("CCID:", ccid);
  77. String imei = modem.getIMEI();
  78. DBG("IMEI:", imei);
  79. String cop = modem.getOperator();
  80. DBG("Operator:", cop);
  81. IPAddress local = modem.localIP();
  82. DBG("Local IP:", local);
  83. int csq = modem.getSignalQuality();
  84. DBG("Signal quality:", csq);
  85. // This is NOT supported on M590
  86. int battLevel = modem.getBattPercent();
  87. DBG("Battery lavel:", battLevel);
  88. // This is only supported on SIMxxx series
  89. float battVoltage = modem.getBattVoltage() / 1000.0F;
  90. DBG("Battery voltage:", battVoltage);
  91. // This is only supported on SIMxxx series
  92. String gsmLoc = modem.getGsmLocation();
  93. DBG("GSM location:", gsmLoc);
  94. String ussd_balance = modem.sendUSSD("*111#");
  95. DBG("Balance (USSD):", ussd_balance);
  96. String ussd_phone_num = modem.sendUSSD("*161#");
  97. DBG("Phone number (USSD):", ussd_phone_num);
  98. #if defined(TINY_GSM_MODEM_SIM808)
  99. modem.enableGPS();
  100. String gps_raw = modem.getGPSraw();
  101. modem.disableGPS();
  102. DBG("GPS raw data:", gps_raw);
  103. #endif
  104. #if defined(SMS_TARGET)
  105. res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
  106. DBG("SMS:", res ? "OK" : "fail");
  107. // This is only supported on SIMxxx series
  108. res = modem.sendSMS_UTF16(SMS_TARGET, u"Привіііт!", 9);
  109. DBG("UTF16 SMS:", res ? "OK" : "fail");
  110. #endif
  111. #if defined(CALL_TARGET)
  112. DBG("Calling:", CALL_TARGET);
  113. // This is NOT supported on M590
  114. res = modem.callNumber(CALL_TARGET);
  115. DBG("Call:", res ? "OK" : "fail");
  116. if (res) {
  117. delay(5000L);
  118. res = modem.callHangup();
  119. DBG("Hang up:", res ? "OK" : "fail");
  120. }
  121. #endif
  122. modem.gprsDisconnect();
  123. DBG("GPRS disconnected");
  124. // Try to power-off (modem may decide to restart automatically)
  125. // To turn off modem completely, please use Reset/Enable pins
  126. modem.poweroff();
  127. DBG("Poweroff.");
  128. // Do nothing forevermore
  129. while (true) {
  130. modem.maintain();
  131. }
  132. }