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.

195 lines
5.0 KiB

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