using System;using Microsoft.SPOT;namespace WebServer{ using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; public class WebServer : IDisposable { private Socket socket = null; //open connection to onbaord led so we can blink it with every request private OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); static OutputPort Device1 = new OutputPort(Pins.GPIO_PIN_D5, false); static OutputPort Device2 = new OutputPort(Pins.GPIO_PIN_D6, false); static OutputPort Device3 = new OutputPort(Pins.GPIO_PIN_D7, false); static OutputPort Device4 = new OutputPort(Pins.GPIO_PIN_D8, false); static OutputPort Device5 = new OutputPort(Pins.GPIO_PIN_D9, false); static OutputPort Device6 = new OutputPort(Pins.GPIO_PIN_D10, 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); //Start listen for web requests socket.Listen(10); ListenForRequest(); } public void ListenForRequest() { while (true) { using (Socket clientSocket = socket.Accept()) { //Get clients IP IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint; EndPoint clientEndPoint = clientSocket.RemoteEndPoint; //int byteCount = cSocket.Available; int bytesReceived = clientSocket.Available; if (bytesReceived > 0) { //Get request byte[] buffer = new byte[bytesReceived]; int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None); string request = new string(Encoding.UTF8.GetChars(buffer)); string filename = ProcessResponse.GetTextBetween(request, "(java 1.4)"); //Debug.Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); if(filename.Length>0){ filename.ToCharArray(); String DeviceID = "0"; if (!(filename[0].Equals(null))) { DeviceID = filename[0].ToString(); } String DeviceSTATE = "0"; if (!(filename[2].Equals(null))) { DeviceSTATE = filename[2].ToString(); } Debug.Print("Device ID "+ DeviceID + " Value is " + DeviceSTATE); //Debug.Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); if (DeviceID.Equals("1")) { if(DeviceSTATE.Equals("1")){ Device1.Write(true); } else if (DeviceSTATE.Equals("0")) { Device1.Write(false); } } else if (DeviceID.Equals("2")) { if (DeviceSTATE.Equals("1")) { Device2.Write(true); } else if (DeviceSTATE.Equals("0")) { Device2.Write(false); } } else if (DeviceID.Equals("3")) { if (DeviceSTATE.Equals("1")) { Device3.Write(true); } else if (DeviceSTATE.Equals("0")) { Device3.Write(false); } } else if (DeviceID.Equals("4")) { if (DeviceSTATE.Equals("1")) { Device4.Write(true); } else if (DeviceSTATE.Equals("0")) { Device4.Write(false); } } else if (DeviceID.Equals("5")) { if (DeviceSTATE.Equals("1")) { Device5.Write(true); } else if (DeviceSTATE.Equals("0")) { Device5.Write(false); } } else if (DeviceID.Equals("6")) { if (DeviceSTATE.Equals("1")) { Device6.Write(true); } else if (DeviceSTATE.Equals("0")) { Device6.Write(false); } } } //Debug.Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //Compose a response string response = "Hello World"; string header = "HTTP/1.0 200 OKrnContent-Type: text; charset=utf-8rnContent-Length: " + response.Length.ToString() + "rnConnection: closernrn"; 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 } public static class ProcessResponse { public static string GetTextBetween(string str, string a) { if (str == null || str == String.Empty) { return String.Empty; } int aIdx = str.IndexOf(a); if (aIdx == -1) { return String.Empty; } int strt = aIdx + a.Length; return str.Substring(strt).TrimStart(); //return str.Substring(strt, strt).Trim(); } }}
- Netduino Forums
- → Kazi Murtaza's Content
Kazi Murtaza's Content
There have been 9 items by Kazi Murtaza (Search limited from 06-June 23)
#42139 Netduino Plus Ethernet Shield
Posted by Kazi Murtaza on 25 December 2012 - 01:44 PM in Netduino Plus 2 (and Netduino Plus 1)
#42438 Netduino Plus Ethernet Shield
Posted by Kazi Murtaza on 30 December 2012 - 02:51 PM in Netduino Plus 2 (and Netduino Plus 1)
no everything works fine.. now my last post has the updated code video and other details are availible in the direct link below on my blog, if I am missing something let me know I will share here.
#42497 Netduino Plus Ethernet Shield
Posted by Kazi Murtaza on 31 December 2012 - 12:50 PM in Netduino Plus 2 (and Netduino Plus 1)
http://forums.netdui...eld/#entry42139
this post is the best answer XD, I fixed it myself now no more breaking and to view the video of my experiment go to my blog i hope i am clear
#42062 Netduino Plus Ethernet Shield
Posted by Kazi Murtaza on 23 December 2012 - 06:33 PM in Netduino Plus 2 (and Netduino Plus 1)
using System;using Microsoft.SPOT;using System.Net.Sockets;using System.Net;using System.Threading;using System.Text;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware.NetduinoPlus;namespace WebserverHelloWorld{ public class WebServer : IDisposable { private Socket socket = null; //open connection to onbaord 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); //Start listen for web requests socket.Listen(10); ListenForRequest(); } public void ListenForRequest() { while (true) { using (Socket clientSocket = socket.Accept()) { //Get clients IP IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint; EndPoint clientEndPoint = clientSocket.RemoteEndPoint; //int byteCount = cSocket.Available; int bytesReceived = clientSocket.Available; if (bytesReceived > 0) { //Get request 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 = "Hello World"; string header = "HTTP/1.0 200 OKrnContent-Type: text; charset=utf-8rnContent-Length: " + response.Length.ToString() + "rnConnection: closernrn"; 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 }}
using System;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.NetduinoPlus;namespace WebserverHelloWorld{ public class Program { public static void Main() { Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].EnableDhcp(); WebServer webServer = new WebServer(); webServer.ListenForRequest(); } }}
#42128 Netduino Plus Ethernet Shield
Posted by Kazi Murtaza on 25 December 2012 - 10:25 AM in Netduino Plus 2 (and Netduino Plus 1)
using System;using Microsoft.SPOT;namespace WebServer{ using System; using Microsoft.SPOT; using System.Net.Sockets; using System.Net; using System.Threading; using System.Text; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; using SecretLabs.NETMF.Hardware; public class WebServer : IDisposable { private Socket socket = null; //open connection to onbaord led so we can blink it with every request private OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); static OutputPort Device1 = new OutputPort(Pins.GPIO_PIN_D5, false); static OutputPort Device2 = new OutputPort(Pins.GPIO_PIN_D6, false); static OutputPort Device3 = new OutputPort(Pins.GPIO_PIN_D7, false); static OutputPort Device4 = new OutputPort(Pins.GPIO_PIN_D8, false); static OutputPort Device5 = new OutputPort(Pins.GPIO_PIN_D9, false); static OutputPort Device6 = new OutputPort(Pins.GPIO_PIN_D10, 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); //Start listen for web requests socket.Listen(10); ListenForRequest(); } public void ListenForRequest() { while (true) { using (Socket clientSocket = socket.Accept()) { //Get clients IP IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint; EndPoint clientEndPoint = clientSocket.RemoteEndPoint; //int byteCount = cSocket.Available; int bytesReceived = clientSocket.Available; if (bytesReceived > 0) { //Get request byte[] buffer = new byte[bytesReceived]; int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None); string request = new string(Encoding.UTF8.GetChars(buffer)); string filename = ProcessResponse.GetTextBetween(request, "(java 1.4)"); //Debug.Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); filename.ToCharArray(); String DeviceID = filename[0].ToString(); //[size="5"][b]<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< code breaks here[/b][/size] String DeviceSTATE = filename[2].ToString(); Debug.Print("Device ID "+ DeviceID + " Value is " + DeviceSTATE); //Debug.Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); if (DeviceID.Equals("1")) { if(DeviceSTATE.Equals("1")){ Device1.Write(true); } else if (DeviceSTATE.Equals("0")) { Device1.Write(false); } } else if (DeviceID.Equals("2")) { if (DeviceSTATE.Equals("1")) { Device2.Write(true); } else if (DeviceSTATE.Equals("0")) { Device2.Write(false); } } else if (DeviceID.Equals("3")) { if (DeviceSTATE.Equals("1")) { Device3.Write(true); } else if (DeviceSTATE.Equals("0")) { Device3.Write(false); } } else if (DeviceID.Equals("4")) { if (DeviceSTATE.Equals("1")) { Device4.Write(true); } else if (DeviceSTATE.Equals("0")) { Device4.Write(false); } } else if (DeviceID.Equals("5")) { if (DeviceSTATE.Equals("1")) { Device5.Write(true); } else if (DeviceSTATE.Equals("0")) { Device5.Write(false); } } else if (DeviceID.Equals("6")) { if (DeviceSTATE.Equals("1")) { Device6.Write(true); } else if (DeviceSTATE.Equals("0")) { Device6.Write(false); } } //Debug.Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //Compose a response string response = "Hello World"; string header = "HTTP/1.0 200 OKrnContent-Type: text; charset=utf-8rnContent-Length: " + response.Length.ToString() + "rnConnection: closernrn"; 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 } public static class ProcessResponse { public static string GetTextBetween(string str, string a) { if (str == null || str == String.Empty) { return String.Empty; } int aIdx = str.IndexOf(a); if (aIdx == -1) { return String.Empty; } int strt = aIdx + a.Length; return str.Substring(strt).TrimStart(); //return str.Substring(strt, strt).Trim(); } }}but the code still throws exception above I have marked it, output is below;
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesMicrosoft .NET Micro Frameworkv4.1Assemblieslemscorlib.dll', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesMicrosoft .NET Micro Frameworkv4.1AssembliesleMicrosoft.SPOT.Native.dll', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesMicrosoft .NET Micro Frameworkv4.1AssembliesleMicrosoft.SPOT.Hardware.dll', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesMicrosoft .NET Micro Frameworkv4.1AssembliesleMicrosoft.SPOT.Net.dll', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesMicrosoft .NET Micro Frameworkv4.1AssembliesleSystem.dll', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesMicrosoft .NET Micro Frameworkv4.1AssembliesleMicrosoft.SPOT.IO.dll', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesMicrosoft .NET Micro Frameworkv4.1AssembliesleSystem.IO.dll', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesMicrosoft .NET Micro Frameworkv4.1AssembliesleMicrosoft.SPOT.Hardware.SerialPort.dll', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesMicrosoft .NET Micro Frameworkv4.1AssembliesleMicrosoft.SPOT.Hardware.Usb.dll', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesSecret LabsNetduino SDKAssembliesv4.1leSecretLabs.NETMF.Hardware.dll', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'SecretLabs.NETMF.Diagnostics''Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:UserskazimurtazaAppDataLocalTemporary ProjectsWebServerbinDebugleWebServer.exe', Symbols loaded.'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:Program FilesSecret LabsNetduino SDKAssembliesv4.1leSecretLabs.NETMF.Hardware.NetduinoPlus.dll', Symbols loaded.The thread '<No Name>' (0x2) has exited with code 0 (0x0).10.0.0.11Device ID 3 Value is 1Device ID 3 Value is 0Device ID 3 Value is 1Device ID 3 Value is 0 #### Exception System.ArgumentOutOfRangeException - CLR_E_OUT_OF_RANGE (1) #### #### Message: #### System.String::get_Chars [IP: 0000] #### #### WebServer.WebServer::ListenForRequest [IP: 0062] #### #### WebServer.WebServer::.ctor [IP: 0049] #### #### WebServer.Program::Main [IP: 0016] ####A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dllAn unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
#42089 Netduino Plus Ethernet Shield
Posted by Kazi Murtaza on 24 December 2012 - 02:39 PM in Netduino Plus 2 (and Netduino Plus 1)
10.0.0.11xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx #### Exception System.ArgumentOutOfRangeException - CLR_E_OUT_OF_RANGE (1) #### #### Message: #### System.String::get_Chars [IP: 0000] #### #### WebServer.WebServer::ListenForRequest [IP: 0069] #### #### WebServer.WebServer::.ctor [IP: 0049] #### #### WebServer.Program::Main [IP: 0016] ####A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dllAn unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
using System;using Microsoft.SPOT;namespace WebServer{ using System; using Microsoft.SPOT; using System.Net.Sockets; using System.Net; using System.Threading; using System.Text; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; public class WebServer : IDisposable { private Socket socket = null; //open connection to onbaord 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); //Start listen for web requests socket.Listen(10); ListenForRequest(); } public void ListenForRequest() { while (true) { using (Socket clientSocket = socket.Accept()) { //Get clients IP IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint; EndPoint clientEndPoint = clientSocket.RemoteEndPoint; //int byteCount = cSocket.Available; int bytesReceived = clientSocket.Available; if (bytesReceived > 0) { //Get request byte[] buffer = new byte[bytesReceived]; int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None); string request = new string(Encoding.UTF8.GetChars(buffer)); string filename = ProcessResponse.GetTextBetween(request, "(java 1.4)"); Debug.Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); filename.ToCharArray(); int DeviceID = int.Parse(filename[0].ToString()); int DeviceSTATE = int.Parse(filename[2].ToString()); Debug.Print("Device ID "+ DeviceID + " Value is " + DeviceSTATE); Debug.Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //Compose a response string response = "Hello World"; string header = "HTTP/1.0 200 OKrnContent-Type: text; charset=utf-8rnContent-Length: " + response.Length.ToString() + "rnConnection: closernrn"; 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 } public static class ProcessResponse { public static string GetTextBetween(string str, string a) { if (str == null || str == String.Empty) { return String.Empty; } int aIdx = str.IndexOf(a); if (aIdx == -1) { return String.Empty; } int strt = aIdx + a.Length; return str.Substring(strt).TrimStart(); //return str.Substring(strt, strt).Trim(); } }}
public class Program { public static void Main() { Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].EnableStaticIP("10.0.0.11","255.255.255.0","10.0.0.1"); WebServer webServer = new WebServer(); webServer.ListenForRequest(); } }
#42123 Netduino Plus Ethernet Shield
Posted by Kazi Murtaza on 25 December 2012 - 09:07 AM in Netduino Plus 2 (and Netduino Plus 1)
int DeviceID = int.Parse(filename[0].ToString());this line breaks and yes for sure, I will upload the video here.. and i added below code but it didn't trigger the device
if (DeviceID.Equals("1")) { if(DeviceSTATE.Equals("1")){ Device1.Write(true); } else if (DeviceSTATE.Equals("0")) { Device1.Write(false); } } else if (DeviceID.Equals("2")) { if (DeviceSTATE.Equals("1")) { Device2.Write(true); } else if (DeviceSTATE.Equals("0")) { Device2.Write(false); } } else if (DeviceID.Equals("3")) { if (DeviceSTATE.Equals("1")) { Device3.Write(true); } else if (DeviceSTATE.Equals("0")) { Device3.Write(false); } } else if (DeviceID.Equals("4")) { if (DeviceSTATE.Equals("1")) { Device4.Write(true); } else if (DeviceSTATE.Equals("0")) { Device4.Write(false); } } else if (DeviceID.Equals("5")) { if (DeviceSTATE.Equals("1")) { Device5.Write(true); } else if (DeviceSTATE.Equals("0")) { Device5.Write(false); } } else if (DeviceID.Equals("6")) { if (DeviceSTATE.Equals("1")) { Device6.Write(true); } else if (DeviceSTATE.Equals("0")) { Device6.Write(false); } }
#42085 Netduino Plus Ethernet Shield
Posted by Kazi Murtaza on 24 December 2012 - 09:48 AM in Netduino Plus 2 (and Netduino Plus 1)
thank youinstead of enabling dhcp setting an ip
#42141 Updating Netduino Plus
Posted by Kazi Murtaza on 25 December 2012 - 03:09 PM in Netduino Plus 2 (and Netduino Plus 1)
- Netduino Forums
- → Kazi Murtaza's Content
- Privacy Policy