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

}
}

Arduino WiFi Shield : JSON Parsing

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
167
168
#include ;
#include ;

char ssid[] = "Kazi-House"; // your network SSID (name)
char pass[] = "123456789"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(80);

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network.
// Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
pinMode(45, OUTPUT);
pinMode(46, OUTPUT);
pinMode(47, OUTPUT);
pinMode(48, OUTPUT);
pinMode(49, OUTPUT);
pinMode(51, OUTPUT);
}

void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
//Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.write(c);
if(c=='1'){
if(client.read() == '=') {
char onoff = client.read();
if( onoff == '1'){
Serial.println("Center Bulb On");
digitalWrite(45,HIGH);
}
else if(onoff == '0'){
Serial.println("Center Bulb Off");
digitalWrite(45,LOW);
}
}
}
else if(c=='2'){
if(client.read() == '=') {
char onoff = client.read();
if( onoff == '1'){
Serial.println("No Device 1 On");
digitalWrite(46,HIGH);
}
else if(onoff == '0'){
Serial.println("No Device 1 Off");
digitalWrite(46,LOW);
}
}
}
else if(c=='3'){
if(client.read() == '=') {
char onoff = client.read();
if( onoff == '1'){
Serial.println("BathRoom TubeLight On");
digitalWrite(47,HIGH);
}
else if(onoff == '0'){
Serial.println("BathRoom TubeLight Off");
digitalWrite(47,LOW);
}
}
}
else if(c=='4'){
if(client.read() == '=') {
char onoff = client.read();
if( onoff == '1'){
Serial.println("No Device 2 On");
digitalWrite(48,HIGH);
}
else if(onoff == '0'){
Serial.println("No Device 2 Off");
digitalWrite(48,LOW);
}
}
}
else if(c=='5'){
if(client.read() == '=') {
char onoff = client.read();
if( onoff == '1'){
Serial.println("Room TubeLight On");
digitalWrite(49,HIGH);
}
else if(onoff == '0'){
Serial.println("Room TubeLight Off");
digitalWrite(49,LOW);
}
}
}
else if(c=='6'){
if(client.read() == '=') {
char onoff = client.read();
if( onoff == '1'){
Serial.println("No Device 3 On");
digitalWrite(51,HIGH);
}
else if(onoff == '0'){
Serial.println("No Device 3 Off");
digitalWrite(51,LOW);
}
}
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
//Serial.println("client disonnected");
}
}

void printWifiStatus() {
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}

Communication Mediums: Arduino WiFi, RN-42 Bluetooth, Netduino Ethernet

Hello, I have gotten my hands on couple of communication devices which could be used for android and Arduino/ Netduino communication, first RN-42 SMD bought from robotics.pk – link, Arduino WiFi Shield also bought from robotics.pk – link and third is Netduino it self which comes with Ethernet shield built-in, and Netduino is given to me by my teacher for educational purposes only, sad cant keep it.

Any ways lets start of with, RN-42 SMD is fairly cheap comparatively to others available for sale on spark fun  and is very easy to use you just need 4 connections VCC(3.3v),GND,RX and TX and you are done and by default RN-42 runs on 115200 braud rate, I found this to be very cheap and easy to use although for making connections on the smd, could be stressing, you would need small and steady hands, after making connections and connecting it to my Arduino Mega 2560, I used my serial Bluetooth android app and data transfer was done perfectly and fast, and range was acceptable, about 50 to 60 feet. it is perfect fit for short range, battery powered application.

Second device is WiFi Shield, which lets Arduino board connect to the internet using the 802.11 wireless specification and comes with a micro-SD card socket for saving of html pages and data logging,  we could either use the WiFi Shield as Server or Client and in my case I use it as a server, although Arduino cant process anything above HTML and JavaScript but we can do with these all so well, with WiFi Shield, we use Get and Post methods – (which I will explain in next post how?) to send and receive data from Arduino, from then on its same as serial monitor or Bluetooth. So from my android I send a request on the ip which is assigned to WiFi Shield through DHCP, request could be Get or Post and Arduino executes that request.

Arduino and WiFi Shield : Controlling my room lights from Kazi Murtaza on Vimeo.

Third device which is Netduino in-built Ethernet Shield, I have not gotten around to configure it yet, but will do it next. I feel the results will be same as Arduino WiFi Shield.

So in end I realized one thing, WiFi Shield is a bit buggy, sometimes requests in buffer of Arduino don’t really execute and if they do its 4-5 sec delay, I guess it takes time to process the request and its fairly new so needs a firmware update, so WiFi shield is not recommended for on second execution for example like WiFi controlled car, for such projects Bluetooth should be preferred, and for projects such as Home Automation and such WiFi is ideal.

 

 

things

Netduino 101 : potentiometer example

December 19, 2012  |  Hardware, Netduino  |  , ,  |  1 Comment  |  Share

Things

