Main Site Documentation

FEZ Connect hangs after a while


#1

Hello I’m having a problem with the following code, I run FEZ Connect with panda2 and after a while the board just hangs and is not available even through local address anymore:

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using System.Text;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.NetworkInformation;
namespace FEZ_Panda_II_Application8
{
    public class Program
    {
        static Thread thread;
        static OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.An4, false);

        static bool running;
        //static 
        public static void MainLoop()
        {

            while (running)
            {
                HttpWebRequest request = HttpWebRequest.Create("http://myftpsite/index.php") as HttpWebRequest;
                //request.KeepAlive = true;
                WebResponse resp = null;
                try
                {
                    resp = request.GetResponse();
                }
                catch (Exception e)
                {
                    Debug.Print("Exception in HttpWebRequest.GetResponse(): " + e.ToString());
                } 
                Thread.Sleep(6000000);
            }
        }
        public static void Main()
        {

            byte[] ip = { 192, 168, 1, 176 };
            byte[] subnet = { 255, 255, 255, 0 };
            byte[] gateway = { 192, 168, 1, 254 };
            byte[] mac = { 0x00, 0x88, 0x98, 0x90, 0xD4, 0xE0 };


            WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di7, true);

            NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
            NetworkInterface.EnableStaticDns(gateway);
            // start server
            HttpListener listener = new HttpListener("http", 80);
            listener.Start();


            // Begin calling ddns
            running = true;
            thread = new Thread(MainLoop);
            thread.Priority = ThreadPriority.Highest;
            thread.Start();

            while (true)
            {
                HttpListenerResponse response = null;
                HttpListenerContext context = null;
                try
                {
                    context = listener.GetContext();
                    response = context.Response;
                    // The button is pressed
                    if (context.Request.HttpMethod == "POST")
                    {

                        led.Write(true);
                        Thread.Sleep(1000);
                        led.Write(false);
                    }
                    // Sends response
                    response.StatusCode = (int)HttpStatusCode.OK;
                    byte[] HTML = Encoding.UTF8.GetBytes(
                        "<head>" +
                    //"<META HTTP-EQUIV=\"refresh\" CONTENT=\"120\">" +
                        
                    "</head>" +
                    "<html><body>" +
                    "<h1>Doors</h1>" +
                    "<form action=\"\" method=\"post\">" +
                    "<input type=\"submit\" value=\"Open/Close\">" +
                    "</form>" +
                    "</body></html>");
                    response.ContentType = "text/html";
                    response.OutputStream.Write(HTML, 0, HTML.Length);
                    response.Close();
                }
                catch
                {
                    if (context != null)
                    {
                        context.Close();
                    }
                }
            }


        }
    }
}



So the problem is within tread calling every 6000 seconds. I know this is wrong but what I’m trying to do is my own implementation of ddns service since nothing else is working with my poor router. So what I’m trying to do is to HTTP/GET a document on my ftp server every once a while. Then I have simple php script that stores the ip of the board so I can call the board with domain I have registered. (I’m not caling it once a minute since the firewall on my ftp server obviusly blocks it after 15 min).

So should I go and try with a timer to call every like 3600 sec or is that actauly the same as above?.Does anyone with more experience have better idea how to it?.

Would password protect with javascript be ok or should I add another input field for password?

And one more thing. I’m running the board with 9v through vin ports and at the same time powering transistor BC547 with the same power supply (9v 1000ma). I was reading that Fez Connect should be power from 6-7,5 V becouse it might overheat the onboard regulators. Is it ok if I power it with 9v?


#2

The much more sensible (and understandable !) method is to structure everything so that you have the main() set up TCP/IP, and then it periodically calls a method to execute your GET. The method you call is just a linear start–do-my-stuff–stop method, no need for loops etc.

I am not sure what you’re trying to test and prove working here, that relates to DDNS? DDNS is not going to require or rely on HTTP GETs, and the paradigm will be much different.

What does “hang” mean. Do you mean it stops at a particular line of code??

I personally would restructure the main loop like I said. I’d use TWO timers, one for a LED flash (flash every second, so you know the app is still running on the board - a simple heartbeat) and one to periodically call the HTTP method.

Then I’d actually take a look at jasdev’s code on codeshare https://www.ghielectronics.com/community/codeshare/entry/363 and adapt that for your testing - it’s a pretty robust source to use.