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@1:35888a9f0a48, 2010-08-10 (annotated)
- Committer:
- elliotb
- Date:
- Tue Aug 10 14:37:02 2010 +0000
- Revision:
- 1:35888a9f0a48
- Parent:
- 0:f46e4f26425a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elliotb | 0:f46e4f26425a | 1 | #include "mbed.h" |
elliotb | 0:f46e4f26425a | 2 | #include "EthernetNetIf.h" |
elliotb | 0:f46e4f26425a | 3 | #include "HTTPClient.h" |
elliotb | 0:f46e4f26425a | 4 | |
elliotb | 0:f46e4f26425a | 5 | EthernetNetIf eth; |
elliotb | 0:f46e4f26425a | 6 | HTTPClient http; |
elliotb | 1:35888a9f0a48 | 7 | HTTPStream stream; |
elliotb | 0:f46e4f26425a | 8 | HTTPResult result; |
elliotb | 1:35888a9f0a48 | 9 | Ticker update; |
elliotb | 1:35888a9f0a48 | 10 | |
elliotb | 1:35888a9f0a48 | 11 | char * location; |
elliotb | 1:35888a9f0a48 | 12 | char temperature[3]; // two digit characters plus null terminating char |
elliotb | 1:35888a9f0a48 | 13 | char humidity[3]; |
elliotb | 1:35888a9f0a48 | 14 | char BigBuf[512 + 1] = {0}; |
elliotb | 0:f46e4f26425a | 15 | bool completed = false; |
elliotb | 1:35888a9f0a48 | 16 | |
elliotb | 0:f46e4f26425a | 17 | void request_callback(HTTPResult r) { |
elliotb | 0:f46e4f26425a | 18 | result = r; |
elliotb | 0:f46e4f26425a | 19 | completed = true; |
elliotb | 0:f46e4f26425a | 20 | } |
elliotb | 1:35888a9f0a48 | 21 | void checkweather(void); |
elliotb | 0:f46e4f26425a | 22 | |
elliotb | 1:35888a9f0a48 | 23 | /* main */ |
elliotb | 0:f46e4f26425a | 24 | int main() { |
elliotb | 0:f46e4f26425a | 25 | printf("Start\n"); |
elliotb | 0:f46e4f26425a | 26 | printf("Setting up...\n"); |
elliotb | 0:f46e4f26425a | 27 | EthernetErr ethErr = eth.setup(); |
elliotb | 0:f46e4f26425a | 28 | if (ethErr) { |
elliotb | 0:f46e4f26425a | 29 | printf("Error %d in setup.\n", ethErr); |
elliotb | 0:f46e4f26425a | 30 | return -1; |
elliotb | 0:f46e4f26425a | 31 | } |
elliotb | 0:f46e4f26425a | 32 | printf("Setup OK\n"); |
elliotb | 1:35888a9f0a48 | 33 | checkweather(); // call check weather to start off |
elliotb | 1:35888a9f0a48 | 34 | update.attach(&checkweather, 5*60); // check the weather every 5 mins |
elliotb | 1:35888a9f0a48 | 35 | |
elliotb | 1:35888a9f0a48 | 36 | while (1) { // forever loop...weather checking will be interrupt based |
elliotb | 0:f46e4f26425a | 37 | |
elliotb | 0:f46e4f26425a | 38 | } |
elliotb | 0:f46e4f26425a | 39 | } |
elliotb | 1:35888a9f0a48 | 40 | void checkweather(void) { |
elliotb | 1:35888a9f0a48 | 41 | stream.readNext((byte*)BigBuf, 512); |
elliotb | 1:35888a9f0a48 | 42 | HTTPResult r = http.get("http://wedgefest.wind.ttu.edu/obs/TTU_LBBW.html", &stream, request_callback); //Load a very large page |
elliotb | 0:f46e4f26425a | 43 | while (!completed) { |
elliotb | 0:f46e4f26425a | 44 | Net::poll(); //Polls the Networking stack |
elliotb | 0:f46e4f26425a | 45 | if (stream.readable()) { |
elliotb | 0:f46e4f26425a | 46 | BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string |
elliotb | 0:f46e4f26425a | 47 | |
elliotb | 0:f46e4f26425a | 48 | // look for key words in the html text |
elliotb | 0:f46e4f26425a | 49 | location = strstr(BigBuf,"Temperature"); |
elliotb | 0:f46e4f26425a | 50 | if (location != NULL) { |
elliotb | 1:35888a9f0a48 | 51 | strncpy(temperature,location+43,2); |
elliotb | 0:f46e4f26425a | 52 | location = NULL; |
elliotb | 0:f46e4f26425a | 53 | } |
elliotb | 0:f46e4f26425a | 54 | location = strstr(BigBuf,"Humidity"); |
elliotb | 0:f46e4f26425a | 55 | if (location != NULL) { |
elliotb | 1:35888a9f0a48 | 56 | strncpy(humidity,location+40,2); |
elliotb | 0:f46e4f26425a | 57 | location = NULL; |
elliotb | 0:f46e4f26425a | 58 | } |
elliotb | 0:f46e4f26425a | 59 | stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it |
elliotb | 0:f46e4f26425a | 60 | } |
elliotb | 0:f46e4f26425a | 61 | } |
elliotb | 0:f46e4f26425a | 62 | printf("\n--------------\n"); |
elliotb | 0:f46e4f26425a | 63 | if (result == HTTP_OK) { |
elliotb | 0:f46e4f26425a | 64 | printf("Read completely\n"); |
elliotb | 0:f46e4f26425a | 65 | printf("Temperature: %s deg F \n",temperature); |
elliotb | 0:f46e4f26425a | 66 | printf("Humidity: %s percent \n",humidity); |
elliotb | 1:35888a9f0a48 | 67 | completed = false; // allows the weather to be checked more than once |
elliotb | 0:f46e4f26425a | 68 | } else { |
elliotb | 0:f46e4f26425a | 69 | printf("Error %d\n", result); |
elliotb | 0:f46e4f26425a | 70 | } |
elliotb | 0:f46e4f26425a | 71 | } |