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.

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