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.

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