Ethernet only allowing 1 connection

Hi Team,

Can you help me out on this please.

I have the ethernet shield and the below code uploaded and running.

I am getting data output from both serial and intranet ip address as long as it is one machine.

Ie. I type the ip address onto a machine which the arduino is not connected and the data is displayed correctly. If I go to another machine and type in the ip address I get that data but the the first machine looses connection and I get IP address not found.

Is the arduino Ethernet shild limited to one machine reading at once or is it to do with the time delay.

Much appreciate your help.

Here’s the generic code…

/*
Arduino and Ethernet
*/

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

byte mac = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip = {192,168,1,66}; // don’t forget to change this IP address for your own situation

EthernetServer server(80);

void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
server.begin();
}

float voltage=0;
float sensor=0;

#define SENSOR_COUNT 1
const int analog_pins[SENSOR_COUNT] = {1};

void loop()
{
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if we’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (c == ‘\n’ && current_line_is_blank) {
// send a standard http response header
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println();
client.println("");
client.println("");
client.println("");
client.println(“Greenhill Mushrooms”);
client.println("<meta http-equiv=“refresh” content=“1”>");
client.println("");
client.println("");
client.println(“

Greenhill Mushrooms

”);
client.println(“

Room Monitoring System

”);
client.println("");
client.println("");

for(int I=0; I < SENSOR_COUNT; I++){
int sensorValue = analogRead(analog_pins*);*

  • float voltage;*

_ { voltage = ((sensorValue) * (5.15 / 1023.0));}_

  • if (voltage < 0){ voltage = 0.00;}*

_ voltage = voltage * 10;_
_ voltage = voltage * 2;_

  • client.print(“Room “);*
    client.print(analog_pins*);
    _
    client.print(”'s Temperature is “);_
    _
    client.print(voltage);_
    _
    client.println(”_
    _
    ”); // new line *_

* Serial.print("Sensor Value is : ");*
* Serial.print(sensorValue);*
* Serial.println();*
* Serial.print("Sensor Voltage is : ");*
* Serial.print(voltage, 3);*
* Serial.println();*

* delay (3000);*

* }*
* break;*
* }*
* if (c == ‘\n’) {*
* // we’re starting a new line*
* current_line_is_blank = true;
_
}_
_
else if (c != ‘\r’) {_
_
// we’ve gotten a character on the current line*_
* current_line_is_blank = false;
_
}_
_
}_
_
}_
_
// give the web browser time to receive the data*_
* delay(1);*
* client.stop();*
* }*
}
Regards
Witsend.

I’m nearly positive that your code does NOT look like that.

There are two stickies at the top of the forum that you clearly didn’t read, or you would have posted the code correctly. Go read them, and fix your post.

My Apologiees PaulS and team

I have read both of the stickies now thank you…

Sorry about missing the Start and End blocks. This code was cut and paste from the Ide. Which in turn came from another source.

My requirements for this code is to read of several room temp probes and have them display both via serial output and also via the Intranet. (Not internet for the public to see).

I have reduced the reads to only 1 shed for debugging purposes.

/*
 Arduino and Ethernet
 */
 
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192,168,1,66}; // Internal IP address within the Intranet.

EthernetServer server(80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  server.begin();
}

float voltage=0;
float sensor=0;

#define SENSOR_COUNT 1
const int analog_pins[SENSOR_COUNT] = {1};

