Writing a full website to socket with microncontroller

516 views Asked by At

I'm using a web server to control devices in the house with a microcontroller running .netMF (netduino plus 2). The code below writes a simple html page to a device that connects to the microcontroller over the internet.

       while (true)
            {
                Socket clientSocket = listenerSocket.Accept();
                bool dataReady = clientSocket.Poll(5000000, SelectMode.SelectRead);
                if (dataReady && clientSocket.Available > 0)
                {
                    byte[] buffer = new byte[clientSocket.Available];
                    int bytesRead = clientSocket.Receive(buffer);
                    string request =
                    new string(System.Text.Encoding.UTF8.GetChars(buffer));
                    if (request.IndexOf("ON") >= 0)
                    {
                        outD7.Write(true);
                    }
                    else if (request.IndexOf("OFF") >= 0)
                    {
                        outD7.Write(false);
                    }
                    string statusText = "Light is " + (outD7.Read() ? "ON" : "OFF") + ".";

                    string response = WebPage.startHTML(statusText, ip);
                    clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(response));
                }
                clientSocket.Close();
            }

public static string startHTML(string ledStatus, string ip)
        {
            string code = "<html><head><title>Netduino Home Automation</title></head><body> <div class=\"status\"><p>" + ledStatus + " </p></div>        <div class=\"switch\"><p><a href=\"http://" + ip + "/ON\">On</a></p><p><a href=\"http://" + ip + "/OFF\">Off</a></p></div></body></html>";
            return code;
        }

This works great, so I wrote a full jquery mobile website to use instead of the simple html. This website is stored on the SD card of the device and using the code below, should write the full website in place of the simple html above.

However, my problem is the netduino only writes the single HTML page to the browser, with none of the JS/CSS style files that are referenced in the HTML. How can I make sure the browser reads all of these files, as a full website?

The code I wrote to read the website from the SD is:

private static string getWebsite()
        {
            try
            {
                using (StreamReader reader = new StreamReader(@"\SD\index.html"))
                {
                    text = reader.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                throw new Exception("Failed to read " + e.Message);
            }

            return text;
        }

I replaced string code = " etc bit with

string code = getWebsite();
1

There are 1 answers

1
CodeCaster On

How can I make sure the browser reads all of these files, as a full website?

Isn't it already? Use an HTTP debugging tool like Fiddler. As I read from your code, your listenerSocket is supposed to listen on port 80. Your browser will first retrieve the results of the getWebsite call and parse the HTML.

Then it'll fire more requests, as it finds CSS and JS references in your HTML (none shown). These requests will, as far as we can see from your code, again receive the results of the getWebsite call.

You'll need to parse the incoming HTTP request to see what resource is being requested. It'll become a lot easier if the .NET implementation you run supports the HttpListener class (and it seems to).