Shop OBEX P1 Docs P2 Docs Learn Events
Spinneret or PC — Parallax Forums

Spinneret or PC

bennettdanbennettdan Posts: 614
edited 2012-09-04 17:15 in Accessories
I have a project I have been working on and I want to be able to serve a web page on a PC and use that page to send data to a home network of spinneret's that I will use for input/output control.
I plan to use this for a home automation project. The end results would be a picture of the room and just click on the object to be controlled. I have seen many examples on how to turn on/off an LED from a spinneret but I would like the PC to be a central hub to the outside world not a bunch of spinneret's.
Any examples or direction would be great.
Thanks Dan B.

Comments

  • Mike GMike G Posts: 2,702
    edited 2012-09-04 17:15
    bennettdan, the idea is to open a socket and send data to a Spinneret.

    Below is a C# code I use to proxy an HTTP GET from a web server to a spinneret on a home local network. I use this method on the LED Control panel. The proxy grabs the GET request on page load and reads the querystring. This proxy page contains only implementation no HTML. The page is looking for two parameters, LED ID and LED state. The two parameters are used to format a RESTful URL. The GetWebRequest method invokes the RESTful HTTP GET request to the remote Spinneret.

    Sounds like you have multiple Spinnerets? You'll have to pass additional information like a Spinneret IP and port along with whatever parameters are required. Or I guess you could use configuration to build the remote IP and port.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;
    
    public partial class spinneret_ajaxhandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string ledState = string.IsNullOrEmpty(Request.QueryString["ledstate"]) ? "off" : Request.QueryString["ledstate"];
            string id = string.IsNullOrEmpty(Request.QueryString["id"]) ? "off" : Request.QueryString["id"];
            Response.Write(GetWebRequest(id, ledState));
        }
    
    
        private string GetWebRequest(string id, string ledState)
        {
            string url = string.Empty;
            //http://spinneret.servebeer.com:5000
            url = string.Format("http://spinneret.servebeer.com:5000/led/{0}/{1}", id, ledState);
            Uri uri = new Uri(url);
    
            try
            {
                // Create a request using a URL that can receive a post.
                WebRequest request = WebRequest.Create(uri);
    
                // Set the Method property of the request to POST.
                request.Method = "GET";
    
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
    
                // Get the response.
                WebResponse response = request.GetResponse();
    
                // Display the status.
                string status = ((HttpWebResponse)response).StatusDescription;
    
                if (status.ToLower() != "ok")
                    return string.Format("Sorry; the spinneret is unavailable.");
    
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
    
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
    
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
    
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();
    
                return responseFromServer;
            }
            catch (Exception)
            {
                return string.Format("Sorry; the spinneret is unavailable.");
            }
        }
    }
    
Sign In or Register to comment.