void loop()
{
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if we've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so we can send a reply
        if (c == '\n' && current_line_is_blank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>Greenhill Mushrooms</title>");
          client.println("<meta http-equiv=\"refresh\" content=\"1\">");
          client.println("</head>");
          client.println("<body>");
          client.println("<h1 align=center>Greenhill Mushrooms</h1>");
          client.println("<h1 align=center>Room Monitoring System</h2>");          
          client.println("</body>");
          client.println("</html>");

        for(int I=0; I < SENSOR_COUNT; I++){
          int sensorValue = analogRead(analog_pins);
          float voltage;
      
          { voltage = ((sensorValue) * (5.15 / 1023.0));}
          if (voltage < 0){ voltage = 0.00;}
          
          voltage = voltage * 10;
          voltage = voltage * 2;
          
          client.print("Room ");
          client.print(analog_pins);
          client.print("'s Temperature is ");
          client.print(voltage);
          client.println("
"); // new line      
          
          Serial.print("Sensor Value is : ");
          Serial.print(sensorValue);
          Serial.println();
          Serial.print("Sensor Voltage is : ");
          Serial.print(voltage, 3);
          Serial.println();
          
          delay (3000);
        
          }
           break;
        }
        if (c == '\n') {
          // we're starting a new line
          current_line_is_blank = true;
        } 
        else if (c != '\r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
   client.stop();
  }
}

Below is some web page test code that up loads the analog pin values to the web page. It can accept request from multiple browser instances with the same IP address, but haven’t tried it with browsers with differing IP addresses.

// zoomkat's meta refresh data frame test page 5/25/13
// use http://192.168.1.102:84 in your brouser for main page
// http://192.168.1.102:84/data static data page
// http://192.168.1.102:84/datastart meta refresh data page
// for use with W5100 based ethernet shields
// set the refresh rate to 0 for fastest update
// use STOP for single data updates

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

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
unsigned long int x=0; //set refresh counter to 0
String readString; 

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

void setup(){
  Serial.begin(9600);
    // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("meta refresh data frame test 5/25/13"); // so I can keep track of what is loaded
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         if (readString.length() < 100) {
          readString += c; 
         } 
        //check if HTTP request has ended
        if (c == '\n') {

          //check get atring received
          Serial.println(readString);

          //output HTML data header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          //generate data page
          if(readString.indexOf("data") >0) {  //checks for "data" page
            x=x+1; //page upload counter
            client.print("<HTML><HEAD>");
            //meta-refresh page every 1 seconds if "datastart" page
            if(readString.indexOf("datastart") >0) client.print("<meta http-equiv='refresh' content='1'>"); 
            //meta-refresh 0 for fast data
            if(readString.indexOf("datafast") >0) client.print("<meta http-equiv='refresh' content='0'>"); 
            client.print("<title>Zoomkat's meta-refresh test</title></head><BODY>
");
            client.print("page refresh number: ");
            client.print(x); //current refresh count
            client.print("

");
            
              //output the value of each analog input pin
            client.print("analog input0 is: ");
            client.print(analogRead(analogInPin0));
            
            client.print("
analog input1 is: ");
            client.print(analogRead(analogInPin1));
                        
            client.print("
analog input2 is: ");
            client.print(analogRead(analogInPin2));
            
            client.print("
analog input3 is: ");
            client.print(analogRead(analogInPin3));
                                    
            client.print("
analog input4 is: ");
            client.print(analogRead(analogInPin4));
            
            client.print("
analog input5 is: ");
            client.print(analogRead(analogInPin5));
            client.println("
</BODY></HTML>");
           }
          //generate main page with iframe
          else
          {
            client.print("<HTML><HEAD><TITLE>Zoomkat's frame refresh test</TITLE></HEAD>");
            client.print("Zoomkat's Arduino frame meta refresh test 5/25/13");
            client.print("

Arduino analog input data frame:
");
            client.print("&nbsp;&nbsp;<a href='http://192.168.1.102:84/datastart' target='DataBox' title=''yy''>META-REFRESH</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/data' target='DataBox' title=''xx''>SINGLE-STOP</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/datafast' target='DataBox' title=''zz''>FAST-DATA</a>
");
            client.print("<iframe src='http://192.168.1.102:84/data' width='350' height='250' name='DataBox'>");
            client.print("</iframe>
</HTML>");
          }
          delay(1);
          //stopping client
          client.stop();
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

Thanks Zoomcat for the productive reply and the example.. Much appreciated

I shall give your code a whirl..

float voltage=0;

A global variable. Probably doesn’t need to be global, but, OK.

          float voltage;

A local variable of the same name. Never a good idea.
An uninitialized local variable is not a good idea, either.

          int sensorValue = analogRead(analog_pins);

analog_pins is an array. You can’t pass an array to analogRead().

          { voltage = ((sensorValue) * (5.15 / 1023.0));}
          if (voltage < 0){ voltage = 0.00;}

Useless curly braces and useless parentheses and a useless test, all in two lines of code. Impressive.

          voltage = voltage * 10;
          voltage = voltage * 2;

I’m nearly certain that if the Arduino can multiply by 10 and can multiply by 2, that it can multiply by 20.

Of course, the value no longer represents a voltage, so the purpose of this escapes me.

          client.print("Room ");
          client.print(analog_pins);
          client.print("'s Temperature is ");

analog_pins is an array. Dumping an array this way hardly makes sense.

          delay (3000);

The users of your website will not be frequent visitors when the page takes 3 seconds PER ROOM to load.

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>Greenhill Mushrooms</title>");
          client.println("<meta http-equiv=\"refresh\" content=\"1\">");
          client.println("</head>");
          client.println("<body>");
          client.println("<h1 align=center>Greenhill Mushrooms</h1>");
          client.println("<h1 align=center>Room Monitoring System</h2>");          
          client.println("</body>");
          client.println("</html>");

That’s a lot of data to be storing in the limited amount of SRAM available. Especially since that isn’t necessary.

Do this:
client.println(F(“HTTP/1.1 200 OK”));
to keep the string literals out of SRAM.

PaulS,

Thanks for the info… My take on what you said…

int sensorValue = analogRead(analog_pins);

As I was reading each of the 16 pins, I used the analog_pins as the pointer to each of the pins. (This works). This version of the pgm only has the 1 shed as I was trying to figure out why 2 machines could not get the data at the same time…

          { voltage = ((sensorValue) * (5.15 / 1023.0));}
          if (voltage < 0){ voltage = 0.00;}

Thanks for the positive input…
Basic maths tells you to do what is in the brackets separately then apply the left with the right. Its also called readability.
Yes useless bit of code

          voltage = voltage * 10;
          voltage = voltage * 2;

Agree it should be the one calculation… This was to be changed to * 20

          client.print("Room ");
          client.print(analog_pins);
          client.print("'s Temperature is ");

Analog pins is used to say… room 1’s temperature is xx, etc. Keep in mind there are 16 sheds and the array is used to identify each room.

delay (3000);

I will decrease the delay but will have the page refresh at a greater value once all rooms have been displayed.

client.println(F(“HTTP/1.1 200 OK”));

I’ll implement this…

I convert the data from a voltage (DC) to a value to be used in a VB .Net program that displays the values for each room. Hence why I don’t need the voltage once its read in.

Thanks for your feedback, and remember some of us are new to this world of Arduino. I have over 27 years programming in languages from Cobol, PLI, Assembler, Fortran, C, Basic, VB… (International Corporations) blah blah blah…but when learning a new language you try, make mistakes, learn from these mistakes and listen to others and improve.

Basic maths tells you to do what is in the brackets separately then apply the left with the right. Its also called readability.

          { voltage = ((sensorValue) * (5.15 / 1023.0));}
          if (voltage < 0){ voltage = 0.00;}

The parentheses around the 5.15 / 1023.0 part are good. The ones around sensorValue really do not help with readability. The { and } at the beginning and end do not help with readability.

Readability is improved by putting one statement per line.

          voltage = (sensorValue * (5.15 / 1023.0));
          if (voltage < 0)
          {
             voltage = 0.00;
          }

As I was reading each of the 16 pins, I used the analog_pins as the pointer to each of the pins. (This works).

No, it doesn’t work.

int sensorValue = analogRead(analog_pins[i]);

would.

Analog pins is used to say… room 1’s temperature is xx, etc. Keep in mind there are 16 sheds and the array is used to identify each room.

I disagree, on two counts. First, analog_pins[ i ] is the pin number that a sensor is attached to. Second, the room number might be i; It is not analog_pins[ i ].

I will decrease the delay

You misspelled “remove”. The data sent to the client should be sent as fast as possible. The refresh rate should be controlled by the client

I convert the data from a voltage (DC) to a value to be used in a VB .Net program that displays the values for each room. Hence why I don’t need the voltage once its read in.

Orders of magnitude faster to do that on the PC side, then. Send the PC app the raw value (as an int) and let it manipulate it to get the value, as a float, that it needs. The PC has a math co-processor. The Arduino does not.

Anyway, hopefully getting all the string literals out of SRAM will resolve your real issue. If not, post again, and we’ll keep trying.

Cheers..

I will implement as you stated.... Thanks for your help