Esempio n. 1
0
        public static void Main()
        {
            M8_TempMgr tempMgr;
            M8_PID     pid;
            M8_SSR     ssr;

            M8_WebServer server;

            DateTime updateIn = new DateTime();

            // We want to update every second
            updateIn = DateTime.Now.AddMilliseconds(1000);

            pid     = new M8_PID(M8_PID.defaultPGain, M8_PID.defaultIGain, M8_PID.defaultDGain);
            ssr     = new M8_SSR((Cpu.Pin)FEZ_Pin.Digital.Di6);
            tempMgr = new M8_TempMgr((Cpu.Pin)FEZ_Pin.Digital.Di5);
            tempMgr.setPidThermometer(0);

            Debug.Print("W5100.Enable");
            WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di9, false);
            //We need to give the Wiz chip some "alone time"
            Thread.Sleep(1000);

            NetworkInterface.EnableStaticIP(new byte[] { 192, 168, 1, 177 }, new byte[] { 255, 255, 255, 0 }, new byte[] { 192, 168, 1, 1 }, new byte[] { 0x90, 0xA2, 0xDA, 0x00, 0x14, 0x14 });
            NetworkInterface.EnableStaticDns(new byte[] { 192, 168, 1, 1 });

            server = new M8_WebServer(new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp), tempMgr, pid, ssr);

            server.startServer(80);

            while (true)
            {
                //Read the temps in
                if (DateTime.Now > updateIn)
                {
                    tempMgr.update();

                    //Calculate the PID value and set it on the SSR
                    pid.calcPID(tempMgr.getTemp(), tempMgr.getError());
                    ssr.setPower(pid.getSSRValue());

                    updateIn = DateTime.Now.AddMilliseconds(1000);

                    Debug.Print("-----------------" + DateTime.Now.ToString());
                }

                //Update the SSR
                ssr.update();

                server.update();
            }
        }
