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.

146 lines
3.3 KiB

8 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
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_SIM808
  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. // Increase RX buffer if needed
  19. //#define TINY_GSM_RX_BUFFER 512
  20. #include <TinyGsmClient.h>
  21. // Uncomment this if you want to see all AT commands
  22. //#define DUMP_AT_COMMANDS
  23. // Uncomment this if you want to use SSL
  24. //#define USE_SSL
  25. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  26. #define SerialMon Serial
  27. // Use Hardware Serial on Mega, Leonardo, Micro
  28. #define SerialAT Serial1
  29. // or Software Serial on Uno, Nano
  30. //#include <SoftwareSerial.h>
  31. //SoftwareSerial SerialAT(2, 3); // RX, TX
  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. // Server details
  38. const char server[] = "vsh.pp.ua";
  39. const char resource[] = "/TinyGSM/logo.txt";
  40. #ifdef DUMP_AT_COMMANDS
  41. #include <StreamDebugger.h>
  42. StreamDebugger debugger(SerialAT, SerialMon);
  43. TinyGsm modem(debugger);
  44. #else
  45. TinyGsm modem(SerialAT);
  46. #endif
  47. #ifdef USE_SSL
  48. TinyGsmClientSecure client(modem);
  49. const int port = 443;
  50. #else
  51. TinyGsmClient client(modem);
  52. const int port = 80;
  53. #endif
  54. void setup() {
  55. // Set console baud rate
  56. SerialMon.begin(115200);
  57. delay(10);
  58. // Set GSM module baud rate
  59. SerialAT.begin(115200);
  60. delay(3000);
  61. // Restart takes quite some time
  62. // To skip it, call init() instead of restart()
  63. SerialMon.println(F("Initializing modem..."));
  64. modem.restart();
  65. String modemInfo = modem.getModemInfo();
  66. SerialMon.print(F("Modem: "));
  67. SerialMon.println(modemInfo);
  68. // Unlock your SIM card with a PIN
  69. //modem.simUnlock("1234");
  70. }
  71. void loop() {
  72. SerialMon.print(F("Waiting for network..."));
  73. if (!modem.waitForNetwork()) {
  74. SerialMon.println(" fail");
  75. delay(10000);
  76. return;
  77. }
  78. SerialMon.println(" OK");
  79. SerialMon.print(F("Connecting to "));
  80. SerialMon.print(apn);
  81. if (!modem.gprsConnect(apn, user, pass)) {
  82. SerialMon.println(" fail");
  83. delay(10000);
  84. return;
  85. }
  86. SerialMon.println(" OK");
  87. SerialMon.print(F("Connecting to "));
  88. SerialMon.print(server);
  89. if (!client.connect(server, port)) {
  90. SerialMon.println(" fail");
  91. delay(10000);
  92. return;
  93. }
  94. SerialMon.println(" OK");
  95. // Make a HTTP GET request:
  96. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  97. client.print(String("Host: ") + server + "\r\n");
  98. client.print("Connection: close\r\n\r\n");
  99. unsigned long timeout = millis();
  100. while (client.connected() && millis() - timeout < 10000L) {
  101. // Print available data
  102. while (client.available()) {
  103. char c = client.read();
  104. SerialMon.print(c);
  105. timeout = millis();
  106. }
  107. }
  108. SerialMon.println();
  109. // Shutdown
  110. client.stop();
  111. SerialMon.println(F("Server disconnected"));
  112. modem.gprsDisconnect();
  113. SerialMon.println(F("GPRS disconnected"));
  114. // Do nothing forevermore
  115. while (true) {
  116. delay(1000);
  117. }
  118. }