Post topics, source code that relate to the Arduino Platform

User avatar
By sarunas.z
#76642 Hello,
I have problem. I tryed to read my web page information. I am using this code
Code: Select all#include <ESP8266WiFi.h>
const char* ssid = "Signalizacija";
const char* password =  "20170214";
const char* host = "ortex.lt";
String line;

void setup()
{
  Serial.begin(115200);
  Serial.println();

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");
}


void loop()
{
  WiFiClient client;
  Serial.printf("\n[Connecting to %s ... ", host);
  if (client.connect(host, 80))
  {
    Serial.println("connected]");

    Serial.println("[Sending a request]");
    String url = "/testas.php";
    client.print(String("GET /") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
               "Connection: close\r\n" +
                "\r\n"
                );

    Serial.println("[Response:]");
    while (client.connected())
   {   
    line = client.readStringUntil('\r');
    Serial.print(line);
   }
   
    client.stop();
    Serial.println("\n[Disconnected]");
  }
  else
  {
    Serial.println("connection failed!]");
    client.stop();
  } 
  delay(5000);
}


I got this information, but I want to read only word which I underline red color "saule". How need separate this word from total html code? Could somebody give to my advice how to do it?
Image
User avatar
By Pablo2048
#76654 Hi,
you have to do 2 steps:
1. read and parse HTTP protocol - you can use HTTPClient class for this job
2. then you have to parse HTML stream - there is no library I'm aware of so write simple streaming HTML parser class
and you are done... ;-)
User avatar
By QuickFix
#76663
sarunas.z wrote:I tryed to read my web page information.

If it's indeed your own website, don't serve an HTML-page, but just a simple one with only the content you require (or, if you need more flexibility, output as JSON, XML or something similar to that).
User avatar
By torntrousers
#76664 A little hacky, but something like this:
Code: Select allwhile(client.available()){
    if (client.read() == '<') {   
      if (client.read() == 'b') {   
        if (client.read() == 'o') {   
          if (client.read() == 'd') {   
            if (client.read() == 'y') {   
              if (client.read() == '>') {   
                String theData = client.readStringUntil('<');
                client.stop();
                return theData;
              }
            }
          }
        }
      }
    }
  }
}