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.

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