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.

152 lines
3.5 KiB

8 years ago
6 years ago
6 years ago
8 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /**************************************************************
  2. *
  3. * This sketch connects to a website and downloads a page.
  4. * It can be used to perform HTTP/RESTful API calls.
  5. *
  6. * TinyGSM Getting Started guide:
  7. * http://tiny.cc/tiny-gsm-readme
  8. *
  9. **************************************************************/
  10. // Select your modem:
  11. #define TINY_GSM_MODEM_SIM800
  12. // #define TINY_GSM_MODEM_SIM900
  13. // #define TINY_GSM_MODEM_SIM808
  14. // #define TINY_GSM_MODEM_SIM868
  15. // #define TINY_GSM_MODEM_UBLOX
  16. // #define TINY_GSM_MODEM_M95
  17. // #define TINY_GSM_MODEM_BG96
  18. // #define TINY_GSM_MODEM_A6
  19. // #define TINY_GSM_MODEM_A7
  20. // #define TINY_GSM_MODEM_M590
  21. // #define TINY_GSM_MODEM_MC60
  22. // #define TINY_GSM_MODEM_MC60E
  23. // #define TINY_GSM_MODEM_ESP8266
  24. // #define TINY_GSM_MODEM_XBEE
  25. // Increase RX buffer if needed
  26. //#define TINY_GSM_RX_BUFFER 512
  27. #include <TinyGsmClient.h>
  28. // Uncomment this if you want to see all AT commands
  29. //#define DUMP_AT_COMMANDS
  30. // Uncomment this if you want to use SSL
  31. //#define USE_SSL
  32. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  33. #define SerialMon Serial
  34. // Use Hardware Serial on Mega, Leonardo, Micro
  35. #define SerialAT Serial1
  36. // or Software Serial on Uno, Nano
  37. //#include <SoftwareSerial.h>
  38. //SoftwareSerial SerialAT(2, 3); // RX, TX
  39. // Your GPRS credentials
  40. // Leave empty, if missing user or pass
  41. const char apn[] = "YourAPN";
  42. const char user[] = "";
  43. const char pass[] = "";
  44. // Server details
  45. const char server[] = "vsh.pp.ua";
  46. const char resource[] = "/TinyGSM/logo.txt";
  47. #ifdef DUMP_AT_COMMANDS
  48. #include <StreamDebugger.h>
  49. StreamDebugger debugger(SerialAT, SerialMon);
  50. TinyGsm modem(debugger);
  51. #else
  52. TinyGsm modem(SerialAT);
  53. #endif
  54. #ifdef USE_SSL
  55. TinyGsmClientSecure client(modem);
  56. const int port = 443;
  57. #else
  58. TinyGsmClient client(modem);
  59. const int port = 80;
  60. #endif
  61. void setup() {
  62. // Set console baud rate
  63. SerialMon.begin(115200);
  64. delay(10);
  65. // Set GSM module baud rate
  66. SerialAT.begin(115200);
  67. delay(3000);
  68. // Restart takes quite some time
  69. // To skip it, call init() instead of restart()
  70. SerialMon.println(F("Initializing modem..."));
  71. modem.restart();
  72. String modemInfo = modem.getModemInfo();
  73. SerialMon.print(F("Modem: "));
  74. SerialMon.println(modemInfo);
  75. // Unlock your SIM card with a PIN
  76. //modem.simUnlock("1234");
  77. }
  78. void loop() {
  79. SerialMon.print(F("Waiting for network..."));
  80. if (!modem.waitForNetwork()) {
  81. SerialMon.println(" fail");
  82. delay(10000);
  83. return;
  84. }
  85. SerialMon.println(" OK");
  86. SerialMon.print(F("Connecting to "));
  87. SerialMon.print(apn);
  88. if (!modem.gprsConnect(apn, user, pass)) {
  89. SerialMon.println(" fail");
  90. delay(10000);
  91. return;
  92. }
  93. SerialMon.println(" OK");
  94. SerialMon.print(F("Connecting to "));
  95. SerialMon.print(server);
  96. if (!client.connect(server, port)) {
  97. SerialMon.println(" fail");
  98. delay(10000);
  99. return;
  100. }
  101. SerialMon.println(" OK");
  102. // Make a HTTP GET request:
  103. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  104. client.print(String("Host: ") + server + "\r\n");
  105. client.print("Connection: close\r\n\r\n");
  106. unsigned long timeout = millis();
  107. while (client.connected() && millis() - timeout < 10000L) {
  108. // Print available data
  109. while (client.available()) {
  110. char c = client.read();
  111. SerialMon.print(c);
  112. timeout = millis();
  113. }
  114. }
  115. SerialMon.println();
  116. // Shutdown
  117. client.stop();
  118. SerialMon.println(F("Server disconnected"));
  119. modem.gprsDisconnect();
  120. SerialMon.println(F("GPRS disconnected"));
  121. // Do nothing forevermore
  122. while (true) {
  123. delay(1000);
  124. }
  125. }