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.

377 lines
9.6 KiB

6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
  1. /**************************************************************
  2. *
  3. * TinyGSM Getting Started guide:
  4. * https://tiny.cc/tinygsm-readme
  5. *
  6. * NOTE:
  7. * Some of the functions may be unavailable for your modem.
  8. * Just comment them out.
  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. // See all AT commands, if wanted
  40. // #define DUMP_AT_COMMANDS
  41. // Define the serial console for debug prints, if needed
  42. #define TINY_GSM_DEBUG SerialMon
  43. // Range to attempt to autobaud
  44. #define GSM_AUTOBAUD_MIN 9600
  45. #define GSM_AUTOBAUD_MAX 38400
  46. /*
  47. * Tests enabled
  48. */
  49. #define TINY_GSM_TEST_GPRS true
  50. #define TINY_GSM_TEST_WIFI false
  51. #define TINY_GSM_TEST_TCP true
  52. #define TINY_GSM_TEST_SSL true
  53. #define TINY_GSM_TEST_CALL false
  54. #define TINY_GSM_TEST_SMS false
  55. #define TINY_GSM_TEST_USSD false
  56. #define TINY_GSM_TEST_BATTERY true
  57. #define TINY_GSM_TEST_TEMPERATURE true
  58. #define TINY_GSM_TEST_GSM_LOCATION true
  59. #define TINY_GSM_TEST_TIME true
  60. #define TINY_GSM_TEST_GPS true
  61. // powerdown modem after tests
  62. #define TINY_GSM_POWERDOWN false
  63. // set GSM PIN, if any
  64. #define GSM_PIN ""
  65. // Set phone numbers, if you want to test SMS and Calls
  66. // #define SMS_TARGET "+380xxxxxxxxx"
  67. // #define CALL_TARGET "+380xxxxxxxxx"
  68. // Your GPRS credentials, if any
  69. const char apn[] = "YourAPN";
  70. const char gprsUser[] = "";
  71. const char gprsPass[] = "";
  72. // Your WiFi connection credentials, if applicable
  73. const char wifiSSID[] = "YourSSID";
  74. const char wifiPass[] = "YourWiFiPass";
  75. // Server details to test TCP/SSL
  76. const char server[] = "vsh.pp.ua";
  77. const char resource[] = "/TinyGSM/logo.txt";
  78. #include <TinyGsmClient.h>
  79. #if TINY_GSM_TEST_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
  80. #undef TINY_GSM_TEST_GPRS
  81. #undef TINY_GSM_TEST_WIFI
  82. #define TINY_GSM_TEST_GPRS false
  83. #define TINY_GSM_TEST_WIFI true
  84. #endif
  85. #if TINY_GSM_TEST_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
  86. #undef TINY_GSM_USE_GPRS
  87. #undef TINY_GSM_USE_WIFI
  88. #define TINY_GSM_USE_GPRS true
  89. #define TINY_GSM_USE_WIFI false
  90. #endif
  91. #ifdef DUMP_AT_COMMANDS
  92. #include <StreamDebugger.h>
  93. StreamDebugger debugger(SerialAT, SerialMon);
  94. TinyGsm modem(debugger);
  95. #else
  96. TinyGsm modem(SerialAT);
  97. #endif
  98. void setup() {
  99. // Set console baud rate
  100. SerialMon.begin(115200);
  101. delay(10);
  102. // !!!!!!!!!!!
  103. // Set your reset, enable, power pins here
  104. // !!!!!!!!!!!
  105. DBG("Wait...");
  106. delay(6000);
  107. // Set GSM module baud rate
  108. TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
  109. // SerialAT.begin(9600);
  110. }
  111. void loop() {
  112. // Restart takes quite some time
  113. // To skip it, call init() instead of restart()
  114. DBG("Initializing modem...");
  115. if (!modem.restart()) {
  116. // if (!modem.init()) {
  117. DBG("Failed to restart modem, delaying 10s and retrying");
  118. delay(10000);
  119. // restart autobaud in case GSM just rebooted
  120. // TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
  121. return;
  122. }
  123. String name = modem.getModemName();
  124. DBG("Modem Name:", name);
  125. String modemInfo = modem.getModemInfo();
  126. DBG("Modem Info:", modemInfo);
  127. #if TINY_GSM_TEST_GPRS
  128. // Unlock your SIM card with a PIN if needed
  129. if (GSM_PIN && modem.getSimStatus() != 3) {
  130. modem.simUnlock(GSM_PIN);
  131. }
  132. #endif
  133. #if TINY_GSM_TEST_WIFI
  134. DBG("Setting SSID/password...");
  135. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  136. DBG(" fail");
  137. delay(10000);
  138. return;
  139. }
  140. SerialMon.println(" success");
  141. #endif
  142. #if TINY_GSM_TEST_GPRS && defined TINY_GSM_MODEM_XBEE
  143. // The XBee must run the gprsConnect function BEFORE waiting for network!
  144. modem.gprsConnect(apn, gprsUser, gprsPass);
  145. #endif
  146. DBG("Waiting for network...");
  147. if (!modem.waitForNetwork(600000L)) {
  148. delay(10000);
  149. return;
  150. }
  151. if (modem.isNetworkConnected()) {
  152. DBG("Network connected");
  153. }
  154. #if TINY_GSM_TEST_GPRS
  155. DBG("Connecting to", apn);
  156. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  157. delay(10000);
  158. return;
  159. }
  160. bool res = modem.isGprsConnected();
  161. DBG("GPRS status:", res ? "connected" : "not connected");
  162. String ccid = modem.getSimCCID();
  163. DBG("CCID:", ccid);
  164. String imei = modem.getIMEI();
  165. DBG("IMEI:", imei);
  166. String imsi = modem.getIMSI();
  167. DBG("IMSI:", imsi);
  168. String cop = modem.getOperator();
  169. DBG("Operator:", cop);
  170. IPAddress local = modem.localIP();
  171. DBG("Local IP:", local);
  172. int csq = modem.getSignalQuality();
  173. DBG("Signal quality:", csq);
  174. #endif
  175. #if TINY_GSM_TEST_USSD && defined TINY_GSM_MODEM_HAS_SMS
  176. String ussd_balance = modem.sendUSSD("*111#");
  177. DBG("Balance (USSD):", ussd_balance);
  178. String ussd_phone_num = modem.sendUSSD("*161#");
  179. DBG("Phone number (USSD):", ussd_phone_num);
  180. #endif
  181. #if TINY_GSM_TEST_TCP && defined TINY_GSM_MODEM_HAS_TCP
  182. TinyGsmClient client(modem);
  183. const int port = 80;
  184. DBG("Connecting to ", server);
  185. if (!client.connect(server, port)) {
  186. DBG("... failed");
  187. } else {
  188. // Make a HTTP GET request:
  189. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  190. client.print(String("Host: ") + server + "\r\n");
  191. client.print("Connection: close\r\n\r\n");
  192. // Wait for data to arrive
  193. while (client.connected() && !client.available()) {
  194. delay(100);
  195. };
  196. // Read data
  197. uint32_t timeout = millis();
  198. while (client.connected() && millis() - timeout < 10000L) {
  199. while (client.available()) {
  200. SerialMon.write(client.read());
  201. timeout = millis();
  202. }
  203. }
  204. client.stop();
  205. }
  206. #endif
  207. #if TINY_GSM_TEST_SSL && defined TINY_GSM_MODEM_HAS_SSL
  208. TinyGsmClientSecure secureClient(modem);
  209. const int securePort = 443;
  210. DBG("Connecting to ", server);
  211. if (!secureClient.connect(server, securePort)) {
  212. DBG("... failed");
  213. } else {
  214. // Make a HTTP GET request:
  215. secureClient.print(String("GET ") + resource + " HTTP/1.0\r\n");
  216. secureClient.print(String("Host: ") + server + "\r\n");
  217. secureClient.print("Connection: close\r\n\r\n");
  218. // Wait for data to arrive
  219. while (secureClient.connected() && !secureClient.available()) {
  220. delay(100);
  221. };
  222. // Read data
  223. uint32_t timeoutS = millis();
  224. while (secureClient.connected() && millis() - timeoutS < 10000L) {
  225. while (secureClient.available()) {
  226. SerialMon.write(secureClient.read());
  227. timeoutS = millis();
  228. }
  229. }
  230. secureClient.stop();
  231. }
  232. #endif
  233. #if TINY_GSM_TEST_GPS && defined TINY_GSM_MODEM_HAS_GPS
  234. modem.enableGPS();
  235. String gps_raw = modem.getGPSraw();
  236. modem.disableGPS();
  237. DBG("GPS raw data:", gps_raw);
  238. #endif
  239. #if TINY_GSM_TEST_SMS && defined TINY_GSM_MODEM_HAS_SMS && defined SMS_TARGET
  240. res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
  241. DBG("SMS:", res ? "OK" : "fail");
  242. // This is only supported on SIMxxx series
  243. res = modem.sendSMS_UTF8_begin(SMS_TARGET);
  244. if (res) {
  245. auto stream = modem.sendSMS_UTF8_stream();
  246. stream.print(F("Привіііт! Print number: "));
  247. stream.print(595);
  248. res = modem.sendSMS_UTF8_end();
  249. }
  250. DBG("UTF8 SMS:", res ? "OK" : "fail");
  251. #endif
  252. #if TINY_GSM_TEST_CALL && defined TINY_GSM_MODEM_HAS_CALLING && \
  253. defined CALL_TARGET
  254. DBG("Calling:", CALL_TARGET);
  255. // This is NOT supported on M590
  256. res = modem.callNumber(CALL_TARGET);
  257. DBG("Call:", res ? "OK" : "fail");
  258. if (res) {
  259. delay(1000L);
  260. // Play DTMF A, duration 1000ms
  261. modem.dtmfSend('A', 1000);
  262. // Play DTMF 0..4, default duration (100ms)
  263. for (char tone = '0'; tone <= '4'; tone++) {
  264. modem.dtmfSend(tone);
  265. }
  266. delay(5000);
  267. res = modem.callHangup();
  268. DBG("Hang up:", res ? "OK" : "fail");
  269. }
  270. #endif
  271. #if TINY_GSM_TEST_BATTERY && defined TINY_GSM_MODEM_HAS_BATTERY
  272. uint8_t chargeState = -99;
  273. int8_t percent = -99;
  274. uint16_t milliVolts = -9999;
  275. modem.getBattStats(chargeState, percent, milliVolts);
  276. DBG("Battery charge state:", chargeState);
  277. DBG("Battery charge 'percent':", percent);
  278. DBG("Battery voltage:", milliVolts / 1000.0F);
  279. #endif
  280. #if TINY_GSM_TEST_TEMPERATURE && defined TINY_GSM_MODEM_HAS_TEMPERATURE
  281. float temp = modem.getTemperature();
  282. DBG("Chip temperature:", temp);
  283. #endif
  284. #if TINY_GSM_TEST_GSM_LOCATION && defined TINY_GSM_MODEM_HAS_GSM_LOCATION
  285. String location = modem.getGsmLocation();
  286. DBG("GSM Based Location:", location);
  287. #endif
  288. #if TINY_GSM_TEST_TIME && defined TINY_GSM_MODEM_HAS_TIME
  289. String time = modem.getGSMDateTime(DATE_FULL);
  290. DBG("Current Network Time:", time);
  291. #endif
  292. #if TINY_GSM_TEST_GPRS
  293. modem.gprsDisconnect();
  294. if (!modem.isGprsConnected()) {
  295. DBG("GPRS disconnected");
  296. } else {
  297. DBG("GPRS disconnect: Failed.");
  298. }
  299. #endif
  300. #if TINY_GSM_TEST_WIFI
  301. modem.networkDisconnect();
  302. DBG("WiFi disconnected");
  303. #endif
  304. #if TINY_GSM_POWERDOWN
  305. // Try to power-off (modem may decide to restart automatically)
  306. // To turn off modem completely, please use Reset/Enable pins
  307. modem.poweroff();
  308. DBG("Poweroff.");
  309. #endif
  310. // Do nothing forevermore
  311. while (true) {
  312. modem.maintain();
  313. }
  314. }