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.

252 lines
7.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
8 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
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
  60. // Leave empty, if missing user or pass
  61. const char apn[] = "YourAPN";
  62. const char gprsUser[] = "";
  63. const char gprsPass[] = "";
  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. #ifdef DUMP_AT_COMMANDS
  71. #include <StreamDebugger.h>
  72. StreamDebugger debugger(SerialAT, SerialMon);
  73. TinyGsm modem(debugger);
  74. #else
  75. TinyGsm modem(SerialAT);
  76. #endif
  77. #ifdef USE_SSL
  78. TinyGsmClientSecure client(modem);
  79. const int port = 443;
  80. #else
  81. TinyGsmClient client(modem);
  82. const int port = 80;
  83. #endif
  84. void setup() {
  85. // Set console baud rate
  86. SerialMon.begin(115200);
  87. delay(10);
  88. // !!!!!!!!!!!
  89. // Set your reset, enable, power pins here
  90. // !!!!!!!!!!!
  91. SerialMon.println("Wait...");
  92. // Set GSM module baud rate
  93. TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
  94. // SerialAT.begin(115200);
  95. delay(3000);
  96. }
  97. void loop() {
  98. // Restart takes quite some time
  99. // To skip it, call init() instead of restart()
  100. SerialMon.print("Initializing modem...");
  101. if (!modem.restart()) {
  102. // if (!modem.init()) {
  103. SerialMon.println(F(" [fail]"));
  104. SerialMon.println(F("************************"));
  105. SerialMon.println(F(" Is your modem connected properly?"));
  106. SerialMon.println(F(" Is your serial speed (baud rate) correct?"));
  107. SerialMon.println(F(" Is your modem powered on?"));
  108. SerialMon.println(F(" Do you use a good, stable power source?"));
  109. SerialMon.println(F(" Try useing File -> Examples -> TinyGSM -> tools -> AT_Debug to find correct configuration"));
  110. SerialMon.println(F("************************"));
  111. delay(10000);
  112. return;
  113. }
  114. SerialMon.println(F(" [OK]"));
  115. String modemInfo = modem.getModemInfo();
  116. SerialMon.print("Modem Info: ");
  117. SerialMon.println(modemInfo);
  118. #if TINY_GSM_USE_GPRS
  119. // Unlock your SIM card with a PIN if needed
  120. if ( GSM_PIN && modem.getSimStatus() != 3 ) {
  121. modem.simUnlock(GSM_PIN);
  122. }
  123. #endif
  124. #if TINY_GSM_USE_WIFI
  125. SerialMon.print(F("Setting SSID/password..."));
  126. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  127. SerialMon.println(" fail");
  128. delay(10000);
  129. return;
  130. }
  131. SerialMon.println(" success");
  132. #endif
  133. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE
  134. // The XBee must run the gprsConnect function BEFORE waiting for network!
  135. modem.gprsConnect(apn, gprsUser, gprsPass);
  136. #endif
  137. SerialMon.print("Waiting for network...");
  138. if (!modem.waitForNetwork(600000L)) { // You may need lengthen this in poor service areas
  139. SerialMon.println(F(" [fail]"));
  140. SerialMon.println(F("************************"));
  141. SerialMon.println(F(" Is your sim card locked?"));
  142. SerialMon.println(F(" Do you have a good signal?"));
  143. SerialMon.println(F(" Is antenna attached?"));
  144. SerialMon.println(F(" Does the SIM card work with your phone?"));
  145. SerialMon.println(F("************************"));
  146. delay(10000);
  147. return;
  148. }
  149. SerialMon.println(F(" [OK]"));
  150. #if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_HAS_GPRS
  151. SerialMon.print("Connecting to ");
  152. SerialMon.print(apn);
  153. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  154. SerialMon.println(F(" [fail]"));
  155. SerialMon.println(F("************************"));
  156. SerialMon.println(F(" Is GPRS enabled by network provider?"));
  157. SerialMon.println(F(" Try checking your card balance."));
  158. SerialMon.println(F("************************"));
  159. delay(10000);
  160. return;
  161. }
  162. SerialMon.println(F(" [OK]"));
  163. #endif
  164. IPAddress local = modem.localIP();
  165. SerialMon.print("Local IP: ");
  166. SerialMon.println(local);
  167. SerialMon.print(F("Connecting to "));
  168. SerialMon.print(server);
  169. if (!client.connect(server, port)) {
  170. SerialMon.println(F(" [fail]"));
  171. delay(10000);
  172. return;
  173. }
  174. SerialMon.println(F(" [OK]"));
  175. // Make a HTTP GET request:
  176. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  177. client.print(String("Host: ") + server + "\r\n");
  178. client.print("Connection: close\r\n\r\n");
  179. // Wait for data to arrive
  180. while (client.connected() && !client.available()) {
  181. delay(100);
  182. SerialMon.print('.');
  183. };
  184. SerialMon.println();
  185. // Skip all headers
  186. client.find("\r\n\r\n");
  187. // Read data
  188. unsigned long timeout = millis();
  189. unsigned long bytesReceived = 0;
  190. while (client.connected() && millis() - timeout < 10000L) {
  191. while (client.available()) {
  192. char c = client.read();
  193. //SerialMon.print(c);
  194. bytesReceived += 1;
  195. timeout = millis();
  196. }
  197. }
  198. client.stop();
  199. SerialMon.println(F("Server disconnected"));
  200. modem.gprsDisconnect();
  201. SerialMon.println(F("GPRS disconnected"));
  202. SerialMon.println();
  203. SerialMon.println(F("************************"));
  204. SerialMon.print (F(" Received: "));
  205. SerialMon.print(bytesReceived);
  206. SerialMon.println(F(" bytes"));
  207. SerialMon.print (F(" Test: "));
  208. SerialMon.println((bytesReceived == 121) ? "PASSED" : "FAILED");
  209. SerialMon.println(F("************************"));
  210. // Do nothing forevermore
  211. while (true) {
  212. delay(1000);
  213. }
  214. }