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.

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