Esempio n. 2
0
        public static void Main()
        {
            M8_TempMgr tempMgr;
            M8_PID pid;
            M8_SSR ssr;

            M8_WebServer server;

            DateTime updateIn = new DateTime();

            // We want to update every second
            updateIn = DateTime.Now.AddMilliseconds(1000);

            pid = new M8_PID(M8_PID.defaultPGain, M8_PID.defaultIGain, M8_PID.defaultDGain);
            ssr = new M8_SSR((Cpu.Pin)FEZ_Pin.Digital.Di6);
            tempMgr = new M8_TempMgr((Cpu.Pin)FEZ_Pin.Digital.Di5);
            tempMgr.setPidThermometer(0);

            Debug.Print("W5100.Enable");
            WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di9, false);
            //We need to give the Wiz chip some "alone time"
            Thread.Sleep(1000);

            NetworkInterface.EnableStaticIP(new byte[] { 192, 168, 1, 177 }, new byte[] { 255, 255, 255, 0 }, new byte[] { 192, 168, 1, 1 }, new byte[] { 0x90, 0xA2, 0xDA, 0x00, 0x14, 0x14 });
            NetworkInterface.EnableStaticDns(new byte[] { 192, 168, 1, 1 });

            server = new M8_WebServer( new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp), tempMgr, pid, ssr);

            server.startServer( 80 );

            while (true)
            {
                //Read the temps in
                if (DateTime.Now > updateIn)
                {
                    tempMgr.update();

                    //Calculate the PID value and set it on the SSR
                    pid.calcPID(tempMgr.getTemp(), tempMgr.getError());
                    ssr.setPower(pid.getSSRValue());

                    updateIn = DateTime.Now.AddMilliseconds(1000);

                    Debug.Print("-----------------" + DateTime.Now.ToString());
                }

                //Update the SSR
                ssr.update();

                server.update();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        private void ProcessRequest()
        {
            const Int32 c_microsecondsPerSecond = 1000000;

            Boolean closeTags = true;     //Do we need to append the closing HTML?

            // 'using' ensures that the client's socket gets closed.
            using (m_clientSocket)
            {
                // Wait for the client request to start to arrive.
                Byte[] buffer = new Byte[1024];
                if (m_clientSocket.Poll(5 * c_microsecondsPerSecond,
                                        SelectMode.SelectRead))
                {
                    // If 0 bytes in buffer, then the connection has been closed,
                    // reset, or terminated.
                    if (m_clientSocket.Available == 0)
                    {
                        return;
                    }
                    // Read the first chunk of the request (we don't actually do
                    // anything with it).
                    Int32 bytesRead = m_clientSocket.Receive(buffer,
                                                             m_clientSocket.Available, SocketFlags.None);

                    /* here is where we need to process the request */
                    //Convert the buffer to something more usable
                    String incoming = bytesToString(buffer);
                    //Remove the "\r"s we'll just worry about the "\n"s
                    incoming.Trim('\r');
                    //Slip the request by the "\n"s

                    //Preload the header into the output string
                    String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\ncharset=utf-8\r\n\r\n<html>";

                    /*
                     * These are the data requests
                     */
                    if (incoming.IndexOf("GET /temp") >= 0)
                    {
                        lock (m_tempMgr)
                        {
                            s += "<head><title>TEMP</title></head><body><bold>" + m_tempMgr.getTemp().ToString() + "</bold>";
                        }
                    }
                    else if (incoming.IndexOf("GET /target") >= 0)
                    {
                        lock (m_tempMgr)
                        {
                            s += "<head><title>TARGET</title></head><body><bold>" + m_tempMgr.getTarget().ToString() + "</bold>";
                        }
                    }
                    else if (incoming.IndexOf("GET /pid") >= 0)
                    {
                        lock (m_pid)
                        {
                            s += "<head><title>PID</title></head><body><bold>" + m_pid.getValue().ToString() + "</bold>";
                        }
                    }
                    else if (incoming.IndexOf("GET /dataasxml") >= 0)
                    {
                        M8_DataLog log = new M8_DataLog();

                        log.addTimeStamp();

                        lock (m_tempMgr)
                        {
                            for (int i = 0; i < m_tempMgr.getCount(); i++)
                            {
                                log.addTempData(i, m_tempMgr.getTemp(i), m_tempMgr.getTarget(i));
                            }

                            lock (m_pid)
                                log.addPIDData(m_tempMgr.getPidThermometer(), m_pid.getSSRValue());
                            lock (m_ssr)
                                log.addSSRData(m_tempMgr.getPidThermometer(), m_ssr.getPower());
                        }

                        s  = "HTTP/1.1 200 OK\r\nContent-Type: text/xml\r\ncharset=utf-8\r\n\r\n";
                        s += log.dumpData();
                        Debug.Print(s);
                        closeTags = false;     // we're just going to dump the xml out.
                    }

                    /*
                     * These are the form webpages
                     */
                    else if (incoming.IndexOf("GET /control") >= 0)
                    {
                        s += "<head><title>Change PID</title></head><body>Please enter the index of the sensor to use with the PID control<form name=\"input\" action=\"yournewpid\" method=\"get\">Sensor to use: <input type=\"text\" name=\"pid\" /><input type=\"submit\" value=\"Submit\" /></form>";
                    }

                    /*
                     * These are the changes to data
                     */
                    else if (incoming.IndexOf("GET /yournewpid?pid=") >= 0)
                    {
                        //GET /yournewtemp?newTemp=70 HTTP/1.1
                        //try to get the new set temp
                        string tempStr = incoming.Substring(("GET /yournewpid?pid=").Length, incoming.IndexOf(" HTTP/") - ("GET /yournewpid?pid=").Length);
                        int    tempInt = Convert.ToInt32(tempStr);

                        //get rid of the preloaded header, to add a refresh to return the user to the default page
                        s = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nRefresh: 5; url=http://m8.dyndns-server.com/\r\n\r\n<html><head><title>PID</title></head><body>Setting PID to use sensor <bold>" + tempInt.ToString() + "</bold><br>You will be redirected back";

                        lock (m_tempMgr)
                        {
                            m_tempMgr.setPidThermometer(tempInt);
                        }
                    }
                    else if (incoming.IndexOf("GET /yournewtemp?newTemp=") >= 0)
                    {
                        //GET /yournewtemp?newTemp=70 HTTP/1.1
                        //try to get the new set temp
                        string tempStr = incoming.Substring(("GET /yournewtemp?newTemp=").Length, incoming.IndexOf(" HTTP/") - ("GET /yournewtemp?newTemp=").Length);
                        int    tempInt = Convert.ToInt32(tempStr);

                        //Add refresh to the header to return the user to the default page
                        s = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nRefresh: 5; url=http://m8.dyndns-server.com/\r\n\r\n<html><head><title>PID</title></head><body>Setting Target Temp to <bold>" + tempInt.ToString() + "</bold><br>You will be redirected back";

                        lock (m_tempMgr)
                        {
                            m_tempMgr.setTarget(tempInt);
                        }
                    }

                    /*
                     * This is the default page
                     */
                    else
                    {
                        //This adds a refresh to the default page
                        //                        s = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nRefresh: 5; url=http://m8.dyndns-server.com/\r\n\r\n<html>";
                        s += "<head><title>M8 Brewery</title></head><body><table border=\"1\" align=\"center\"><tr><th>Sensor</th><th>Current Temp C</th><th>Target Temp</th><th>PID Result</th><th>PID Control</th></tr>";

                        lock (m_tempMgr)
                            for (int i = 0; i < m_tempMgr.getCount(); i++)
                            {
                                s += "<tr><td>" + i.ToString() + "</td>";
                                s += "<td>" + m_tempMgr.getTemp(i).ToString() + "</td>";
                                s += "<td>" + m_tempMgr.getTarget(i).ToString() + "</td>";

                                s += "<td>";
                                if (m_tempMgr.getPidThermometer() == i)
                                {
                                    lock (m_pid)
                                        s += m_pid.getValue().ToString();
                                }
                                s += "</td>";

                                s += "<td align=\"center\">";
                                if (m_tempMgr.getPidThermometer() == i)
                                {
                                    s += "X";
                                }
                                s += "</td></tr>";
                            }
                        s += "</table><hr><form name=\"input\" action=\"yournewtemp\" method=\"get\">New Target Temp: <input type=\"text\" name=\"newTemp\" /><input type=\"submit\" value=\"Submit\" /></form>";
                    }
                    if (closeTags == true)
                    {
                        //Close the tags
                        s += "<hr>";
                        s += DateTime.Now.ToString();
                        s += "</body></html>";
                    }
                    else
                    {
                        Debug.Print(s);
                    }

                    //We don't have a "facicon.icp" and my browser asks for one right after the page request
                    if (incoming.IndexOf("GET /favicon.ico HTTP/1.1") < 0)
                    {
                        byte[] buf    = Encoding.UTF8.GetBytes(s);
                        int    offset = 0;
                        int    ret    = 0;
                        int    len    = buf.Length;
                        while (len > 0)
                        {
                            ret     = m_clientSocket.Send(buf, offset, len, SocketFlags.None);
                            len    -= ret;
                            offset += ret;
                        }
                    }
                    m_clientSocket.Close();
                }
            }
        }