PPP Trials part 2 problems

Yes, the new version of the cellular driver downloaded from bitbucket is much more stable than the older ones. Thanks @ John for that.

I have added a few lines of debug code inside the driver to get some meaningful output.

But the PPP is now on the bench, and I cant figure out whats going wrong for my code:


using Gadgeteer.Modules.GHIElectronics;
using GHI.Networking;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;
using System.Net;
using System.Text;
using System.Threading;
using Gadgeteer.Modules.GHIElectronics;
using G120 = GHI.Pins.G120;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules.Module;


namespace PPPTest2
{
    public partial class Program
    {
        void ProgramStarted()
        {
            this.RegisterExtenderSockets();
            this.cellularRadio = new CellularRadio(8);
            cellularRadio.DebugPrintEnabled = true;

            NetworkChange.NetworkAvailabilityChanged += (a, b) => Debug.Print("Network availability changed: " + b.IsAvailable.ToString());
            NetworkChange.NetworkAddressChanged += (a, b) => Debug.Print("Network address changed");

            Debug.Print("Now starting cellular..");
               this.cellularRadio.PowerOn();
               Thread.Sleep(60000);
               Debug.Print("Now using cellular..");
               cellularRadio_ModuleInitialized(this.cellularRadio);
        }

        void cellularRadio_ModuleInitialized(CellularRadio sender)
        {
            Debug.Print("Starting test request code..");
            new Thread(() =>
            {
                Thread.Sleep(1000);

                Debug.Print("Now setting up interface..");
                this.cellularRadio.UseThisNetworkInterface("internet", "", "", PPPSerialModem.AuthenticationType.Pap);

                Debug.Print("Now waiting for IP..");
                
                while (this.cellularRadio.NetworkInterface.IPAddress == "0.0.0.0")
                {
                    Debug.Print("Waiting on DHCP");
                    Thread.Sleep(1000);
                }

                Debug.Print("IP:" + this.cellularRadio.NetworkInterface.IPAddress);

                using (var req = HttpWebRequest.Create("http://httpbin.org/ip") as HttpWebRequest)
                {
                    req.KeepAlive = false;
                    req.ContentLength = 0;

                    using (var res = req.GetResponse() as HttpWebResponse) // HERE IT THROWS CLR_E_FAIL!!
                    {
                        using (var stream = res.GetResponseStream())
                        {
                            int read = 0, total = 0;
                            var buffer = new byte[1024];

                            do
                            {
                                read = stream.Read(buffer, 0, buffer.Length);
                                total += read;

                                Thread.Sleep(20);
                            } while (read != 0);

                            Debug.Print(new string(Encoding.UTF8.GetChars(buffer)));
                        }
                    }
                }
            }).Start();
        }
    }
}

OUTPUT:


Now starting cellular..
CellularRadio : MODEM:NORMAL POWER DOWN
CellularRadio : MODEM:+CREG: 3
CellularRadio : MODEM:+CGREG: 3
CellularRadio : SEND TO MODEM:AT
CellularRadio : SEND TO MODEM:ATE0
CellularRadio : SEND TO MODEM:AT+CMGF=1
CellularRadio : MODEM:AT

CellularRadio : MODEM:OK
CellularRadio : MODEM:ATE0

CellularRadio : MODEM:OK
CellularRadio : SEND TO MODEM:AT+CSDH=0
CellularRadio : SEND TO MODEM:AT+CPBS="SM"
CellularRadio : SEND TO MODEM:AT+CPMS="SM"
CellularRadio : MODEM:OK
CellularRadio : MODEM:+CREG: 2
CellularRadio : MODEM:OK
CellularRadio : MODEM:OK
CellularRadio : SEND TO MODEM:AT+COLP=1
CellularRadio : SEND TO MODEM:AT+CGREG=1
CellularRadio : MODEM:+CMS ERROR: SMS reserved
CellularRadio : MODEM:OK
CellularRadio : MODEM:OK
CellularRadio : SEND TO MODEM:AT+CREG=1
CellularRadio : MODEM:OK
WARN: Total initialization time exceeds 10 seconds.
    : ProgramStarted is blocking execution, which means events and timers will not run properly.
    : Make sure not to use blocking code such as while(true) - use a GT.Timer instead.
CellularRadio : MODEM:+CGREG: 0
CellularRadio : MODEM:+CREG: 1
CellularRadio : MODEM:*PSUTTZ: 2014, 12, 21, 22, 43, 11, "+4", 1
CellularRadio : MODEM:DST: 1
CellularRadio : MODEM:*PSUTTZ: 2014, 12, 21, 22, 43, 14, "+4", 0
CellularRadio : MODEM:DST: 0
CellularRadio : MODEM:+CGREG: 1
WARN: Total initialization time exceeds 20 seconds.
    : ProgramStarted is blocking execution, which means events and timers will not run properly.
    : Make sure not to use blocking code such as while(true) - use a GT.Timer instead.
