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.

211 lines
5.6 KiB

7 years ago
6 years ago
7 years ago
8 years ago
8 years ago
6 years ago
6 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
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
7 years ago
7 years ago
7 years ago
7 years ago
7 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
  1. /**************************************************************
  2. *
  3. * For this example, you need to install CRC32 library:
  4. * https://github.com/bakercp/CRC32
  5. * or from http://librarymanager/all#CRC32+checksum
  6. *
  7. * TinyGSM Getting Started guide:
  8. * https://tiny.cc/tinygsm-readme
  9. *
  10. * ATTENTION! Downloading big files requires of knowledge of
  11. * the TinyGSM internals and some modem specifics,
  12. * so this is for more experienced developers.
  13. *
  14. **************************************************************/
  15. // Select your modem:
  16. #define TINY_GSM_MODEM_SIM800
  17. // #define TINY_GSM_MODEM_SIM900
  18. // #define TINY_GSM_MODEM_SIM808
  19. // #define TINY_GSM_MODEM_SIM868
  20. // #define TINY_GSM_MODEM_SIM7000
  21. // #define TINY_GSM_MODEM_UBLOX
  22. // #define TINY_GSM_MODEM_M95
  23. // #define TINY_GSM_MODEM_BG96
  24. // #define TINY_GSM_MODEM_A6
  25. // #define TINY_GSM_MODEM_A7
  26. // #define TINY_GSM_MODEM_M590
  27. // #define TINY_GSM_MODEM_MC60
  28. // #define TINY_GSM_MODEM_MC60E
  29. // #define TINY_GSM_MODEM_ESP8266
  30. // #define TINY_GSM_MODEM_XBEE
  31. // Increase RX buffer if needed
  32. #define TINY_GSM_RX_BUFFER 1024
  33. #include <TinyGsmClient.h>
  34. #include <CRC32.h>
  35. // Uncomment this if you want to see all AT commands
  36. //#define DUMP_AT_COMMANDS
  37. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  38. #define SerialMon Serial
  39. // Use Hardware Serial on Mega, Leonardo, Micro
  40. #define SerialAT Serial1
  41. // or Software Serial on Uno, Nano
  42. //#include <SoftwareSerial.h>
  43. //SoftwareSerial SerialAT(2, 3); // RX, TX
  44. // Your GPRS credentials
  45. // Leave empty, if missing user or pass
  46. const char apn[] = "YourAPN";
  47. const char user[] = "";
  48. const char pass[] = "";
  49. // Server details
  50. const char server[] = "vsh.pp.ua";
  51. const int port = 80;
  52. const char resource[] = "/TinyGSM/test_1k.bin";
  53. uint32_t knownCRC32 = 0x6f50d767;
  54. uint32_t knownFileSize = 1024; // In case server does not send it
  55. #ifdef DUMP_AT_COMMANDS
  56. #include <StreamDebugger.h>
  57. StreamDebugger debugger(SerialAT, SerialMon);
  58. TinyGsm modem(debugger);
  59. #else
  60. TinyGsm modem(SerialAT);
  61. #endif
  62. TinyGsmClient client(modem);
  63. void setup() {
  64. // Set console baud rate
  65. SerialMon.begin(115200);
  66. delay(10);
  67. // Set GSM module baud rate
  68. SerialAT.begin(115200);
  69. delay(3000);
  70. // Restart takes quite some time
  71. // To skip it, call init() instead of restart()
  72. SerialMon.println(F("Initializing modem..."));
  73. modem.restart();
  74. String modemInfo = modem.getModemInfo();
  75. SerialMon.print(F("Modem: "));
  76. SerialMon.println(modemInfo);
  77. // Unlock your SIM card with a PIN
  78. //modem.simUnlock("1234");
  79. }
  80. void printPercent(uint32_t readLength, uint32_t contentLength) {
  81. // If we know the total length
  82. if (contentLength != -1) {
  83. SerialMon.print("\r ");
  84. SerialMon.print((100.0 * readLength) / contentLength);
  85. SerialMon.print('%');
  86. } else {
  87. SerialMon.println(readLength);
  88. }
  89. }
  90. void loop() {
  91. SerialMon.print(F("Waiting for network..."));
  92. if (!modem.waitForNetwork()) {
  93. SerialMon.println(" fail");
  94. delay(10000);
  95. return;
  96. }
  97. SerialMon.println(" OK");
  98. SerialMon.print(F("Connecting to "));
  99. SerialMon.print(apn);
  100. if (!modem.gprsConnect(apn, user, pass)) {
  101. SerialMon.println(" fail");
  102. delay(10000);
  103. return;
  104. }
  105. SerialMon.println(" OK");
  106. SerialMon.print(F("Connecting to "));
  107. SerialMon.print(server);
  108. if (!client.connect(server, port)) {
  109. SerialMon.println(" fail");
  110. delay(10000);
  111. return;
  112. }
  113. SerialMon.println(" OK");
  114. // Make a HTTP GET request:
  115. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  116. client.print(String("Host: ") + server + "\r\n");
  117. client.print("Connection: close\r\n\r\n");
  118. long timeout = millis();
  119. while (client.available() == 0) {
  120. if (millis() - timeout > 5000L) {
  121. SerialMon.println(F(">>> Client Timeout !"));
  122. client.stop();
  123. delay(10000L);
  124. return;
  125. }
  126. }
  127. SerialMon.println(F("Reading response header"));
  128. uint32_t contentLength = knownFileSize;
  129. while (client.available()) {
  130. String line = client.readStringUntil('\n');
  131. line.trim();
  132. //SerialMon.println(line); // Uncomment this to show response header
  133. line.toLowerCase();
  134. if (line.startsWith("content-length:")) {
  135. contentLength = line.substring(line.lastIndexOf(':') + 1).toInt();
  136. } else if (line.length() == 0) {
  137. break;
  138. }
  139. }
  140. SerialMon.println(F("Reading response data"));
  141. timeout = millis();
  142. uint32_t readLength = 0;
  143. CRC32 crc;
  144. unsigned long timeElapsed = millis();
  145. printPercent(readLength, contentLength);
  146. while (readLength < contentLength && client.connected() && millis() - timeout < 10000L) {
  147. while (client.available()) {
  148. uint8_t c = client.read();
  149. //SerialMon.print((char)c); // Uncomment this to show data
  150. crc.update(c);
  151. readLength++;
  152. if (readLength % (contentLength / 13) == 0) {
  153. printPercent(readLength, contentLength);
  154. }
  155. timeout = millis();
  156. }
  157. }
  158. printPercent(readLength, contentLength);
  159. timeElapsed = millis() - timeElapsed;
  160. SerialMon.println();
  161. // Shutdown
  162. client.stop();
  163. SerialMon.println(F("Server disconnected"));
  164. modem.gprsDisconnect();
  165. SerialMon.println(F("GPRS disconnected"));
  166. float duration = float(timeElapsed) / 1000;
  167. SerialMon.println();
  168. SerialMon.print("Content-Length: "); SerialMon.println(contentLength);
  169. SerialMon.print("Actually read: "); SerialMon.println(readLength);
  170. SerialMon.print("Calc. CRC32: 0x"); SerialMon.println(crc.finalize(), HEX);
  171. SerialMon.print("Known CRC32: 0x"); SerialMon.println(knownCRC32, HEX);
  172. SerialMon.print("Duration: "); SerialMon.print(duration); SerialMon.println("s");
  173. // Do nothing forevermore
  174. while (true) {
  175. delay(1000);
  176. }
  177. }