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.

207 lines
5.4 KiB

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