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.

155 lines
3.5 KiB

  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_SIM900
  14. // #define TINY_GSM_MODEM_A6
  15. // #define TINY_GSM_MODEM_A7
  16. // #define TINY_GSM_MODEM_M590
  17. // #define TINY_GSM_MODEM_ESP8266
  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. // Set phone numbers, if you want to test SMS and Calls
  28. //#define SMS_TARGET "+380xxxxxxxxx"
  29. //#define CALL_TARGET "+380xxxxxxxxx"
  30. // Your GPRS credentials
  31. // Leave empty, if missing user or pass
  32. const char apn[] = "YourAPN";
  33. const char user[] = "";
  34. const char pass[] = "";
  35. #include <TinyGsmClient.h>
  36. #ifdef DUMP_AT_COMMANDS
  37. #include <StreamDebugger.h>
  38. StreamDebugger debugger(SerialAT, SerialMon);
  39. TinyGsm modem(debugger);
  40. #else
  41. TinyGsm modem(SerialAT);
  42. #endif
  43. void setup() {
  44. // Set console baud rate
  45. SerialMon.begin(115200);
  46. delay(10);
  47. // Set GSM module baud rate
  48. SerialAT.begin(115200);
  49. delay(3000);
  50. }
  51. void loop() {
  52. // Restart takes quite some time
  53. // To skip it, call init() instead of restart()
  54. DBG("Initializing modem...");
  55. if (!modem.restart()) {
  56. delay(10000);
  57. return;
  58. }
  59. // Unlock your SIM card with a PIN
  60. //modem.simUnlock("1234");
  61. DBG("Waiting for network...");
  62. if (!modem.waitForNetwork()) {
  63. delay(10000);
  64. return;
  65. }
  66. DBG("Connecting to", apn);
  67. if (!modem.gprsConnect(apn, user, pass)) {
  68. delay(10000);
  69. return;
  70. }
  71. bool res;
  72. String modemInfo = modem.getModemInfo();
  73. DBG("Modem:", modemInfo);
  74. String ccid = modem.getSimCCID();
  75. DBG("CCID:", ccid);
  76. String imei = modem.getIMEI();
  77. DBG("IMEI:", imei);
  78. String cop = modem.getOperator();
  79. DBG("Operator:", cop);
  80. IPAddress local = modem.localIP();
  81. DBG("Local IP:", local);
  82. int csq = modem.getSignalQuality();
  83. DBG("Signal quality:", csq);
  84. int battLevel = modem.getBattPercent();
  85. DBG("Battery lavel:", battLevel);
  86. // This is only supported on SIMxxx series
  87. float battVoltage = modem.getBattVoltage() / 1000.0F;
  88. DBG("Battery voltage:", battVoltage);
  89. // This is only supported on SIMxxx series
  90. String gsmLoc = modem.getGsmLocation();
  91. DBG("GSM location:", gsmLoc);
  92. String ussd_balance = modem.sendUSSD("*111#");
  93. DBG("Balance (USSD):", ussd_balance);
  94. String ussd_phone_num = modem.sendUSSD("*161#");
  95. DBG("Phone number (USSD):", ussd_phone_num);
  96. #ifdef SMS_TARGET
  97. res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
  98. DBG("SMS:", res ? "OK" : "fail");
  99. // This is only supported on SIMxxx series
  100. res = modem.sendSMS_UTF16(SMS_TARGET, u"Привіііт!", 9);
  101. DBG("UTF16 SMS:", res ? "OK" : "fail");
  102. #endif
  103. #ifdef CALL_TARGET
  104. // This is NOT supported on M590
  105. res = modem.callNumber(CALL_TARGET);
  106. DBG("Call:", res ? "OK" : "fail");
  107. if (res) {
  108. delay(5000L);
  109. res = modem.callHangup();
  110. DBG("Hang up:", res ? "OK" : "fail");
  111. }
  112. #endif
  113. modem.gprsDisconnect();
  114. DBG("GPRS disconnected");
  115. // Do nothing forevermore
  116. while (true) {
  117. delay(1000);
  118. }
  119. }