mirror of
				https://github.com/KevinMidboe/TinyGSM.git
				synced 2025-10-29 18:00:18 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			166 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			166 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/**************************************************************
 | 
						|
 *
 | 
						|
 * TinyGSM Getting Started guide:
 | 
						|
 *   http://tiny.cc/tiny-gsm-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_SIM900
 | 
						|
// #define TINY_GSM_MODEM_A6
 | 
						|
// #define TINY_GSM_MODEM_A7
 | 
						|
// #define TINY_GSM_MODEM_M590
 | 
						|
// #define TINY_GSM_MODEM_ESP8266
 | 
						|
 | 
						|
// Set serial for debug console (to the Serial Monitor, 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
 | 
						|
 | 
						|
 | 
						|
//#define DUMP_AT_COMMANDS
 | 
						|
#define TINY_GSM_DEBUG SerialMon
 | 
						|
 | 
						|
// Set phone numbers, if you want to test SMS and Calls
 | 
						|
//#define SMS_TARGET  "+380xxxxxxxxx"
 | 
						|
//#define CALL_TARGET "+380xxxxxxxxx"
 | 
						|
 | 
						|
// Your GPRS credentials
 | 
						|
// Leave empty, if missing user or pass
 | 
						|
const char apn[]  = "YourAPN";
 | 
						|
const char user[] = "";
 | 
						|
const char pass[] = "";
 | 
						|
 | 
						|
#include <TinyGsmClient.h>
 | 
						|
 | 
						|
#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 GSM module baud rate
 | 
						|
  TinyGsmAutoBaud(SerialAT);
 | 
						|
  delay(3000);
 | 
						|
}
 | 
						|
 | 
						|
void loop() {
 | 
						|
 | 
						|
  // Restart takes quite some time
 | 
						|
  // To skip it, call init() instead of restart()
 | 
						|
  DBG("Initializing modem...");
 | 
						|
  if (!modem.restart()) {
 | 
						|
    delay(10000);
 | 
						|
    return;
 | 
						|
  }
 | 
						|
 | 
						|
  String modemInfo = modem.getModemInfo();
 | 
						|
  DBG("Modem:", modemInfo);
 | 
						|
 | 
						|
  // Unlock your SIM card with a PIN
 | 
						|
  //modem.simUnlock("1234");
 | 
						|
 | 
						|
  DBG("Waiting for network...");
 | 
						|
  if (!modem.waitForNetwork()) {
 | 
						|
    delay(10000);
 | 
						|
    return;
 | 
						|
  }
 | 
						|
 | 
						|
  DBG("Connecting to", apn);
 | 
						|
  if (!modem.gprsConnect(apn, user, pass)) {
 | 
						|
    delay(10000);
 | 
						|
    return;
 | 
						|
  }
 | 
						|
 | 
						|
  bool res;
 | 
						|
 | 
						|
  String ccid = modem.getSimCCID();
 | 
						|
  DBG("CCID:", ccid);
 | 
						|
 | 
						|
  String imei = modem.getIMEI();
 | 
						|
  DBG("IMEI:", imei);
 | 
						|
 | 
						|
  String cop = modem.getOperator();
 | 
						|
  DBG("Operator:", cop);
 | 
						|
  
 | 
						|
  IPAddress local = modem.localIP();
 | 
						|
  DBG("Local IP:", local);
 | 
						|
 | 
						|
  int csq = modem.getSignalQuality();
 | 
						|
  DBG("Signal quality:", csq);
 | 
						|
 | 
						|
  // This is NOT supported on M590
 | 
						|
  int battLevel = modem.getBattPercent();
 | 
						|
  DBG("Battery lavel:", battLevel);
 | 
						|
 | 
						|
  // This is only supported on SIMxxx series
 | 
						|
  float battVoltage = modem.getBattVoltage() / 1000.0F;
 | 
						|
  DBG("Battery voltage:", battVoltage);
 | 
						|
 | 
						|
  // This is only supported on SIMxxx series
 | 
						|
  String gsmLoc = modem.getGsmLocation();
 | 
						|
  DBG("GSM location:", gsmLoc);
 | 
						|
 | 
						|
  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);
 | 
						|
 | 
						|
#ifdef 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_UTF16(SMS_TARGET, u"Привіііт!", 9);
 | 
						|
  DBG("UTF16 SMS:", res ? "OK" : "fail");
 | 
						|
#endif
 | 
						|
 | 
						|
#ifdef 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(5000L);
 | 
						|
  
 | 
						|
    res = modem.callHangup();
 | 
						|
    DBG("Hang up:", res ? "OK" : "fail");
 | 
						|
  }
 | 
						|
#endif
 | 
						|
 | 
						|
  modem.gprsDisconnect();
 | 
						|
  DBG("GPRS disconnected");
 | 
						|
 | 
						|
  // Try to power-off (modem may decide to restart automatically)
 | 
						|
  // To turn off modem completely, please use Reset/Enable pins
 | 
						|
  modem.poweroff();
 | 
						|
  DBG("Poweroff.");
 | 
						|
 | 
						|
  // Do nothing forevermore
 | 
						|
  while (true) {
 | 
						|
    modem.maintain();
 | 
						|
  }
 | 
						|
}
 | 
						|
 |