Repeating XML code

Hello:

So, I’ve finally managed to get this server thing going, and all the functions working. What I’m failing to do is to get the call to trigger the Arduino.

Basically right now Arduino just spits the same 10 lines of XML over and over again, which messes up the whole thing because then the header gets repeated so line 11 is “Junk”.

How would I make the code wait till it gets the call? Basically it would have to do 1 set of the 10 lines of code and then wait till its asked to do it again.

Currently it looks like this:

Client 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 (c == '\n' && current_line_is_blank) {
          client.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
          client.println();
          client.println("<node id=\"1\">");
          client.print("<sensor id=\"0\">");  //Sensor 0
          client.print(SensorValue[0]);
          client.println("</sensor>");
          client.print("<sensor id=\"1\">");  //Sensor 1
          client.print(SensorValue[1]);
          client.println("</sensor>");
          client.print("<sensor id=\"2\">");  //Sensor 2
          client.print(SensorValue[2]);
          client.println("</sensor>");
          client.print("<sensor id=\"3\">");  //Sensor 3
          client.print(SensorValue[3]);
          client.println("</sensor>");
          client.print("<sensor id=\"4\">");  //Sensor 4
          client.print(SensorValue[4]);
          client.println("</sensor>");
          client.print("<sensor id=\"5\">");  //Sensor 5
          client.print(SensorValue[5]);
          client.println("</sensor>");
          client.println("</node>");
        }
        }
        break;
      }
    }
      delay(1);
     
  }

Thanks!!!

By the way, what version of avr-gcc compiler you are using?

That code snippet does not tell the whole story. Post ALL of your code, if you expect help.

VilluV:
Release 21 I believe.

PaulS:
Sorry about that, I sent some to my software developer and that’s all I had on my clipboard, then my internet died :frowning:

#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192, 168, 0, 101};

Server server(80);

int Sensor[] = {0,1,2,3,4,5}; //Array for 6 analog inputs
int SensorValue[] = {0,0,0,0,0,0};  //Set all SensorValue at 0
int count = 6; //Sensor array total

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

void loop()
  {
  //Read analog sensor:
  for(int i=0; i<count; i++)
    {
   SensorValue[i] = analogRead(Sensor[i]);
    } 
  
  Client 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 (c == '\n' && current_line_is_blank) {
          client.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
          client.println();
          client.println("<node id=\"1\">");
          client.print("<sensor id=\"0\">");  //Sensor 0
          client.print(SensorValue[0]);
          client.println("</sensor>");
          client.print("<sensor id=\"1\">");  //Sensor 1
          client.print(SensorValue[1]);
          client.println("</sensor>");
          client.print("<sensor id=\"2\">");  //Sensor 2
          client.print(SensorValue[2]);
          client.println("</sensor>");
          client.print("<sensor id=\"3\">");  //Sensor 3
          client.print(SensorValue[3]);
          client.println("</sensor>");
          client.print("<sensor id=\"4\">");  //Sensor 4
          client.print(SensorValue[4]);
          client.println("</sensor>");
          client.print("<sensor id=\"5\">");  //Sensor 5
          client.print(SensorValue[5]);
          client.println("</sensor>");
          client.println("</node>");
        }
        }
        break;
      }
    }
      delay(1);
     
  }

I guess (and this is a very broad guess), that what I would need is to have the loop only working in the AnalogRead sensor, so the Arduino is refreshing the sensor reading, but to only send the XML package when called by the client.

I tried moving the break and delay up to the SensorValue definition, but I’m guessing something in the Server functions is making it give me an error. Unfortunately I’m not sure exactly what.

Am I on the right track?

Thanks! :smiley:

What is this for?

    boolean current_line_is_blank = true;

You never change it's value, so it is always true. Testing something that can never be anything but true seems pointless.

Where do you tell the client to go away? If you only want to supply one response per request, then, after you've served the current customer, you have to tell that customer to go away, and call on the next one.

It seems like your while(client.connected()) should be an if(client.connected()).

What is the purpose of the delay()? Do you really want to introduce an unnecessary delay into an already slow process?

How would you feel if the forum software added unnecessary delays? Or the compiler?

Actually both the delay and the boolean function are remnants from code I borrowed to do a server client. I've been very confused about the boolean function myself.

How do I tell the client to go away? I figured the "stop" function would do it, but that isn't listed under server, just client.

Thanks for the help, I appreciate the insight!

How do I tell the client to go away?

client.stop();

should do it.

Oh hey look at that…it works!

#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192, 168, 0, 101};

Server server(80);

int Sensor[] = {0,1,2,3,4,5}; //Array for 6 analog inputs
int SensorValue[] = {0,0,0,0,0,0};  //Set all SensorValue at 0
int count = 6; //Sensor array total

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

