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.

180 lines
4.7 KiB

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/vshymanskyy/CRC32.git
  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. #include <TinyGsmClient.h>
  24. #include <CRC32.h>
  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. TinyGsm modem(SerialAT);
  36. TinyGsmClient client(modem);
  37. const char server[] = "cdn.rawgit.com";
  38. const char resource[] = "/vshymanskyy/tinygsm/master/extras/test_1k.bin";
  39. uint32_t knownCRC32 = 0x6f50d767;
  40. uint32_t knownFileSize = 1024; // In case server does not send it
  41. void setup() {
  42. // Set console baud rate
  43. Serial.begin(115200);
  44. delay(10);
  45. // Set GSM module baud rate
  46. SerialAT.begin(115200);
  47. delay(3000);
  48. // Restart takes quite some time
  49. // To skip it, call init() instead of restart()
  50. Serial.println("Initializing modem...");
  51. modem.restart();
  52. // Unlock your SIM card with a PIN
  53. //modem.simUnlock("1234");
  54. }
  55. void printPercent(uint32_t readLength, uint32_t contentLength) {
  56. // If we know the total length
  57. if (contentLength != -1) {
  58. Serial.print("\r ");
  59. Serial.print((100.0 * readLength) / contentLength);
  60. Serial.print('%');
  61. } else {
  62. Serial.println(readLength);
  63. }
  64. }
  65. void loop() {
  66. Serial.print("Waiting for network...");
  67. if (!modem.waitForNetwork()) {
  68. Serial.println(" fail");
  69. delay(10000);
  70. return;
  71. }
  72. Serial.println(" OK");
  73. Serial.print("Connecting to ");
  74. Serial.print(apn);
  75. if (!modem.gprsConnect(apn, user, pass)) {
  76. Serial.println(" fail");
  77. delay(10000);
  78. return;
  79. }
  80. Serial.println(" OK");
  81. Serial.print("Connecting to ");
  82. Serial.print(server);
  83. // if you get a connection, report back via serial:
  84. if (!client.connect(server, 80)) {
  85. Serial.println(" fail");
  86. delay(10000);
  87. return;
  88. }
  89. Serial.println(" OK");
  90. // Make a HTTP request:
  91. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  92. client.print(String("Host: ") + server + "\r\n");
  93. client.print("Connection: close\r\n\r\n");
  94. long timeout = millis();
  95. while (client.available() == 0) {
  96. if (millis() - timeout > 5000L) {
  97. Serial.println(">>> Client Timeout !");
  98. client.stop();
  99. delay(10000L);
  100. return;
  101. }
  102. }
  103. Serial.println("Reading response header");
  104. uint32_t contentLength = knownFileSize;
  105. while (client.available()) {
  106. String line = client.readStringUntil('\n');
  107. line.trim();
  108. //Serial.println(line); // Uncomment this to show response header
  109. line.toLowerCase();
  110. if (line.startsWith("content-length:")) {
  111. contentLength = line.substring(line.lastIndexOf(':') + 1).toInt();
  112. } else if (line.length() == 0) {
  113. break;
  114. }
  115. }
  116. Serial.println("Reading response data");
  117. timeout = millis();
  118. uint32_t readLength = 0;
  119. CRC32 crc;
  120. unsigned long timeElapsed = millis();
  121. printPercent(readLength, contentLength);
  122. while (readLength < contentLength && client.connected() && millis() - timeout < 10000L) {
  123. while (client.available()) {
  124. uint8_t c = client.read();
  125. //Serial.print((char)c); // Uncomment this to show data
  126. crc.update(c);
  127. readLength++;
  128. if (readLength % (contentLength / 13) == 0) {
  129. printPercent(readLength, contentLength);
  130. }
  131. timeout = millis();
  132. }
  133. }
  134. printPercent(readLength, contentLength);
  135. timeElapsed = millis() - timeElapsed;
  136. Serial.println();
  137. client.stop();
  138. Serial.println("Server disconnected");
  139. modem.gprsDisconnect();
  140. Serial.println("GPRS disconnected");
  141. Serial.println();
  142. float duration = float(timeElapsed) / 1000;
  143. Serial.print("Content-Length: "); Serial.println(contentLength);
  144. Serial.print("Actually read: "); Serial.println(readLength);
  145. Serial.print("Calc. CRC32: 0x"); Serial.println(crc.finalize(), HEX);
  146. Serial.print("Known CRC32: 0x"); Serial.println(knownCRC32, HEX);
  147. Serial.print("Duration: "); Serial.print(duration); Serial.println("s");
  148. // Do nothing forevermore
  149. while (true) {
  150. delay(1000);
  151. }
  152. }