![](/media/cache/group/ep_srxDLMd.webp.50x50_q85.jpg)
Beispiel Abfrage Yahoo Weather fuer Zuerich
Dependencies: EthernetInterface mbed-rtos mbed spxml
Fork of HTTP_GET by
Mittels Yahoo Weather kann das aktuelle Wetter und eine Vorhersage für einen Bestimmten Ort abgefragt werden.
Links
Beispiel Abfrage Wetter in Zürich
Aufruf mittels curl:
curl http://query.yahooapis.com/v1/public/yql \ -d q="select * from weather.forecast where woeid=784794" -d format=xml
Antwort als XML von Yahoo:
<?xml version="1.0" encoding="UTF-8"?> <query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2016-07-22T13:17:05Z" yahoo:lang="en-US"> <results> <channel> <yweather:units xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" distance="mi" pressure="in" speed="mph" temperature="F" /> <title>Yahoo! Weather - Zurich, ZH, CH</title> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-784794/</link> <description>Yahoo! Weather for Zurich, ZH, CH</description> <language>en-us</language> <lastBuildDate>Fri, 22 Jul 2016 03:17 PM CEST</lastBuildDate> <ttl>60</ttl> <yweather:location xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" city="Zurich" country="Switzerland" region=" ZH" /> <yweather:wind xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" chill="72" direction="150" speed="11" /> <yweather:atmosphere xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" humidity="72" pressure="957.0" rising="0" visibility="16.1" /> <yweather:astronomy xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" sunrise="5:54 am" sunset="9:10 pm" /> <image> <title>Yahoo! Weather</title> <width>142</width> <height>18</height> <link>http://weather.yahoo.com</link> <url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url> </image> <item> <title>Conditions for Zurich, ZH, CH at 02:00 PM CEST</title> <geo:lat xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">47.38427</geo:lat> <geo:long xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">8.52959</geo:long> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-784794/</link> <pubDate>Fri, 22 Jul 2016 02:00 PM CEST</pubDate> <yweather:condition xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="39" date="Fri, 22 Jul 2016 02:00 PM CEST" temp="72" text="Scattered Showers" /> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="4" date="22 Jul 2016" day="Fri" high="77" low="66" text="Thunderstorms" /> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="47" date="23 Jul 2016" day="Sat" high="76" low="64" text="Scattered Thunderstorms" /> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="39" date="24 Jul 2016" day="Sun" high="78" low="63" text="Scattered Showers" /> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="47" date="25 Jul 2016" day="Mon" high="77" low="64" text="Scattered Thunderstorms" /> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="47" date="26 Jul 2016" day="Tue" high="75" low="64" text="Scattered Thunderstorms" /> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="4" date="27 Jul 2016" day="Wed" high="75" low="61" text="Thunderstorms" /> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="28 Jul 2016" day="Thu" high="76" low="60" text="Partly Cloudy" /> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="29 Jul 2016" day="Fri" high="79" low="59" text="Partly Cloudy" /> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="39" date="30 Jul 2016" day="Sat" high="80" low="58" text="Scattered Showers" /> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="31 Jul 2016" day="Sun" high="78" low="59" text="Partly Cloudy" /> <description></description> <guid isPermaLink="false" /> </item> </channel> </results> </query>
main.cpp@1:2e29a33cd918, 2015-01-17 (annotated)
- Committer:
- marcel1691
- Date:
- Sat Jan 17 14:11:01 2015 +0000
- Revision:
- 1:2e29a33cd918
- Parent:
- 0:fb5060c39dd1
- Child:
- 2:c9e058ee6f87
Beispiel fuer Verwendung der HTTP Methoden (GET, POST, PUT, DELETE)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcel1691 | 1:2e29a33cd918 | 1 | /** Beispiel fuer Verwendung der HTTP Methoden (GET, POST, PUT, DELETE) |
marcel1691 | 1:2e29a33cd918 | 2 | * |
marcel1691 | 1:2e29a33cd918 | 3 | * Braucht ein Board mit Ethernet |
marcel1691 | 1:2e29a33cd918 | 4 | */ |
WiredHome | 0:fb5060c39dd1 | 5 | #include "mbed.h" |
WiredHome | 0:fb5060c39dd1 | 6 | #include "HTTPClient.h" |
marcel1691 | 1:2e29a33cd918 | 7 | #include "HTTPText.h" |
WiredHome | 0:fb5060c39dd1 | 8 | #include "EthernetInterface.h" |
WiredHome | 0:fb5060c39dd1 | 9 | |
WiredHome | 0:fb5060c39dd1 | 10 | Serial pc(USBTX, USBRX); |
WiredHome | 0:fb5060c39dd1 | 11 | |
WiredHome | 0:fb5060c39dd1 | 12 | EthernetInterface eth; |
WiredHome | 0:fb5060c39dd1 | 13 | HTTPClient http; |
marcel1691 | 1:2e29a33cd918 | 14 | char message[6000]; |
marcel1691 | 1:2e29a33cd918 | 15 | char str[512]; |
WiredHome | 0:fb5060c39dd1 | 16 | |
WiredHome | 0:fb5060c39dd1 | 17 | DigitalOut myled(LED1); |
WiredHome | 0:fb5060c39dd1 | 18 | |
marcel1691 | 1:2e29a33cd918 | 19 | int main() |
marcel1691 | 1:2e29a33cd918 | 20 | { |
marcel1691 | 1:2e29a33cd918 | 21 | pc.baud(9600); |
WiredHome | 0:fb5060c39dd1 | 22 | printf("HTTP Client - Build " __DATE__ " - " __TIME__ "\r\n"); |
WiredHome | 0:fb5060c39dd1 | 23 | eth.init(); |
WiredHome | 0:fb5060c39dd1 | 24 | eth.connect(); |
WiredHome | 0:fb5060c39dd1 | 25 | |
marcel1691 | 1:2e29a33cd918 | 26 | while(1) |
marcel1691 | 1:2e29a33cd918 | 27 | { |
WiredHome | 0:fb5060c39dd1 | 28 | myled = 1; |
marcel1691 | 1:2e29a33cd918 | 29 | printf( "GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt\n" ); |
marcel1691 | 1:2e29a33cd918 | 30 | //int ret = http.get("https://raw.githubusercontent.com/mc-b/microHOME/master/README.md", message, sizeof(message)); |
marcel1691 | 1:2e29a33cd918 | 31 | int ret = http.get("http://developer.mbed.org/media/uploads/mbed_official/hello.txt", message, sizeof(message)); |
marcel1691 | 1:2e29a33cd918 | 32 | if ( !ret ) |
marcel1691 | 1:2e29a33cd918 | 33 | { |
WiredHome | 0:fb5060c39dd1 | 34 | printf("Success - read %d characters.\r\n", strlen(message)); |
WiredHome | 0:fb5060c39dd1 | 35 | printf("%s\r\n", message); |
WiredHome | 0:fb5060c39dd1 | 36 | } |
marcel1691 | 1:2e29a33cd918 | 37 | else |
marcel1691 | 1:2e29a33cd918 | 38 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
marcel1691 | 1:2e29a33cd918 | 39 | |
WiredHome | 0:fb5060c39dd1 | 40 | myled = 0; |
marcel1691 | 1:2e29a33cd918 | 41 | |
marcel1691 | 1:2e29a33cd918 | 42 | //POST data |
marcel1691 | 1:2e29a33cd918 | 43 | HTTPMap map; |
marcel1691 | 1:2e29a33cd918 | 44 | HTTPText inText(str, 512); |
marcel1691 | 1:2e29a33cd918 | 45 | map.put("Hello", "World"); |
marcel1691 | 1:2e29a33cd918 | 46 | map.put("test", "1234"); |
marcel1691 | 1:2e29a33cd918 | 47 | printf("\nTrying to post data...\n"); |
marcel1691 | 1:2e29a33cd918 | 48 | ret = http.post("http://httpbin.org/post", map, &inText); |
marcel1691 | 1:2e29a33cd918 | 49 | if ( ! ret ) |
marcel1691 | 1:2e29a33cd918 | 50 | { |
marcel1691 | 1:2e29a33cd918 | 51 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
marcel1691 | 1:2e29a33cd918 | 52 | printf("Result: %s\n", str); |
marcel1691 | 1:2e29a33cd918 | 53 | } |
marcel1691 | 1:2e29a33cd918 | 54 | else |
marcel1691 | 1:2e29a33cd918 | 55 | { |
marcel1691 | 1:2e29a33cd918 | 56 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
marcel1691 | 1:2e29a33cd918 | 57 | } |
marcel1691 | 1:2e29a33cd918 | 58 | |
marcel1691 | 1:2e29a33cd918 | 59 | //PUT data |
marcel1691 | 1:2e29a33cd918 | 60 | strcpy(str, "This is a PUT test!"); |
marcel1691 | 1:2e29a33cd918 | 61 | HTTPText outText(str); |
marcel1691 | 1:2e29a33cd918 | 62 | //HTTPText inText(str, 512); |
marcel1691 | 1:2e29a33cd918 | 63 | printf("\nTrying to put resource...\n"); |
marcel1691 | 1:2e29a33cd918 | 64 | ret = http.put("http://httpbin.org/put", outText, &inText); |
marcel1691 | 1:2e29a33cd918 | 65 | if (!ret) { |
marcel1691 | 1:2e29a33cd918 | 66 | printf("Executed PUT successfully - read %d characters\n", strlen(str)); |
marcel1691 | 1:2e29a33cd918 | 67 | printf("Result: %s\n", str); |
marcel1691 | 1:2e29a33cd918 | 68 | } else { |
marcel1691 | 1:2e29a33cd918 | 69 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
marcel1691 | 1:2e29a33cd918 | 70 | } |
marcel1691 | 1:2e29a33cd918 | 71 | |
marcel1691 | 1:2e29a33cd918 | 72 | //DELETE data |
marcel1691 | 1:2e29a33cd918 | 73 | //HTTPText inText(str, 512); |
marcel1691 | 1:2e29a33cd918 | 74 | printf("\nTrying to delete resource...\n"); |
marcel1691 | 1:2e29a33cd918 | 75 | ret = http.del("http://httpbin.org/delete", &inText); |
marcel1691 | 1:2e29a33cd918 | 76 | if (!ret) { |
marcel1691 | 1:2e29a33cd918 | 77 | printf("Executed DELETE successfully - read %d characters\n", strlen(str)); |
marcel1691 | 1:2e29a33cd918 | 78 | printf("Result: %s\n", str); |
marcel1691 | 1:2e29a33cd918 | 79 | } else { |
marcel1691 | 1:2e29a33cd918 | 80 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
marcel1691 | 1:2e29a33cd918 | 81 | } |
marcel1691 | 1:2e29a33cd918 | 82 | |
WiredHome | 0:fb5060c39dd1 | 83 | wait(10); |
marcel1691 | 1:2e29a33cd918 | 84 | break; |
WiredHome | 0:fb5060c39dd1 | 85 | } |
WiredHome | 0:fb5060c39dd1 | 86 | // eth.disconnect(); |
WiredHome | 0:fb5060c39dd1 | 87 | } |