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.

464 lines
12 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
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 115200
  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 true
  54. #define TINY_GSM_TEST_SMS true
  55. #define TINY_GSM_TEST_USSD true
  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. pinMode(23, OUTPUT);
  105. digitalWrite(23, HIGH);
  106. // !!!!!!!!!!!
  107. DBG("Wait...");
  108. delay(6000);
  109. // Set GSM module baud rate
  110. TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
  111. // SerialAT.begin(9600);
  112. }
  113. void loop() {
  114. // Restart takes quite some time
  115. // To skip it, call init() instead of restart()
  116. DBG("Initializing modem...");
  117. if (!modem.restart()) {
  118. // if (!modem.init()) {
  119. DBG("Failed to restart modem, delaying 10s and retrying");
  120. delay(10000L);
  121. // restart autobaud in case GSM just rebooted
  122. // TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
  123. return;
  124. }
  125. String name = modem.getModemName();
  126. DBG("Modem Name:", name);
  127. String modemInfo = modem.getModemInfo();
  128. DBG("Modem Info:", modemInfo);
  129. #if TINY_GSM_TEST_GPRS
  130. // Unlock your SIM card with a PIN if needed
  131. if (GSM_PIN && modem.getSimStatus() != 3) {
  132. modem.simUnlock(GSM_PIN);
  133. }
  134. #endif
  135. #if TINY_GSM_TEST_WIFI
  136. DBG("Setting SSID/password...");
  137. if (!modem.networkConnect(wifiSSID, wifiPass)) {
  138. DBG(" fail");
  139. delay(10000);
  140. return;
  141. }
  142. SerialMon.println(" success");
  143. #endif
  144. #if TINY_GSM_TEST_GPRS && defined TINY_GSM_MODEM_XBEE
  145. // The XBee must run the gprsConnect function BEFORE waiting for network!
  146. modem.gprsConnect(apn, gprsUser, gprsPass);
  147. #endif
  148. DBG("Waiting for network...");
  149. if (!modem.waitForNetwork(600000L)) {
  150. delay(10000);
  151. return;
  152. }
  153. if (modem.isNetworkConnected()) {
  154. DBG("Network connected");
  155. }
  156. #if TINY_GSM_TEST_GPRS
  157. DBG("Connecting to", apn);
  158. if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
  159. delay(10000);
  160. return;
  161. }
  162. bool res = modem.isGprsConnected();
  163. DBG("GPRS status:", res ? "connected" : "not connected");
  164. String ccid = modem.getSimCCID();
  165. DBG("CCID:", ccid);
  166. String imei = modem.getIMEI();
  167. DBG("IMEI:", imei);
  168. String imsi = modem.getIMSI();
  169. DBG("IMSI:", imsi);
  170. String cop = modem.getOperator();
  171. DBG("Operator:", cop);
  172. IPAddress local = modem.localIP();
  173. DBG("Local IP:", local);
  174. int csq = modem.getSignalQuality();
  175. DBG("Signal quality:", csq);
  176. #endif
  177. #if TINY_GSM_TEST_USSD && defined TINY_GSM_MODEM_HAS_SMS
  178. String ussd_balance = modem.sendUSSD("*111#");
  179. DBG("Balance (USSD):", ussd_balance);
  180. String ussd_phone_num = modem.sendUSSD("*161#");
  181. DBG("Phone number (USSD):", ussd_phone_num);
  182. #endif
  183. #if TINY_GSM_TEST_TCP && defined TINY_GSM_MODEM_HAS_TCP
  184. TinyGsmClient client(modem, 0);
  185. const int port = 80;
  186. DBG("Connecting to ", server);
  187. if (!client.connect(server, port)) {
  188. DBG("... failed");
  189. } else {
  190. // Make a HTTP GET request:
  191. client.print(String("GET ") + resource + " HTTP/1.0\r\n");
  192. client.print(String("Host: ") + server + "\r\n");
  193. client.print("Connection: close\r\n\r\n");
  194. // Wait for data to arrive
  195. uint32_t start = millis();
  196. while (client.connected() && !client.available() &&
  197. millis() - start < 30000L) {
  198. delay(100);
  199. };
  200. // Read data
  201. start = millis();
  202. while (client.connected() && millis() - start < 5000L) {
  203. while (client.available()) {
  204. SerialMon.write(client.read());
  205. start = millis();
  206. }
  207. }
  208. client.stop();
  209. }
  210. #endif
  211. #if TINY_GSM_TEST_SSL && defined TINY_GSM_MODEM_HAS_SSL
  212. TinyGsmClientSecure secureClient(modem, 1);
  213. const int securePort = 443;
  214. DBG("Connecting to ", server);
  215. if (!secureClient.connect(server, securePort)) {
  216. DBG("... failed");
  217. } else {
  218. // Make a HTTP GET request:
  219. secureClient.print(String("GET ") + resource + " HTTP/1.0\r\n");
  220. secureClient.print(String("Host: ") + server + "\r\n");
  221. secureClient.print("Connection: close\r\n\r\n");
  222. // Wait for data to arrive
  223. uint32_t startS = millis();
  224. while (secureClient.connected() && !secureClient.available() &&
  225. millis() - startS < 30000L) {
  226. delay(100);
  227. };
  228. // Read data
  229. startS = millis();
  230. while (secureClient.connected() && millis() - startS < 5000L) {
  231. while (secureClient.available()) {
  232. SerialMon.write(secureClient.read());
  233. startS = millis();
  234. }
  235. }
  236. secureClient.stop();
  237. }
  238. #endif
  239. #if TINY_GSM_TEST_CALL && defined TINY_GSM_MODEM_HAS_CALLING && \
  240. defined CALL_TARGET
  241. DBG("Calling:", CALL_TARGET);
  242. // This is NOT supported on M590
  243. res = modem.callNumber(CALL_TARGET);
  244. DBG("Call:", res ? "OK" : "fail");
  245. if (res) {
  246. delay(1000L);
  247. // Play DTMF A, duration 1000ms
  248. modem.dtmfSend('A', 1000);
  249. // Play DTMF 0..4, default duration (100ms)
  250. for (char tone = '0'; tone <= '4'; tone++) {
  251. modem.dtmfSend(tone);
  252. }
  253. delay(5000);
  254. res = modem.callHangup();
  255. DBG("Hang up:", res ? "OK" : "fail");
  256. }
  257. #endif
  258. #if TINY_GSM_TEST_SMS && defined TINY_GSM_MODEM_HAS_SMS && defined SMS_TARGET
  259. res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
  260. DBG("SMS:", res ? "OK" : "fail");
  261. // This is only supported on SIMxxx series
  262. res = modem.sendSMS_UTF8_begin(SMS_TARGET);
  263. if (res) {
  264. auto stream = modem.sendSMS_UTF8_stream();
  265. stream.print(F("Привіііт! Print number: "));
  266. stream.print(595);
  267. res = modem.sendSMS_UTF8_end();
  268. }
  269. DBG("UTF8 SMS:", res ? "OK" : "fail");
  270. #endif
  271. #if TINY_GSM_TEST_GSM_LOCATION && defined TINY_GSM_MODEM_HAS_GSM_LOCATION
  272. float lat = 0;
  273. float lon = 0;
  274. float accuracy = 0;
  275. int year = 0;
  276. int month = 0;
  277. int day = 0;
  278. int hour = 0;
  279. int min = 0;
  280. int sec = 0;
  281. for (int8_t i = 5; i; i--) {
  282. DBG("Requesting current GSM location");
  283. if (modem.getGsmLocation(&lat, &lon, &accuracy, &year, &month, &day, &hour,
  284. &min, &sec)) {
  285. DBG("Latitude:", String(lat, 8), "\tLongitude:", String(lon, 8));
  286. DBG("Accuracy:", accuracy);
  287. DBG("Year:", year, "\tMonth:", month, "\tDay:", day);
  288. DBG("Hour:", hour, "\tMinute:", min, "\tSecond:", sec);
  289. break;
  290. } else {
  291. DBG("Couldn't get GSM location, retrying in 15s.");
  292. delay(15000L);
  293. }
  294. }
  295. DBG("Retrieving GSM location again as a string");
  296. String location = modem.getGsmLocation();
  297. DBG("GSM Based Location String:", location);
  298. #endif
  299. #if TINY_GSM_TEST_GPS && defined TINY_GSM_MODEM_HAS_GPS
  300. DBG("Enabling GPS/GNSS/GLONASS and waiting 15s for warm-up");
  301. modem.enableGPS();
  302. delay(15000L);
  303. float lat2 = 0;
  304. float lon2 = 0;
  305. float speed2 = 0;
  306. int alt2 = 0;
  307. int vsat2 = 0;
  308. int usat2 = 0;
  309. float accuracy2 = 0;
  310. int year2 = 0;
  311. int month2 = 0;
  312. int day2 = 0;
  313. int hour2 = 0;
  314. int min2 = 0;
  315. int sec2 = 0;
  316. for (int8_t i = 5; i; i--) {
  317. DBG("Requesting current GPS/GNSS/GLONASS location");
  318. if (modem.getGPS(&lat2, &lon2, &speed2, &alt2, &vsat2, &usat2, &accuracy2,
  319. &year2, &month2, &day2, &hour2, &min2, &sec2)) {
  320. DBG("Latitude:", String(lat2, 8), "\tLongitude:", String(lon2, 8));
  321. DBG("Speed:", speed2, "\tAltitude:", alt2);
  322. DBG("Visible Satellites:", vsat2, "\tUsed Satellites:", usat2);
  323. DBG("Accuracy:", accuracy2);
  324. DBG("Year:", year2, "\tMonth:", month2, "\tDay:", day2);
  325. DBG("Hour:", hour2, "\tMinute:", min2, "\tSecond:", sec2);
  326. break;
  327. } else {
  328. DBG("Couldn't get GPS/GNSS/GLONASS location, retrying in 15s.");
  329. delay(15000L);
  330. }
  331. }
  332. DBG("Retrieving GPS/GNSS/GLONASS location again as a string");
  333. String gps_raw = modem.getGPSraw();
  334. DBG("GPS/GNSS Based Location String:", gps_raw);
  335. DBG("Disabling GPS");
  336. modem.disableGPS();
  337. #endif
  338. #if TINY_GSM_TEST_TIME && defined TINY_GSM_MODEM_HAS_TIME
  339. int year3 = 0;
  340. int month3 = 0;
  341. int day3 = 0;
  342. int hour3 = 0;
  343. int min3 = 0;
  344. int sec3 = 0;
  345. float timezone = 0;
  346. for (int8_t i = 5; i; i--) {
  347. DBG("Requesting current network time");
  348. if (modem.getNetworkTime(&year3, &month3, &day3, &hour3, &min3, &sec3,
  349. &timezone)) {
  350. DBG("Year:", year3, "\tMonth:", month3, "\tDay:", day3);
  351. DBG("Hour:", hour3, "\tMinute:", min3, "\tSecond:", sec3);
  352. DBG("Timezone:", timezone);
  353. break;
  354. } else {
  355. DBG("Couldn't get network time, retrying in 15s.");
  356. delay(15000L);
  357. }
  358. }
  359. DBG("Retrieving time again as a string");
  360. String time = modem.getGSMDateTime(DATE_FULL);
  361. DBG("Current Network Time:", time);
  362. #endif
  363. #if TINY_GSM_TEST_GPRS
  364. modem.gprsDisconnect();
  365. delay(5000L);
  366. if (!modem.isGprsConnected()) {
  367. DBG("GPRS disconnected");
  368. } else {
  369. DBG("GPRS disconnect: Failed.");
  370. }
  371. #endif
  372. #if TINY_GSM_TEST_WIFI
  373. modem.networkDisconnect();
  374. DBG("WiFi disconnected");
  375. #endif
  376. #if TINY_GSM_TEST_BATTERY && defined TINY_GSM_MODEM_HAS_BATTERY
  377. uint8_t chargeState = -99;
  378. int8_t percent = -99;
  379. uint16_t milliVolts = -9999;
  380. modem.getBattStats(chargeState, percent, milliVolts);
  381. DBG("Battery charge state:", chargeState);
  382. DBG("Battery charge 'percent':", percent);
  383. DBG("Battery voltage:", milliVolts / 1000.0F);
  384. #endif
  385. #if TINY_GSM_TEST_TEMPERATURE && defined TINY_GSM_MODEM_HAS_TEMPERATURE
  386. float temp = modem.getTemperature();
  387. DBG("Chip temperature:", temp);
  388. #endif
  389. #if TINY_GSM_POWERDOWN
  390. // Try to power-off (modem may decide to restart automatically)
  391. // To turn off modem completely, please use Reset/Enable pins
  392. modem.poweroff();
  393. DBG("Poweroff.");
  394. #endif
  395. DBG("End of tests.");
  396. // Do nothing forevermore
  397. while (true) {
  398. modem.maintain();
  399. }
  400. }