smd.iotkit2.ch / Mbed 2 deprecated 2-10-02-Uebung

Dependencies:   EthernetInterface MbedJSONValue NTPClient mbed-rtos mbed

Fork of SunriseSunset by smd.iotkit2.ch

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /** 10.2 Kombiniert die Übung 9.3 Licht zeitgesteuert Ein- und Ausschalten mit dem Sunset Sunrise Dienst, 
00002     damit bei Sonnenuntergang das Licht ein und bei Sonnenaufgang das Licht ausgeschaltet wird
00003  */
00004 #include "mbed.h"
00005 #include "HTTPClient.h"
00006 #include "HTTPText.h"
00007 #include "EthernetInterface.h"
00008 #include "MbedJSONValue.h"
00009 #include <string>
00010 #include "NTPClient.h"
00011 
00012 EthernetInterface eth;
00013 NTPClient ntp;
00014 // HTTPClient Hilfsklasse
00015 HTTPClient http;
00016 // I/O Buffer
00017 char message[6000];
00018 
00019 DigitalOut myled(LED1);
00020 // Licht
00021 DigitalOut led( D10 );
00022 
00023 int main()
00024 {
00025     printf("\tSunriseSunset Cloud Dienst - Licht ein-/ausschalten\n");
00026     eth.init();
00027     eth.connect();
00028     
00029     // Zeit vom Time Server holen
00030     printf("Trying to update time...\r\n");
00031     if (ntp.setTime("1.pool.ntp.org") == 0) 
00032     {
00033         printf("Set time successfully\r\n");
00034         time_t ctTime;
00035         ctTime = time(NULL);
00036         printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
00037     } 
00038     else
00039         printf("Error\r\n");
00040 
00041     while(1) 
00042     {
00043         myled = 1;
00044         int ret = http.get("http://api.sunrise-sunset.org/json?lat=47.3686498&lng=8.5391825", message, sizeof(message));
00045         if ( !ret ) 
00046         {
00047             MbedJSONValue parser;
00048             // HTTP GET (JSON) parsen  
00049             parse( parser, message );  
00050             
00051             std::string sunrise;
00052             std::string sunset;            
00053             
00054             sunrise = parser["results"]["sunrise"].get<std::string>();
00055             sunset  = parser["results"]["sunset"] .get<std::string>(); 
00056             
00057             // Umwandlung nach int. Damit die Zeiten besser verglichen werden können.
00058             int rh, rm, rs, sh, sm, ss;
00059             sscanf( sunrise.c_str(), "%d:%d:%d AM", &rh, &rm, &rs );
00060             sscanf( sunset .c_str(), "%d:%d:%d PM", &sh, &sm, &ss );
00061             
00062             printf( "Sonnenauf- %02d.%02d.%02d und untergang %02d.%02d.%02d\n", rh+2, rm, rs, sh+2+12, sm, ss );
00063             
00064             // Aktuelle Zeit
00065             time_t seconds = time(NULL);
00066             struct tm * now = localtime( & seconds );
00067             printf( "%d.%d.%d %2d:%2d:%2d\n", now->tm_mday, now->tm_mon + 1, now->tm_year + 1900, now->tm_hour, now->tm_min, now->tm_sec );             
00068             
00069             // Einfache Variante - nur Stunden abfragen
00070             if  ( now->tm_hour < rh || now->tm_hour > (sh+12) )
00071                 led = 1;          
00072             else
00073                 led = 0;
00074         }
00075         else
00076             printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00077         
00078         myled = 0;
00079 
00080         wait(10);
00081     }
00082     //eth.disconnect();
00083 }