WARN: Total initialization time exceeds 30 seconds.
    : ProgramStarted is blocking execution, which means events and timers will not run properly.
    : Make sure not to use blocking code such as while(true) - use a GT.Timer instead.
WARN: Total initialization time exceeds 40 seconds.
    : ProgramStarted is blocking execution, which means events and timers will not run properly.
    : Make sure not to use blocking code such as while(true) - use a GT.Timer instead.
WARN: Total initialization time exceeds 50 seconds.
    : ProgramStarted is blocking execution, which means events and timers will not run properly.
    : Make sure not to use blocking code such as while(true) - use a GT.Timer instead.
WARN: Total initialization time exceeds 60 seconds.
    : ProgramStarted is blocking execution, which means events and timers will not run properly.
    : Make sure not to use blocking code such as while(true) - use a GT.Timer instead.
Now using cellular..
Starting test request code..
Now setting up interface..
CellularRadio : SEND TO MODEM:AT+CGDCONT=1,"IP","internet"
CellularRadio : SEND TO MODEM:ATDT*99***1#
CellularRadio : MODEM:OK
CellularRadio : MODEM:CONNECT
The thread '<No Name>' (0x7) has exited with code 0 (0x0).
Now waiting for IP..
Waiting on DHCP
The thread '<No Name>' (0x5) has exited with code 0 (0x0).
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Waiting on DHCP
Network address changed
Network availability changed: True
IP:10.160.179.81
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (8) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::send [IP: 0000] ####
    #### System.Net.Sockets.Socket::Send [IP: 0018] ####
    #### System.Net.Sockets.NetworkStream::Write [IP: 0051] ####
    #### System.Net.InputNetworkStreamWrapper::Write [IP: 000a] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 007d] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### PPPTest2.Program::<cellularRadio_ModuleInitialized>b__4 [IP: 0073] ####
    #### SocketException ErrorCode = 10053
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = 10053
    #### SocketException ErrorCode = 10053
    #### SocketException ErrorCode = 10053
    #### Exception System.Net.WebException - 0x00000000 (8) ####
    #### Message: 
    #### System.Net.HttpWebRequest::GetResponse [IP: 00c8] ####
    #### PPPTest2.Program::<cellularRadio_ModuleInitialized>b__4 [IP: 0073] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
An unhandled exception of type 'System.Net.WebException' occurred in System.Http.dll
The program '[15] Micro Framework application: Managed' has exited with code 0 (0x0).


Just to add…I have extra antenna, and extra power supply on the module. And checking the signal I get “Very strong”.

@ njbuch - Using the latest cellular radio source from bitbucket and your above code, it ran without issue for me.

It looks like you might have some more AT commands used based on your output, so perhaps that is messing with it?

Aargh, it was a power-issue - the double supply was swapped. Putting in the barrel-connector from the right supply suddenly changed things to the positive.

Sorry to waste your time :wink:

While we are at it:
1: How would it be possible to test that there is not enough power, and then alert using SMS (which does not require as much power…)

2: What is the best way to shutdown the PPP connection, power down the modem, and release all networking. (missing from documentation)

Hi Neils,

There should be a Disconnect() function in the PPP to do this but if there is not, you can send +++ to the modem and follow this with ATH0. Use a 1 second guard time after the +++.

Powering off the modem without doing a proper network disconnect is bad and some phone networks will block you if you do this often (well, they have done this here for sure when I was doing testing :slight_smile: ). It’s like pulling the battery from your phone. When you want to switch off you have to do a proper disconnect from the network.

@ Dave McLaughlin - Thanks again for valuable input!

@ John - This sums up the issues with the PPP functionality quite well:

[ol]
The driver still needs a “SendATWithExpectedResponse” function, that waits for the expected output, has a timeout, and returns if the modem responds with something else.
The driver still needs some kind of sequencing/blocking/mutex mechanism, to make it more prone to multiple commands coming from different threads.
The driver needs to support disconnecting the PPP session with proper commands.
The driver needs to handle exceptions or ? in the network layer (happens when the modem does not get enough power, or loses radio signal.
The documentation needs to be updated with correct closing down the network layer and the PPP connection.[/ol]

Anything else?

@ njbuch - Thanks. I still have a lot of work to do in the driver, including tying AT commands to a response and more graceful handling of the PPP lifecycle.

Until then, for disconnecting PPP, you can access the PPPSerialModem object under the NetworkInterface property to call Disconnect and use SentATCommand to send the commands Dave mentioned.