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.

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