Med Amin

Med Amin

  • NA
  • 16
  • 9.1k

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
 
  1. //////////INCLUDE HEADER FILES //////////////////  
  2. #include <Ethernet.h>  
  3. #include <SPI.h>  
  4. #include <EEPROM.h>  
  5. #include <LiquidCrystal.h>  
  6. #include <SD.h>  
  7. #include <Wire.h>  
  8.   
  9. EthernetServer server(23 );  
  10. String content;  
  11. File myFile;  
  12. int ledPin = 7;  
  13. int  outputpin = 0;  
  14. void setup()  
  15. {  
  16.   IPAddress ip( 192, 168,1,110);  
  17.   byte mac[] = {   
  18.     0xDE,0xAE,0xBE,0xEF,0xFE,0xED  
  19.   };  
  20.   
  21.   Ethernet.begin( mac, ip );  
  22.   server.begin();  
  23.   if ( ! SD.begin( 4 ) )   
  24.   {  
  25.     return;  
  26.   }  
  27.   
  28.   Serial.begin(9600);  
  29. }  
  30.   
  31. void loop()  
  32. {  
  33.   waitIncommingConnection();                                                               
  34. }  
  35.   
  36. void waitIncommingConnection()  
  37. {  
  38.   
  39.   String pwd            = "";  
  40.   String inData         = "";  
  41.   EthernetClient client = server.available();  
  42.   
  43.   if ( client  )  
  44.   {  
  45.     while ( client.connected() )   
  46.     {        
  47.       if ( client.available() > 0)  
  48.       {   
  49.         char recieved = client.read();  
  50.         inData += recieved;                         
  51.         if (recieved == '\n')  
  52.         {                             
  53.           switch( inData[0] )  
  54.           {                              
  55.             case (char)'o' :   
  56.               client.println("o");  
  57.               digitalWrite(ledPin, HIGH);  
  58.             break;  
  59.   
  60.             case (char)'f' :  
  61.               client.println("f");  
  62.               digitalWrite(ledPin, LOW);  
  63.   
  64.             break;  
  65.   
  66.             case (char)'*' :  
  67.               Logout(client);    
  68.             break;  
  69.   
  70.             default:  
  71.                client.println('d');  
  72.             break;  
  73.           }  
  74.           inData = "";   
  75.         }  
  76.   
  77.       }  
  78.     }  
  79.   }  
  80.   else  
  81.   {  
  82.    client.println('v');  
  83.   }  
  84. }  
  85.   
  86. void Logout(EthernetClient client )  
  87. {   
  88.   client.print('x');  
  89.   client.stop();   
  90.   

 
My C# code
  1. public partial class Form1 : Form  
  2. {  
  3.     public Client client;  
  4.     public delegate void FeedBackCallback(string text);  
  5.     public delegate void UpdateMessageBoxCallback(string text);  
  6.   
  7.     public Form1()  
  8.     {  
  9.         InitializeComponent();  
  10.     }  
  11.   
  12.     private void connect_Click(object sender, EventArgs e)  
  13.     {  
  14.         string ipAddr = ip1.Text + "." + ip2.Text + "." + ip3.Text + "." + ip4.Text;  
  15.         string port = portInput.Text;  
  16.   
  17.         if (IsValidIPAddress(ipAddr) == true)  
  18.         {  
  19.             try  
  20.             {  
  21.                 if (client == null)  
  22.                     client = new Client(this);  
  23.   
  24.                 client.Connect(ipAddr, port);  
  25.                 //  client.Send(Encoding.GetEncoding(Constant.EncodingFormat).GetBytes("c"+'\n'));  
  26.                 disconect.Enabled = true;  
  27.                 connect.Enabled = false;  
  28.                 on.Enabled = true;  
  29.             }  
  30.             catch (SocketException se)  
  31.             {  
  32.                 MessageBox.Show("Unable to Connect.\r\n" + se.ToString());  
  33.             }  
  34.         }  
  35.         else  
  36.         {  
  37.             MessageBox.Show("Invaild Ip Adrress""Invaild Ip Adrress", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  38.         }  
  39.     }  
  40.   
  41.     private bool IsEmptyUserNamePasswordFields(string userName, string password)  
  42.     {  
  43.   
  44.         if (userName.Length == 0 || password.Length == 0)  
  45.         {  
  46.             MessageBox.Show("Password and Username field is required!""Required Fileds Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  47.             return false;  
  48.         }  
  49.         else  
  50.         {  
  51.             return true;  
  52.         }  
  53.     }  
  54.   
  55.     private bool IsValidIPAddress(string ipaddr)//Validate the input IP address  
  56.     {  
  57.         try  
  58.         {  
  59.             IPAddress.Parse(ipaddr);  
  60.             return true;  
  61.         }  
  62.         catch (Exception e)  
  63.         {  
  64.             return false;  
  65.         }  
  66.     }  
  67.   
  68.     private void on_Click(object sender, EventArgs e)  
  69.     {  
  70.         disconect.PerformClick();  
  71.         connect.PerformClick();  
  72.         try  
  73.         {  
  74.             if (client == null)  
  75.                 client = new Client(this);  
  76. client.Send(Encoding.GetEncoding(Constant.EncodingFormat).GetBytes("o" + '\n'));  
  77.             off.Enabled = true;  
  78.             on.Enabled = false;  
  79.         }  
  80.         catch (SocketException se)  
  81.         {  
  82.             MessageBox.Show("Unable to Connect.\r\n" + se.ToString());  
  83.         }  
  84.     }  
  85.   
  86.     private void off_Click(object sender, EventArgs e)  
  87.     {  
  88.         disconect.PerformClick();  
  89.         connect.PerformClick();  
  90.         try  
  91.         {  
  92.             if (client == null)  
  93.                 client = new Client(this);  
  94. client.Send(Encoding.GetEncoding(Constant.EncodingFormat).GetBytes("f" + '\n'));  
  95.             on.Enabled = true;  
  96.             off.Enabled = false;  
  97.         }  
  98.         catch (SocketException se)  
  99.         {  
  100.             MessageBox.Show("Unable to Connect.\r\n" + se.ToString());  
  101.         }  
  102.     }  
  103.   
  104.     private void disconect_Click(object sender, EventArgs e)  
  105.     {  
  106.         connect.Enabled = true;  
  107.         disconect.Enabled = false;  
  108.         client.Disconnect();  
  109.     }  

 
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