![](/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"}
main.cpp@5:cfe277d00d6b, 2015-05-27 (annotated)
- Committer:
- stefan1691
- Date:
- Wed May 27 16:08:21 2015 +0000
- Revision:
- 5:cfe277d00d6b
- Parent:
- 3:aad64a4b6ff6
Beispiel Abfrage Cloud Dienst Sunrise / Sunset
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stefan1691 | 5:cfe277d00d6b | 1 | /** Beispiel Abfrage Cloud Dienst Sunrise / Sunset |
marcel1691 | 1:2e29a33cd918 | 2 | */ |
WiredHome | 0:fb5060c39dd1 | 3 | #include "mbed.h" |
WiredHome | 0:fb5060c39dd1 | 4 | #include "HTTPClient.h" |
marcel1691 | 1:2e29a33cd918 | 5 | #include "HTTPText.h" |
WiredHome | 0:fb5060c39dd1 | 6 | #include "EthernetInterface.h" |
stefan1691 | 5:cfe277d00d6b | 7 | #include "MbedJSONValue.h" |
stefan1691 | 5:cfe277d00d6b | 8 | #include <string> |
WiredHome | 0:fb5060c39dd1 | 9 | |
WiredHome | 0:fb5060c39dd1 | 10 | EthernetInterface eth; |
stefan1691 | 3:aad64a4b6ff6 | 11 | // HTTPClient Hilfsklasse |
WiredHome | 0:fb5060c39dd1 | 12 | HTTPClient http; |
stefan1691 | 3:aad64a4b6ff6 | 13 | // I/O Buffer |
marcel1691 | 1:2e29a33cd918 | 14 | char message[6000]; |
WiredHome | 0:fb5060c39dd1 | 15 | |
WiredHome | 0:fb5060c39dd1 | 16 | DigitalOut myled(LED1); |
WiredHome | 0:fb5060c39dd1 | 17 | |
marcel1691 | 1:2e29a33cd918 | 18 | int main() |
marcel1691 | 1:2e29a33cd918 | 19 | { |
stefan1691 | 5:cfe277d00d6b | 20 | printf("\tSunriseSunset Cloud Dienst\n"); |
WiredHome | 0:fb5060c39dd1 | 21 | eth.init(); |
WiredHome | 0:fb5060c39dd1 | 22 | eth.connect(); |
WiredHome | 0:fb5060c39dd1 | 23 | |
marcel1691 | 1:2e29a33cd918 | 24 | while(1) |
marcel1691 | 1:2e29a33cd918 | 25 | { |
WiredHome | 0:fb5060c39dd1 | 26 | myled = 1; |
stefan1691 | 5:cfe277d00d6b | 27 | int ret = http.get("http://api.sunrise-sunset.org/json?lat=47.3686498&lng=8.5391825", message, sizeof(message)); |
marcel1691 | 1:2e29a33cd918 | 28 | if ( !ret ) |
marcel1691 | 1:2e29a33cd918 | 29 | { |
stefan1691 | 5:cfe277d00d6b | 30 | MbedJSONValue parser; |
stefan1691 | 5:cfe277d00d6b | 31 | // HTTP GET (JSON) parsen |
stefan1691 | 5:cfe277d00d6b | 32 | parse( parser, message ); |
stefan1691 | 5:cfe277d00d6b | 33 | |
stefan1691 | 5:cfe277d00d6b | 34 | std::string sunrise; |
stefan1691 | 5:cfe277d00d6b | 35 | std::string sunset; |
stefan1691 | 5:cfe277d00d6b | 36 | |
stefan1691 | 5:cfe277d00d6b | 37 | sunrise = parser["results"]["sunrise"].get<std::string>(); |
stefan1691 | 5:cfe277d00d6b | 38 | sunset = parser["results"]["sunset"] .get<std::string>(); |
stefan1691 | 5:cfe277d00d6b | 39 | |
stefan1691 | 5:cfe277d00d6b | 40 | // Umwandlung nach int. Damit die Zeiten besser verglichen werden können. |
stefan1691 | 5:cfe277d00d6b | 41 | int rh, rm, rs, sh, sm, ss; |
stefan1691 | 5:cfe277d00d6b | 42 | sscanf( sunrise.c_str(), "%d:%d:%d AM", &rh, &rm, &rs ); |
stefan1691 | 5:cfe277d00d6b | 43 | sscanf( sunset .c_str(), "%d:%d:%d PM", &sh, &sm, &ss ); |
stefan1691 | 5:cfe277d00d6b | 44 | |
stefan1691 | 5:cfe277d00d6b | 45 | printf( "Sonnenauf- %02d.%02d.%02d und untergang %02d.%02d.%02d\n", rh+2, rm, rs, sh+2+12, sm, ss ); |
WiredHome | 0:fb5060c39dd1 | 46 | } |
marcel1691 | 1:2e29a33cd918 | 47 | else |
marcel1691 | 1:2e29a33cd918 | 48 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
marcel1691 | 1:2e29a33cd918 | 49 | |
WiredHome | 0:fb5060c39dd1 | 50 | myled = 0; |
marcel1691 | 1:2e29a33cd918 | 51 | |
WiredHome | 0:fb5060c39dd1 | 52 | wait(10); |
WiredHome | 0:fb5060c39dd1 | 53 | } |
stefan1691 | 5:cfe277d00d6b | 54 | //eth.disconnect(); |
WiredHome | 0:fb5060c39dd1 | 55 | } |