void loop()
  {
  //Read analog sensor:
  for(int i=0; i<count; i++)
    {
   SensorValue[i] = analogRead(Sensor[i]);
    } 
  
  Client client = server.available();
  if (client) {
    if (client.connected()) {
      if (client.available()) {
        char c = client.read();
          client.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
          client.println();
          client.println("<node id=\"1\" max=\"1025\">");
          client.print("<sensor id =\"0\" ts=\"#timestamp#\">");  //Sensor 0
          client.print(SensorValue[0]);
          client.println("</sensor>");
          client.print("<sensor id=\"1\" ts=\"#timestamp#\">");  //Sensor 1
          client.print(SensorValue[1]);
          client.println("</sensor>");
          client.print("<sensor id=\"2\" ts=\"#timestamp#\">");  //Sensor 2
          client.print(SensorValue[2]);
          client.println("</sensor>");
          client.print("<sensor id=\"3\" ts=\"#timestamp#\">");  //Sensor 3
          client.print(SensorValue[3]);
          client.println("</sensor>");
          client.print("<sensor id=\"4\" ts=\"#timestamp#\">");  //Sensor 4
          client.print(SensorValue[4]);
          client.println("</sensor>");
          client.print("<sensor id=\"5\" ts=\"#timestamp#\">");  //Sensor 5
          client.print(SensorValue[5]);
          client.println("</sensor>");
          client.println("</node>");
          client.stop();
        }
      }
    }
  }

I was convinced that Server and Client were 2 different ways of using the ethernet library/shield…so I thought the client.stop wouldn’t work with the server type. Goes to show you hwo much I know hahaha

Thanks for all the help! ;D

I was convinced that Server and Client were 2 different ways of using the ethernet library/shield...so I thought the client.stop wouldn't work with the server type. Goes to show you hwo much I know hahaha

They are actually two different setups. You are using the server code to communicate with a client. There is also client code to communicate with a server.

The part

          client.print("<sensor id=\"0\">");  //Sensor 0
          client.print(SensorValue[0]);
          client.println("</sensor>");
          client.print("<sensor id=\"1\">");  //Sensor 1
          client.print(SensorValue[1]);
          client.println("</sensor>");
          client.print("<sensor id=\"2\">");  //Sensor 2
          client.print(SensorValue[2]);
          client.println("</sensor>");
          client.print("<sensor id=\"3\">");  //Sensor 3
          client.print(SensorValue[3]);
          client.println("</sensor>");
          client.print("<sensor id=\"4\">");  //Sensor 4
          client.print(SensorValue[4]);
          client.println("</sensor>");
          client.print("<sensor id=\"5\">");  //Sensor 5
          client.print(SensorValue[5]);

Could be done in a for loop, something like (not tested or compiled)

  for (int i=0; i<6; i++)
  {
    String s = "<sensor id=\"" + i;
    s += "\">" + SensorValue[i];
    s += "</sensor>";
    Serial.println(s);
  }

Oh I havent started playing with strings yet...I figured there was a way of doing it, but I wasn't sure how!

I'll give it a go, thanks :)

Alright, so, new problem...and one that I dont understand unless its coming from a physical point of view, but...all the XML threads are giving me values very close to each other....even if I only have 1 connected to anything.

So, for example I plug the 3.3V output of Arduino into A4, and it gives me a value of around 670, but then the other threads also spit out values of around the same quantity. Can this be because of it not being properly grounded, or is there something in my code doing this? I can't spot anything.

Readings from analog pins are only going to mean something if something is plugged in. You should not expect 0 just because there is nothing connected.

The reason behind this has to do with HOW an analog to digital converter works. There is a capacitor that is charged by whatever is connected to the analog pin. The ADC times how long it takes to charge the capacitor, and returns a value that is a function of this time.

If there is nothing connected, it will take forever to charge the capacitor. one over forever is 0, so when the function to compute the return value divides by 0, the result is undefined.

I understand that, I just find it odd that all the values give me the same result, or around the same result.

If I plug a 3V signal into A3, I expect that one to return a value, say 700, but the others to be random. Instead I find that they ALL give me 700, some will give me 680, some 710....you get the gist.

Maybe it is because they are are disconnected and are getting leakage. I'll give that a shot too!

Maybe it is because they are are disconnected and are getting leakage.

No, it's because there is ONE ADC on the Arduino. It is multiplexed to all the analog pins.

When one pin does not produce a valid reading, you often get the value left from the previous valid reading. Or slightly less, as the capacitor has discharged a bit.

Try connecting 2 different valid signals, at different levels, and reading all 6 pins. Do you then see a pattern?