Reading data from txt file over Eth

Hello everyone,

For my project i’m trying to read data in a text file over on my server.
I have a web interface to update that file.

The whole code seems to work except from the part when I red in the file. I always get 1 even if I put 0.

My .txt looks like this <info>110000</info> where each number indicates if an LED is on or off.

Here is the arduino code:

#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(168,254,36,73);
// initialize the library instance:
EthernetClient client;
const unsigned long requestInterval = 1000;  // delay between requests
char serverName[] = "4ltamarii.fr";
boolean requested;                   // whether you've made a request since connecting
unsigned long lastAttemptTime = 0;   // last time you connected to the server, in milliseconds

String currentLine = "";            // string to hold the text from server
String message = "";                  // string to hold the message
boolean lecture = false;       // if you're currently reading the message
int myLed[6]={0, 0, 0, 0, 0, 0};
int cmpt=0;
int DecValWeb=0;

/** For Lights **/
int outPuts[6]={2, 3, 4, 5, 6, 7};

void setup() {
  // reserve space for the strings:
  currentLine.reserve(256);
  message.reserve(150);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // attempt a DHCP connection:
  Serial.println("Attempting to get an IP address using DHCP:");
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Serial.println("Failed to get an IP address using DHCP, trying manually");
    Ethernet.begin(mac, ip);
  }
  Serial.print("Mon adresse:");
  Serial.println(Ethernet.localIP());
  // premiere connection :
  for(int i=0;i<6;i++)
  {
    pinMode(outPuts[i], OUTPUT); // Set the mode to OUTPUT
    digitalWrite(outPuts[i], LOW);
  }
  connectToServer();

}

void loop(){
  if(message.length() > 0 && lecture==false){
    Serial.print("je viens de recevoir un message : ");
    Serial.println(message);
    message=""; 
  }

  if (client.connected()) {
    checke_page(); 

  }
  else if (millis() - lastAttemptTime > requestInterval) {
    // if you're not connected, and two minutes have passed since
    // your last connection, then attempt to connect again:
    connectToServer();
  } 
}

void checke_page(){
  if (client.available()) {
    // read incoming bytes:
    char inChar = client.read();
    // add incoming byte to end of line:
    currentLine += inChar; 

    // si on a un retour chariot, c'est que ce n'est pas encore le debut, on supprime:
    if (inChar == '\n') {
      currentLine = "";
    } 

    // si on trouve le message <text>, c'est 
    // que le message suit:
    if ( currentLine.endsWith("<info>")) {

      // debut du message, on vide la string message:
      lecture = true; 
      message = "";
      inChar = client.read(); // lire le caractere suivant
    }
    // on lit caractere par caractere,
    // et les ajoute au message
    if (lecture) {
      if (inChar != '<') {
        message += inChar;
        if(inChar=='1') myLed[cmpt]=1;
        if(inChar=='0') myLed[cmpt]=0;
        cmpt++;
        if(cmpt==6) cmpt=0;
      } 
      else {
        // isi vous avez un "<",
        // c'est la fin du message:
        lecture = false;  
        // fermer la connexion au serveur:
        client.stop();
        Serial.print("Array:");

        for(int i=0;i<6;i++)
        {
          Serial.print(myLed[i]);
        }
         DecValWeb=Cvg_tab_dec();
         creat_webmask();
      }
    }
  }
}

void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    Serial.println("making HTTP request...");
    // make HTTP GET request:
    client.println("GET /stage/save.txt HTTP/1.1");
    client.println("Host: 4ltamarii.fr");
    client.println();
  }
  // remettre le compteur a zero pour la prochaine connexion:
  lastAttemptTime = millis();
}  


void creat_webmask()
{
  Turn_Lights();
}

void Turn_Lights()
{
  for(int i=0;i<6;i++)
  {
    int temp = bitRead(DecValWeb,i);

    // Serial.println(temp);
    if(temp==1 && i==0)  digitalWrite(outPuts[i], HIGH);
    if(temp==0 && i==0)  digitalWrite(outPuts[i], LOW);
    if(temp==1 && i==1)  digitalWrite(outPuts[i], HIGH);
    if(temp==0 && i==1)  digitalWrite(outPuts[i], LOW);
    if(temp==1 && i==2)  digitalWrite(outPuts[i], HIGH);
    if(temp==0 && i==2)  digitalWrite(outPuts[i], LOW);
    if(temp==1 && i==3)  digitalWrite(outPuts[i], HIGH);
    if(temp==0 && i==3)  digitalWrite(outPuts[i], LOW);
    if(temp==1 && i==4)  digitalWrite(outPuts[i], HIGH);
    if(temp==0 && i==4)  digitalWrite(outPuts[i], LOW);
    if(temp==1 && i==5)  digitalWrite(outPuts[i], HIGH);
    if(temp==0 && i==5)  digitalWrite(outPuts[i], LOW);
  }
}

int Cvg_tab_dec()
{
  int temp=0;
  int comp=0;
  do{
    if (myLed[comp] == 1)
      temp = temp * 2 + 1;
    else if (myLed[comp] == 0)
        temp *= 2;
    comp++;
  }
  while (comp !=6);
  Serial.print("dec=");
  Serial.print(temp);
  return temp;
}

If you could help me it would be great.

Bye Thank you

CODE tags, not QUOTE tags. You did it correct the first time...

I have the felling it works, but gets the old values. How old i can’t tell you.

I’ ve changed my code to get the string. I get the message between ‘<’ and ‘>’.
And my output is info because i used this tag earlier.

Is this possible?

Regards

I don’t understand what you are doing. When the client connects to the server, and the GET resutls in a response that contains “”, you read data that may not yet have arrived. Assuming that that never results in an error, you keep reading data from the client.

I fail to see what that has to do with reading from a text file.

If the server has problems reading from the text file, that’s not something that the Arduino forum is going to help you with. The Arduino is NOT the server.

The index into the myLed array needs to be set to 0 when the string is detected.

When the < that precedes the tag is detected, you should not call client.stop(). You should continue reading and discarding the data until the whole response has been read. Otherwise, since there is still unread data, the socket that was in use will become unavailable.

I have the felling it works

You have the feeling that what works?

but gets the old values.

If the server is sending old data, there is not a damned thing the client can do about it. You need to fix the server issue.

How old i can’t tell you.

You need to change the server end, then, to timestamp the data.