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.

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