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.

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