GPRS Connection Problem

Hi all,

First of all, I must say that I am newbie on Open AT and embedded modules. Please accept my apologies if I ask stupid questions. :slight_smile:

I am using Q2687H embedded modules and developing an application to connect remote server. I have some troubles with GPRS connection part of my code. All the settings are OK for APN and the other initial procedures (I think so :interrobang:). When I download the app to the module to test if it works I got a problem. It gives me WIP_BEV_CONN_FAILED error first but when I remove and insert again the sim card it connects to GPRS. :frowning:

You can see the related code snippets below:

Main:

void adl_main(adl_InitType_e InitType)
{
    switch (InitType)
    {
    case ADL_INIT_POWER_ON:
        break;

    case ADL_INIT_REBOOT_FROM_EXCEPTION:
        ATRSP("\r\nModem Start After Exception\r\n");
        break;

    case ADL_INIT_DOWNLOAD_SUCCESS:
        ATRSP("\r\nModem Start After DOTA OK\r\n");
        break;

    case ADL_INIT_DOWNLOAD_ERROR:
        ATRSP("\r\nModem Start After DOTA ERROR\r\n");
        break;

    case ADL_INIT_RTC:
        ATRSP("\r\nModem Start After RTC\r\n");
        break;
    }

	UART2_Enable();
	GPIO_Enable();
    adl_simSubscribe(Sim_Handler, (ascii *) SimPin);
}

Sim_Handler:

void Sim_Handler(u8 Event)
{
	switch (Event)
	{
	case ADL_SIM_EVENT_FULL_INIT:
		TRACE((1, "ADL_SIM_EVENT_FULL_INIT"));
		GPRS_Enable();
		break;
	case ADL_SIM_EVENT_PIN_WAIT:
		TRACE((1, "ADL_SIM_EVENT_PIN_WAIT"));
		break;
	case ADL_SIM_EVENT_PIN_OK:
		TRACE((1, "ADL_SIM_EVENT_PIN_OK"));
		break;
	case ADL_SIM_EVENT_INSERTED:
		TRACE((1, "ADL_SIM_EVENT_INSERTED"));
		break;
	case ADL_SIM_EVENT_REMOVED:
		TRACE((1, "ADL_SIM_EVENT_REMOVED"));
		break;
	case ADL_SIM_EVENT_PIN_ERROR:
		TRACE((1, "ADL_SIM_EVENT_PIN_ERROR"));
		break;
	case ADL_SIM_EVENT_PUK_WAIT:
		TRACE((1, "ADL_SIM_EVENT_PUK_WAIT"));
		break;
	case ADL_SIM_EVENT_PUK_ERROR:
		TRACE((1, "ADL_SIM_EVENT_PUK_ERROR"));
		break;
	case ADL_SIM_EVENT_FAILURE:
		TRACE((1, "ADL_SIM_EVENT_FAILURE"));
		break;
	case ADL_SIM_EVENT_NET_LOCK:
		TRACE((1, "ADL_SIM_EVENT_NET_LOCK"));
		break;
	case ADL_SIM_EVENT_LAST:
		TRACE((1, "ADL_SIM_EVENT_LAST"));
		break;
	default:
		TRACE((1, "default..."));
		break;
	}
}

GPRS_Enable:

