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