![](/media/cache/group/ep_srxDLMd.webp.50x50_q85.jpg)
Beispiel Abfrage Cloud Dienst Sunrise / Sunset
Dependencies: EthernetInterface MbedJSONValue mbed-rtos mbed
Fork of HTTP_GET by
Sunrise Sunset stellt ein API zur Verfügung, mittels dem die Sonnen Auf- und Untergangszeiten für einen bestimmten Ort abgefragt werden können.
Links
Beispiel: Abfrage für Zürich
http://api.sunrise-sunset.org/json?lat=47.3686498&lng=8.5391825
{"results": { "sunrise":"5:38:12 AM", "sunset":"5:31:12 PM", "solar_noon":"11:34:42 AM", "day_length":"11:53:00", "civil_twilight_begin":"5:07:47 AM", "civil_twilight_end":"6:01:38 PM", "nautical_twilight_begin":"4:32:04 AM", "nautical_twilight_end":"6:37:21 PM", "astronomical_twilight_begin":"3:55:32 AM", "astronomical_twilight_end":"7:13:52 PM" }, "status":"OK"}
Diff: main.cpp
- Revision:
- 5:cfe277d00d6b
- Parent:
- 3:aad64a4b6ff6
--- a/main.cpp Wed May 27 08:11:32 2015 +0000 +++ b/main.cpp Wed May 27 16:08:21 2015 +0000 @@ -1,9 +1,11 @@ -/** Beispiel HTTP GET +/** Beispiel Abfrage Cloud Dienst Sunrise / Sunset */ #include "mbed.h" #include "HTTPClient.h" #include "HTTPText.h" #include "EthernetInterface.h" +#include "MbedJSONValue.h" +#include <string> EthernetInterface eth; // HTTPClient Hilfsklasse @@ -15,18 +17,32 @@ int main() { - printf("HTTP Client - GET\n"); + printf("\tSunriseSunset Cloud Dienst\n"); eth.init(); eth.connect(); while(1) { myled = 1; - int ret = http.get("http://developer.mbed.org/media/uploads/mbed_official/hello.txt", message, sizeof(message)); + int ret = http.get("http://api.sunrise-sunset.org/json?lat=47.3686498&lng=8.5391825", message, sizeof(message)); if ( !ret ) { - printf("Success - read %d characters.\r\n", strlen(message)); - printf("%s\r\n", message); + MbedJSONValue parser; + // HTTP GET (JSON) parsen + parse( parser, message ); + + std::string sunrise; + std::string sunset; + + sunrise = parser["results"]["sunrise"].get<std::string>(); + sunset = parser["results"]["sunset"] .get<std::string>(); + + // Umwandlung nach int. Damit die Zeiten besser verglichen werden können. + int rh, rm, rs, sh, sm, ss; + sscanf( sunrise.c_str(), "%d:%d:%d AM", &rh, &rm, &rs ); + sscanf( sunset .c_str(), "%d:%d:%d PM", &sh, &sm, &ss ); + + printf( "Sonnenauf- %02d.%02d.%02d und untergang %02d.%02d.%02d\n", rh+2, rm, rs, sh+2+12, sm, ss ); } else printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); @@ -35,5 +51,5 @@ wait(10); } - eth.disconnect(); + //eth.disconnect(); }