Cellular Driver and PPP

As some of you may know, I have been working on getting a stable PPP connection for the least few weeks since the latest SDK was released.

My progress up until last week was looking good until I decided the leave the unit working over night. I woke to find that the modem was no longer responding to AT commands or at least it appeared to not be. On checking the TXD line from the modem with a scope, I could see that the modem was indeed sending out replies but the code was not seeing them. My debug output in OnLineReceived was not being fired.

I have spent the last few days tracking this down and I appear to have this now working. It does require code outside of the Cellular Driver to recover from this and I have also done some changes to the Cellular Driver to add extra handling to give additional features that assist with this. It basically comes down to the fact that when the modem drops the connection, you need to run a few tasks to close the PPP connection and re-start the background serial handler. Something I was not doing and sometimes due to the way the code runs, especially if the PPP connection does not connect when called. This is now fixed in the driver.

I woke this morning to find that after approx 24 hours running, it is still connecting to PPP and after a short time the GSM network sees that I am not sending so drops the connection and then my code recovers from this and restarts the connection after a short time. Itā€™s been doing this with success on every reconnect now.

Just in time too as I have to deliver 2 demo units next week. Talk about cutting it close :slight_smile:

Iā€™ll be leaving this on test over the weekend so if all goes to plan I will release the modified driver to Codeshare but please note that this is targeted for Pure NETMF and not Gadgeteer. Using the original driver will allow you to port this to Gadgeteer with ease. My version allows you to set the baud rate and I have been testing with 115200 with good success and using hardware handshaking.

8 Likes

@ Dave McLaughlin - What modem are you using?

SIM5320E

It also works with the SIM900B that I have.

1 Like

@ Dave McLaughlin - My deepest respect for this work. I appreciate this a lot. :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Hi Dave,

I have been doing some work with your driver and a SIMCOM 5216A and have been getting an exception in the background thread.

After some debugging I think it might be a typo in the code CGREG handling around line 1075. I wonder in the index != 1 should be an != -1


try
{
   if (response.IndexOf(",") != 1)
      greg = int.Parse(response.Split(',')[1]);
   else
      greg = int.Parse(response);
}
catch(Exception)
{ }

Thanks for sharing you code, itā€™s been a great help

@ KiwiBryn
blog.devmobile.co.nz

1 Like

Hi Bryn

That could well be. I do occasionally get the exception but it gets handled and continues on. Your fix would stop this happening. Iā€™ll implement this and test and then update the codeshare.

Thanks for testing it :slight_smile:

PSā€¦ I just shipped the first 2 prototypes to the client today so fingers crossed the drive behaves itself. :slight_smile:

1 Like

Hi Dave,

Would it be possible for you to extend your sample code to show the steps to initiate a 2.5/3G data connection? I want to make an HttpWebRequest but I donā€™t ever see a NetworkChange.NetworkAvailabilityChanged event, is that expected?

