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.

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