potentiometer example takes analog input and light up leds accounting to the input 1023 divide by 5;

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
namespace potentiometer_netduino
{
public class Program
{
public static void Main()
{
var voltagePort = new AnalogInput(Pins.GPIO_PIN_A1);
var lowPort = new OutputPort(Pins.GPIO_PIN_A0, false);
var highPort = new OutputPort(Pins.GPIO_PIN_A2, true);

OutputPort led1 = new OutputPort(Pins.GPIO_PIN_D1, false);
OutputPort led2 = new OutputPort(Pins.GPIO_PIN_D2, false);
OutputPort led3 = new OutputPort(Pins.GPIO_PIN_D3, false);
OutputPort led4 = new OutputPort(Pins.GPIO_PIN_D4, false);
OutputPort led5 = new OutputPort(Pins.GPIO_PIN_D5, false);

AnalogInput socket = new AnalogInput(Pins.GPIO_PIN_A5);

int INPUT=0;
while (true)
{
INPUT = socket.Read();
if (INPUT > 100 && INPUT <= 200) {
led1.Write(true); led2.Write(false);
led3.Write(false); led4.Write(false);
led5.Write(false);
}
else if (INPUT > 200 && INPUT <= 400) {
led1.Write(true); led2.Write(true);
led3.Write(false); led4.Write(false);
led5.Write(false);
}

else if (INPUT > 400 && INPUT <= 600) {
led1.Write(true); led2.Write(true);
led3.Write(true); led4.Write(false);
led5.Write(false);
}
else if (INPUT > 600 && INPUT <= 800) {
led1.Write(true); led2.Write(true);
led3.Write(true); led4.Write(true);
led5.Write(false);
}
else if (INPUT > 800 && INPUT <= 1000) {
led1.Write(true); led2.Write(true);
led3.Write(true); led4.Write(true);
led5.Write(true);
}
else if (INPUT < 100) {
led1.Write(false); led2.Write(false);
led3.Write(false); led4.Write(false);
led5.Write(false);
}
Debug.Print(INPUT + " ");
}
}
}
}

Arduino vs Netduino : the more things change, more they remain same.

December 19, 2012  |  Hardware, Netduino  |  ,  |  No Comments  |  Share

netduinoArduino and Netduino are kinda of like small computers that have allowed us to do marvelous things, like controlling our environment and more they are not really that expensive Arduino mega 2560 is 7k and Netduino plus is 5k, if you have not heard of this before, this article is not really for you.

I recently got new Netduino Plus thanks to my sir for lending it to me, and I have been playing with it. I have done few small Arduino projects so far, so I was interested in seeing how they were all that different. I have to say, I was very impressed with it, but there are differences you should know about before you jump into using a Netduino.

The basic difference is Netduino is C# based on .net for programming liking the intellisense part a lot, and Netduino is 48MHz where as Arduino has 16MHz, both are open source.

The Netduino board is the same size and shape as an Arduino UNO board. There is a place to install a 6-pin header at the back of the board, though no header is installed, in the same spot Arduinos have a similar header. The TX and RX monitor LEDs that Arduinos have do not exist on the Netduino.

Netduino IDE only works on windows as you might have expected that by now, but you can setup the entire IDE for free, you will have to use visual studio 2010, which is easy to use.

Netduino has very good debugging environment, we can emulation and in circuit debugging(like any C# program which breakpoints and stuff) which is really great unlike Arduino, when you start debugging it downloads the code in Netduino and stops at the breakpoint so you can check each line while debugging.

Netduino does support the option to program it in Visual Basic but i prefer C#, Intellisense FTW. Here are some of difference between Netduino Plus and Arduino Mega 2560 Rev 3.

Category Difference
 Chip power  Internally, the CPU runs at 3.3V, not 5V like the Arduino, though it uses the same power sources
 Digital I/Os  Go from 0V to 3.3V, not 5V.  It will work with most 5V logic circuits, input and output.
 Analog Inputs  Must not go higher than 3.3V!
 PWM Outputs  PWM is often used like an analog output.   Since 100% averages to 3.3V instead of 5V, circuits may work differently
 Libraries  None of the Arduino libraries, which are C and C++ code, will work on the Netduino without modification.  If you use a board-specific library, you may have to rewrite it.
USB Connector  Uses cell-phone type mini USB connector
 I/O Current  The pins on the CPU can drive a maximum of 8mA of current, which is less than Arduino
 CPU  32-bit Atmel ARM, instead of 8-bit ATmega
 Speed  48MHz instead of 16MHz
 Program Memory  128K instead of 256K
 RAM  60K instead of 8K
 EEPROM  Netduino has none
 In-circuit debugging  Netduino has it
 Emulation  Netduino development environment has it

if you are making a project that requires a lot of processing power and awesome debugging environment than get yourself Netduino, if you want to use Arduino shields and libraries then Netduino is not for you.

Getting Started
Here are some links to get you started on Netduino:

Netduino Site
Netduino Getting Started PDF
Atmel Microcontroller Data
Atmel Microcontroller Full Datasheet
Netduino Schematic
Netduino Forums

Development Software
Microsoft Visual C# Express 2010
.NET Micro Framework SDK v4.1
Netduino SDK v4.1 (32-bit)
Netduino SDK v4.1 (64-bit)
.NET Micro Framework Reference