TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Med Amin
NA
16
9.4k
Arduino Led On/Off on Ethernet not working
Apr 26 2016 6:27 AM
I am trying to On/Off a led from desktop app on C# to Arduino using Ethernet (for serial port communication works), I am connecting to an IP Address to communicate with Arduino, but it looks something wrong with my code or my sketch for Arduino. (I am working with Ethernet shield) my code Arduino
//////////INCLUDE HEADER FILES //////////////////
#include <Ethernet.h>
#include <SPI.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
#include <SD.h>
#include <Wire.h>
EthernetServer server(23 );
String content;
File myFile;
int
ledPin = 7;
int
outputpin = 0;
void
setup()
{
IPAddress ip( 192, 168,1,110);
byte mac[] = {
0xDE,0xAE,0xBE,0xEF,0xFE,0xED
};
Ethernet.begin( mac, ip );
server.begin();
if
( ! SD.begin( 4 ) )
{
return
;
}
Serial.begin(9600);
}
void
loop()
{
waitIncommingConnection();
}
void
waitIncommingConnection()
{
String pwd =
""
;
String inData =
""
;
EthernetClient client = server.available();
if
( client )
{
while
( client.connected() )
{
if
( client.available() > 0)
{
char
recieved = client.read();
inData += recieved;
if
(recieved ==
'\n'
)
{
switch
( inData[0] )
{
case
(
char
)
'o'
:
client.println(
"o"
);
digitalWrite(ledPin, HIGH);
break
;
case
(
char
)
'f'
:
client.println(
"f"
);
digitalWrite(ledPin, LOW);
break
;
case
(
char
)
'*'
:
Logout(client);
break
;
default
:
client.println(
'd'
);
break
;
}
inData =
""
;
}
}
}
}
else
{
client.println(
'v'
);
}
}
void
Logout(EthernetClient client )
{
client.print(
'x'
);
client.stop();
}
My C# code
public
partial
class
Form1 : Form
{
public
Client client;
public
delegate
void
FeedBackCallback(
string
text);
public
delegate
void
UpdateMessageBoxCallback(
string
text);
public
Form1()
{
InitializeComponent();
}
private
void
connect_Click(
object
sender, EventArgs e)
{
string
ipAddr = ip1.Text +
"."
+ ip2.Text +
"."
+ ip3.Text +
"."
+ ip4.Text;
string
port = portInput.Text;
if
(IsValidIPAddress(ipAddr) ==
true
)
{
try
{
if
(client ==
null
)
client =
new
Client(
this
);
client.Connect(ipAddr, port);
// client.Send(Encoding.GetEncoding(Constant.EncodingFormat).GetBytes("c"+'\n'));
disconect.Enabled =
true
;
connect.Enabled =
false
;
on.Enabled =
true
;
}
catch
(SocketException se)
{
MessageBox.Show(
"Unable to Connect.\r\n"
+ se.ToString());
}
}
else
{
MessageBox.Show(
"Invaild Ip Adrress"
,
"Invaild Ip Adrress"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private
bool
IsEmptyUserNamePasswordFields(
string
userName,
string
password)
{
if
(userName.Length == 0 || password.Length == 0)
{
MessageBox.Show(
"Password and Username field is required!"
,
"Required Fileds Error"
, MessageBoxButtons.OK, MessageBoxIcon.Error);
return
false
;
}
else
{
return
true
;
}
}
private
bool
IsValidIPAddress(
string
ipaddr)
//Validate the input IP address
{
try
{
IPAddress.Parse(ipaddr);
return
true
;
}
catch
(Exception e)
{
return
false
;
}
}
private
void
on_Click(
object
sender, EventArgs e)
{
disconect.PerformClick();
connect.PerformClick();
try
{
if
(client ==
null
)
client =
new
Client(
this
);
client.Send(Encoding.GetEncoding(Constant.EncodingFormat).GetBytes(
"o"
+
'\n'
));
off.Enabled =
true
;
on.Enabled =
false
;
}
catch
(SocketException se)
{
MessageBox.Show(
"Unable to Connect.\r\n"
+ se.ToString());
}
}
private
void
off_Click(
object
sender, EventArgs e)
{
disconect.PerformClick();
connect.PerformClick();
try
{
if
(client ==
null
)
client =
new
Client(
this
);
client.Send(Encoding.GetEncoding(Constant.EncodingFormat).GetBytes(
"f"
+
'\n'
));
on.Enabled =
true
;
off.Enabled =
false
;
}
catch
(SocketException se)
{
MessageBox.Show(
"Unable to Connect.\r\n"
+ se.ToString());
}
}
private
void
disconect_Click(
object
sender, EventArgs e)
{
connect.Enabled =
true
;
disconect.Enabled =
false
;
client.Disconnect();
}
}
images for the Arduino
I don't know where is my problem, the app connect to the arduino, but the On/Off button not works for the led. if u want to see the code foe serial port communication this is the link here, Serial Port
Reply
Answers (
0
)
c# how to specify wich database to use
timer opens two windows instead of one