Go Down

Topic: Communication problems (Read 343 times) previous topic - next topic

theredone

Hey there,

i have written an programm to communicate between my PC and the Arduino Ethernet. But after a few successful conections, something break and the board accept no connections. (Timeout). Is there a mistake in my code?

Code: [Select]
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xDD, 0x5C }; 
byte ip[] = { 192, 168, 87, 201 };   
byte gateway[] = { 192, 168, 87, 254 };
byte subnet[] = { 255, 255, 255, 0 };

int val = 0;

EthernetServer server(8765);



void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
 
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
 
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
 
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  /*pinMode(10, INPUT);
  pinMode(11, INPUT);*/
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true)
  {
    byte input = client.read();
   
    if(input == 110) //Tor 1 Hoch
    {
      digitalWrite(0, HIGH);
      delay(500);
      digitalWrite(0, LOW);
      server.write(input);
    }
    else if(input == 111) //Tor 1 Runter
    {
      digitalWrite(1, HIGH);
      delay(500);
      digitalWrite(1, LOW);
      server.write(input);
    }
    else if(input == 112) //Tor Status Up
    {
      val = digitalRead(2);
      if(val == HIGH)
        server.write(200);
      else
        server.write(100);
     
    }
    else if(input == 113) //Tor Status Down
    {
      val = digitalRead(3);
      if(val == HIGH)
        server.write(200);
      else
        server.write(100);
     
    }
   
    //#######################################
   
    else if(input == 120) //Tor 2 Hoch
    {
      digitalWrite(4, HIGH);
      delay(500);
      digitalWrite(4, LOW);
      server.write(input);
    }
    else if(input == 121) //Tor 2 Runter
    {
      digitalWrite(5, HIGH);
      delay(500);
      digitalWrite(5, LOW);
      server.write(input);
    }
    else if(input == 122) //Tor Status Up
    {
      val = digitalRead(6);
      if(val == HIGH)
        server.write(200);
      else
        server.write(100);
     
    }
    else if(input == 123) //Tor Status Down
    {
      val = digitalRead(7);
      if(val == HIGH)
        server.write(200);
      else
        server.write(100);
     
    }
   
    //#######################################
   
    else if(input == 130) //Tor 3 Hoch
    {
      digitalWrite(8, HIGH);
      delay(500);
      digitalWrite(8, LOW);
      server.write(input);
    }
    else if(input == 131) //Tor 3 Runter
    {
      digitalWrite(9, HIGH);
      delay(500);
      digitalWrite(9, LOW);
      server.write(input);
    }
    else
    {
      server.write(44);
    }
  }
}

SurferTim

You are not closing the connections, so you should run out of sockets after 4 connections. The last thing you should do is close the connection.
Code: [Select]
   client.stop();
You should use the client.write() calls instead of server.write().

It has no error checking like timeout, but it should work more than just 4 times.

PaulS

Code: [Select]
  EthernetClient client = server.available();
  if (client == true)

client will never equal true.

Code: [Select]
    byte input = client.read();
Then, whether the client has sent anything, or not, you read exactly one byte from the client. You never close the client connection, so, after 4 successful connections, the 4 sockets are all in use, and no more clients can connect.

Yes, you are doing something (more than one something, actually) wrong.

Look at how the example server code works, and how much stuff you left out.

theredone

Now it works.
Thanks a lot!

Go Up