Ethernet Shield als Server und Client gleichzeitig?

Oké, sorry to continue in English.

I used this in a project of mine: CastDuino. More on this you can read at: http://www.jo3ri.be/a/jo3ri.be/jo3ri/arduino/projects/castduino

So what I did was use webserver and webclient alternating like this: (this in only partial code)

void setup()
{
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  server.begin();
  delay(3000); // we wait 3 sec to get the shield up and running
}

void loop()
{
  WebServer(); // in this void we will present our webpage(s) => web server
  SetTime(); // in this void we will get the time from an NTP server => web client
  CheckTwitter(); // in this void we will get a twitter feed => other web client
}

void WebServer(){
  EthernetClient client = server.available();
  if (client) {
    TextFinder finder(client,1); // number of seconds to wait for the next character before aborting the find and get methods.
    byte type = 0;
    while (client.connected()) {
      if (client.available()) {
        // GET, POST, or HEAD
        if(finder.getString("","/", buffer2,sizeof(buffer2)))
        { 
          if(strcmp(buffer2, "GET ") == 0 )
            type = 1;
          else if(strcmp(buffer2,"POST ") == 0)
            type = 2;
          // look for the page name
          if(finder.getString( "", "/", buffer2, sizeof(buffer2))) 
          {
            if(strcasecmp(buffer2, "showjobs") == 0)
              PageJobs(client, finder, type == 2);
            else if(strcasecmp(buffer2, "setslave") == 0)
              PageSetup(client, finder, type == 2);
            else if(strcasecmp(buffer2, "setupeth") == 0)
              PageConfigEth(client, finder, type == 2);
            else
              PageStandard(client);
          }
        }
        break;
      }
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }
}

void SetTime(){
  if (UseTimeServer == true){
    unsigned long currentMillis = millis();
    if((currentMillis - previousMillis > (Interval *3600000)) || (TimeUpdate == true)) {
      TimeUpdate = false;
      previousMillis = currentMillis;
      sendNTPpacket(TimeServer); // send an NTP packet to a time server
      // wait to see if a reply is available
      delay(1000);  
      if ( Udp.parsePacket() ) {
        // We've received a packet, read the data from it
        Udp.read(packetBufferNTP,NTP_PACKET_SIZE);  // read the packet into the buffer
        //the timestamp starts at byte 40 of the received packet and is four bytes,
        // or two words, long. First, esxtract the two words:
        unsigned long highWord = word(packetBufferNTP[40], packetBufferNTP[41]);
        unsigned long lowWord = word(packetBufferNTP[42], packetBufferNTP[43]);
        unsigned long secsSince1900 = highWord << 16 | lowWord;
        byte DTS = 0;

        const unsigned long seventyYears = 2208988800UL - (TimeOffset * 3600L) - (DayLightSaving * 3600L);
        unsigned long epoch = secsSince1900 - seventyYears;
        setTime(epoch);
        // now that we have time set, we will have to I2C it to the output ATMEGA
        buffer[0] = epoch >> 24;
        buffer[1] = epoch >> 16;
        buffer[2] = epoch >> 8;
        buffer[3] = epoch;
        Wire.beginTransmission(4); // transmit to device #4
        Wire.write('T');        // first send a 'T' so I2C slave knows we are sending time
        for (byte i = 0;i<4;i++){
          Wire.write(buffer[i]);
        }
        Wire.endTransmission();    // stop buffering and start transmitting
      }
    }
  }
}

void CheckTwitter(){
  unsigned long currentMillis = millis();
  if((currentMillis - TwitterMillis > 600000) || (TimeUpdate == true)) {
    TimeUpdate = false;
    TwitterMillis = currentMillis;
    String TwitterName; //We will get the twittername out of the twittercommand in the TXTjob
    String SearchString ="<title>";
    if (buffer[0] == '#' && buffer[1] == 'T'  && buffer[2] == '#'){
      // first let's get the name out of the twitter command.
      for (byte i = 3; i <19; i++){
        if (buffer[i] != 0x03){
          TwitterName[i-3] = buffer[i];
        }
      }
      if (client.connect("api.twitter.com", 80)) {
        TextFinder  finder( client,2 );
        // make HTTP GET request to twitter:
        client.print("GET /1/statuses/user_timeline.rss?screen_name=");
        client.print(TwitterName);
        client.println("&count=1 HTTP/1.1");
        client.println("HOST: api.twitter.com");
        client.println();
        while (client.connected()) {
          if (client.available()) {
            SearchString = SearchString + TwitterName + ": ";
            byte charsize = SearchString.length() + 1;
            char StartHere[charsize];
            char EndHere[] = "</title>";
            SearchString.toCharArray(StartHere,charsize);
            if((finder.find("<item>")&&(finder.getString(StartHere,EndHere,buffer,140)!=0)))
            break;
          }
        }
        delay(1);
        client.stop();
      }
      // don't make this less than 30000 (30 secs), because you can't connect to the twitter servers faster (you'll be banned)
      // off course it would be better to use the "Blink without delay", but I leave that to you.
    }
  }
}

Ok, so what the actual code of castduino does is (only small part is shown here):

  • receives text over UDP from another arduino (web client)
  • shows the webpage embedded in the code (web server)
  • sets time from a NTP server every 6 hours (web client)
  • checks day, hour, minutes
  • if correct day, hour, minute, it fill's a buffer with a text or tweet
  • gets a twitter feed (web client)
  • sends the text or tweet buffer over I2C to an other arduino that makes is show on a dot matrix.

And all this keeps repeating (so fast => 16 mhz) you won't really notice. and what does it mean?
I can check my embedded website, fill out forms, commit them, while time updates, text scrolls and tweet updates keep going on.

Hope it helps.