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.

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