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.

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