Posts Tagged ‘digital life assitance software’

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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)");

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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()[0].EnableStaticIP("10.0.0.11","255.255.255.0","10.0.0.1");
WebServer webServer = new WebServer();
webServer.ListenForRequest();
}

}
}

My Jarvis

Jarvis, the one from iron man movie, Yes, Digital Life Assistance crazy idea for hobby project, right ? well we have seen many different kind’s of implementation’s in hope’s of achieving what that rich bastard has, but if we were to have it, we must first have platform to build upon, as far as I can see we need a system which is capable of achieving tasks in real world and virtual world, so to start off my first task is controlling the lights with Arduino, don’t worry I will be taking it slow. Idea, if it was not clear above,  idea is to use Arduino without spending a lot of money(very crucial) to control devices such as bulbs, fans and stuff,  I plan on using some sort of communication shield, When done it will send some MESSEAGE(not really clear on that) to Arduino, Arduino will move some relays and give me back a  response, toggling devices.

next post will be about requirements and basic concept of controlling a device through relay.