void GPRS_Enable(void)
{
	TRACE (( 1, "GPRS_Enable" ));
    gprsIsValid = FALSE;
    gprsLed(FALSE);

    if (adl_gprsSubscribe(gprs_Handler) == OK)
    {
        s8RetVal = wip_bearerClose(gprs_bearer);
        s8RetVal = wip_netInit();

		if (s8RetVal < 0)
			TRACE((10, "ADL_SIM_EVENT_FULL_INIT : Bearer Close %d", s8RetVal));
		else
			TRACE((10, "ADL_SIM_EVENT_FULL_INIT : Bearer Closed"));

		s8RetVal = wip_bearerOpen(&gprs_bearer, "GPRS", gprsBearerHandler, NULL);

		if (s8RetVal < 0 && s8RetVal != -27)
			TRACE((10, "ADL_SIM_EVENT_FULL_INIT : Bearer Open %d", s8RetVal));
		else
			s8RetVal = wip_bearerSetOpts(gprs_bearer, WIP_BOPT_GPRS_CID,  1, WIP_BOPT_GPRS_APN, APN_Name,  WIP_BOPT_LOGIN,  APN_Username, WIP_BOPT_PASSWORD,  APN_Password,  WIP_BOPT_END);

		if (s8RetVal < 0)
			TRACE((10, "ADL_SIM_EVENT_FULL_INIT : Bearer SetOpts %d", s8RetVal));
		else
			s8RetVal = wip_bearerStart(gprs_bearer);

		if (s8RetVal < 0)
			TRACE((10, "ADL_SIM_EVENT_FULL_INIT : Bearer Start %d", s8RetVal));
		else
			TRACE((10, "ADL_SIM_EVENT_FULL_INIT : Bearer Started %d", s8RetVal));
		
		wm_sprintf(rsp, "status:%d\r\n", s8RetVal);
		ATRSP(rsp);
    }
    else
    {
        ATRSP("\r\nInit error\r\n");
        TRACE((1, "GPRS init error"));
    }
}

gprsBearerHandler:

void gprsBearerHandler(wip_bearer_t br, s8 event, void *ctx)
{
    ascii RspStr[128];
    ascii dns1Str[16], dns2Str[16];
    wip_in_addr_t ip, dns1, dns2;
    s8 errcode = 0;
    u8 gprsCID;

    if (br != gprs_bearer)
        return;

    switch (event)
    {
		case WIP_BEV_IP_CONNECTED:
			TRACE((10, "gprs_bearer: WIP_BEV_IP_CONNECTED"));
			//Get Bearer Info
			s8RetVal = wip_bearerGetOpts(br, WIP_BOPT_IP_ADDR, &ip, WIP_BOPT_IP_DNS1, &dns1, WIP_BOPT_IP_DNS2, &dns2, WIP_BOPT_END);

			if (s8RetVal < 0)
				TRACE((10, "WIP_BEV_IP_CONNECTED : GetOpts %d", s8RetVal));

			wip_inet_ntoa(ip, ipStr, sizeof(ipStr));
			wip_inet_ntoa(dns1, dns1Str, sizeof(dns1Str));
			wip_inet_ntoa(dns2, dns2Str, sizeof(dns2Str));
			wm_sprintf(RspStr, "\r\nGPRS: CONNECTED IP=%s DNS1=%s DNS2=%s\r\n",ipStr, dns1Str, dns2Str);
			gprsIsValid = TRUE;
			gprsLed(TRUE);
			TRACE((10, "WIP_BEV_IP_CONNECTED"));
			ATRSP(RspStr);
			break;

		case WIP_BEV_IP_DISCONNECTED:
			TRACE((10, "gprs_bearer: WIP_BEV_IP_DISCONNECTED"));
			gprsIsValid = FALSE;
			break;

		case WIP_BEV_STOPPED:
			TRACE((10, "gprs_bearer: WIP_BEV_STOPPED"));
			s8RetVal = wip_bearerClose(br);

			if (s8RetVal < 0)
				TRACE((12, "WIP_BEV_STOPPED : Stop %d", s8RetVal));

			gprsIsValid = FALSE;
			gprs_bearer = NULL;
			break;

		case WIP_BEV_CONN_FAILED:
			TRACE((10, "gprs_bearer: WIP_BEV_CONN_FAILED"));
			ATRSP("\r\n+WIPSTART: FAILED\r\n");
			s8RetVal = wip_bearerGetOpts(br, WIP_BOPT_GPRS_CID, &gprsCID, WIP_BOPT_ERROR, &errcode, WIP_BOPT_END);

			if (s8RetVal < 0)
				TRACE((10, "WIP_BEV_CONN_FAILED : GetOpts %d", s8RetVal));

			wm_sprintf(RspStr, "CID:%d\r\n", gprsCID);
			ATRSP(RspStr);
			wm_sprintf(RspStr, "Error Code:%d\r\n", errcode);
			ATRSP(RspStr);
			TRACE((10, "gprs_bearer: CID: %d Error Code: %d", gprsCID, errcode));
			wip_bearerClose(br);

			if (s8RetVal < 0)
				TRACE((10, "WIP_BEV_CONN_FAILED : Close %d", s8RetVal));

			gprsIsValid = FALSE;
			gprs_bearer = NULL;
			break;

		default:
			TRACE((10, "gprs_bearer: DEFAULT"));
			wm_sprintf(RspStr, "wip_bearer Code: %d\r\n", event);
			ATRSP(RspStr);
			break;
    }
}

