Using Web server with Ethernet Shield for a volume Indicator with two gauges

Hi Everybody !
I'm new in the forum
I need some help for a project, I'm developing an indicator of volume [700L and1000 L] via ultrasonics sensors HC-SR04
Then Im sending that data via IP with an Ethernet Shield storing the information in an Index

The issue here its that just one information is sent to the interface, the other sensor doesn´t read and just send that the volume is 1000L and doesn't move anymore because its no reading.
please if you have any Idea of how to get it going with both sensors at the same time , let me Know

I'm attaching my arduino Code, my HTML code and the web page I considered to make the code.

PS. The code has some words in spanish ... I'm from Mexico
PS1. In the HTML I'm using canvas code
PS2. My reference :Two Gauge Arduino Web Server
PS3. I attached the HTML Code due to the maximum allowed length of the post

Code For arduino:

include <LiquidCrystal.h>// LIBRERIA PARA LA LCD
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <NewPing.h>
#define enviasonicodere 7 // TRIG del sensor va conectado en el Pin Digital 8 de el ARDUINO 
#define recibesonicodere 8 //ECHO del sensor va conectado en el Pin Digital  9 de el ARDUINO
#define enviasonicoizq 9 // TRIG del sensor va conectado en el pin Digital 10 de el ARDUINO
#define recibesonicoizq 10 //ECHO del sensor va conectado en el pin Digital 11 de el ARDUINO
#define REQ_BUF_SZ   90    // Buffer para las peticiones HTTP
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 6, 50, 157); // Nuestra dirección IP
//IPAddress gateway(192, 168, 1, 254);
//IPAddress subnet(255, 255, 255, 0);
EthernetServer server(80);  
File webFile;               // El fichero de la pagina web en la SD card
char HTTP_req[REQ_BUF_SZ] = {0}; 
char req_index = 0;    
LiquidCrystal lcd (12,11,5,4,3,2);//PINES A LOS QUE VA LA LCD= RS,E,D4,D3,D2,D1
//----------------------------  INICIA SETUP --------------------------------
void setup()
{
    lcd.begin(16,2);
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("  VOLUMEN ES           ");
     pinMode(53, OUTPUT);  
     pinMode(10, OUTPUT);
     digitalWrite(10, HIGH);
     Serial.begin(9600);       // for debugging
//----------------------- Incializa MicroSD -------------------------
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) 
    {   Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    }
    Serial.println("SUCCESS - SD card initialized.");
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    }
    Serial.println("SUCCESS - Found index.htm file.");
    
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
}
//-------------------------------  INICIA LOOP -------------------------
void loop()
{  EthernetClient client = server.available();  
if (client) {                           // Si tenemos cliente
boolean currentLineIsBlank = true;      
while (client.connected()) {
if (client.available())   {  // Hay algo pendiente de leer 
char c = client.read(); 
 //Serial.println(c);
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c;          // Montar la peticion HTTP
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");                    
if (StrContains(HTTP_req, "ajax_inputs")) {
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
XML_response(client);
 }
 else if (StrContains(HTTP_req, "GET / ")
|| StrContains(HTTP_req, "GET /index.htm")) {                                   
client.println("Content-Type: text/html");  //  image/jpg   or    text/html
client.println("Connection: keep-alive");  //Connection: close OR keep-alive
client.println();
webFile = SD.open("index.htm");        // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
} 
 }                  
Serial.print(HTTP_req);
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
                // every line of text received from the client ends with \r\n
                if (c == '\n') { 
                     currentLineIsBlank = true;
                }
                else if (c != '\r') { 
                    currentLineIsBlank = false;
                }   
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
        delay(2);                                  
    } // end if (client)
    

}


       

void XML_response(EthernetClient cl)
{  
    int analog_val;
    cl.print("<?xml version = \"1.0\" ?>");
    cl.print("<inputs>");
    analog_val = capacidad();
    cl.print("<analog>");
    cl.print(analog_val);
    cl.print("</analog>");
    analog_val = capacidad2();
    cl.print("<analog>");
    cl.print(analog_val);
    cl.print("</analog>");
    cl.print("</inputs>");
  

}



void StrClear(char *str, char length)
{
    for (int i = 0; i < length; i++) {
        str[i] = 0;
    }
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
    char found = 0;
    char index = 0;
    char len;
    len = strlen(str);    
    if (strlen(sfind) > len) {
        return 0;
    }
    while (index < len) {
        if (str[index] == sfind[found]) {
            found++;
            if (strlen(sfind) == found) {
                return 1;
            }
        }
        else {
            found = 0;
        }
        index++;
    }
    return 0;
}


 
      float capacidad()
      {
       NewPing ultradere(enviasonicodere, recibesonicodere, 300);//(Trig8,Echo9)
      
        delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = ultradere.ping(); // Send ping, get ping time in microseconds (uS).
 /* Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
  */
       float volumen=0, volumen1=0, volumen2=0, H=0, Hh=0; 
       double x=0, L=152.5;
       int R=38, sen=0, nivelb=86;

       int boton=13; //BOTON PARA MOSTRAR VOLUMEN
       int state=0; //ESTADO DEL BOTON
       sen=((-R+H)/R);
      // long microsec=ultradere.Timing();
     //  H = ultradere.Ranging(CM);
     H=(uS / US_ROUNDTRIP_CM);
       Hh=H-10;
       volumen=(L*(((3.1415*(R*R))/2)+((Hh-R)*sqrt(2*R*Hh-(Hh*Hh)))+(R*R)*asin((-R+Hh)/R)));
       volumen1=volumen/1000;
       volumen2=700-volumen1;
       
       if (Hh <= nivelb)
       {
        volumen=(L*(((3.1415*(R*R))/2)+((Hh-R)*sqrt(2*R*Hh-(Hh*Hh)))+(R*R)*asin((-R+Hh)/R)));
        volumen1=volumen/1000;
        volumen2=700-volumen1;
       }
       else if (Hh > nivelb){ volumen2 = 0; }
   
       if (Hh < 0 ){ volumen2 = 700; }
       
       return volumen2;
       }

       
       
       float capacidad2()
       {
         NewPing ultraizq(enviasonicoizq, recibesonicoizq, 300);//(Trig8,Echo9)
      
        delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS1 = ultraizq.ping(); // Send ping, get ping time in microseconds (uS).
  /*
  Serial.print("Ping: ");
  Serial.print(uS1 / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
*/
       float volumen2=0, volumen12=0, volumen22=0, H2=0, Hh2=0; 
       double L2=153;
       int R2=46, sen2=0, nivelb2=102;
       //
       //
       sen2=((-R2+H2)/R2);
      // long microsec=ultraizq.Timing();
      // H2 = ultraizq.Ranging(CM);
       H2 = (uS1 / US_ROUNDTRIP_CM);
       Hh2 = H2-10;
       volumen2 = (L2*(((3.1415*(R2*R2))/2)+((Hh2-R2)*sqrt(2*R2*Hh2-(Hh2*Hh2)))+(R2*R2)*asin((-R2+Hh2)/R2)));
       volumen12 = volumen2/1000;
       volumen22 = 1000-volumen12;
       //
              ///
       if (Hh2 <= nivelb2)
       {
        volumen2=(L2*(((3.1415*(R2*R2))/2)+((Hh2-R2)*sqrt(2*R2*Hh2-(Hh2*Hh2)))+(R2*R2)*asin((-R2+Hh2)/R2)));
        volumen12=volumen2/1000;
        volumen22=1000-volumen12;
       }
       else if (Hh2 > nivelb2){ volumen22 = 0; }
   
       if (Hh2 < 0 ){ volumen22 = 1000; }
       
       return volumen22;
       
       }

index1.txt (16.8 KB)

Then Im sending that data via IP with an Ethernet Shield storing the information in an Index

This doesn't make sense

void loop()
{  EthernetClient client = server.available();  
if (client) {

.
NOTHING goes after the {.

Use Tools + Auto Format to properly indent your code.

What does the XML file that gets sent look like?

The XML File is attached.

Thanks for answering.

And for the code here is it again, Dont' understand how to Use Tools + Auto Format to properly indent your code... could you explain it to me please?

include <LiquidCrystal.h>// LIBRERIA PARA LA LCD
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <NewPing.h>
#define enviasonicodere 7 // TRIG del sensor va conectado en el Pin Digital 8 de el ARDUINO
#define recibesonicodere 8 //ECHO del sensor va conectado en el Pin Digital  9 de el ARDUINO
#define enviasonicoizq 9 // TRIG del sensor va conectado en el pin Digital 10 de el ARDUINO
#define recibesonicoizq 10 //ECHO del sensor va conectado en el pin Digital 11 de el ARDUINO
#define REQ_BUF_SZ   90    // Buffer para las peticiones HTTP
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 6, 50, 157); // Nuestra dirección IP
//IPAddress gateway(192, 168, 1, 254);
//IPAddress subnet(255, 255, 255, 0);
EthernetServer server(80);  
File webFile;               // El fichero de la pagina web en la SD card
char HTTP_req[REQ_BUF_SZ] = {0};
char req_index = 0;    
LiquidCrystal lcd (12,11,5,4,3,2);//PINES A LOS QUE VA LA LCD= RS,E,D4,D3,D2,D1
//----------------------------  INICIA SETUP --------------------------------
void setup()
{
    lcd.begin(16,2);
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("  VOLUMEN ES           ");
     pinMode(53, OUTPUT);  
     pinMode(10, OUTPUT);
     digitalWrite(10, HIGH);
     Serial.begin(9600);       // for debugging
//----------------------- Incializa MicroSD -------------------------
    Serial.println("Initializing SD card...");
    if (!SD.begin(4))
    {   Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    }
    Serial.println("SUCCESS - SD card initialized.");
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    }
    Serial.println("SUCCESS - Found index.htm file.");
    
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
}
//-------------------------------  INICIA LOOP -------------------------
void loop()
{  EthernetClient client = server.available();  
if (client) {                           // Si tenemos cliente
boolean currentLineIsBlank = true;      
while (client.connected()) {
if (client.available())   {  // Hay algo pendiente de leer
char c = client.read();
 //Serial.println(c);
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c;          // Montar la peticion HTTP
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");                    
if (StrContains(HTTP_req, "ajax_inputs")) {
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
XML_response(client);
 }
 else if (StrContains(HTTP_req, "GET / ")
|| StrContains(HTTP_req, "GET /index.htm")) {                                  
client.println("Content-Type: text/html");  //  image/jpg   or    text/html
client.println("Connection: keep-alive");  //Connection: close OR keep-alive
client.println();
webFile = SD.open("index.htm");        // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
 }                  
Serial.print(HTTP_req);
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                     currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    currentLineIsBlank = false;
                }  
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
        delay(2);                                  
    } // end if (client)
    

}


      

void XML_response(EthernetClient cl)
{  
    int analog_val;
    cl.print("<?xml version = \"1.0\" ?>");
    cl.print("<inputs>");
    analog_val = capacidad();
    cl.print("<analog>");
    cl.print(analog_val);
    cl.print("</analog>");
    analog_val = capacidad2();
    cl.print("<analog>");
    cl.print(analog_val);
    cl.print("</analog>");
    cl.print("</inputs>");
  

}



void StrClear(char *str, char length)
{
    for (int i = 0; i < length; i++) {
        str[i] = 0;
    }
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
    char found = 0;
    char index = 0;
    char len;
    len = strlen(str);    
    if (strlen(sfind) > len) {
        return 0;
    }
    while (index < len) {
        if (str[index] == sfind[found]) {
            found++;
            if (strlen(sfind) == found) {
                return 1;
            }
        }
        else {
            found = 0;
        }
        index++;
    }
    return 0;
}


 
      float capacidad()
      {
       NewPing ultradere(enviasonicodere, recibesonicodere, 300);//(Trig8,Echo9)
      
        delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = ultradere.ping(); // Send ping, get ping time in microseconds (uS).
 /* Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
  */
       float volumen=0, volumen1=0, volumen2=0, H=0, Hh=0;
       double x=0, L=152.5;
       int R=38, sen=0, nivelb=86;

       int boton=13; //BOTON PARA MOSTRAR VOLUMEN
       int state=0; //ESTADO DEL BOTON
       sen=((-R+H)/R);
      // long microsec=ultradere.Timing();
     //  H = ultradere.Ranging(CM);
     H=(uS / US_ROUNDTRIP_CM);
       Hh=H-10;
       volumen=(L*(((3.1415*(R*R))/2)+((Hh-R)*sqrt(2*R*Hh-(Hh*Hh)))+(R*R)*asin((-R+Hh)/R)));
       volumen1=volumen/1000;
       volumen2=700-volumen1;
      
       if (Hh <= nivelb)
       {
        volumen=(L*(((3.1415*(R*R))/2)+((Hh-R)*sqrt(2*R*Hh-(Hh*Hh)))+(R*R)*asin((-R+Hh)/R)));
        volumen1=volumen/1000;
        volumen2=700-volumen1;
       }
       else if (Hh > nivelb){ volumen2 = 0; }
  
       if (Hh < 0 ){ volumen2 = 700; }
      
       return volumen2;
       }

      
      
       float capacidad2()
       {
         NewPing ultraizq(enviasonicoizq, recibesonicoizq, 300);//(Trig8,Echo9)
      
        delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS1 = ultraizq.ping(); // Send ping, get ping time in microseconds (uS).
  /*
  Serial.print("Ping: ");
  Serial.print(uS1 / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
*/
       float volumen2=0, volumen12=0, volumen22=0, H2=0, Hh2=0;
       double L2=153;
       int R2=46, sen2=0, nivelb2=102;
       //
       //
       sen2=((-R2+H2)/R2);
      // long microsec=ultraizq.Timing();
      // H2 = ultraizq.Ranging(CM);
       H2 = (uS1 / US_ROUNDTRIP_CM);
       Hh2 = H2-10;
       volumen2 = (L2*(((3.1415*(R2*R2))/2)+((Hh2-R2)*sqrt(2*R2*Hh2-(Hh2*Hh2)))+(R2*R2)*asin((-R2+Hh2)/R2)));
       volumen12 = volumen2/1000;
       volumen22 = 1000-volumen12;
       //
              ///
       if (Hh2 <= nivelb2)
       {
        volumen2=(L2*(((3.1415*(R2*R2))/2)+((Hh2-R2)*sqrt(2*R2*Hh2-(Hh2*Hh2)))+(R2*R2)*asin((-R2+Hh2)/R2)));
        volumen12=volumen2/1000;
        volumen22=1000-volumen12;
       }
       else if (Hh2 > nivelb2){ volumen22 = 0; }
  
       if (Hh2 < 0 ){ volumen22 = 1000; }
      
       return volumen22;
      
       }

Thank you, The problem was solved! :smiley:

The XML File is attached.

No, it wasn't.

Dont' understand how to Use Tools + Auto Format to properly indent your code... could you explain it to me please?

There is a Tools menu on the IDE. One of the options on that menu is Auto Format.

Thank you, The problem was solved!

How?

No,it wasn't

It was attached, sorry dude but because I'm new here I don't know it works, but I did attach the file

How?

We modified the code, there were some variables that was called in the same named, but for different purposes, just we needed to look carefully the code.

Thanks for answering !

It was attached, sorry dude but because I'm new here I don't know it works, but I did attach the file

There was a file attached. It was a jpg file, not an xml file.

just we needed to look carefully the code.

Good idea. 8)