The GET request on php page not working

Hello,

Here’s how I do to send data in my database with the GET method. But it does not add.
As soon as you have an idea?

C# CODE



namespace Wifi
{
    public partial class Program
    {
        void ProgramStarted()
        {
            Debug.Print("Program Started");

            if (Startwifi())
            {
                ReqGet();
            }
        }

        bool Startwifi()
        {
            if (wifi_RS21 != null)
            {
                if (wifi_RS21.Interface.IsOpen)
                {
                    wifi_RS21.Interface.Disconnect();
                }
            }

            string SSID = "SGC";
            string password = "TechCFPT_2012";
            bool conn = false;

            wifi_RS21.Interface.Open();
            NetworkInterfaceExtension.AssignNetworkingStackTo(wifi_RS21.Interface);
            WiFiNetworkInfo[] infos = wifi_RS21.Interface.Scan();
            foreach (WiFiNetworkInfo info in infos)
            {
                if (info.SSID == SSID)
                {
                    wifi_RS21.Interface.NetworkInterface.EnableDhcp();
                    wifi_RS21.Interface.Join(info, password);
                    Debug.Print(SSID + " Connected");
                    conn = true;
                    break;
                }
                else
                {
                    conn = false;
                    Debug.Print(SSID + " not found");
                }
            }
            return conn;
        }

        void ReqGet()
        {
            string finalUri = "http://albentrix.com/fez.php?value=20";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(finalUri);
            HttpWebResponse rsponse = (HttpWebResponse)request.GetResponse();
            Debug.Print("ReqGet OK");
        }
    }
}

PHP CODE


<?php
require 'id.php';

mysql_connect($dbhost, $dbuser, $dbpassword) or die(mysql_error());
mysql_select_db("134387_ecole") or die(mysql_error());
mysql_query("INSERT INTO fez (current) VALUES (" .$_GET['value']. ")") or die(mysql_error());  
?>

Your code seems to make sense looking over it.

If try you use the URL directly in a webbrowser, does the value get added to the DB?

Could you return a value from your PHP page & see if the rsponse object gets that values?

1 Like

Step into code (netmf is great for this !)

What do the calls do?

On your PHP server, what gets called? Can you check web server logs there to see if the call is ever made?

Given you are using a Gadgeteer app, I think you should NOT do this in this way. You should set up Wifi in ProgramStarted. But you then need to let ProgramStarted complete, and then have another event (button press, or even a single shot timer) that then kicks in and does the HTTP work. Remember that the construct behind the ProgramStarted methodology is all about setting up netmf to abstract some things from you, and it may be that networking needs to be one of those. This may not change the behaviour but it also might… definitely worth a try in my view

1 Like

Missed that (program started) … for one off tests like this, I’ve just used a Timer(1000) and the call back is my method, in this case it would be a method wrapping up this:


if (Startwifi())
{
     ReqGet();
}

1 Like
if (info.SSID == SSID)
                {
                    wifi_RS21.Interface.NetworkInterface.EnableDhcp();
                    wifi_RS21.Interface.Join(info, password);
                    Debug.Print(SSID + " Connected");
                    conn = true;
                    break;
                }
                else
                {
                    conn = true;
                    Debug.Print(SSID + " not found");
                }

In this clause, you are returning a successful status for connection of the WiFi network, regardless if it was able to actually connect or not. You may not be connecting to the network properly, but because in both cases ‘true’ is returned, you would still be attempting to send the request.

1 Like

@ Alicorne - Is there any updates? Is your issue solved or on-going?

1 Like

@ mhectorgato - When I try directly in the url it works well.

Here is the text output in debug mode on my Visual studio 2010.

-> Debug Print: Program Started
-> Debug Print: SGC Connected
-> Debug Print: ReqGet OK

What you’re saying is that I need to send a request to see if I am connecting to wireless networks?

Indeed there is an error with the code “conn = true”, I’m changing but its not always add in my database.

Yes I can ping my device.

It is not possible because host not give the log.

I must go to my work so I give you the answer later. Thank you.

Hello,

I solved my problem, I can send data to my database.

CODE C#


namespace Wifi
{
    public partial class Program
    {
        void ProgramStarted()
        {
            Debug.Print("Program Started");

            if (Startwifi())
            {
                if (testReq())
                {
                    ReqGet();
                }
            }
        }

        bool testReq()
        {
            HttpWebResponse response;
            HttpWebRequest request;
            Debug.Print("Generating Request");
            request = (HttpWebRequest)HttpWebRequest.Create("http://google.ch");
            request.Method = "GET";
            request.UserAgent = "MyApp NETMF";
            request.KeepAlive = false;
            request.Timeout = 1000;

            Debug.Print("Awaiting response...");
            response = (HttpWebResponse)request.GetResponse();
            Debug.Print("Done.\n" + response.ContentLength.ToString() + " bytes");
            if (response.ContentLength > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        bool Startwifi()
        {
            if (wifi_RS21 != null)
            {
                if (wifi_RS21.Interface.IsOpen)
                {
                    wifi_RS21.Interface.Disconnect();
                }
            }

            string SSID = "SGC";
            string password = "TechCFPT_2012";
            bool conn = false;

            wifi_RS21.Interface.Open();
            NetworkInterfaceExtension.AssignNetworkingStackTo(wifi_RS21.Interface);
            WiFiNetworkInfo[] infos = wifi_RS21.Interface.Scan();
            foreach (WiFiNetworkInfo info in infos)
            {
                if (info.SSID == SSID)
                {
                    wifi_RS21.Interface.NetworkInterface.EnableDhcp();
                    wifi_RS21.Interface.Join(info, password);
                    Debug.Print(SSID + " Connected");
                    conn = true;
                    break;
                }
                else
                {
                    conn = false;
                    Debug.Print(SSID + " not found");
                }
            }
            return conn;
        }

        void ReqGet()
        {
            string finalUri = "http://albentrix.com/fez.php?value=20";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(finalUri);
            request.Method = "GET";
            request.UserAgent = "MyApp NETMF";
            request.KeepAlive = false;
            request.Timeout = 1000;

            WebResponse resp = null;
            try
            {
                resp = request.GetResponse();
            }
            catch (Exception e)
            {
                Debug.Print("Exception in HttpWebRequest.GetResponse(): " + e.ToString());
            }

            if (resp != null)
            {
                Debug.Print("ReqOK");
            }
        }
    }
}

I use a timer to send every 30 seconds, my data from my current sensor.