I have had to modify your code a little bit (usage of power & reset pins) to work with my FEZCobra III & 3G card (https://www.itead.cc/development-platform/arduino/shields/itead-3g-shield-sim5216j.html setup) but my changes are pretty small. I was looking at how to extend the code so it coped with different hardware setups.

I can see my device looping on startup, then then registering on the GSM & GPRS networks. I then call gsmModem.UseThisNetworkInterface(ā€œinternetā€) but Iā€™m not certain this is the right spot.

gsmModem_LineReceived +CREG: 0,1
gsmModem_GsmNetworkRegistrationChanged 1
Gprs Network Registration Changed - Registered
gsmModem_LineReceived OK
gsmModem_LineReceived PB DONE

But I never get NetworkAvailabilityChanged event firing

Any suggestions?

@ KiwiBryn
blog.devmobile.co.nz

I think the reason for the fact that the NetworkAvailabilityChanged is never called is that you are not actually getting a PPP connection to your network.

I check the IsNetworkConnected to know if the network is alive or not. If not and I need a connection, I make this call:


UseThisNetworkInterface(gprs_apn, gprs_username, gprs_password, GHI.Networking.PPPSerialModem.AuthenticationType.Any);

Are you using the correct APN for your network? Is ā€œinternetā€ correct? Does it need a username and password etc. Which phone network are you trying to connect to?

@ Dave McLaughlin - Got stuck in debugging another issue in an Azure application, back on NetMF this evening

First I wait for the network to sort itself out, then initiate the connection but it never connects


while (gsmState != CellularRadio.NetworkRegistrationState.Registered && gsmState != CellularRadio.NetworkRegistrationState.Roaming)
{
      gsmModem.RequestNetState();
      gsmModem.RequestSignalStrength();
      gsmModem.SendATCommand("AT+CREG?");     // Get the registration state
      Attempts++;
      if (Attempts > 60)          // We should wait 60 seconds for GSM attach
      {
         Attempts = 0;

         gsmModem.PowerOn(true);     // Try to power up again
      }
   Thread.Sleep(1000);
}
...
gsmModem.UseThisNetworkInterface("internet");
...
while (!gsmModem.IsNetworkConnected)
{
   Status.Write(!Status.Read());
   Thread.Sleep(250);
 }

In the output window I see (send logging off as too much noise)

gsmModem_LineReceived +CREG: 0,1
gsmModem_GsmNetworkRegistrationChanged 1
Gsm Network Registration Changed - Registered
gsmModem_LineReceived OK
gsmModem_LineReceived AT+CGDCONT=1,ā€œIPā€,ā€œinternetā€
gsmModem_LineReceived OK
gsmModem_LineReceived ATD99**1#
gsmModem_LineReceived CONNECT 115200

But I never exit the loop waiting for IsNetworkConnected

The APN for TelecomNZ is ā€œinternetā€ and the SIM has plenty of credit & does data just fine when in a mobile.

Do you have any suggestions?

@ KiwiBryn
blog.devmobile.co.nz

Hi Bryn

We may need GHI to look at this again seeing as you are using a different modem. It has been tested with SIM5230 and SIM900 so there might be something in the PPP packet negotiation that is different with your SIM5216. Wish we could enable debug output from the PPP driver so we could see what it is doing.

If anyone from GHI is reading this and knows about the PPP connection, how are you handling the magic number negotiation? Iā€™ve found that can be an issue and having a way to disable or enable this often means PPP negotiates or doesnā€™t.

1 Like

Hi Dave,

Dug out a SIM900 based shield (from Seeedstudio) for my Fez Cobra III

Runs on the local Vodafone network rather than TelecomNZ so some slight changes to the apn as well as baudrate.

In the output window I see

smModem_LineReceived +CSQ: 9,0
gsmModem_SignalStrengthRequested 9
gsmModem_LineReceived OK
gsmModem_LineReceived AT+CREG?

gsmModem_LineReceived +CREG: 1,2
gsmModem_GsmNetworkRegistrationChanged 2
Gsm Network Registration Changed - Searching
gsmModem_LineReceived OK
gsmModem_LineReceived +CREG: 1
gsmModem_GsmNetworkRegistrationChanged 1
Gsm Network Registration Changed - Registered
gsmModem_LineReceived +CGREG: 0
gsmModem_GprsNetworkRegistrationChanged 0
Gprs Network Registration Changed - Not Searching
gsmModem_LineReceived AT+CGDCONT=1,ā€œIPā€,ā€œvodafoneā€
gsmModem_LineReceived OK
gsmModem_LineReceived ATD99**1#
gsmModem_LineReceived CONNECT
The thread ā€˜ā€™ (0x4) has exited with code 0 (0x0).

But gsmModem.IsNetworkConnected never gets set to true so I never kick off my http request

Odd thing is, if I leave the device running for a while waiting for ā€œIsNetworkConnectedā€ I get

NetworkChange_NetworkAddressChanged
NetworkChange_NetworkAvailabilityChanged False

Which makes me think the GPRS connection was established then timed out

Thoughts?

@ KiwiBryn
blog.devmobile.co.nz

Bryn

Just a quick test. Can you drop the baud rate to 19200 and see if it connects?

Hopefully the modem is auto baud.

Hi Dave,

The simplest example I could create. The only mod to your cellular driver code is changing the GPIO port number to work with my shield.


gsmModem = new CellularRadio("COM1", 19200);
int Attempts = 0;
gsmModem.GsmNetworkRegistrationChanged += gsmModem_GsmNetworkRegistrationChanged;
gsmModem.SmsListReceived += gsmModem_SmsListReceived;
gsmModem.SignalStrengthRequested += gsmModem_SignalStrengthRequested;
gsmModem.GprsNetworkRegistrationChanged += gsmModem_GprsNetworkRegistrationChanged;
gsmModem.ClockRequested += gsmModem_ClockRequested;
gsmModem.NetStateRequested += gsmModem_NetStateRequested;
gsmModem.ImeiRequested += gsmModem_ImeiRequested;
gsmModem.LineReceived += gsmModem_LineReceived;
gsmModem.LineSent += gsmModem_LineSent;

NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;

gsmModem.PowerOn(false);

//
// We need to wait until the modem is ready and registered on the network
// We can go around and around in here forever because if no modem, no
// point going past this point.
//
while (gsmState != CellularRadio.NetworkRegistrationState.Registered && gsmState != CellularRadio.NetworkRegistrationState.Roaming)
{
   gsmModem.RequestNetState();
   gsmModem.RequestSignalStrength();
   gsmModem.SendATCommand("AT+CREG?");     // Get the registration state
   Attempts++;
   if (Attempts > 60)          // We should wait 60 seconds for GSM attach
   {
      Attempts = 0;

      gsmModem.PowerOn(true);     // Try to power up again
   }
   Thread.Sleep(1000);
}

gsmModem.UseThisNetworkInterface("vodafone");

while (!gsmModem.IsNetworkConnected )
{
   Status.Write(!Status.Read());
   Thread.Sleep(250);
}

try
{
   using (HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.co.nz"))
   {
      request.Method = "GET";
      request.ContentType = "text/csv";
      request.KeepAlive = false;
      request.Timeout = 5000;
      request.ReadWriteTimeout = 5000;

      using (var response = (HttpWebResponse)request.GetResponse())
      {
         Debug.Print("HTTP Status:" + response.StatusCode + " : " + response.StatusDescription);
      }
   }
}
catch (Exception ex)
{
   Debug.Print(ex.Message);
}

Thread.Sleep(Timeout.Infinite);

I get this output
ā€¦
gsmModem_LineReceived AT+CSQ
gsmModem_LineReceived +CSQ: 13,0
gsmModem_SignalStrengthRequested 13
gsmModem_LineReceived OK
gsmModem_LineReceived AT+CREG?
gsmModem_LineReceived +CREG: 1,2
gsmModem_GsmNetworkRegistrationChanged 2
Gsm Network Registration Changed - Searching
gsmModem_LineReceived OK
gsmModem_LineReceived +CREG: 1
gsmModem_GsmNetworkRegistrationChanged 1
Gsm Network Registration Changed - Registered
gsmModem_LineReceived +CGREG: 0
gsmModem_GprsNetworkRegistrationChanged 0
Gprs Network Registration Changed - Not Searching
gsmModem_LineSent AT+CGDCONT=1,ā€œIPā€,ā€œvodafoneā€
gsmModem_LineReceived AT+CGDCONT=1,ā€œIPā€,ā€œvodafoneā€
gsmModem_LineReceived OK
gsmModem_LineSent ATD991#
gsmModem_LineReceived ATD
99
**1#
gsmModem_LineReceived CONNECT
The thread ā€˜ā€™ (0x3) has exited with code 0 (0x0).
Wait for a while (30secs or a minute maybe )
NetworkChange_NetworkAddressChanged
NetworkChange_NetworkAvailabilityChanged False

Dave, any suggestions?

If someone from GHI is watching any hints on debugging?

Thanks

@ KiwiBryn
blog.devmobile.co.nz

@ Bryn Lewis - Unfortunately there is no debug output from the native PPP driver. The best route at this point is to monitor the two serial lines, capture the traffic, and decode the PPP communication to see what error is happening.