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.

148 lines
3.4 KiB

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