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

Committer:
elliotb
Date:
Mon Aug 09 14:12:07 2010 +0000
Revision:
0:f46e4f26425a
Child:
1:35888a9f0a48

        

Who changed what in which revision?

UserRevisionLine numberNew 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 0:f46e4f26425a 7
elliotb 0:f46e4f26425a 8 HTTPResult result;
elliotb 0:f46e4f26425a 9 bool completed = false;
elliotb 0:f46e4f26425a 10 void request_callback(HTTPResult r) {
elliotb 0:f46e4f26425a 11 result = r;
elliotb 0:f46e4f26425a 12 completed = true;
elliotb 0:f46e4f26425a 13 }
elliotb 0:f46e4f26425a 14
elliotb 0:f46e4f26425a 15 void checkweather(HTTPStream &stream);
elliotb 0:f46e4f26425a 16
elliotb 0:f46e4f26425a 17 char * location;
elliotb 0:f46e4f26425a 18 char temperature[3];
elliotb 0:f46e4f26425a 19 char humidity[3];
elliotb 0:f46e4f26425a 20 char BigBuf[512 + 1] = {0};
elliotb 0:f46e4f26425a 21
elliotb 0:f46e4f26425a 22 int main() {
elliotb 0:f46e4f26425a 23 printf("Start\n");
elliotb 0:f46e4f26425a 24 printf("Setting up...\n");
elliotb 0:f46e4f26425a 25 EthernetErr ethErr = eth.setup();
elliotb 0:f46e4f26425a 26 if (ethErr) {
elliotb 0:f46e4f26425a 27 printf("Error %d in setup.\n", ethErr);
elliotb 0:f46e4f26425a 28 return -1;
elliotb 0:f46e4f26425a 29 }
elliotb 0:f46e4f26425a 30 printf("Setup OK\n");
elliotb 0:f46e4f26425a 31 HTTPStream stream;
elliotb 0:f46e4f26425a 32 stream.readNext((byte*)BigBuf, 512);
elliotb 0:f46e4f26425a 33
elliotb 0:f46e4f26425a 34 checkweather(stream);
elliotb 0:f46e4f26425a 35
elliotb 0:f46e4f26425a 36
elliotb 0:f46e4f26425a 37 while (1) {
elliotb 0:f46e4f26425a 38
elliotb 0:f46e4f26425a 39 }
elliotb 0:f46e4f26425a 40 return 0;
elliotb 0:f46e4f26425a 41 }
elliotb 0:f46e4f26425a 42
elliotb 0:f46e4f26425a 43 void checkweather(HTTPStream &stream) {
elliotb 0:f46e4f26425a 44 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
elliotb 0:f46e4f26425a 45 while (!completed) {
elliotb 0:f46e4f26425a 46 Net::poll(); //Polls the Networking stack
elliotb 0:f46e4f26425a 47 if (stream.readable()) {
elliotb 0:f46e4f26425a 48 BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
elliotb 0:f46e4f26425a 49
elliotb 0:f46e4f26425a 50 // look for key words in the html text
elliotb 0:f46e4f26425a 51 location = strstr(BigBuf,"Temperature");
elliotb 0:f46e4f26425a 52 if (location != NULL) {
elliotb 0:f46e4f26425a 53 strncpy(temperature,location+13,2);
elliotb 0:f46e4f26425a 54 location = NULL;
elliotb 0:f46e4f26425a 55 }
elliotb 0:f46e4f26425a 56
elliotb 0:f46e4f26425a 57 location = strstr(BigBuf,"Humidity");
elliotb 0:f46e4f26425a 58 if (location != NULL) {
elliotb 0:f46e4f26425a 59 strncpy(humidity,location+10,2);
elliotb 0:f46e4f26425a 60 location = NULL;
elliotb 0:f46e4f26425a 61 }
elliotb 0:f46e4f26425a 62
elliotb 0:f46e4f26425a 63 stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
elliotb 0:f46e4f26425a 64 }
elliotb 0:f46e4f26425a 65 }
elliotb 0:f46e4f26425a 66 printf("\n--------------\n");
elliotb 0:f46e4f26425a 67 if (result == HTTP_OK) {
elliotb 0:f46e4f26425a 68 printf("Read completely\n");
elliotb 0:f46e4f26425a 69 printf("Temperature: %s deg F \n",temperature);
elliotb 0:f46e4f26425a 70 printf("Humidity: %s percent \n",humidity);
elliotb 0:f46e4f26425a 71
elliotb 0:f46e4f26425a 72 } else {
elliotb 0:f46e4f26425a 73 printf("Error %d\n", result);
elliotb 0:f46e4f26425a 74 }
elliotb 0:f46e4f26425a 75 }