mirror of
				https://github.com/KevinMidboe/TinyGSM.git
				synced 2025-10-29 18:00:18 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			432 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			432 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /**************************************************************
 | |
|  *
 | |
|  * TinyGSM Getting Started guide:
 | |
|  *   https://tiny.cc/tinygsm-readme
 | |
|  *
 | |
|  * NOTE:
 | |
|  * Some of the functions may be unavailable for your modem.
 | |
|  * Just comment them out.
 | |
|  *
 | |
|  **************************************************************/
 | |
| 
 | |
| // Select your modem:
 | |
| #define TINY_GSM_MODEM_SIM800
 | |
| // #define TINY_GSM_MODEM_SIM808
 | |
| // #define TINY_GSM_MODEM_SIM868
 | |
| // #define TINY_GSM_MODEM_SIM900
 | |
| // #define TINY_GSM_MODEM_SIM7000
 | |
| // #define TINY_GSM_MODEM_SIM5360
 | |
| // #define TINY_GSM_MODEM_SIM7600
 | |
| // #define TINY_GSM_MODEM_UBLOX
 | |
| // #define TINY_GSM_MODEM_SARAR4
 | |
| // #define TINY_GSM_MODEM_M95
 | |
| // #define TINY_GSM_MODEM_BG96
 | |
| // #define TINY_GSM_MODEM_A6
 | |
| // #define TINY_GSM_MODEM_A7
 | |
| // #define TINY_GSM_MODEM_M590
 | |
| // #define TINY_GSM_MODEM_MC60
 | |
| // #define TINY_GSM_MODEM_MC60E
 | |
| // #define TINY_GSM_MODEM_ESP8266
 | |
| // #define TINY_GSM_MODEM_XBEE
 | |
| // #define TINY_GSM_MODEM_SEQUANS_MONARCH
 | |
| 
 | |
| // Set serial for debug console (to the Serial Monitor, default speed 115200)
 | |
| #define SerialMon Serial
 | |
| 
 | |
| // Set serial for AT commands (to the module)
 | |
| // Use Hardware Serial on Mega, Leonardo, Micro
 | |
| #define SerialAT Serial1
 | |
| 
 | |
| // or Software Serial on Uno, Nano
 | |
| //#include <SoftwareSerial.h>
 | |
| //SoftwareSerial SerialAT(2, 3); // RX, TX
 | |
| 
 | |
| // See all AT commands, if wanted
 | |
| // #define DUMP_AT_COMMANDS
 | |
| 
 | |
| // Define the serial console for debug prints, if needed
 | |
| #define TINY_GSM_DEBUG SerialMon
 | |
| 
 | |
| // Range to attempt to autobaud
 | |
| #define GSM_AUTOBAUD_MIN 9600
 | |
| #define GSM_AUTOBAUD_MAX 115200
 | |
| 
 | |
| /*
 | |
|  * Tests enabled
 | |
|  */
 | |
| #define TINY_GSM_TEST_GPRS true
 | |
| #define TINY_GSM_TEST_WIFI false
 | |
| #define TINY_GSM_TEST_TCP true
 | |
| #define TINY_GSM_TEST_SSL true
 | |
| #define TINY_GSM_TEST_CALL false
 | |
| #define TINY_GSM_TEST_SMS false
 | |
| #define TINY_GSM_TEST_USSD false
 | |
| #define TINY_GSM_TEST_BATTERY true
 | |
| #define TINY_GSM_TEST_TEMPERATURE true
 | |
| #define TINY_GSM_TEST_GSM_LOCATION true
 | |
| #define TINY_GSM_TEST_TIME true
 | |
| #define TINY_GSM_TEST_GPS true
 | |
| // powerdown modem after tests
 | |
| #define TINY_GSM_POWERDOWN false
 | |
| 
 | |
| // set GSM PIN, if any
 | |
| #define GSM_PIN ""
 | |
| 
 | |
| // Set phone numbers, if you want to test SMS and Calls
 | |
| // #define SMS_TARGET  "+380xxxxxxxxx"
 | |
| // #define CALL_TARGET "+380xxxxxxxxx"
 | |
| 
 | |
| // Your GPRS credentials, if any
 | |
| const char apn[]  = "YourAPN";
 | |
| const char gprsUser[] = "";
 | |
| const char gprsPass[] = "";
 | |
| 
 | |
| // Your WiFi connection credentials, if applicable
 | |
| const char wifiSSID[]  = "YourSSID";
 | |
| const char wifiPass[] = "YourWiFiPass";
 | |
| 
 | |
| // Server details to test TCP/SSL
 | |
| const char server[] = "vsh.pp.ua";
 | |
| const char resource[] = "/TinyGSM/logo.txt";
 | |
| 
 | |
| #include <TinyGsmClient.h>
 | |
