Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the GSM connection procedure #21

Open
1 task done
gideonmaina opened this issue Jan 26, 2024 · 0 comments
Open
1 task done

Improve the GSM connection procedure #21

gideonmaina opened this issue Jan 26, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@gideonmaina
Copy link
Contributor

The connectGSM() function can be modified to return a boolean type to determine whether GPRS functionality is available on setup. The return value will determine whether or not to switch to a WiFi connection.

The procedure can be improved by:

  1. Setting additional flags:
  • SIM_available/usable: This will determine whether we can use the SIM even if GPRS is not available
  • GPRS_active: This can be set after running a test to ping a DNS
  1. Steps to initiate the service
  • The obvious is to check for serial communication with the GSM chip. If improperly configured set gsm_capable flag to false
  • Check if SIM is available. There are a few ways to do this. e.g AT+CCID
  • Check if PIN is set/and or attempt to set pin:
  • Check whether the module is registered to a network. At this point, the SIM_available flag can be set.
  • Enable GPRS
  • Run a connection test and set the GPRS_active flag
  1. Audit the amount of time it takes to connect to a network. A warmup time delay would be suitable during the initialization process.

*** Suggestions ***
Rename function to initGSM(). This function can be called when restarting the GSM either via hardware or software serial.

/****
 //ToDo:
 1.  Change return type to boolean
 2.  Set flag to check if SIM is inserted/usable
 //? change function name to initGSM() maybe to set different flags. GSM restart can call this function;
 ***/
void connectGSM()
{

	int retry_count = 0;

	fonaSerial->begin(4800);
	if (!fona.begin(*fonaSerial))
	{
		debug_outln(F("Couldn't find FONA"), DEBUG_MIN_INFO);

		debug_outln(F("Switching to Wifi"), DEBUG_MIN_INFO);
		gsm_capable = 0;
		connectWifi(); // ToDo: Return false instead
	}
	else
	{
		debug_outln(F("FONA is OK"), DEBUG_MIN_INFO); //
													  // ToDO: Check if SIM is installed. Run CCID
		unlock_pin();								  // ToDO check if PIN is set. If not return false and set SIM usable and GPRS capable flags to false

		char imei[16] = {0}; // MUST use a 16 character buffer for IMEI!
		uint8_t imeiLen = fona.getIMEI(imei);
		if (imeiLen > 0)
		{
			debug_outln(F("Module IMEI: "), DEBUG_MIN_INFO);
			debug_out(String(imei), DEBUG_MIN_INFO);
		}

		fona.setGPRSNetworkSettings(F("internet"), F(""), F(""));

		while ((fona.getNetworkStatus() != GSM_CONNECTED) && (retry_count < 40))
		{
			Serial.println("Not registered on network");
			delay(5000); // ?: Takes 2.5mins until until retry_count=30. It will take 5sec longer every other time and GSM is always restarted!
			retry_count++;

			if (retry_count > 30) // ?: Review this. Seems to be increasing the connect time. Maybe try 3 times
			{
				delay(5000);
				restart_GSM();
			}

			flushSerial();
		}

		if (fona.getNetworkStatus() != GSM_CONNECTED) // ToDo: Return false and enable wifi connect somewhere else
		{
			String fss(cfg::fs_ssid);
			display_debug(fss.substring(0, 16), fss.substring(16));

			wifiConfig();
			if (fona.getNetworkStatus() != GSM_CONNECTED)
			{
				retry_count = 0;
				while ((fona.getNetworkStatus() != GSM_CONNECTED) && (retry_count < 20))
				{
					delay(500);
					debug_outln(".", DEBUG_MIN_INFO);
					retry_count++;
				}
				debug_outln("", DEBUG_MIN_INFO);
			}
		}
		else
		{
			enableGPRS(); // ToDO: Expect a boolean value from this and set GPRS flag
			Serial.println("GPRS ENABLED");
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants