Multiples network connections

Hello,

I wanted to make a program which use webserver to control relay (easy) but send json url when a contact state change.

I've made this (from internet inspiration :slight_smile: ) :

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //assign arduino mac address
byte ip[] = { 192, 168, 77, 107 }; // ip in lan assigned to arduino
byte gateway[] = { 192, 168, 77, 254 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port arduino server will use
EthernetClient client;
//char serverName[] = "web.comporium.net"; // (DNS) zoomkat's test web page server
byte serverName[] = { 192, 168, 77, 100 }; // (IP) zoomkat web page server IP address
String readString; //used by server to capture GET request 

//////////////////////////

////////////////////////////
void setup(){

  pinMode(4, OUTPUT); //pin 5 selected to control
  pinMode(5, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  Ethernet.begin(mac,ip,gateway,gateway,subnet); 
  server.begin();
  Serial.begin(9600); 
  Serial.println("server/client 1.0 test 7/03/12"); // keep track of what is loaded
  Serial.println("Send an g in serial monitor to test client"); // what to do to test client
  attachInterrupt(digitalPinToInterrupt(2),sendChange, CHANGE);
  
  
}

void loop(){
  // check for serial input
  

  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 Zoomkat");
            client.println();
            client.println();  
          }
          else {   
            client.println("HTTP/1.1 200 OK"); //send new page on browser request
            client.println("Content-Type: text/html");
            client.println();

            client.println("<HTML>");
            client.println("<HEAD>");
            client.println("<TITLE>Arduino GET test page</TITLE>");
            client.println("</HEAD>");
            client.println("<BODY>");

            client.println("<H1>Zoomkat's simple Arduino 1.0 button</H1>");

            client.println("<a href='/?1on' target='inlineframe'>ON</a>"); 
            client.println("<a href='/?1off' target='inlineframe'>OFF</a>"); 
            client.println("<a href='/?2on' target='inlineframe'>2ON</a>"); 
            client.println("<a href='/?2off' target='inlineframe'>2OFF</a>"); 
            client.println("<IFRAME name=inlineframe style='display:none'>");          
            client.println("</IFRAME>");

            client.println("</BODY>");
            client.println("</HTML>");
          }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("1on") >0)//checks for on
          {
            digitalWrite(4, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("1off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          if(readString.indexOf("2on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("2off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}
void sendChange()
{
  if (digitalRead(2) == HIGH)
  {
    if (client.connect(serverName, 8084)) {
    Serial.println("connectedOn");
    client.println("GET /json.htm?type=command&param=switchlight&idx=156&switchcmd=On HTTP/1.0");
    client.println();
  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }
  
  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();
}
else
{
    if (client.connect(serverName, 8084)) {
    Serial.println("connectedOff");
    client.println("GET /json.htm?type=command&param=switchlight&idx=156&switchcmd=Off HTTP/1.0");
    client.println();
  } 
  else {
    Serial.println("connection failed");
    Serial.println();
  }
  
  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();
}
}

It works, but when I'm playing with the contact webserver stop responding.

Can anyone see want's wrong ?

It works, but when I'm playing with the contact webserver stop responding.

You mean the contact attached to pin 2 ?
Do you see any output from your Serial.print() debug statements ?
You have one potentially infinite loop() which would fail silently:

while(client.connected() && !client.available()) delay(1); //waits for data

Yes Contact attached to pin2

On the debug I see disconnecting at the end, so i don't think it's a loop.

I've try to put a debug print on the loop()

After interrupt program continues to loop() but webserver is unavailable.
I

Found a dirty solution.
If I move server.begin() to the loop() it works :slight_smile: