Main Site Documentation

Webclient and timer


#1

Hi,

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 = "192.168.1.222";
        const string subnetMask = "255.255.255.0";
        const string gatewayAdress = "192.168.1.254";
        static string[] dns = { "192.168.1.254" };
        
        const string url = "http://www.greg-photo.net/essai.php?id=123456";

        #endregion
        
        public static void Main()
        {
            Debug.Print("Initialising Network...");
            eth0.Open();
            eth0.EnableStaticIP(ipAdress, subnetMask, gatewayAdress);
            eth0.EnableStaticDns(dns);

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

            while (eth0.NetworkInterface.IPAddress == "0.0.0.0")
            {
                Debug.Print("Waiting for DHCP");
                Thread.Sleep(250);
            }

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

            Thread.Sleep(Timeout.Infinite);
        }

        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;
                Thread.Sleep(1000);
                Debug.Print("Sending Request...");
                Debug.Print(request.URL);
                request.SendRequest();
        }

        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);
            }
            else
            {
                Debug.Print("Problem HTTP-Response: " + response.StatusCode);
            }
        }
    }
}


[line]
‘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 : 192.168.1.222
SN : 255.255.255.0
GW : 192.168.1.254
DNS : 192.168.1.254
response request_ResponseReceived…
Response code : 200
url : http://www.greg-photo.net/essai.php?id=123456
Server : text/html; charset=UTF-8
Response : OK
Le thread ‘’ (0x5) s’est arrêté avec le code 0 (0x0).
Sending Request…
http://www.greg-photo.net/essai.php?id=123456
Le thread ‘’ (0x6) s’est arrêté avec le code 0 (0x0).
Sending Request…
http://www.greg-photo.net/essai.php?id=123456
Le thread ‘’ (0x7) s’est arrêté avec le code 0 (0x0).
Sending Request…
http://www.greg-photo.net/essai.php?id=123456
Le programme ‘[26] Micro Framework application: Managé’ s’est arrêté avec le code 0 (0x0).


#2

@ 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.


#3

@ Mike - yes,you are right. I know that perhaps it could be possible to use the webclient lib according to this document : https://www.ghielectronics.com/docs/349/using-gadgeteer-after-2016-r1.
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.