Arduino TextFinder

Hi!
I try to make the code, which will read the data on the server and write to LCD.
There's something wrong with the code.
Code running in loop about 10 times, then all stops.

In serialmonitor all freezes, the values on lcd are no longer refreshed.
Strangely for me is, when I close the Serialmonitor and re-open, the code starts to run again. :o
Can you help me ?

CODE:

#include <SPI.h>         
#include <Ethernet.h>
#include <TextFinder.h>
#include <LiquidCrystal_I2C.h>
byte mac[] = { 0x84, 0x42, 0x8B, 0xBA, 0xB2, 0x31 }; //mac address of ethernet shield
IPAddress server(192, 168, 5, 91);
IPAddress ip(192, 168, 5, 98); 
IPAddress gateway(192, 168, 5, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;
TextFinder  finder( client );  
LiquidCrystal_I2C lcd1(0x3B, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup()
{
  Serial.begin(115200);
  Ethernet.begin(mac, ip);
  lcd1.begin(20, 4);
  Serial.println("connecting...");
  delay(10000);
}


void loop(){
    
    if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("/search?q=arduino HTTP/1.1");
    client.println("Host: 192.168.5.91");
    client.println("Connection: close");
    client.println();
    } 
    else {
    Serial.println("connection failed"); 
    }
    while(client.connected() && !client.available()) delay(3000); //waits for data
    
    finder.find("TEMP1:");
    float value1 = finder.getFloat("."); 
    Serial.println(value1);
    Serial.print('\n');

    finder.find("TEMP2:");
    float value2 = finder.getFloat("."); 
    Serial.println(value2);
    Serial.print('\n');
    
    finder.find("TEMP3:");
    float value3 = finder.getFloat("."); 
    Serial.println(value3);
    Serial.print('\n');
  
    lcd1.setCursor(0, 0);
    lcd1.print("TEMP1:");
    lcd1.setCursor(14, 0 );
    lcd1.print(value1,2);//

    lcd1.setCursor(0, 1);
    lcd1.print("TEMP2:");
    lcd1.setCursor(14, 1);
    lcd1.print(value2,2);//

    lcd1.setCursor(0, 2);
    lcd1.print("TEMP3:");
    lcd1.setCursor(14, 2);
    lcd1.print(value3,2);// 
    
    client.stop();
    client.flush(); 
}

I have never heard of the Textfinder library. I wonder if it uses the String class?

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Strangely for me is, when I close the Serialmonitor and re-open, the code starts to run again.

That's because closing and reopening the serial monitor reset your arduino - so it's not strange

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

When you are lucky there is data and you don't wait and it's all good but when you are unlucky you might end up stuck for 3s in this loop for a while and data accumulates and may be overflows... read more info on usage and look at the examples, the wait is not needed

(Writing your own function to read until a string is not rocket science I posted one not too long ago cf post #28)

Robin2:
I have never heard of the Textfinder library. I wonder if it uses the String class?

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

you mean it :

(char)'\0';

Where, at the end of loop ?

J-M-L:
That's because closing and reopening the serial monitor reset your arduino - so it's not strange

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

When you are lucky there is data and you don't wait and it's all good but when you are unlucky you might end up stuck for 3s in this loop for a while and data accumulates and may be overflows... read more info on usage and look at the examples, the wait is not needed

(Writing your own function to read until a string is not rocket science I posted one not too long ago cf post #28)

Delete this line ?

svedr:
Where, at the end of loop ?

Alas it is not nearly so simple in your case. You need to find out how the Textfinder library works by looking at its documentation, or maybe at its source code. I have no idea what is inside it. It is entirely possible that it already uses cstrings.

Maybe it would be easier to do what you want without using that library,

Debugging intermittent problems can be very tricky. Oftentimes the problem is not where you first think it is.

...R

You're proposing another code ?
I just want to read float value form server, and write to serial or LCD.....

Again have a look at the example

they do this - no waiting with a clumsy while

  if (client.connected())
  {
    finder.find("Results <b>");  // seek to the Results field
    finder.find("of about <b>"); // skip past this 
    long value = finder.getValue(); // get numeric value
    Serial.print(value);
    Serial.println(" hits");
  }

But would suggest to write your own function - this way you are in full control

svedr:
You're proposing another code ?
I just want to read float value form server, and write to serial or LCD.....

I don't have your equipment so I cannot replicate your problem. I can't think of any other suggestions.

...R

Now i little change my code, but stil stop working after aprox. 50 cycle ....

Code;

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

#include <LiquidCrystal_I2C.h>
byte mac[] = { 0x84, 0x42, 0x8B, 0xBA, 0xB2, 0x31 }; //mac address of ethernet shield
IPAddress server(192, 168, 5, 91);
IPAddress ip(192, 168, 5, 98); 
IPAddress gateway(192, 168, 5, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;
TextFinder  finder( client );  
LiquidCrystal_I2C lcd1(0x3B, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

float value1;
float value2; 
float value3; 
int counter = 0; 
void setup()
{
  Serial.begin(115200);
  Ethernet.begin(mac, ip);
  lcd1.begin(20, 4);
  Serial.println("connecting...");
  //delay(10000);
}


void loop(){
    
    {
  if (client.connect(server, 80)) {
    Serial.print("connected... ");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
  if (client.connected()) {
    value1 = 0; 
    value2 = 0; 
    value3 = 0; 
    
      if(finder.find("T1:")){     
         value1 = finder.getFloat(".");
         Serial.println();
         Serial.println("T1:");      
         Serial.print(value1);
         Serial.print(" C"); 
          lcd1.setCursor(0, 0);
          lcd1.print("T1:");
          lcd1.setCursor(14, 0 );
          lcd1.print(value1,2);//
         }       
         
       if(finder.find("T2:")){     
         value2 = finder.getFloat(".");
         Serial.println();
         Serial.println("T2:");      
         Serial.print(value2);
         Serial.print(" C");
         lcd1.setCursor(0, 1);
         lcd1.print("T2:");
         lcd1.setCursor(14, 1);
         lcd1.print(value2,2);//   
         }    
          
       if(finder.find("T3:")){     
         value3 = finder.getFloat(".");
         Serial.println();
         Serial.println("T3:");      
         Serial.print(value3);
         Serial.print(" C");  
         lcd1.setCursor(0, 2);
         lcd1.print("Garaza:");
         lcd1.setCursor(14, 2);
         lcd1.print(value3,2);//        
         }
    counter = counter + 1;
    Serial.println();
    Serial.println("Counter of readings:");
    Serial.print(counter);
    Serial.println();
    }
    
    delay(2000); // check again in 10 seconds
    value1 = 0; 
    value2 = 0; 
    value3 = 0; 
  }
 
  client.stop();
  
}

what does the server response look like when you do a get request from using cURL?

I don't understand why you are sending the example search string here:

client.println("GET /search?q=arduino HTTP/1.0");

for your private server:

IPAddress server(192, 168, 5, 91);

I do not even know why... :roll_eyes:
I change this way ?

client.println("GET 192.168.5.91 HTTP/1.1");

or

client.println("GET HTTP/1.1");
    client.println("Host: 192.168.5.91");

I change this way ?

No. A GET request defines a script to execute, and any data to send to it, between the GET and the HTTP. At a minimum, you need a / there.

   GET / HTTP/1.1
   Host: 192.168.5.91

The host information is NOT part of the GET statement.

now i change :

client.println("GET / HTTP/1.1");
    client.println("Host: 192.168.5.91");

but stop after 45-50 circles.....

Now i try this code.....

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

#include <LiquidCrystal_I2C.h>
byte mac[] = { 0x84, 0x42, 0x8B, 0xBA, 0xB2, 0x31 }; //mac address of ethernet shield
IPAddress server(192, 168, 5, 91);
IPAddress ip(192, 168, 5, 98); 
IPAddress gateway(192, 168, 5, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;
TextFinder  finder( client );  
LiquidCrystal_I2C lcd1(0x3B, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

float value1;
float value2; 
float value3; 
int counter = 0; 

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers

void setup()
{
  Serial.begin(115200);
  Ethernet.begin(mac, ip);
  lcd1.begin(20, 4);
  Serial.println("connecting...");
  //delay(10000);
}


void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP GET request:
    client.println("GET / HTTP/1.1");
    client.println("Host: 192.168.5.91");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }


  
  if (client.connected()) {
    value1 = 0; 
    value2 = 0; 
    value3 = 0; 
    
      if(finder.find("t1:")){     
         value1 = finder.getFloat(".");
         Serial.println();
         Serial.println("t1:");      
         Serial.print(value1);
         Serial.print(" C"); 
          lcd1.setCursor(0, 0);
          lcd1.print("t1:");
          lcd1.setCursor(14, 0 );
          lcd1.print(value1,2);//
         }       
         
       if(finder.find("t2:")){     
         value2 = finder.getFloat(".");
         Serial.println();
         Serial.println("t2:");      
         Serial.print(value2);
         Serial.print(" C");
         lcd1.setCursor(0, 1);
         lcd1.print("t2:");
         lcd1.setCursor(14, 1);
         lcd1.print(value2,2);//   
         }    
          
       if(finder.find("t3:")){     
         value3 = finder.getFloat(".");
         Serial.println();
         Serial.println("t3:");      
         Serial.print(value3);
         Serial.print(" C");  
         lcd1.setCursor(0, 2);
         lcd1.print("t3:");
         lcd1.setCursor(14, 2);
         lcd1.print(value3,2);//        
         }
    counter = counter + 1;
    Serial.println();
    Serial.println("Counter of readings:");
    Serial.print(counter);
    Serial.println();
    }
    
    delay(2000); // check again in 10 seconds
    value1 = 0; 
    value2 = 0; 
    value3 = 0; 
  }

This code run 280 circles and then stop to...
What should I do to work the code?

Serial.println("connecting...");(and the rest) needs more F() macro, but it sounds like that's only going to give you a few more cycles.

Solved the problem. A stupid mistake...
I forgoooooooot to change ....... :-[ :-[ :-[ ...
...MAC adress, that was the same on server and on client :zipper_mouth_face: :zipper_mouth_face:

It's been working since yesterday :smiley:
I apologize to anyone who want to help me.....