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.

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