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.

257 lines
7.0 KiB

7 years ago
6 years ago
7 years ago
8 years ago
8 years ago
6 years ago
6 years ago
8 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
8 years ago
5 years ago
8 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 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
  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. * https://tiny.cc/tinygsm-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_SIM868
  19. // #define TINY_GSM_MODEM_SIM900
  20. // #define TINY_GSM_MODEM_SIM7000
  21. // #define TINY_GSM_MODEM_UBLOX
  22. // #define TINY_GSM_MODEM_SARAR4
  23. // #define TINY_GSM_MODEM_M95
  24. // #define TINY_GSM_MODEM_BG96
  25. // #define TINY_GSM_MODEM_A6
  26. // #define TINY_GSM_MODEM_A7
  27. // #define TINY_GSM_MODEM_M590
  28. // #define TINY_GSM_MODEM_MC60
  29. // #define TINY_GSM_MODEM_MC60E
  30. // #define TINY_GSM_MODEM_ESP8266
  31. // #define TINY_GSM_MODEM_XBEE
  32. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  33. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  34. #define SerialMon Serial
  35. // Set serial for AT commands (to the module)
  36. // Use Hardware Serial on Mega, Leonardo, Micro
  37. #define SerialAT Serial1
  38. // or Software Serial on Uno, Nano
  39. //#include <SoftwareSerial.h>
  40. //SoftwareSerial SerialAT(2, 3); // RX, TX
  41. // Increase RX buffer to capture the entire response
  42. // Chips without internal buffering (A6/A7, ESP8266, M590)
  43. // need enough space in the buffer for the entire response
  44. // else data will be lost (and the http library will fail).
  45. #define TINY_GSM_RX_BUFFER 1024
  46. // See all AT commands, if wanted
  47. //#define DUMP_AT_COMMANDS
  48. // Define the serial console for debug prints, if needed
  49. #define TINY_GSM_DEBUG SerialMon
  50. //#define LOGGING // <- Logging is for the HTTP library
  51. // Add a reception delay, if needed
  52. //#define TINY_GSM_YIELD() { delay(2); }
  53. #define TINY_GSM_USE_GPRS true
  54. #define TINY_GSM_USE_WIFI false
  55. // set GSM PIN, if any
  56. #define GSM_PIN ""
  57. // Your GPRS credentials
  58. // Leave empty, if missing user or pass
  59. const char apn[] = "YourAPN";
  60. const char gprsUser[] = "";
  61. const char gprsPass[] = "";
  62. const char wifiSSID[] = "YourSSID";
  63. const char wifiPass[] = "YourWiFiPass";
  64. // Server details
  65. const char server[] = "vsh.pp.ua";
  66. const int port = 80;
  67. const char resource[] = "/TinyGSM/test_1k.bin";
  68. uint32_t knownCRC32 = 0x6f50d767;
  69. uint32_t knownFileSize = 1024; // In case server does not send it
  70. #include <TinyGsmClient.h>
  71. #include <CRC32.h>
  72. #ifdef DUMP_AT_COMMANDS
  73. #include <StreamDebugger.h>
  74. StreamDebugger debugger(SerialAT, SerialMon);
  75. TinyGsm modem(debugger);
  76. #else
  77. TinyGsm modem(SerialAT);
  78. #endif
  79. TinyGsmClient client(modem);
  80. void setup() {
  81. // Set console baud rate
  82. SerialMon.begin(115200);
  83. delay(10);
  84. // Set GSM module baud rate
  85. SerialAT.begin(115200);
  86. delay(3000);
  87. // Restart takes quite some time
  88. // To skip it, call init() instead of restart()
  89. SerialMon.println("Initializing modem...");
  90. modem.restart();
  91. String modemInfo = modem.getModemInfo();
  92. SerialMon.print("Modem: ");
  93. SerialMon.println(modemInfo);
  94. #if TINY_GSM_USE_GPRS
  95. // Unlock your SIM card with a PIN if needed
  96. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  97. modem.simUnlock(GSM_PIN);
  98. }
  99. #endif
  100. }
  101. void printPercent(uint32_t readLength, uint32_t contentLength) {
  102. // If we know the total length
  103. if (contentLength != -1) {
  104. SerialMon.print("\r ");
  105. SerialMon.print((100.0 * readLength) / contentLength);
  106. SerialMon.print('%');
  107. } else {
  108. SerialMon.println(readLength);
  109. }
  110. }
  111. void loop() {
  112. #if defined TINY_GSM_USE_WIFI && defined TINY_GSM_MODEM_HAS_WIFI
  113. SerialMon.print(F("Setting SSID/password..."));
  114. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  115. SerialMon.println(" fail");
  116. delay(10000);
  117. return;
  118. }
  119. SerialMon.println(" success");
  120. #endif
  121. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  122. // The XBee must run the gprsConnect function BEFORE waiting for network!
  123. modem.gprsConnect(apn, gprsUser, gprsPass);
  124. #endif
  125. SerialMon.print("Waiting for network...");
  126. if (!modem.waitForNetwork()) {
  127. SerialMon.println(" fail");
  128. delay(10000);
  129. return;
  130. }
  131. SerialMon.println(" success");
  132. if (modem.isNetworkConnected()) {
  133. SerialMon.println("Network connected");
  134. }
  135. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  136. SerialMon.print(F("Connecting to "));
  137. SerialMon.print(apn);
  138. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  139. SerialMon.println(" fail");
  140. delay(10000);
  141. return;
  142. }
  143. SerialMon.println(" success");
  144. #endif
  145. SerialMon.print(F("Connecting to "));
  146. SerialMon.print(server);
  147. if (!client.connect(server, port)) {
  148. SerialMon.println(" fail");
  149. delay(10000);
  150. return;
  151. }
  152. SerialMon.println(" success");
  153. // Make a HTTP GET request:
  154. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  155. client.print(String("Host: ") + server + "\r\n");
  156. client.print("Connection: close\r\n\r\n");
  157. long timeout = millis();
  158. while (client.available() == 0) {
  159. if (millis() - timeout > 5000L) {
  160. SerialMon.println(F(">>> Client Timeout !"));
  161. client.stop();
  162. delay(10000L);
  163. return;
  164. }
  165. }
  166. SerialMon.println(F("Reading response header"));
  167. uint32_t contentLength = knownFileSize;
  168. while (client.available()) {
  169. String line = client.readStringUntil('\n');
  170. line.trim();
  171. //SerialMon.println(line); // Uncomment this to show response header
  172. line.toLowerCase();
  173. if (line.startsWith("content-length:")) {
  174. contentLength = line.substring(line.lastIndexOf(':') + 1).toInt();
  175. } else if (line.length() == 0) {
  176. break;
  177. }
  178. }
  179. SerialMon.println(F("Reading response data"));
  180. timeout = millis();
  181. uint32_t readLength = 0;
  182. CRC32 crc;
  183. unsigned long timeElapsed = millis();
  184. printPercent(readLength, contentLength);
  185. while (readLength < contentLength && client.connected() && millis() - timeout < 10000L) {
  186. while (client.available()) {
  187. uint8_t c = client.read();
  188. //SerialMon.print((char)c); // Uncomment this to show data
  189. crc.update(c);
  190. readLength++;
  191. if (readLength % (contentLength / 13) == 0) {
  192. printPercent(readLength, contentLength);
  193. }
  194. timeout = millis();
  195. }
  196. }
  197. printPercent(readLength, contentLength);
  198. timeElapsed = millis() - timeElapsed;
  199. SerialMon.println();
  200. // Shutdown
  201. client.stop();
  202. SerialMon.println(F("Server disconnected"));
  203. modem.gprsDisconnect();
  204. SerialMon.println(F("GPRS disconnected"));
  205. float duration = float(timeElapsed) / 1000;
  206. SerialMon.println();
  207. SerialMon.print("Content-Length: "); SerialMon.println(contentLength);
  208. SerialMon.print("Actually read: "); SerialMon.println(readLength);
  209. SerialMon.print("Calc. CRC32: 0x"); SerialMon.println(crc.finalize(), HEX);
  210. SerialMon.print("Known CRC32: 0x"); SerialMon.println(knownCRC32, HEX);
  211. SerialMon.print("Duration: "); SerialMon.print(duration); SerialMon.println("s");
  212. // Do nothing forevermore
  213. while (true) {
  214. delay(1000);
  215. }
  216. }