| 
 | |
| #if TINY_GSM_TEST_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS
 | |
| #undef TINY_GSM_TEST_GPRS
 | |
| #undef TINY_GSM_TEST_WIFI
 | |
| #define TINY_GSM_TEST_GPRS false
 | |
| #define TINY_GSM_TEST_WIFI true
 | |
| #endif
 | |
| #if TINY_GSM_TEST_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI
 | |
| #undef TINY_GSM_USE_GPRS
 | |
| #undef TINY_GSM_USE_WIFI
 | |
| #define TINY_GSM_USE_GPRS true
 | |
| #define TINY_GSM_USE_WIFI false
 | |
| #endif
 | |
| 
 | |
| #ifdef DUMP_AT_COMMANDS
 | |
|   #include <StreamDebugger.h>
 | |
|   StreamDebugger debugger(SerialAT, SerialMon);
 | |
|   TinyGsm modem(debugger);
 | |
| #else
 | |
|   TinyGsm modem(SerialAT);
 | |
| #endif
 | |
| 
 | |
| void setup() {
 | |
|   // Set console baud rate
 | |
|   SerialMon.begin(115200);
 | |
|   delay(10);
 | |
| 
 | |
|   // !!!!!!!!!!!
 | |
|   // Set your reset, enable, power pins here
 | |
|   // !!!!!!!!!!!
 | |
| 
 | |
|   DBG("Wait...");
 | |
|   delay(6000);
 | |
| 
 | |
|   // Set GSM module baud rate
 | |
|   TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
 | |
|   // SerialAT.begin(9600);
 | |
| }
 | |
| 
 | |
| void loop() {
 | |
|   // Restart takes quite some time
 | |
|   // To skip it, call init() instead of restart()
 | |
|   DBG("Initializing modem...");
 | |
|   if (!modem.restart()) {
 | |
|     // if (!modem.init()) {
 | |
|     DBG("Failed to restart modem, delaying 10s and retrying");
 | |
|     delay(10000);
 | |
|     // restart autobaud in case GSM just rebooted
 | |
|     // TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
 | |
|     return;
 | |
|   }
 | |
| 
 | |
|   String name = modem.getModemName();
 | |
|   DBG("Modem Name:", name);
 | |
| 
 | |
|   String modemInfo = modem.getModemInfo();
 | |
|   DBG("Modem Info:", modemInfo);
 | |
| 
 | |
| #if TINY_GSM_TEST_GPRS
 | |
|   // Unlock your SIM card with a PIN if needed
 | |
|   if (GSM_PIN && modem.getSimStatus() != 3) {
 | |
|     modem.simUnlock(GSM_PIN);
 | |
|   }
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_WIFI
 | |
|   DBG("Setting SSID/password...");
 | |
|   if (!modem.networkConnect(wifiSSID, wifiPass)) {
 | |
|     DBG(" fail");
 | |
|     delay(10000);
 | |
|     return;
 | |
|   }
 | |
|   SerialMon.println(" success");
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_GPRS && defined TINY_GSM_MODEM_XBEE
 | |
|   // The XBee must run the gprsConnect function BEFORE waiting for network!
 | |
|   modem.gprsConnect(apn, gprsUser, gprsPass);
 | |
| #endif
 | |
| 
 | |
|   DBG("Waiting for network...");
 | |
|   if (!modem.waitForNetwork(600000L)) {
 | |
|     delay(10000);
 | |
|     return;
 | |
|   }
 | |
| 
 | |
|   if (modem.isNetworkConnected()) {
 | |
|     DBG("Network connected");
 | |
|   }
 | |
| 
 | |
| #if TINY_GSM_TEST_GPRS
 | |
|   DBG("Connecting to", apn);
 | |
|   if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
 | |
|     delay(10000);
 | |
|     return;
 | |
|   }
 | |
| 
 | |
|   bool res = modem.isGprsConnected();
 | |
|   DBG("GPRS status:", res ? "connected" : "not connected");
 | |
| 
 | |
|   String ccid = modem.getSimCCID();
 | |
|   DBG("CCID:", ccid);
 | |
| 
 | |
|   String imei = modem.getIMEI();
 | |
|   DBG("IMEI:", imei);
 | |
| 
 | |
|   String imsi = modem.getIMSI();
 | |
|   DBG("IMSI:", imsi);
 | |
| 
 | |
|   String cop = modem.getOperator();
 | |
|   DBG("Operator:", cop);
 | |
| 
 | |
|   IPAddress local = modem.localIP();
 | |
|   DBG("Local IP:", local);
 | |
| 
 | |
|   int csq = modem.getSignalQuality();
 | |
|   DBG("Signal quality:", csq);
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_USSD && defined TINY_GSM_MODEM_HAS_SMS
 | |
