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.

251 lines
6.9 KiB

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