The Netduino forums have been replaced by new forums at community.wildernesslabs.co.
This site has been preserved for archival purposes only
and the ability to make new accounts or posts has been turned off.
Hello, I deployed the code bellow and for some reason, I kept getting 192.168.5.100 in debug window, what should I do to change the ip to 10.0.0.0
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(); } }}
I keep getting this error from the code below, if this is fixed I have set everything else to control my room lights
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(); } }
I think its fixed, i am going to connect everything now.I think the error was because of empty string so I am checking the string length before executing anything.
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(); } }}
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.
Great work Kazi! I'd love to start automating my house and all of these posts lately from yourself and a few other members are really getting me excited. I can't wait to see some more additions, and please do share more pictures and videos.