Edit: I’ve added the Trace logs as following;

Timestamp Id Level Message
2019/01/15;14:15:59:111 - 001 ADL 1 Binary header at 00260000
2019/01/15;14:15:59:111 - 002 ADL 1 Let’s Begin
2019/01/15;14:15:59:111 - 003 ADL 13 UART2 FCM Available
2019/01/15;14:15:59:127 - 001 ADL 13 FCM2 HANDLE RESULT: 0
2019/01/15;14:15:59:127 - 002 ADL 1 LED GPIO Subscribe OK
2019/01/15;14:15:59:127 - 003 ADL 1 GPIO OK
2019/01/15;14:15:59:127 - 004 ADL 1 ADL_SIM_EVENT_INSERTED
2019/01/15;14:15:59:127 - 005 ADL 13 UART2 is in DATA mode
2019/01/15;14:15:59:860 - 001 ADL 1 ADL_SIM_EVENT_PIN_OK
2019/01/15;14:16:04:291 - 001 ADL 1 ADL_SIM_EVENT_FULL_INIT
2019/01/15;14:16:04:291 - 002 ADL 1 GPRS_Enable
2019/01/15;14:16:04:291 - 003 ADL 10 ADL_SIM_EVENT_FULL_INIT : Bearer Closed
2019/01/15;14:16:04:291 - 004 ADL 10 ADL_SIM_EVENT_FULL_INIT : Bearer Start -27
2019/01/15;14:16:04:291 - 005 ADL 12 gprs_handler: Embedded : Event receive 25 CID: 1
2019/01/15;14:16:04:306 - 001 ADL 12 gprs_handler: ADL_GPRS_EVENT_SETUP_OK
2019/01/15;14:16:04:306 - 002 ADL 12 gprs_handler: Embedded : Event receive 25 CID: 1
2019/01/15;14:16:04:306 - 004 ADL 12 gprs_handler: Embedded : Event receive 23 CID: 1
2019/01/15;14:16:04:306 - 005 ADL 12 gprs_handler: ADL_GPRS_EVENT_ACTIVATE_KO : CID 1
2019/01/15;14:16:04:337 - 001 ADL 10 gprs_bearer: WIP_BEV_CONN_FAILED
2019/01/15;14:16:04:337 - 002 ADL 10 gprs_bearer: CID: 1 Error Code: -36
2019/01/15;14:16:18:268 - 001 ADL 1 ADL_SIM_EVENT_REMOVED
2019/01/15;14:16:19:750 - 001 ADL 1 ADL_SIM_EVENT_INSERTED
2019/01/15;14:16:20:655 - 001 ADL 1 ADL_SIM_EVENT_PIN_OK
2019/01/15;14:16:25:257 - 001 ADL 1 ADL_SIM_EVENT_FULL_INIT
2019/01/15;14:16:25:273 - 001 ADL 1 GPRS_Enable
2019/01/15;14:16:25:273 - 002 ADL 10 ADL_SIM_EVENT_FULL_INIT : Bearer Closed
2019/01/15;14:16:25:273 - 003 ADL 10 ADL_SIM_EVENT_FULL_INIT : Bearer Start -27
2019/01/15;14:16:25:273 - 004 ADL 12 gprs_handler: Embedded : Event receive 25 CID: 1
2019/01/15;14:16:25:273 - 005 ADL 12 gprs_handler: ADL_GPRS_EVENT_SETUP_OK
2019/01/15;14:16:25:288 - 001 ADL 12 gprs_handler: Embedded : Event receive 25 CID: 1
2019/01/15;14:16:25:288 - 002 ADL 12 gprs_handler: ADL_GPRS_EVENT_SETUP_OK
2019/01/15;14:16:26:255 - 001 ADL 12 gprs_handler: Embedded : Event receive 27 CID: 1
2019/01/15;14:16:26:255 - 002 ADL 12 gprs_handler: ADL_GPRS_EVENT_ME_ATTACH
2019/01/15;14:16:26:271 - 001 ADL 12 gprs_handler: Embedded : Event receive 27 CID: 1
2019/01/15;14:16:26:271 - 002 ADL 12 gprs_handler: ADL_GPRS_EVENT_ME_ATTACH
2019/01/15;14:16:27:737 - 001 ADL 12 gprs_handler: Embedded : Event receive 16 CID: 1
2019/01/15;14:16:27:737 - 002 ADL 12 gprs_handler: ADL_GPRS_EVENT_ANSWER_OK_FROM_EXT
2019/01/15;14:16:27:753 - 001 ADL 12 gprs_handler: Embedded : Event receive 16 CID: 1
2019/01/15;14:16:27:753 - 002 ADL 12 gprs_handler: ADL_GPRS_EVENT_ANSWER_OK_FROM_EXT
2019/01/15;14:16:27:769 - 001 ADL 12 gprs_handler: Embedded : Event receive 16 CID: 1
2019/01/15;14:16:27:769 - 002 ADL 12 gprs_handler: ADL_GPRS_EVENT_ANSWER_OK_FROM_EXT
2019/01/15;14:16:27:769 - 003 ADL 10 gprs_bearer: WIP_BEV_IP_CONNECTED
2019/01/15;14:16:27:769 - 004 ADL 10 WIP_BEV_IP_CONNECTED

