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.

356 lines
10 KiB

7 years ago
6 years ago
7 years ago
8 years ago
8 years ago
5 years ago
6 years ago
6 years ago
8 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
5 years ago
8 years ago
7 years ago
5 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
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_SIM7000SSL
  22. // #define TINY_GSM_MODEM_SIM7080
  23. // #define TINY_GSM_MODEM_SIM5360
  24. // #define TINY_GSM_MODEM_SIM7600
  25. // #define TINY_GSM_MODEM_UBLOX
  26. // #define TINY_GSM_MODEM_SARAR4
  27. // #define TINY_GSM_MODEM_M95
  28. // #define TINY_GSM_MODEM_BG96
  29. // #define TINY_GSM_MODEM_A6
  30. // #define TINY_GSM_MODEM_A7
  31. // #define TINY_GSM_MODEM_M590
  32. // #define TINY_GSM_MODEM_MC60
  33. // #define TINY_GSM_MODEM_MC60E
  34. // #define TINY_GSM_MODEM_ESP8266
  35. // #define TINY_GSM_MODEM_XBEE
  36. // #define TINY_GSM_MODEM_SEQUANS_MONARCH
  37. // Set serial for debug console (to the Serial Monitor, default speed 115200)
  38. #define SerialMon Serial
  39. // Set serial for AT commands (to the module)
  40. // Use Hardware Serial on Mega, Leonardo, Micro
  41. #ifndef __AVR_ATmega328P__
  42. #define SerialAT Serial1
  43. // or Software Serial on Uno, Nano
  44. #else
  45. #include <SoftwareSerial.h>
  46. SoftwareSerial SerialAT(2, 3); // RX, TX
  47. #endif
  48. // Increase RX buffer to capture the entire response
  49. // Chips without internal buffering (A6/A7, ESP8266, M590)
  50. // need enough space in the buffer for the entire response
  51. // else data will be lost (and the http library will fail).
  52. #if !defined(TINY_GSM_RX_BUFFER)
  53. #define TINY_GSM_RX_BUFFER 1024
  54. #endif
  55. // See all AT commands, if wanted
  56. // #define DUMP_AT_COMMANDS
  57. // Define the serial console for debug prints, if needed
  58. #define TINY_GSM_DEBUG SerialMon
  59. // #define LOGGING // <- Logging is for the HTTP library
  60. // Add a reception delay, if needed.
  61. // This may be needed for a fast processor at a slow baud rate.
  62. // #define TINY_GSM_YIELD() { delay(2); }
  63. // Define how you're planning to connect to the internet.
  64. // This is only needed for this example, not in other code.
  65. #define TINY_GSM_USE_GPRS true
  66. #define TINY_GSM_USE_WIFI false
  67. // set GSM PIN, if any
  68. #define GSM_PIN ""
  69. // Your GPRS credentials, if any
  70. const char apn[] = "YourAPN";
  71. const char gprsUser[] = "";
  72. const char gprsPass[] = "";
  73. // Your WiFi connection credentials, if applicable
  74. const char wifiSSID[] = "YourSSID";
  75. const char wifiPass[] = "YourWiFiPass";
  76. // Server details
  77. const char server[] = "vsh.pp.ua";
  78. const int port = 80;
  79. #include <TinyGsmClient.h>
  80. #include <CRC32.h>
  81. // Just in case someone defined the wrong thing..
  82. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  83. #undef TINY_GSM_USE_GPRS
  84. #undef TINY_GSM_USE_WIFI
  85. #define TINY_GSM_USE_GPRS false
  86. #define TINY_GSM_USE_WIFI true
  87. #endif
  88. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  89. #undef TINY_GSM_USE_GPRS
  90. #undef TINY_GSM_USE_WIFI
  91. #define TINY_GSM_USE_GPRS true
  92. #define TINY_GSM_USE_WIFI false
  93. #endif
  94. const char resource[] = "/TinyGSM/test_1k.bin";
  95. uint32_t knownCRC32 = 0x6f50d767;
  96. uint32_t knownFileSize = 1024; // In case server does not send it
  97. #ifdef DUMP_AT_COMMANDS
  98. #include <StreamDebugger.h>
  99. StreamDebugger debugger(SerialAT, SerialMon);
  100. TinyGsm modem(debugger);
  101. #else
  102. TinyGsm modem(SerialAT);
  103. #endif
  104. TinyGsmClient client(modem);
  105. void setup() {
  106. // Set console baud rate
  107. SerialMon.begin(115200);
  108. delay(10);
  109. // !!!!!!!!!!!
  110. // Set your reset, enable, power pins here
  111. // !!!!!!!!!!!
  112. SerialMon.println("Wait...");
  113. // Set GSM module baud rate
  114. SerialAT.begin(115200);
  115. delay(6000);
  116. // Restart takes quite some time
  117. // To skip it, call init() instead of restart()
  118. SerialMon.println("Initializing modem...");
  119. modem.restart();
  120. // modem.init();
  121. String modemInfo = modem.getModemInfo();
  122. SerialMon.print("Modem Info: ");
  123. SerialMon.println(modemInfo);
  124. #if TINY_GSM_USE_GPRS
  125. // Unlock your SIM card with a PIN if needed
  126. if (GSM_PIN && modem.getSimStatus() != 3) { modem.simUnlock(GSM_PIN); }
  127. #endif
  128. }
  129. void printPercent(uint32_t readLength, uint32_t contentLength) {
  130. // If we know the total length
  131. if (contentLength != (uint32_t)-1) {
  132. SerialMon.print("\r ");
  133. SerialMon.print((100.0 * readLength) / contentLength);
  134. SerialMon.print('%');
  135. } else {
  136. SerialMon.println(readLength);
  137. }
  138. }
  139. void loop() {
  140. #if TINY_GSM_USE_WIFI
  141. // Wifi connection parameters must be set before waiting for the network
  142. SerialMon.print(F("Setting SSID/password..."));
  143. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  144. SerialMon.println(" fail");
  145. delay(10000);
  146. return;
  147. }
  148. SerialMon.println(" success");
  149. #endif
  150. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  151. // The XBee must run the gprsConnect function BEFORE waiting for network!
  152. modem.gprsConnect(apn, gprsUser, gprsPass);
  153. #endif
  154. SerialMon.print("Waiting for network...");
  155. if (!modem.waitForNetwork()) {
  156. SerialMon.println(" fail");
  157. delay(10000);
  158. return;
  159. }
  160. SerialMon.println(" success");
  161. if (modem.isNetworkConnected()) { SerialMon.println("Network connected"); }
  162. #if TINY_GSM_USE_GPRS
  163. // GPRS connection parameters are usually set after network registration
  164. SerialMon.print(F("Connecting to "));
  165. SerialMon.print(apn);
  166. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  167. SerialMon.println(" fail");
  168. delay(10000);
  169. return;
  170. }
  171. SerialMon.println(" success");
  172. if (modem.isGprsConnected()) { SerialMon.println("GPRS connected"); }
  173. #endif
  174. SerialMon.print(F("Connecting to "));
  175. SerialMon.print(server);
  176. if (!client.connect(server, port)) {
  177. SerialMon.println(" fail");
  178. delay(10000);
  179. return;
  180. }
  181. SerialMon.println(" success");
  182. // Make a HTTP GET request:
  183. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  184. client.print(String("Host: ") + server + "\r\n");
  185. client.print("Connection: close\r\n\r\n");
  186. // Let's see what the entire elapsed time is, from after we send the request.
  187. uint32_t timeElapsed = millis();
  188. SerialMon.println(F("Waiting for response header"));
  189. // While we are still looking for the end of the header (i.e. empty line
  190. // FOLLOWED by a newline), continue to read data into the buffer, parsing each
  191. // line (data FOLLOWED by a newline). If it takes too long to get data from
  192. // the client, we need to exit.
  193. const uint32_t clientReadTimeout = 5000;
  194. uint32_t clientReadStartTime = millis();
  195. String headerBuffer;
  196. bool finishedHeader = false;
  197. uint32_t contentLength = 0;
  198. while (!finishedHeader) {
  199. int nlPos;
  200. if (client.available()) {
  201. clientReadStartTime = millis();
  202. while (client.available()) {
  203. char c = client.read();
  204. headerBuffer += c;
  205. // Uncomment the lines below to see the data coming into the buffer
  206. // if (c < 16)
  207. // SerialMon.print('0');
  208. // SerialMon.print(c, HEX);
  209. // SerialMon.print(' ');
  210. // if (isprint(c))
  211. // SerialMon.print(reinterpret_cast<char> c);
  212. // else
  213. // SerialMon.print('*');
  214. // SerialMon.print(' ');
  215. // Let's exit and process if we find a new line
  216. if (headerBuffer.indexOf(F("\r\n")) >= 0) break;
  217. }
  218. } else {
  219. if (millis() - clientReadStartTime > clientReadTimeout) {
  220. // Time-out waiting for data from client
  221. SerialMon.println(F(">>> Client Timeout !"));
  222. break;
  223. }
  224. }
  225. // See if we have a new line.
  226. nlPos = headerBuffer.indexOf(F("\r\n"));
  227. if (nlPos > 0) {
  228. headerBuffer.toLowerCase();
  229. // Check if line contains content-length
  230. if (headerBuffer.startsWith(F("content-length:"))) {
  231. contentLength =
  232. headerBuffer.substring(headerBuffer.indexOf(':') + 1).toInt();
  233. // SerialMon.print(F("Got Content Length: ")); // uncomment for
  234. // SerialMon.println(contentLength); // confirmation
  235. }
  236. headerBuffer.remove(0, nlPos + 2); // remove the line
  237. } else if (nlPos == 0) {
  238. // if the new line is empty (i.e. "\r\n" is at the beginning of the line),
  239. // we are done with the header.
  240. finishedHeader = true;
  241. }
  242. }
  243. // The two cases which are not managed properly are as follows:
  244. // 1. The client doesn't provide data quickly enough to keep up with this
  245. // loop.
  246. // 2. If the client data is segmented in the middle of the 'Content-Length: '
  247. // header,
  248. // then that header may be missed/damaged.
  249. //
  250. uint32_t readLength = 0;
  251. CRC32 crc;
  252. if (finishedHeader && contentLength == knownFileSize) {
  253. SerialMon.println(F("Reading response data"));
  254. clientReadStartTime = millis();
  255. printPercent(readLength, contentLength);
  256. while (readLength < contentLength && client.connected() &&
  257. millis() - clientReadStartTime < clientReadTimeout) {
  258. while (client.available()) {
  259. uint8_t c = client.read();
  260. // SerialMon.print(reinterpret_cast<char>c); // Uncomment this to show
  261. // data
  262. crc.update(c);
  263. readLength++;
  264. if (readLength % (contentLength / 13) == 0) {
  265. printPercent(readLength, contentLength);
  266. }
  267. clientReadStartTime = millis();
  268. }
  269. }
  270. printPercent(readLength, contentLength);
  271. }
  272. timeElapsed = millis() - timeElapsed;
  273. SerialMon.println();
  274. // Shutdown
  275. client.stop();
  276. SerialMon.println(F("Server disconnected"));
  277. #if TINY_GSM_USE_WIFI
  278. modem.networkDisconnect();
  279. SerialMon.println(F("WiFi disconnected"));
  280. #endif
  281. #if TINY_GSM_USE_GPRS
  282. modem.gprsDisconnect();
  283. SerialMon.println(F("GPRS disconnected"));
  284. #endif
  285. float duration = float(timeElapsed) / 1000;
  286. SerialMon.println();
  287. SerialMon.print("Content-Length: ");
  288. SerialMon.println(contentLength);
  289. SerialMon.print("Actually read: ");
  290. SerialMon.println(readLength);
  291. SerialMon.print("Calc. CRC32: 0x");
  292. SerialMon.println(crc.finalize(), HEX);
  293. SerialMon.print("Known CRC32: 0x");
  294. SerialMon.println(knownCRC32, HEX);
  295. SerialMon.print("Duration: ");
  296. SerialMon.print(duration);
  297. SerialMon.println("s");
  298. // Do nothing forevermore
  299. while (true) { delay(1000); }
  300. }