10.2 Kombiniert die Übung 9.3 Licht zeitgesteuert Ein- und Ausschalten mit dem Sunset Sunrise Dienst, damit bei Sonnenuntergang das Licht ein und bei Sonnenaufgang das Licht ausgeschaltet wird

Dependencies:   EthernetInterface MbedJSONValue NTPClient mbed-rtos mbed

Fork of SunriseSunset by smd.iotkit2.ch

Revision:
5:cfe277d00d6b
Parent:
3:aad64a4b6ff6
Child:
6:4a7fd17737bc
--- 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();
 }