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.

165 lines
3.7 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_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. #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. #ifdef SMS_TARGET
  99. res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
  100. DBG("SMS:", res ? "OK" : "fail");
  101. // This is only supported on SIMxxx series
  102. res = modem.sendSMS_UTF16(SMS_TARGET, u"Привіііт!", 9);
  103. DBG("UTF16 SMS:", res ? "OK" : "fail");
  104. #endif
  105. #ifdef CALL_TARGET
  106. DBG("Calling:", CALL_TARGET);
  107. // This is NOT supported on M590
  108. res = modem.callNumber(CALL_TARGET);
  109. DBG("Call:", res ? "OK" : "fail");
  110. if (res) {
  111. delay(5000L);
  112. res = modem.callHangup();
  113. DBG("Hang up:", res ? "OK" : "fail");
  114. }
  115. #endif
  116. modem.gprsDisconnect();
  117. DBG("GPRS disconnected");
  118. // Try to power-off (modem may decide to restart automatically)
  119. // To turn off modem completely, please use Reset/Enable pins
  120. modem.poweroff();
  121. DBG("Poweroff.");
  122. // Do nothing forevermore
  123. while (true) {
  124. modem.maintain();
  125. }
  126. }