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.

161 lines
3.7 KiB

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. SerialAT.begin(115200);
  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. int battLevel = modem.getBattPercent();
  86. DBG("Battery lavel:", battLevel);
  87. // This is only supported on SIMxxx series
  88. float battVoltage = modem.getBattVoltage() / 1000.0F;
  89. DBG("Battery voltage:", battVoltage);
  90. // This is only supported on SIMxxx series
  91. String gsmLoc = modem.getGsmLocation();
  92. DBG("GSM location:", gsmLoc);
  93. String ussd_balance = modem.sendUSSD("*111#");
  94. DBG("Balance (USSD):", ussd_balance);
  95. String ussd_phone_num = modem.sendUSSD("*161#");
  96. DBG("Phone number (USSD):", ussd_phone_num);
  97. #ifdef SMS_TARGET
  98. res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
  99. DBG("SMS:", res ? "OK" : "fail");
  100. // This is only supported on SIMxxx series
  101. res = modem.sendSMS_UTF16(SMS_TARGET, u"Привіііт!", 9);
  102. DBG("UTF16 SMS:", res ? "OK" : "fail");
  103. #endif
  104. #ifdef CALL_TARGET
  105. // This is NOT supported on M590
  106. res = modem.callNumber(CALL_TARGET);
  107. DBG("Call:", res ? "OK" : "fail");
  108. if (res) {
  109. delay(5000L);
  110. res = modem.callHangup();
  111. DBG("Hang up:", res ? "OK" : "fail");
  112. }
  113. #endif
  114. modem.gprsDisconnect();
  115. DBG("GPRS disconnected");
  116. // Try to power-off (modem may decide to restart automatically)
  117. // To turn off modem completely, please use Reset/Enable pins
  118. modem.poweroff();
  119. DBG("Poweroff.");
  120. // Do nothing forevermore
  121. while (true) {
  122. modem.maintain();
  123. }
  124. }