Go Down

Topic: Arduino using data from webpage  (Read 488 times) previous topic - next topic

tommyk021

Folks, I want to use my arduino with ethernet shield to access the following webpage "http://www.met.ie/forecasts/sea-area.asp" and take some information.

On the website it has a line "Small Craft Warning"- This is always either "In Operation" or "Nil".

Basically i want to hook up a RGB LED to the duino and if "Small Craft Warning" is "in Operation" i want LED to be Red, and if "Nil" LED can be green.

My issue is i cant figure out hot to go about getting the information from the webpage.- Can the arduino read this directly?

I am confident with arduino but this is my first web based project!

Any Tips or advice would be savage helpful Thanks

PaulRB

#1
Aug 27, 2016, 03:05 pm Last Edit: Aug 27, 2016, 04:43 pm by PaulRB
Should be pretty straight forward. The sketch needs to request the page, and then read the response line-by-line.

When it finds a line containing "Small Craft Warning:" it then needs to check for the string "In Operation" or "Nil" on that line and light the red or green light accordingly.

I don't have an ethernet shield, so I can't suggest a sketch, but I do have ESP-based arduino, which is a WiFi board. The sketch will be 95% the same for an arduino with an ethernet shield I would imagine.

Code: [Select]
#include <ESP8266WiFi.h>

WiFiClient client;

#define RED_LED D2
#define GRN_LED D1

void setup() {

  pinMode(RED_LED, OUTPUT);
  pinMode(GRN_LED, OUTPUT);

  Serial.begin(115200);
  Serial.println();
  Serial.print("Connecting to Wifi");

  WiFi.mode(WIFI_STA); // Station Mode
  WiFi.begin("SSID", "password");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.print("OK\nWiFi connected, IP address: ");
  Serial.println(WiFi.localIP());

}

void loop() {

  if (!client.connect("www.met.ie", 80)) {
    Serial.println("Connection to host failed");
  }
  else {

    Serial.println("Reading page");

    client.println("GET /forecasts/sea-area.asp HTTP/1.1");
    client.println("Host: www.met.ie");
    client.println("Connection: close");
    client.println();

    while (client.connected()) {

      String line = client.readStringUntil('\n');

      if (line.indexOf("Small Craft Warning:") >= 0) {

        Serial.println(line);

        digitalWrite(RED_LED, LOW);
        digitalWrite(GRN_LED, LOW);
        if (line.indexOf("In Operation") >= 0) {
          digitalWrite(RED_LED, HIGH);
        }
        else if (line.indexOf("Nil") >= 0) {
          digitalWrite(GRN_LED, HIGH);
        }
      }
    }

    Serial.println("Connection closed");
    client.stop();

  }

  delay(60000);
}


tommyk021

Thanks Paul, ill try t out.

This lines is catching me out in my head.

   if (line.indexOf("Small Craft Warning:") >= 0) {              What is this line  doing?

        Serial.println(line);

        digitalWrite(RED_LED, LOW);
        digitalWrite(GRN_LED, LOW);
        if (line.indexOf("In Operation") >= 0) {
          digitalWrite(RED_LED, HIGH);
        }
        else if (line.indexOf("Nil") >= 0) {
          digitalWrite(GRN_LED, HIGH);
        }
      }
    }
 

PaulRB

"line" is a String object variable which holds the line of text that was most recently read from the web page. String objects have a method called IndexOf() which searches for a given piece of text within the text it holds. if it finds the given text, it returns its offset from the left. If it cannot find it, it returns -1.

xl97

Some alternate methods (not better or worse, just different)

* Use a .php script housed somewhere to 'scrape' the page for you and do the parsing.

* See if the site in question has an API or webservice to 'call' and get this info returned.

Go Up