|   String ussd_balance = modem.sendUSSD("*111#");
 | |
|   DBG("Balance (USSD):", ussd_balance);
 | |
| 
 | |
|   String ussd_phone_num = modem.sendUSSD("*161#");
 | |
|   DBG("Phone number (USSD):", ussd_phone_num);
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_TCP && defined TINY_GSM_MODEM_HAS_TCP
 | |
|   TinyGsmClient client(modem);
 | |
|   const int port = 80;
 | |
|   DBG("Connecting to ", server);
 | |
|   if (!client.connect(server, port)) {
 | |
|     DBG("... failed");
 | |
|   } else {
 | |
|     // Make a HTTP GET request:
 | |
|     client.print(String("GET ") + resource + " HTTP/1.0\r\n");
 | |
|     client.print(String("Host: ") + server + "\r\n");
 | |
|     client.print("Connection: close\r\n\r\n");
 | |
| 
 | |
|     // Wait for data to arrive
 | |
|     while (client.connected() && !client.available()) {
 | |
|       delay(100);
 | |
|     };
 | |
| 
 | |
|     // Read data
 | |
|     uint32_t timeout = millis();
 | |
|     while (client.connected() && millis() - timeout < 10000L) {
 | |
|       while (client.available()) {
 | |
|         SerialMon.write(client.read());
 | |
|         timeout = millis();
 | |
|       }
 | |
|     }
 | |
|     client.stop();
 | |
|   }
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_SSL && defined TINY_GSM_MODEM_HAS_SSL
 | |
|   TinyGsmClientSecure secureClient(modem);
 | |
|   const int securePort = 443;
 | |
|   DBG("Connecting to ", server);
 | |
|   if (!secureClient.connect(server, securePort)) {
 | |
|     DBG("... failed");
 | |
|   } else {
 | |
|     // Make a HTTP GET request:
 | |
|     secureClient.print(String("GET ") + resource + " HTTP/1.0\r\n");
 | |
|     secureClient.print(String("Host: ") + server + "\r\n");
 | |
|     secureClient.print("Connection: close\r\n\r\n");
 | |
| 
 | |
|     // Wait for data to arrive
 | |
|     while (secureClient.connected() && !secureClient.available()) {
 | |
|       delay(100);
 | |
|     };
 | |
| 
 | |
|     // Read data
 | |
|     uint32_t timeoutS = millis();
 | |
|     while (secureClient.connected() && millis() - timeoutS < 10000L) {
 | |
|       while (secureClient.available()) {
 | |
|         SerialMon.write(secureClient.read());
 | |
|         timeoutS = millis();
 | |
|       }
 | |
|     }
 | |
|     secureClient.stop();
 | |
|   }
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_CALL && defined TINY_GSM_MODEM_HAS_CALLING && \
 | |
|     defined CALL_TARGET
 | |
|   DBG("Calling:", CALL_TARGET);
 | |
| 
 | |
|   // This is NOT supported on M590
 | |
|   res = modem.callNumber(CALL_TARGET);
 | |
|   DBG("Call:", res ? "OK" : "fail");
 | |
| 
 | |
|   if (res) {
 | |
|     delay(1000L);
 | |
| 
 | |
|     // Play DTMF A, duration 1000ms
 | |
|     modem.dtmfSend('A', 1000);
 | |
| 
 | |
|     // Play DTMF 0..4, default duration (100ms)
 | |
|     for (char tone = '0'; tone <= '4'; tone++) {
 | |
|       modem.dtmfSend(tone);
 | |
|     }
 | |
| 
 | |
|     delay(5000);
 | |
| 
 | |
|     res = modem.callHangup();
 | |
|     DBG("Hang up:", res ? "OK" : "fail");
 | |
|   }
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_SMS && defined TINY_GSM_MODEM_HAS_SMS && defined SMS_TARGET
 | |
|   res = modem.sendSMS(SMS_TARGET, String("Hello from ") + imei);
 | |
|   DBG("SMS:", res ? "OK" : "fail");
 | |
| 
 | |
|   // This is only supported on SIMxxx series
 | |
|   res = modem.sendSMS_UTF8_begin(SMS_TARGET);
 | |
|   if (res) {
 | |
|     auto stream = modem.sendSMS_UTF8_stream();
 | |
|     stream.print(F("Привіііт! Print number: "));
 | |
|     stream.print(595);
 | |
|     res = modem.sendSMS_UTF8_end();
 | |
|   }
 | |
|   DBG("UTF8 SMS:", res ? "OK" : "fail");
 | |
| 
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_GSM_LOCATION && defined TINY_GSM_MODEM_HAS_GSM_LOCATION
 | |
|   String location = modem.getGsmLocation();
 | |
|   DBG("GSM Based Location String:", location);
 | |
