Communication Mediums: Netduino Ethernet Part 2

Results were not the same, Netduino Ethernet was way faster than Arduino WiFi, no delay’s instant execution, Netduino Plus is bundle of joy for those who wish to build home automation cheap solution with Ethernet and SD-card slot, wins the race again Arduino & Arduino WiFi, making Netduino a web server and parsing data was a breeze and no bugs, I find it to be wonderfully robust, have a look at the video below;

Netduino & Ethernet Sheild : Controlling my room lights from Kazi Murtaza.

 

and here is the code first webserver.cs

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()[].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 > )
					{
						//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)");

						if(filename.Length>){
							filename.ToCharArray();
							String DeviceID = "0"; if (!(filename[].Equals(null))) { DeviceID = filename[].ToString(); }
							String DeviceSTATE = "0"; if (!(filename[2].Equals(null))) { DeviceSTATE = filename[2].ToString(); }

							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);
								}
							}
						}

						//Compose a response
						string response = "Hello World";
						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
	}
	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();
		}
	}
}
Program.cs
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 WebServer
{
	public class Program
	{
		public static void Main()
		{
			Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[].EnableStaticIP("10.0.0.11","255.255.255.0","10.0.0.1");
			WebServer webServer = new WebServer();
			webServer.ListenForRequest();
		}

	}
}

comments powered by Disqus