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.

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