Could anyone give me a suggestion about the problem?

Note: ATRSP is a simple function to send string to TMConsole.

Thanks in advance.

Best regards

MÇ.

Does this problem happen to sample application like tcp client?

1 Like

Hi again @jyijyi :slight_smile:

I didn’t try it yet. I will try and let you know.

By the way, is everything OK with my code?

Thanks.

MÇ.

Seems ok but better compare with sample code.

Hi @jyijyi,

I’ve tested a TCP Client Sample application and it worked. It seems the promblem is in my code :frowning:

Best regards.

MÇ.

probably the root cause you didn´t check AT+CREG?..
seems you just wait for SIM full init, but there is a chance that it is not yet registered to the network.

Hi @jyijyi

Thanks for your reply.

So, you mean that I should check “AT+CREG?” and “ADL_SIM_EVENT_FULL_INIT” both together and then GPRS_Enable right?

Best regards.

MÇ.

correct.
I remember the TCP client sample code also did the same thing.

Hi @jyijyi

Thank you very much. I will try and post the result.

Best regards.

MÇ.

Hi @jyijyi

I am sorry, I forgot to post the result :slight_smile:

I’ve used the gprs sample codes and it worked. So I can connect to the GPRS network now. After that I’ve added some more code to SET and CLEAR the GPRS LED indicator on the device as following:

To define section:

#define GPIO_LED	2
adl_ioDefs_t GPRSLED_PORT = ADL_IO_GPIO | 2;
adl_ioDefs_t LedGpioConfig[GPIO_LED] = { ADL_IO_GPIO | 1 | ADL_IO_DIR_OUT | ADL_IO_LEV_LOW, ADL_IO_GPIO | 2 | ADL_IO_DIR_OUT | ADL_IO_LEV_LOW };
s32 LedGpioHandle;

The related functions:

