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.

281 lines
8.1 KiB

8 years ago
8 years ago
8 years ago
6 years ago
8 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
8 years ago
5 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
8 years ago
8 years ago
8 years ago
6 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
  1. /**************************************************************
  2. *
  3. * To run this tool you need StreamDebugger library:
  4. * https://github.com/vshymanskyy/StreamDebugger
  5. * or from http://librarymanager/all#StreamDebugger
  6. *
  7. * TinyGSM Getting Started guide:
  8. * https://tiny.cc/tinygsm-readme
  9. *
  10. **************************************************************/
  11. // Select your modem:
  12. #define TINY_GSM_MODEM_SIM800
  13. // #define TINY_GSM_MODEM_SIM808
  14. // #define TINY_GSM_MODEM_SIM868
  15. // #define TINY_GSM_MODEM_SIM900
  16. // #define TINY_GSM_MODEM_SIM7000
  17. // #define TINY_GSM_MODEM_SIM7000SSL
  18. // #define TINY_GSM_MODEM_SIM7080
  19. // #define TINY_GSM_MODEM_SIM5360
  20. // #define TINY_GSM_MODEM_SIM7600
  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. #ifndef __AVR_ATmega328P__
  38. #define SerialAT Serial1
  39. // or Software Serial on Uno, Nano
  40. #else
  41. #include <SoftwareSerial.h>
  42. SoftwareSerial SerialAT(2, 3); // RX, TX
  43. #endif
  44. // Increase RX buffer to capture the entire response
  45. // Chips without internal buffering (A6/A7, ESP8266, M590)
  46. // need enough space in the buffer for the entire response
  47. // else data will be lost (and the http library will fail).
  48. #ifndef TINY_GSM_RX_BUFFER
  49. #define TINY_GSM_RX_BUFFER 1024
  50. #endif
  51. // See all AT commands, if wanted
  52. // #define DUMP_AT_COMMANDS
  53. // Define the serial console for debug prints, if needed
  54. #define TINY_GSM_DEBUG SerialMon
  55. // Range to attempt to autobaud
  56. // NOTE: DO NOT AUTOBAUD in production code. Once you've established
  57. // communication, set a fixed baud rate using modem.setBaud(#).
  58. #define GSM_AUTOBAUD_MIN 9600
  59. #define GSM_AUTOBAUD_MAX 115200
  60. // Add a reception delay - may be needed for a fast processor at a slow baud
  61. // rate #define TINY_GSM_YIELD() { delay(2); }
  62. // Uncomment this if you want to use SSL
  63. // #define USE_SSL
  64. #define TINY_GSM_USE_GPRS true
  65. #define TINY_GSM_USE_WIFI false
  66. // set GSM PIN, if any
  67. #define GSM_PIN ""
  68. // Your GPRS credentials, if any
  69. const char apn[] = "YourAPN";
  70. const char gprsUser[] = "";
  71. const char gprsPass[] = "";
  72. // Your WiFi connection credentials, if applicable
  73. const char wifiSSID[] = "YourSSID";
  74. const char wifiPass[] = "YourWiFiPass";
  75. // Server details
  76. const char server[] = "vsh.pp.ua";
  77. const char resource[] = "/TinyGSM/logo.txt";
  78. #include <TinyGsmClient.h>
  79. // Just in case someone defined the wrong thing..
  80. #if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  81. #undef TINY_GSM_USE_GPRS
  82. #undef TINY_GSM_USE_WIFI
  83. #define TINY_GSM_USE_GPRS false
  84. #define TINY_GSM_USE_WIFI true
  85. #endif
  86. #if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  87. #undef TINY_GSM_USE_GPRS
  88. #undef TINY_GSM_USE_WIFI
  89. #define TINY_GSM_USE_GPRS true
  90. #define TINY_GSM_USE_WIFI false
  91. #endif
  92. #ifdef DUMP_AT_COMMANDS
  93. #include <StreamDebugger.h>
  94. StreamDebugger debugger(SerialAT, SerialMon);
  95. TinyGsm modem(debugger);
  96. #else
  97. TinyGsm modem(SerialAT);
  98. #endif
  99. #ifdef USE_SSL&& defined TINY_GSM_MODEM_HAS_SSL
  100. TinyGsmClientSecure client(modem);
  101. const int port = 443;
  102. #else
  103. TinyGsmClient client(modem);
  104. const int port = 80;
  105. #endif
  106. void setup() {
  107. // Set console baud rate
  108. SerialMon.begin(115200);
  109. delay(10);
  110. // !!!!!!!!!!!
  111. // Set your reset, enable, power pins here
  112. // !!!!!!!!!!!
  113. SerialMon.println("Wait...");
  114. // Set GSM module baud rate
  115. TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
  116. // SerialAT.begin(9600);
  117. delay(6000);
  118. }
  119. void loop() {
  120. // Restart takes quite some time
  121. // To skip it, call init() instead of restart()
  122. SerialMon.print("Initializing modem...");
  123. if (!modem.restart()) {
  124. // if (!modem.init()) {
  125. SerialMon.println(F(" [fail]"));
  126. SerialMon.println(F("************************"));
  127. SerialMon.println(F(" Is your modem connected properly?"));
  128. SerialMon.println(F(" Is your serial speed (baud rate) correct?"));
  129. SerialMon.println(F(" Is your modem powered on?"));
  130. SerialMon.println(F(" Do you use a good, stable power source?"));
  131. SerialMon.println(F(" Try using File -> Examples -> TinyGSM -> tools -> AT_Debug to find correct configuration"));
  132. SerialMon.println(F("************************"));
  133. delay(10000);
  134. return;
  135. }
  136. SerialMon.println(F(" [OK]"));
  137. String modemInfo = modem.getModemInfo();
  138. SerialMon.print("Modem Info: ");
  139. SerialMon.println(modemInfo);
  140. #if TINY_GSM_USE_GPRS
  141. // Unlock your SIM card with a PIN if needed
  142. if (GSM_PIN && modem.getSimStatus() != 3) { modem.simUnlock(GSM_PIN); }
  143. #endif
  144. #if TINY_GSM_USE_WIFI
  145. // Wifi connection parameters must be set before waiting for the network
  146. SerialMon.print(F("Setting SSID/password..."));
  147. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  148. SerialMon.println(" fail");
  149. delay(10000);
  150. return;
  151. }
  152. SerialMon.println(" success");
  153. #endif
  154. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  155. // The XBee must run the gprsConnect function BEFORE waiting for network!
  156. modem.gprsConnect(apn, gprsUser, gprsPass);
  157. #endif
  158. SerialMon.print("Waiting for network...");
  159. if (!modem.waitForNetwork(
  160. 600000L)) { // You may need lengthen this in poor service areas
  161. SerialMon.println(F(" [fail]"));
  162. SerialMon.println(F("************************"));
  163. SerialMon.println(F(" Is your sim card locked?"));
  164. SerialMon.println(F(" Do you have a good signal?"));
  165. SerialMon.println(F(" Is antenna attached?"));
  166. SerialMon.println(F(" Does the SIM card work with your phone?"));
  167. SerialMon.println(F("************************"));
  168. delay(10000);
  169. return;
  170. }
  171. SerialMon.println(F(" [OK]"));
  172. #if TINY_GSM_USE_GPRS
  173. // GPRS connection parameters are usually set after network registration
  174. SerialMon.print("Connecting to ");
  175. SerialMon.print(apn);
  176. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  177. SerialMon.println(F(" [fail]"));
  178. SerialMon.println(F("************************"));
  179. SerialMon.println(F(" Is GPRS enabled by network provider?"));
  180. SerialMon.println(F(" Try checking your card balance."));
  181. SerialMon.println(F("************************"));
  182. delay(10000);
  183. return;
  184. }
  185. SerialMon.println(F(" [OK]"));
  186. #endif
  187. IPAddress local = modem.localIP();
  188. SerialMon.print("Local IP: ");
  189. SerialMon.println(local);
  190. SerialMon.print(F("Connecting to "));
  191. SerialMon.print(server);
  192. if (!client.connect(server, port)) {
  193. SerialMon.println(F(" [fail]"));
  194. delay(10000);
  195. return;
  196. }
  197. SerialMon.println(F(" [OK]"));
  198. // Make a HTTP GET request:
  199. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  200. client.print(String("Host: ") + server + "\r\n");
  201. client.print("Connection: close\r\n\r\n");
  202. // Wait for data to arrive
  203. while (client.connected() && !client.available()) {
  204. delay(100);
  205. SerialMon.print('.');
  206. };
  207. SerialMon.println();
  208. // Skip all headers
  209. client.find("\r\n\r\n");
  210. // Read data
  211. uint32_t timeout = millis();
  212. uint32_t bytesReceived = 0;
  213. while (client.connected() && millis() - timeout < 10000L) {
  214. while (client.available()) {
  215. char c = client.read();
  216. // SerialMon.print(c);
  217. bytesReceived += 1;
  218. timeout = millis();
  219. }
  220. }
  221. client.stop();
  222. SerialMon.println(F("Server disconnected"));
  223. #if TINY_GSM_USE_WIFI
  224. modem.networkDisconnect();
  225. SerialMon.println(F("WiFi disconnected"));
  226. #endif
  227. #if TINY_GSM_USE_GPRS
  228. modem.gprsDisconnect();
  229. SerialMon.println(F("GPRS disconnected"));
  230. #endif
  231. SerialMon.println();
  232. SerialMon.println(F("************************"));
  233. SerialMon.print(F(" Received: "));
  234. SerialMon.print(bytesReceived);
  235. SerialMon.println(F(" bytes"));
  236. SerialMon.print(F(" Test: "));
  237. SerialMon.println((bytesReceived == 121) ? "PASSED" : "FAILED");
  238. SerialMon.println(F("************************"));
  239. // Do nothing forevermore
  240. while (true) { delay(1000); }
  241. }