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.

173 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. // #define TINY_GSM_MODEM_ESP8266
  19. // Set serial for debug console (to the Serial Monitor, speed 115200)
  20. #define SerialMon Serial
  21. // Set serial for AT commands (to the module)
  22. // Use Hardware Serial on Mega, Leonardo, Micro
  23. #define SerialAT Serial1
  24. // or Software Serial on Uno, Nano
  25. //#include <SoftwareSerial.h>
  26. //SoftwareSerial SerialAT(2, 3); // RX, TX
  27. //#define DUMP_AT_COMMANDS
  28. #define TINY_GSM_DEBUG SerialMon
  29. // Set phone numbers, if you want to test SMS and Calls
  30. //#define SMS_TARGET "+380xxxxxxxxx"
  31. //#define CALL_TARGET "+380xxxxxxxxx"
  32. // Your GPRS credentials
  33. // Leave empty, if missing user or pass
  34. const char apn[] = "YourAPN";
  35. const char user[] = "";
  36. const char pass[] = "";
  37. #include <TinyGsmClient.h>
  38. #ifdef DUMP_AT_COMMANDS
  39. #include <StreamDebugger.h>
  40. StreamDebugger debugger(SerialAT, SerialMon);
  41. TinyGsm modem(debugger);
  42. #else
  43. TinyGsm modem(SerialAT);
  44. #endif
  45. void setup() {
  46. // Set console baud rate
  47. SerialMon.begin(115200);
  48. delay(10);
  49. // Set GSM module baud rate
  50. TinyGsmAutoBaud(SerialAT);
  51. delay(3000);
  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. DBG("Connecting to", apn);
  71. if (!modem.gprsConnect(apn, user, pass)) {
  72. delay(10000);
  73. return;
  74. }
  75. bool res;
  76. String ccid = modem.getSimCCID();
  77. DBG("CCID:", ccid);
  78. String imei = modem.getIMEI();
  79. DBG("IMEI:", imei);
  80. String cop = modem.getOperator();
  81. DBG("Operator:", cop);
  82. IPAddress local = modem.localIP();
  83. DBG("Local IP:", local);
  84. int csq = modem.getSignalQuality();
  85. DBG("Signal quality:", csq);
  86. // This is NOT supported on M590
  87. int battLevel = modem.getBattPercent();
  88. DBG("Battery lavel:", battLevel);
  89. // This is only supported on SIMxxx series
  90. float battVoltage = modem.getBattVoltage() / 1000.0F;
  91. DBG("Battery voltage:", battVoltage);
  92. // This is only supported on SIMxxx series
  93. String gsmLoc = modem.getGsmLocation();
  94. DBG("GSM location:", gsmLoc);
  95. String ussd_balance = modem.sendUSSD("*111#");
  96. DBG("Balance (USSD):", ussd_balance);
  97. String ussd_phone_num = modem.sendUSSD("*161#");
  98. DBG("Phone number (USSD):", ussd_phone_num);
  99. #if defined(TINY_GSM_MODEM_SIM808)
  100. modem.enableGPS();
  101. String gps_raw = modem.getGPSraw();
  102. modem.disableGPS();
  103. DBG("GPS raw data:", gps_raw);
  104. #endif
  105. #if defined(SMS_TARGET)
  106. res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
  107. DBG("SMS:", res ? "OK" : "fail");
  108. // This is only supported on SIMxxx series
  109. res = modem.sendSMS_UTF16(SMS_TARGET, u"Привіііт!", 9);
  110. DBG("UTF16 SMS:", res ? "OK" : "fail");
  111. #endif
  112. #if defined(CALL_TARGET)
  113. DBG("Calling:", CALL_TARGET);
  114. // This is NOT supported on M590
  115. res = modem.callNumber(CALL_TARGET);
  116. DBG("Call:", res ? "OK" : "fail");
  117. if (res) {
  118. delay(5000L);
  119. res = modem.callHangup();
  120. DBG("Hang up:", res ? "OK" : "fail");
  121. }
  122. #endif
  123. modem.gprsDisconnect();
  124. DBG("GPRS disconnected");
  125. // Try to power-off (modem may decide to restart automatically)
  126. // To turn off modem completely, please use Reset/Enable pins
  127. modem.poweroff();
  128. DBG("Poweroff.");
  129. // Do nothing forevermore
  130. while (true) {
  131. modem.maintain();
  132. }
  133. }