void gprsLed(bool state)
{
	if (state)
		adl_ioWriteSingle(LedGpioHandle, &GPRSLED_PORT, TRUE);
	else
		adl_ioWriteSingle(LedGpioHandle, &GPRSLED_PORT, FALSE);
}

void GPIO_Enable(void)
{
	LedGpioHandle = adl_ioSubscribe(GPIO_LED, (adl_ioDefs_t *) LedGpioConfig, 0, 0, 0);
	if (LedGpioHandle > 0)
		TRACE((1, "LED GPIO Subscribe ==> OK"));
	else
		TRACE((1, "LED GPIO Subscribe FAILED --> %d", LedGpioHandle));
}

These codes working very well. I am turning on the LED when I got the “WIP_BEV_IP_CONNECTED” event and turning it off when I got the “ADL_GPRS_EVENT_ME_DETACH” or “ADL_GPRS_EVENT_ME_UNREG” events.

Then I wanted to use the GSM network time and write it to RTC time. I’ve write the following code

in Main:

//To subscribe to WIND 15 message
s16 s16RetVal =	adl_atUnSoSubscribe("+WIND: 15", (adl_atUnSoHandler_t) Wind15Handler);

and the WIND handler is:

bool Wind15Handler(adl_atUnsolicited_t *paras)
{
	TRACE((4, "-> (GSM Time) Enter."));

    adl_atUnSoUnSubscribe("+WIND: 15", (adl_atUnSoHandler_t) Wind15Handler);
    //TRACE((4, paras->StrData));
    wm_strGetParameterString(TimeUnmodified, paras->StrData, 7);
	TRACE((4, TimeUnmodified));
    adl_rtcTime_t  RTCTime;
    ascii atCmd[30];

    // YYYYMMDD HHMMSS
    RTCTime.Year = 2000 + wm_atoi(parseString(TimeUnmodified,0,2));
    RTCTime.Month = wm_atoi(parseString(TimeUnmodified,3,2));
    RTCTime.Day = wm_atoi(parseString(TimeUnmodified,6,2));
    RTCTime.Hour = wm_atoi(parseString(TimeUnmodified,9,2)) + 3;
    RTCTime.Minute = wm_atoi(parseString(TimeUnmodified,12,2));
    RTCTime.Second = wm_atoi(parseString(TimeUnmodified,15,2));
    adl_rtcSetTime (&RTCTime );
    wm_sprintf(atCmd, "\r\nGSM Time: \"%02d/%02d/%02d,%02d:%02d:%02d\"\r\n", RTCTime.Year - 2000, RTCTime.Month, RTCTime.Day, RTCTime.Hour, RTCTime.Minute, RTCTime.Second);
    TRACE((4, atCmd));
    ATRSP(atCmd);
    adl_atCmdCreate(atCmd, FALSE, (adl_atRspHandler_t) NULL, NULL);
	return TRUE;
}

Edit: The Output: GSM Time: "19/01/18,16:52:08"

YES, it is working. Everything is perfect till here. :muscle:

I said “… till here” because I could not be able to use both of the functions together (turning on-off the GPRS LED and getting GSM network time). When I comment out one of them the other one is working. It is so strange to me. I am confused :confused:

Do you have any idea?

Thanks in advance.

Best regards.

MÇ.

Network time should not be related to gprs.
You can try to get the network time first, then start the gprs.

1 Like

I mean “GSM Network Time”. Sorry my bad.

Can you receive event WIP_BEV_IP_CONNECTED after wind: 15 ?

1 Like

Hi @jyijyi

Their times are very close. I mean the “WIP_BEV_IP_CONNECTED” and “+WIND: 15” message times. But I get the “+WIND: 15” message first.

Best regards.

MÇ.

Since you can receive both event, Do you mean the problem is from adl_ioWriteSingle()?

1 Like

Hi @jyijyi

Thanks for your reply.

I will work on this later. This issue has not high priority at this moment. :slight_smile:

I will post the result when I done it.

Best regards.

MÇ.