arduino find text in webpage

2.6k views Asked by At

I am trying to make an arduino weather alert, from this website and I need to find the contents of the quotation marks in "weather" after "main". I have looked for a text finder, but the one on the arduino website but it's so undescriptive that it might as well not exist.

Currently my code looks like this:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "api.openweathermap.org";  

IPAddress ip(192,168,1,77);

EthernetClient client;

void setup() {
  Serial.begin(9600);
   while (!Serial) {
    ;
  }

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
  delay(1000);

  if (client.connect(server, 80)) {
    client.println("GET /data/2.5/weather?q=Melbourne,au HTTP/1.1");
    client.println("Host: api.openweathermap.org");
    client.println("Connection: close");
    client.println();
  } 
  else {
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    client.stop();

    while(true);
  }
}

and it returns:

HTTP/1.1 200 OK
Server: nginx
Date: Sun, 07 Jun 2015 21:17:44 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
X-Source: redis
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST

1d2
{"coord":{"lon":144.96,"lat":-37.81},"sys":{"message":0.0117,"country":"AU","sunrise":1433626204,"sunset":1433660872},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}],"base":"stations","main":{"temp":284.532,"temp_min":284.532,"temp_max":284.532,"pressure":1011.27,"sea_level":1030.68,"grnd_level":1011.27,"humidity":80},"wind":{"speed":4.43,"deg":331.501},"clouds":{"all":0},"dt":1433711139,"id":2158177,"name":"Melbourne","cod":200}

0

for this output I want it to say: Clear

1

There are 1 answers

7
Olumide On BEST ANSWER

A very easy way to get the data you want is with TextFinder from the Arduino playground. I believe this should work. Note that I haven't tested it, so fingers crossed.

Add this at the begining

#include <TextFinder.h>
#include <String.h>
String result = "";
TextFinder finder( client );

Then modify your loop() to this

if (client.connected()){
  finder.find("\"weather\"");
  finder.find("\"main\":\"");
  char c = client.read();
  while(c != '\"'){
    result += c;
    c = client.read();
  }
  Serial.print(result);
}
if (!client.connected()) {
  Serial.println();
  client.stop();
  while(true);
}

More info below if you're interested.

The response you're getting is json, to properly interpret it you need a json parser. I found a library on github: ArduinoJson that parses json on the arduino. It seems quite easy to use too. If you need help with it, you can browse it's wiki

Before you start parson the json, you need to first strip the header from the response. To do that you could keep reading the characters until you get a double CRLF (\r\n\r\n)