checks http://wedgefest.wind.ttu.edu/obs/TTU_LBBW.html every 5 minutes and records the temperature and humidity values located in the webpage's text

Dependencies:   EthernetNetIf mbed

getweather.cpp

Committer:
elliotb
Date:
2010-08-09
Revision:
0:f46e4f26425a
Child:
1:35888a9f0a48

File content as of revision 0:f46e4f26425a:

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"

EthernetNetIf eth;
HTTPClient http;

HTTPResult result;
bool completed = false;
void request_callback(HTTPResult r) {
    result = r;
    completed = true;
}

void checkweather(HTTPStream &stream);

char * location;
char temperature[3];
char humidity[3];
char BigBuf[512 + 1] = {0};

int main() {
    printf("Start\n");
    printf("Setting up...\n");
    EthernetErr ethErr = eth.setup();
    if (ethErr) {
        printf("Error %d in setup.\n", ethErr);
        return -1;
    }
    printf("Setup OK\n");
    HTTPStream stream;
    stream.readNext((byte*)BigBuf, 512);

    checkweather(stream);


    while (1) {

    }
    return 0;
}

void checkweather(HTTPStream &stream) {
    HTTPResult r = http.get("http://mobile.weather.gov/port_mp_ns.php?select=3&CityName=Lubbock&site=LUB&State=TX&warnzone=TXZ035", &stream, request_callback); //Load a very large page
    while (!completed) {
        Net::poll(); //Polls the Networking stack
        if (stream.readable()) {
            BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string

            // look for key words in the html text
            location = strstr(BigBuf,"Temperature");
            if (location != NULL) {
                strncpy(temperature,location+13,2);
                location = NULL;
            }

            location = strstr(BigBuf,"Humidity");
            if (location != NULL) {
                strncpy(humidity,location+10,2);
                location = NULL;
            }

            stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
        }
    }
    printf("\n--------------\n");
    if (result == HTTP_OK) {
        printf("Read completely\n");
        printf("Temperature: %s deg F \n",temperature);
        printf("Humidity: %s percent \n",humidity);

    } else {
        printf("Error %d\n", result);
    }
}