Beispiel Abfrage Cloud Dienst Sunrise / Sunset

Dependencies:   EthernetInterface MbedJSONValue mbed-rtos mbed

Fork of HTTP_GET by smd.iotkit2.ch

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"}

Files at this revision

API Documentation at this revision

Comitter:
stefan1691
Date:
Wed May 27 16:08:21 2015 +0000
Parent:
4:320573dd5d07
Commit message:
Beispiel Abfrage Cloud Dienst Sunrise / Sunset

Changed in this revision

MbedJSONValue.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 320573dd5d07 -r cfe277d00d6b MbedJSONValue.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedJSONValue.lib	Wed May 27 16:08:21 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/samux/code/MbedJSONValue/#10a99cdf7846
diff -r 320573dd5d07 -r cfe277d00d6b main.cpp
--- 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();
 }
diff -r 320573dd5d07 -r cfe277d00d6b mbed.bld
--- a/mbed.bld	Wed May 27 08:11:32 2015 +0000
+++ b/mbed.bld	Wed May 27 16:08:21 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/dbbf35b96557
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/cbbeb26dbd92
\ No newline at end of file