|   float lat      = 0;
 | |
|   float lon      = 0;
 | |
|   float accuracy = 0;
 | |
|   int   year     = 0;
 | |
|   int   month    = 0;
 | |
|   int   day      = 0;
 | |
|   int   hour     = 0;
 | |
|   int   min      = 0;
 | |
|   int   sec      = 0;
 | |
|   if (modem.getGsmLocation(&lat, &lon, &accuracy, &year, &month, &day, &hour,
 | |
|                            &min, &sec)) {
 | |
|     DBG("Latitude:", String(lat, 8));
 | |
|     DBG("Longitude:", String(lon, 8));
 | |
|     DBG("Accuracy:", accuracy);
 | |
|     DBG("Year:", year);
 | |
|     DBG("Month:", month);
 | |
|     DBG("Day:", day);
 | |
|     DBG("Hour:", hour);
 | |
|     DBG("Minute:", min);
 | |
|     DBG("Second:", sec);
 | |
|   } else {
 | |
|     DBG("Couldn't get GSM location");
 | |
|   }
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_GPS && defined TINY_GSM_MODEM_HAS_GPS
 | |
|   modem.enableGPS();
 | |
|   String gps_raw = modem.getGPSraw();
 | |
|   DBG("GPS/GNSS Based Location String:", gps_raw);
 | |
|   float lat2      = 0;
 | |
|   float lon2      = 0;
 | |
|   float speed2    = 0;
 | |
|   int   alt2      = 0;
 | |
|   int   vsat2     = 0;
 | |
|   int   usat2     = 0;
 | |
|   float accuracy2 = 0;
 | |
|   int   year2     = 0;
 | |
|   int   month2    = 0;
 | |
|   int   day2      = 0;
 | |
|   int   hour2     = 0;
 | |
|   int   min2      = 0;
 | |
|   int   sec2      = 0;
 | |
|   if (modem.getGPS(&lat2, &lon, &speed2, &alt2, &vsat2, &usat2, &accuracy2,
 | |
|                    &year2, &month2, &day2, &hour2, &min2, &sec2)) {
 | |
|     DBG("Latitude:", String(lat2, 8));
 | |
|     DBG("Longitude:", String(lon2, 8));
 | |
|     DBG("Speed:", speed2);
 | |
|     DBG("Altitude:", alt2);
 | |
|     DBG("Visible Satellites:", vsat2);
 | |
|     DBG("Used Satellites:", usat2);
 | |
|     DBG("Accuracy:", accuracy2);
 | |
|     DBG("Year:", year2);
 | |
|     DBG("Month:", month2);
 | |
|     DBG("Day:", day2);
 | |
|     DBG("Hour:", hour2);
 | |
|     DBG("Minute:", min2);
 | |
|     DBG("Second:", sec2);
 | |
|   } else {
 | |
|     DBG("Couldn't get GPS/GNSS location");
 | |
|   }
 | |
|   modem.disableGPS();
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_TIME && defined TINY_GSM_MODEM_HAS_TIME
 | |
|   String time = modem.getGSMDateTime(DATE_FULL);
 | |
|   DBG("Current Network Time:", time);
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_GPRS
 | |
|   modem.gprsDisconnect();
 | |
|   if (!modem.isGprsConnected()) {
 | |
|     DBG("GPRS disconnected");
 | |
|   } else {
 | |
|     DBG("GPRS disconnect: Failed.");
 | |
|   }
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_WIFI
 | |
|   modem.networkDisconnect();
 | |
|   DBG("WiFi disconnected");
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_BATTERY && defined TINY_GSM_MODEM_HAS_BATTERY
 | |
|   uint8_t  chargeState = -99;
 | |
|   int8_t   percent     = -99;
 | |
|   uint16_t milliVolts  = -9999;
 | |
|   modem.getBattStats(chargeState, percent, milliVolts);
 | |
|   DBG("Battery charge state:", chargeState);
 | |
|   DBG("Battery charge 'percent':", percent);
 | |
|   DBG("Battery voltage:", milliVolts / 1000.0F);
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_TEST_TEMPERATURE && defined TINY_GSM_MODEM_HAS_TEMPERATURE
 | |
|   float temp = modem.getTemperature();
 | |
|   DBG("Chip temperature:", temp);
 | |
| #endif
 | |
| 
 | |
| #if TINY_GSM_POWERDOWN
 | |
|   // Try to power-off (modem may decide to restart automatically)
 | |
|   // To turn off modem completely, please use Reset/Enable pins
 | |
|   modem.poweroff();
 | |
|   DBG("Poweroff.");
 | |
| #endif
 | |
| 
 | |
|   // Do nothing forevermore
 | |
|   while (true) {
 | |
|     modem.maintain();
 | |
|   }
 | |
| }
 |