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.

187 lines
5.2 KiB

  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_SIM900
  15. #define TINY_GSM_MODEM_SIM7000
  16. // #define TINY_GSM_MODEM_UBLOX
  17. // #define TINY_GSM_MODEM_BG96
  18. // #define TINY_GSM_MODEM_A6
  19. // #define TINY_GSM_MODEM_A7
  20. // #define TINY_GSM_MODEM_M590
  21. // #define TINY_GSM_MODEM_ESP8266
  22. // #define TINY_GSM_MODEM_XBEE
  23. // Increase the buffer
  24. #define TINY_GSM_RX_BUFFER 512
  25. // Define the serial console for debug prints, if needed
  26. //#define TINY_GSM_DEBUG Serial
  27. #include <TinyGsmClient.h>
  28. // Your GPRS credentials
  29. // Leave empty, if missing user or pass
  30. const char apn[] = "drei.at";
  31. const char user[] = "";
  32. const char pass[] = "";
  33. // Set serial for debug console (to the Serial Monitor, speed 115200)
  34. #define SerialMon Serial
  35. // Set serial for AT commands (to the module)
  36. // Use Hardware Serial on Mega, Leonardo, Micro, Esp32
  37. #include "HardwareSerial.h"
  38. #define SerialAT Serial2
  39. // or Software Serial on Uno, Nano
  40. //#include <SoftwareSerial.h>
  41. //SoftwareSerial SerialAT(2, 3); // RX, TX
  42. #include <StreamDebugger.h>
  43. StreamDebugger debugger(SerialAT, SerialMon);
  44. TinyGsm modem(debugger);
  45. const char server[] = "vsh.pp.ua";
  46. const char resource[] = "/TinyGSM/logo.txt";
  47. const int port = 80;
  48. TinyGsmClient client(modem);
  49. // For SSL:
  50. //const int port = 443;
  51. //TinyGsmClientSecure client(modem);
  52. void setup() {
  53. // Set console baud rate
  54. Serial.println("Starting Serial");
  55. SerialMon.begin(115200);
  56. delay(10);
  57. // Set GSM module baud rate
  58. SerialAT.begin(9600);
  59. delay(3000);
  60. }
  61. void loop() {
  62. // Restart takes quite some time
  63. // To skip it, call init() instead of restart()
  64. SerialMon.print("Initializing modem...");
  65. if (!modem.restart()) {
  66. SerialMon.println(F(" [fail]"));
  67. SerialMon.println(F("************************"));
  68. SerialMon.println(F(" Is your modem connected properly?"));
  69. SerialMon.println(F(" Is your serial speed (baud rate) correct?"));
  70. SerialMon.println(F(" Is your modem powered on?"));
  71. SerialMon.println(F(" Do you use a good, stable power source?"));
  72. SerialMon.println(F(" Try useing File -> Examples -> TinyGSM -> tools -> AT_Debug to find correct configuration"));
  73. SerialMon.println(F("************************"));
  74. delay(10000);
  75. return;
  76. }
  77. SerialMon.println(F(" [OK]"));
  78. String modemInfo = modem.getModemInfo();
  79. SerialMon.print("Modem: ");
  80. SerialMon.println(modemInfo);
  81. // Unlock your SIM card with a PIN
  82. //modem.simUnlock("1234");
  83. SerialMon.print("Waiting for network...");
  84. if (!modem.waitForNetwork()) {
  85. SerialMon.println(F(" [fail]"));
  86. SerialMon.println(F("************************"));
  87. SerialMon.println(F(" Is your sim card locked?"));
  88. SerialMon.println(F(" Do you have a good signal?"));
  89. SerialMon.println(F(" Is antenna attached?"));
  90. SerialMon.println(F(" Does the SIM card work with your phone?"));
  91. SerialMon.println(F("************************"));
  92. delay(10000);
  93. return;
  94. }
  95. SerialMon.println(F(" [OK]"));
  96. SerialMon.print("Connecting to ");
  97. SerialMon.print(apn);
  98. if (!modem.gprsConnect(apn, user, pass)) {
  99. SerialMon.println(F(" [fail]"));
  100. SerialMon.println(F("************************"));
  101. SerialMon.println(F(" Is GPRS enabled by network provider?"));
  102. SerialMon.println(F(" Try checking your card balance."));
  103. SerialMon.println(F("************************"));
  104. delay(10000);
  105. return;
  106. }
  107. SerialMon.println(F(" [OK]"));
  108. IPAddress local = modem.localIP();
  109. SerialMon.print("Local IP: ");
  110. SerialMon.println(local);
  111. SerialMon.print(F("Connecting to "));
  112. SerialMon.print(server);
  113. if (!client.connect(server, port)) {
  114. SerialMon.println(F(" [fail]"));
  115. delay(10000);
  116. return;
  117. }
  118. SerialMon.println(F(" [OK]"));
  119. // Make a HTTP GET request:
  120. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  121. client.print(String("Host: ") + server + "\r\n");
  122. client.print("Connection: close\r\n\r\n");
  123. // Wait for data to arrive
  124. while (client.connected() && !client.available()) {
  125. delay(100);
  126. SerialMon.print('.');
  127. };
  128. SerialMon.println();
  129. // Skip all headers
  130. client.find("\r\n\r\n");
  131. // Read data
  132. unsigned long timeout = millis();
  133. unsigned long bytesReceived = 0;
  134. while (client.connected() && millis() - timeout < 10000L) {
  135. while (client.available()) {
  136. char c = client.read();
  137. //SerialMon.print(c);
  138. bytesReceived += 1;
  139. timeout = millis();
  140. }
  141. }
  142. client.stop();
  143. SerialMon.println(F("Server disconnected"));
  144. modem.gprsDisconnect();
  145. SerialMon.println(F("GPRS disconnected"));
  146. SerialMon.println();
  147. SerialMon.println(F("************************"));
  148. SerialMon.print (F(" Received: "));
  149. SerialMon.print(bytesReceived);
  150. SerialMon.println(F(" bytes"));
  151. SerialMon.print (F(" Test: "));
  152. SerialMon.println((bytesReceived == 121) ? "PASSED" : "FAILED");
  153. SerialMon.println(F("************************"));
  154. // Do nothing forevermore
  155. while (true) {
  156. delay(1000);
  157. }
  158. }