Main Site Documentation

Webclient and timer



I face a strange behavior to me. I want to grab datas from a webpage on the net.
I use a cobra I but with the webclient gadgeteer library.
The thing is that I can grab my datas correctly one time, but all other try return no response at all…
I really don’t know why. What am I doing wrong ?
My first thought was to think that using the gadgeteer lib was not a good idea with plain netmf, but it works one time ! so…

Any idea ?

Thank you in advance

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Collections;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using GHI.Networking;
using GHI.Pins;
using GHI.Glide;
using GHI.Glide.Display;
using GHI.Glide.UI;
using GHI.IO;
using GHI.Utilities;

using Gadgeteer;
using Gadgeteer.Networking;

namespace NETMF_4._3_webClient_timer
    public class Program
        #region variables
        static readonly EthernetBuiltIn eth0 = new EthernetBuiltIn();

        const string ipAdress = "";
        const string subnetMask = "";
        const string gatewayAdress = "";
        static string[] dns = { "" };
        const string url = "";

        public static void Main()
            Debug.Print("Initialising Network...");
            eth0.EnableStaticIP(ipAdress, subnetMask, gatewayAdress);

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

            while (eth0.NetworkInterface.IPAddress == "")
                Debug.Print("Waiting for DHCP");

            TimerCallback timercallback = null;
            timercallback = new TimerCallback(sendRequest);
            System.Threading.Timer grab = new System.Threading.Timer(timercallback,null,10000,20000);


        static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
            Debug.Print("Network available: " + e.IsAvailable.ToString());

        static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
            Debug.Print("Network Adress Changed");
            Debug.Print("DHCP : " + eth0.IsDhcpEnabled.ToString());
            Debug.Print("IP : " + eth0.NetworkInterface.IPAddress);
            Debug.Print("SN : " + eth0.NetworkInterface.SubnetMask);
            Debug.Print("GW : " + eth0.NetworkInterface.GatewayAddress);
            //Debug.Print("DNS : " + ethernetJ11D.NetworkInterface.DnsAddresses.GetValue(0);
            foreach (string dnsitem in eth0.NetworkInterface.DnsAddresses)
                Debug.Print("DNS : " + dnsitem);

        public static void sendRequest(object o)
                //Send the request gadgeteer
                Gadgeteer.Networking.HttpRequest request = WebClient.GetFromWeb(url);
                request.ResponseReceived += request_ResponseReceived;
                Debug.Print("Sending Request...");

        public static void request_ResponseReceived(HttpRequest sender, HttpResponse response)
            Debug.Print("response request_ResponseReceived...");

            if (response.StatusCode == "200")
                Debug.Print("Response code : " + response.StatusCode);
                Debug.Print("url : " + response.URL);
                Debug.Print("Server : " + response.ContentType);
                Debug.Print("Response : "+response.Text);
                Debug.Print("Problem HTTP-Response: " + response.StatusCode);

‘Microsoft.SPOT.Debugger.CorDebug.12.dll’ (Managé) : ‘C:\Program Files (x86)\Microsoft .NET Gadgeteer\Core\Assemblies.NET Micro Framework 4.3\le\Gadgeteer.WebClient.dll’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.12.dll’ (Managé) : ‘C:\Program Files (x86)\GHI Electronics\NETMF v4.3 SDK\Libraries\le\GHI.Hardware.dll’ chargé
’Microsoft.SPOT.Debugger.CorDebug.12.dll’ (Managé) : ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.3\Assemblies\le\System.Xml.dll’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.12.dll’ (Managé) : ‘C:\Program Files (x86)\GHI Electronics\NETMF v4.3 SDK\Libraries\le\GHI.Glide.dll’ chargé
’Microsoft.SPOT.Debugger.CorDebug.12.dll’ (Managé) : ‘C:\Program Files (x86)\GHI Electronics\NETMF v4.3 SDK\Libraries\le\GHI.Networking.dll’ chargé
’Microsoft.SPOT.Debugger.CorDebug.12.dll’ (Managé) : ‘G:\Cours\STI2D\SIN\C#\Projects\FEZ Cobra\4.3\NETMF-4.3-webClient-timer\NETMF-4.3-webClient-timer\bin\Debug\le\NETMF-4.3-webClient-timer.exe’ chargé, symboles chargés.
‘Microsoft.SPOT.Debugger.CorDebug.12.dll’ (Managé) : ‘C:\Program Files (x86)\GHI Electronics\NETMF v4.3 SDK\Libraries\le\GHI.Pins.dll’ chargé
Le thread ‘’ (0x2) s’est arrêté avec le code 0 (0x0).
Initialising Network…
Network available: True
Network Adress Changed
DHCP : False
IP :
SN :
GW :
response request_ResponseReceived…
Response code : 200
url :
Server : text/html; charset=UTF-8
Response : OK
Le thread ‘’ (0x5) s’est arrêté avec le code 0 (0x0).
Sending Request…
Le thread ‘’ (0x6) s’est arrêté avec le code 0 (0x0).
Sending Request…
Le thread ‘’ (0x7) s’est arrêté avec le code 0 (0x0).
Sending Request…
Le programme ‘[26] Micro Framework application: Managé’ s’est arrêté avec le code 0 (0x0).


@ gregFR14 - I am surprised the WebClient worked once! You are going to have to get into the Gadgeteer code to figure out what is happening. Gadgeteer code is not going to work with plain NETMF without changes.


@ Mike - yes,you are right. I know that perhaps it could be possible to use the webclient lib according to this document :
It says that We need to instanciate the Program class like Gadgeteer does in Program.generated.cs. But I really don’t know how to do it in my code.
And, maybe it will not work…
I will try the NETMF way to make my request.