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.

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