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.

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