monotreme3

Previous page next page

Here is some shots of the code to run first the code in text format so far only alarm board monitoring ( no sensing done on the panel itself as yet it just uses the on board micro button. ( click on the image for larger size ) on the left panel the PC software pings the alarm panel and any alarms are attended to by the operator by clicking on the alarm reset button at the bottom of the screen ( this generated a incident in the reporting panel at the right which is a summary of the alarms states during the session which also records operators log ins and outs with keypad pin security )

Download it from my filestore it is in the network security section its a win32 program ( but has no installer as yet )
just run the executable file MWS.exe ( Monotreme Windows Security executable ) in the archive called Monotreme01.zip when you download it. If it cant find your netduino then just load the IP in Firefox and refresh and then it will start ok. Doh its hardcoded with a IP of  http://169.254.119.5 for now will upload a configurable one soon as that isn't much use just noticed that now.

Source for the Visual C # 2010 part is below the panel uses Delphi 32 to connect to the Netduino Boards C Sharp program using the TCP/IP stack.

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net.NetworkInformation;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;


namespace NetduinoPlusWebserver
{
    class WebServer
    {
        private Socket socket = null;
        //open connection to onboard led so we can blink it with every request
      //  private OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        public WebServer()
        {
            //Initialize Socket class
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //Request and bind to an IP from DHCP server
            socket.Bind(new IPEndPoint(IPAddress.Any, 80));
            //Debug print our IP address
            Debug.Print(Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress);
           // Debug.Print(Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].DnsAddresses[0]);
          //  Debug.Print(Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].SubnetMask);
           
            //Start listen for web requests
            socket.Listen(10);
           
         ListenForRequest() ;
        }

        public void ListenForRequest()
        {
   OutputPort led2 = new OutputPort(Pins.ONBOARD_LED, true);
   int hitdata = 0;
           
            while (true)
            {
                led2.Write(true);
                Thread.Sleep(250);
                led2.Write(false);
             
               // string header = "HTTP/1.0 200 OK\r\nContent-Type: text; charset=utf-8\r\nContent-Length: " + response.Length.ToString() + "\r\nConnection: close\r\n\r\n";
               // clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
               // int bytesRecieved = socket.Available;
              //  if hitdata < 100 then
              
                if (hitdata < 100)
                    hitdata = hitdata + 1;
                if (hitdata >= 100)
               
                    hitdata = 4;
              // if hitdata
               using (Socket clientSocket = socket.Accept())
              
               
           //   clientSocket.RemoteEndPoint
                {
                    //Get clients IP
                    IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
                   // EndPoint clientEndPoint = clientSocket.RemoteEndPoint;
                    //int byteCount = cSocket.Available;
                    int bytesReceived = clientSocket.Available;
                    if (bytesReceived <= 0)
                    {
                        if (hitdata < 100)
                            hitdata = hitdata + 1;
                        if (hitdata >= 100)
                            hitdata = 4;    // send ping ok to the Alarm monitoring Delphi based software
                         string response = "Alarm OK ping number *" + hitdata.ToString()+'*';// from steves alarm.";
                      
                        string header = "HTTP/1.0 200 OK\r\nContent-Type: text; charset=utf-8\r\nContent-Length: " + response.Length.ToString() + "\r\nConnection: close\r\n\r\n";
                      
                        clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
                        clientSocket.Send(Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None);
                    }
                    if (bytesReceived > 0)
                    {
                        //Get request
                        if (hitdata < 100)
                            hitdata = hitdata + 1;
                        if (hitdata >= 100)
                            hitdata = 0;
                        byte[] buffer = new byte[bytesReceived];
                        int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);
                        string request = new string(Encoding.UTF8.GetChars(buffer));
                        Debug.Print(request);
                        //Compose a response
                        string response = "Alarm OK ping number *" + hitdata.ToString() + '*';// from steves alarm.";
                       // string idnumber = clientIP;
                        string header = "HTTP/1.0 200 OK\r\nContent-Type: text; charset=utf-8\r\nContent-Length: " + response.Length.ToString() + "\r\nConnection: close\r\n\r\n";
                        clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
                        clientSocket.Send(Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None);
                        //Blink the onboard LED
                        //led.Write(true);
                       // Thread.Sleep(150);
                       // led.Write(false);
                    }
                }
            }
        }
        #region IDisposable Members
        ~WebServer()
        {
            Dispose();
        }
        public void Dispose()
        {
            if (socket != null)
                socket.Close();
        }
        #endregion
    }

}


The Delphi code is just a TWebBrowser component and attach the event handler for document done.


// thanks to  http://stackoverflow.com/questions/2154933/extract-text-from-web-page-displayed-in-a-twebbrowser for
// the extraction codes

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
  var
  Document: IHtmlDocument2;

begin
 edit1.text:=url;
 edit3.Text := edit1.text;
  document := webbrowser1.document as IHtmlDocument2;
 // memo2.lines.add(trim(document.body.innerhtml));  // to get html
 if  (pos('*98*',trim(document.body.innerText)) <> 0) then
           memo1.Clear;

 if  (pos('*1*',trim(document.body.innerText)) = 0)
 and (pos('*2*',trim(document.body.innerText)) = 0)
 and (pos('webpage',trim(document.body.innerText))=0) then

// etc...............


// The first navigation to the IP address is done using a simple Timer event and refreshing the page in code

procedure TForm1.Timer1Timer(Sender: TObject);
var
  Document: IHtmlDocument2;
begin


//timer1.Enabled := false;
//timer3.enabled := true;
if edit3.Text = '' then
webbrowser1.Navigate(edhttp.text)

// etc then refresh the page each ping event with

else
begin
  webbrowser1.Refresh2;
  document := webbrowser1.document as IHtmlDocument2;

// if ping is 98 then clear control panel text (dont want to overfill the tmemo component storage as this panel has
// been tested for several hours.......

  if  (pos('*98*',trim(document.body.innerText)) <> 0) then
           memo1.Clear;

  if  (pos('*1*',trim(document.body.innerText)) = 0)
  and (pos('*2*',trim(document.body.innerText)) = 0)
  and (pos('webpage',trim(document.body.innerText))=0) then

                
// note this code may be change to work via serial instead of TCP/IP as this would free the TCP stack for
// upgrading the software remotely see N.i.p.p.l.e  

Previous page

Comments